Skip to content

Commit 3856d0b

Browse files
authored
Rollup merge of rust-lang#147680 - chenyukang:yukang-fix-ice-147325, r=estebank
Fix ICE caused by associated_item_def_ids on wrong type in resolve diag Fixes rust-lang#147325 r? ``@estebank``
2 parents 783307c + c00b4ba commit 3856d0b

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

compiler/rustc_resolve/src/lib.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,30 +2332,44 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23322332
fn field_idents(&self, def_id: DefId) -> Option<Vec<Ident>> {
23332333
match def_id.as_local() {
23342334
Some(def_id) => self.field_names.get(&def_id).cloned(),
2335-
None => Some(
2336-
self.tcx
2337-
.associated_item_def_ids(def_id)
2338-
.iter()
2339-
.map(|&def_id| {
2340-
Ident::new(self.tcx.item_name(def_id), self.tcx.def_span(def_id))
2341-
})
2342-
.collect(),
2343-
),
2335+
None if matches!(
2336+
self.tcx.def_kind(def_id),
2337+
DefKind::Struct | DefKind::Union | DefKind::Variant
2338+
) =>
2339+
{
2340+
Some(
2341+
self.tcx
2342+
.associated_item_def_ids(def_id)
2343+
.iter()
2344+
.map(|&def_id| {
2345+
Ident::new(self.tcx.item_name(def_id), self.tcx.def_span(def_id))
2346+
})
2347+
.collect(),
2348+
)
2349+
}
2350+
_ => None,
23442351
}
23452352
}
23462353

23472354
fn field_defaults(&self, def_id: DefId) -> Option<Vec<Symbol>> {
23482355
match def_id.as_local() {
23492356
Some(def_id) => self.field_defaults.get(&def_id).cloned(),
2350-
None => Some(
2351-
self.tcx
2352-
.associated_item_def_ids(def_id)
2353-
.iter()
2354-
.filter_map(|&def_id| {
2355-
self.tcx.default_field(def_id).map(|_| self.tcx.item_name(def_id))
2356-
})
2357-
.collect(),
2358-
),
2357+
None if matches!(
2358+
self.tcx.def_kind(def_id),
2359+
DefKind::Struct | DefKind::Union | DefKind::Variant
2360+
) =>
2361+
{
2362+
Some(
2363+
self.tcx
2364+
.associated_item_def_ids(def_id)
2365+
.iter()
2366+
.filter_map(|&def_id| {
2367+
self.tcx.default_field(def_id).map(|_| self.tcx.item_name(def_id))
2368+
})
2369+
.collect(),
2370+
)
2371+
}
2372+
_ => None,
23592373
}
23602374
}
23612375

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ICE #147325: When the user mistakenly uses struct syntax to construct an enum,
2+
// the field_idents and field_defaults functions will trigger an error
3+
4+
mod m {
5+
struct Priv1;
6+
}
7+
8+
fn main() {
9+
Option { field1: m::Priv1 } //~ ERROR expected struct, variant or union type, found enum
10+
//~^ ERROR unit struct `Priv1` is private
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0574]: expected struct, variant or union type, found enum `Option`
2+
--> $DIR/struct-fields-ice-147325.rs:9:5
3+
|
4+
LL | Option { field1: m::Priv1 }
5+
| ^^^^^^ not a struct, variant or union type
6+
7+
error[E0603]: unit struct `Priv1` is private
8+
--> $DIR/struct-fields-ice-147325.rs:9:25
9+
|
10+
LL | Option { field1: m::Priv1 }
11+
| ^^^^^ private unit struct
12+
|
13+
note: the unit struct `Priv1` is defined here
14+
--> $DIR/struct-fields-ice-147325.rs:5:5
15+
|
16+
LL | struct Priv1;
17+
| ^^^^^^^^^^^^^
18+
19+
error: aborting due to 2 previous errors
20+
21+
Some errors have detailed explanations: E0574, E0603.
22+
For more information about an error, try `rustc --explain E0574`.

0 commit comments

Comments
 (0)