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
50 changes: 32 additions & 18 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2332,30 +2332,44 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
fn field_idents(&self, def_id: DefId) -> Option<Vec<Ident>> {
match def_id.as_local() {
Some(def_id) => self.field_names.get(&def_id).cloned(),
None => Some(
self.tcx
.associated_item_def_ids(def_id)
.iter()
.map(|&def_id| {
Ident::new(self.tcx.item_name(def_id), self.tcx.def_span(def_id))
})
.collect(),
),
None if matches!(
self.tcx.def_kind(def_id),
DefKind::Struct | DefKind::Union | DefKind::Variant
) =>
{
Some(
self.tcx
.associated_item_def_ids(def_id)
.iter()
.map(|&def_id| {
Ident::new(self.tcx.item_name(def_id), self.tcx.def_span(def_id))
})
.collect(),
)
}
_ => None,
}
}

fn field_defaults(&self, def_id: DefId) -> Option<Vec<Symbol>> {
match def_id.as_local() {
Some(def_id) => self.field_defaults.get(&def_id).cloned(),
None => Some(
self.tcx
.associated_item_def_ids(def_id)
.iter()
.filter_map(|&def_id| {
self.tcx.default_field(def_id).map(|_| self.tcx.item_name(def_id))
})
.collect(),
),
None if matches!(
self.tcx.def_kind(def_id),
DefKind::Struct | DefKind::Union | DefKind::Variant
) =>
{
Some(
self.tcx
.associated_item_def_ids(def_id)
.iter()
.filter_map(|&def_id| {
self.tcx.default_field(def_id).map(|_| self.tcx.item_name(def_id))
})
.collect(),
)
}
_ => None,
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/structs/default-field-values/struct-fields-ice-147325.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ICE #147325: When the user mistakenly uses struct syntax to construct an enum,
// the field_idents and field_defaults functions will trigger an error

mod m {
struct Priv1;
}

fn main() {
Option { field1: m::Priv1 } //~ ERROR expected struct, variant or union type, found enum
//~^ ERROR unit struct `Priv1` is private
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0574]: expected struct, variant or union type, found enum `Option`
--> $DIR/struct-fields-ice-147325.rs:9:5
|
LL | Option { field1: m::Priv1 }
| ^^^^^^ not a struct, variant or union type

error[E0603]: unit struct `Priv1` is private
--> $DIR/struct-fields-ice-147325.rs:9:25
|
LL | Option { field1: m::Priv1 }
| ^^^^^ private unit struct
|
note: the unit struct `Priv1` is defined here
--> $DIR/struct-fields-ice-147325.rs:5:5
|
LL | struct Priv1;
| ^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0574, E0603.
For more information about an error, try `rustc --explain E0574`.
Loading