Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/delegation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use rustc_hir::{self as hir, FnDeclFlags, QPath};
use rustc_middle::ty::Asyncness;
use rustc_span::def_id::DefId;
use rustc_span::symbol::kw;
use rustc_span::{Ident, Span, Symbol, sym};
use rustc_span::{Ident, Span, Symbol};

use crate::delegation::generics::{GenericsGenerationResults, GenericsPosition};
use crate::delegation::resolution::resolver::DelegationResolver;
Expand Down Expand Up @@ -480,7 +480,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.arena.alloc_slice(&[hir::ExprField {
hir_id: self.next_id(),
is_shorthand: false,
ident: Ident::new(sym::integer(0), span),
ident: Ident::new(kw::Underscore, span),
expr: self.arena.alloc(call),
span,
}]),
Expand Down
89 changes: 57 additions & 32 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,10 +1898,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let adt_kind = adt.adt_kind();

let get_field_ident = |field: &ty::FieldDef| field.ident(tcx).normalize_to_macros_2_0();
let mut remaining_fields = variant
.fields
.iter_enumerated()
.map(|(i, field)| (field.ident(tcx).normalize_to_macros_2_0(), (i, field)))
.map(|(i, field)| (get_field_ident(field), (i, field)))
.collect::<UnordMap<_, _>>();

let mut seen_fields = FxHashMap::default();
Expand All @@ -1919,41 +1920,65 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Type-check each field.
for (idx, field) in hir_fields.iter().enumerate() {
let ident = tcx.adjust_ident(field.ident, variant.def_id);
let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
seen_fields.insert(ident, field.span);
self.write_field_index(field.hir_id, i);

// We don't look at stability attributes on
// struct-like enums (yet...), but it's definitely not
// a bug to have constructed one.
if adt_kind != AdtKind::Enum {
tcx.check_stability(v_field.did, Some(field.hir_id), field.span, None);
let ident = if field.ident.name == kw::Underscore {
// We are processing delegation `Self` mapping field, we expect that it is a struct
// with one field, so if it is not true we emit an error as we can't infer
// what field to use to initialize the struct.
// The `_` is a keyword so if user uses it during struct initialization as a field name
// it will be error anyway.
if let [v_field] = &variant.fields[..] {
Ok(get_field_ident(v_field))
} else {
Err(tcx.dcx().span_delayed_bug(
field.ident.span,
format!(
"can not infer the delegation `Self` mapping field for {:?}",
self.body_def_id
),
))
}

self.field_ty(field.span, v_field, args)
} else {
error_happened = true;
let guar = if let Some(prev_span) = seen_fields.get(&ident) {
self.dcx().emit_err(FieldMultiplySpecifiedInInitializer {
span: field.ident.span,
prev_span: *prev_span,
ident,
})
} else {
self.report_unknown_field(
adt_ty,
variant,
expr,
field,
hir_fields,
adt.variant_descr(),
)
};

Ty::new_error(tcx, guar)
Ok(tcx.adjust_ident(field.ident, variant.def_id))
};

let field_type = ident
.and_then(|ident| {
if let Some((i, v_field)) = remaining_fields.remove(&ident) {
seen_fields.insert(ident, field.span);
self.write_field_index(field.hir_id, i);

// We don't look at stability attributes on
// struct-like enums (yet...), but it's definitely not
// a bug to have constructed one.
if adt_kind != AdtKind::Enum {
tcx.check_stability(v_field.did, Some(field.hir_id), field.span, None);
}

Ok(self.field_ty(field.span, v_field, args))
} else {
error_happened = true;
let guar = if let Some(prev_span) = seen_fields.get(&ident) {
self.dcx().emit_err(FieldMultiplySpecifiedInInitializer {
span: field.ident.span,
prev_span: *prev_span,
ident,
})
} else {
self.report_unknown_field(
adt_ty,
variant,
expr,
field,
hir_fields,
adt.variant_descr(),
)
};

Err(guar)
}
})
.unwrap_or_else(|err| Ty::new_error(tcx, err));

// Check that the expected field type is WF. Otherwise, we emit no use-site error
// in the case of coercions for non-WF fields, which leads to incorrect error
// tainting. See issue #126272.
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use rustc_middle::ty::{
self, AssocContainer, Const, GenericParamDefKind, PredicateProxy, TraitRef, Ty, TyCtxt,
TypeSuperVisitable, TypeVisitable, TypeVisitor,
};
use rustc_span::{Ident, Span, Symbol, bug, span_bug, sym};
use rustc_span::{Ident, Span, Symbol, bug, kw, span_bug, sym};
use tracing::debug;

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1077,7 +1077,14 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
for field in fields {
let index = self.typeck_results().field_index(field.hir_id);
if self.check_field(field.ident.span, adt, &variant.fields[index]) {
failed_fields.push((field.ident.name, field.ident.span, true));
// kw::Underscore is a `Self` mapping delegation field which is
// generated by compiler, see delegation AST -> HIR lowering code
// and `check_expr_struct_fields` for details.
let name = match field.ident.name {
kw::Underscore => variant.fields[index].name,
name => name,
};
failed_fields.push((name, field.ident.span, true));
}
}
self.emit_unreachable_field_error(failed_fields, adt, None, qpath.span());
Expand Down
4 changes: 2 additions & 2 deletions tests/pretty/delegation/self-mapping-output.pp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
struct W(S);
impl Trait for W {
#[attr = Inline(Hint)]
fn method(self: _) -> _ { from(Self { 0: Trait::method(self.0) }) }
fn method(self: _) -> _ { from(Self { _: Trait::method(self.0) }) }
#[attr = Inline(Hint)]
fn r#static() -> _ { Trait::r#static() }
//~^ WARN: function cannot return without recursing [unconditional_recursion]
Expand All @@ -34,7 +34,7 @@

impl W {
#[attr = Inline(Hint)]
fn method(self: _) -> _ { from(Self { 0: Trait::method(self.0) }) }
fn method(self: _) -> _ { from(Self { _: Trait::method(self.0) }) }
#[attr = Inline(Hint)]
fn r#static() -> _ { Trait::r#static() }
#[attr = Inline(Hint)]
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/delegation/self-mapping-output-privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ impl Trait for S {

mod private {
pub struct W(super::S);

pub struct W1 {
sss: super::S,
}
}

impl Trait for private::W {
Expand All @@ -25,4 +29,14 @@ impl private::W {
//~^ ERROR: field `0` of struct `W` is private
}

impl Trait for private::W1 {
reuse Trait::method { S }
//~^ ERROR: field `sss` of struct `W1` is private
}

impl private::W1 {
reuse Trait::method { S }
//~^ ERROR: field `sss` of struct `W1` is private
}

fn main() {}
18 changes: 15 additions & 3 deletions tests/ui/delegation/self-mapping-output-privacy.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
error[E0451]: field `0` of struct `W` is private
--> $DIR/self-mapping-output-privacy.rs:19:18
--> $DIR/self-mapping-output-privacy.rs:23:18
|
LL | reuse Trait::method { S }
| ^^^^^^ private field

error[E0451]: field `0` of struct `W` is private
--> $DIR/self-mapping-output-privacy.rs:24:18
--> $DIR/self-mapping-output-privacy.rs:28:18
|
LL | reuse Trait::method { S }
| ^^^^^^ private field

error: aborting due to 2 previous errors
error[E0451]: field `sss` of struct `W1` is private
--> $DIR/self-mapping-output-privacy.rs:33:18
|
LL | reuse Trait::method { S }
| ^^^^^^ private field

error[E0451]: field `sss` of struct `W1` is private
--> $DIR/self-mapping-output-privacy.rs:38:18
|
LL | reuse Trait::method { S }
| ^^^^^^ private field

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0451`.
6 changes: 2 additions & 4 deletions tests/ui/delegation/self-mapping-output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ mod no_constructor {
impl Trait for W {
reuse Trait::method { self.0 }
//~^ ERROR: no field `0` on type `&no_constructor::W`
//~| ERROR: struct `no_constructor::W` has no field named `0`
reuse Trait::r#static;
//~^ WARN: function cannot return without recursing [unconditional_recursion]
reuse Trait::raw_S { self.0 }
Expand All @@ -119,7 +118,6 @@ mod no_constructor {
impl W {
reuse Trait::method { self.0 }
//~^ ERROR: no field `0` on type `&no_constructor::W`
//~| ERROR: struct `no_constructor::W` has no field named `0`
reuse Trait::r#static;
reuse Trait::raw_S { self.0 }
//~^ ERROR: no field `0` on type `&no_constructor::W`
Expand All @@ -142,15 +140,15 @@ mod more_than_one_field {
struct W(S, S, S);
impl Trait for W {
reuse Trait::method { self.0 }
//~^ ERROR: missing fields `1` and `2` in initializer of `more_than_one_field::W`
//~^ ERROR: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W`
reuse Trait::r#static;
//~^ WARN: function cannot return without recursing [unconditional_recursion]
reuse Trait::raw_S { self.0 }
}

impl W {
reuse Trait::method { self.0 }
//~^ ERROR: missing fields `1` and `2` in initializer of `more_than_one_field::W`
//~^ ERROR: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W`
reuse Trait::r#static;
reuse Trait::raw_S { self.0 }
}
Expand Down
Loading
Loading