Skip to content

Commit 84a8f8d

Browse files
authored
Rollup merge of rust-lang#96844 - Badel2:actually-fix-96583, r=compiler-errors
Actually fix ICE from rust-lang#96583 PR rust-lang#96746 fixed a very similar bug, so the same logic is used in a different place. I originally concluded that the two issues (rust-lang#96583 and rust-lang#96738) were identical by comparing the backtrace, but I didn't look close enough.
2 parents f4bef2e + 84adf0d commit 84a8f8d

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

compiler/rustc_typeck/src/check/expr.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
4444
use rustc_middle::ty::error::ExpectedFound;
4545
use rustc_middle::ty::error::TypeError::{FieldMisMatch, Sorts};
4646
use rustc_middle::ty::subst::SubstsRef;
47-
use rustc_middle::ty::{self, AdtKind, Ty, TypeFoldable};
47+
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TypeFoldable};
4848
use rustc_session::parse::feature_err;
4949
use rustc_span::hygiene::DesugaringKind;
5050
use rustc_span::lev_distance::find_best_match_for_name;
@@ -2034,17 +2034,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20342034
base: &'tcx hir::Expr<'tcx>,
20352035
def_id: DefId,
20362036
) {
2037-
let local_id = def_id.expect_local();
2038-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_id);
2039-
let node = self.tcx.hir().get(hir_id);
2040-
2041-
if let Some(fields) = node.tuple_fields() {
2042-
let kind = match self.tcx.opt_def_kind(local_id) {
2043-
Some(DefKind::Ctor(of, _)) => of,
2044-
_ => return,
2045-
};
2037+
if let Some(local_id) = def_id.as_local() {
2038+
let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_id);
2039+
let node = self.tcx.hir().get(hir_id);
2040+
2041+
if let Some(fields) = node.tuple_fields() {
2042+
let kind = match self.tcx.opt_def_kind(local_id) {
2043+
Some(DefKind::Ctor(of, _)) => of,
2044+
_ => return,
2045+
};
20462046

2047-
suggest_call_constructor(base.span, kind, fields.len(), err);
2047+
suggest_call_constructor(base.span, kind, fields.len(), err);
2048+
}
2049+
} else {
2050+
// The logic here isn't smart but `associated_item_def_ids`
2051+
// doesn't work nicely on local.
2052+
if let DefKind::Ctor(of, _) = self.tcx.def_kind(def_id) {
2053+
let parent_def_id = self.tcx.parent(def_id);
2054+
let fields = self.tcx.associated_item_def_ids(parent_def_id);
2055+
suggest_call_constructor(base.span, of, fields.len(), err);
2056+
}
20482057
}
20492058
}
20502059

src/test/ui/typeck/issue-96738.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fn main() {
22
Some.nonexistent_method(); //~ ERROR: no method named `nonexistent_method` found
3+
Some.nonexistent_field; //~ ERROR: no field `nonexistent_field`
34
}

src/test/ui/typeck/issue-96738.stderr

+16-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ help: call the constructor
1111
LL | (Some)(_).nonexistent_method();
1212
| + ++++
1313

14-
error: aborting due to previous error
14+
error[E0609]: no field `nonexistent_field` on type `fn(_) -> Option<_> {Option::<_>::Some}`
15+
--> $DIR/issue-96738.rs:3:10
16+
|
17+
LL | Some.nonexistent_field;
18+
| ---- ^^^^^^^^^^^^^^^^^
19+
| |
20+
| this is the constructor of an enum variant
21+
|
22+
help: call the constructor
23+
|
24+
LL | (Some)(_).nonexistent_field;
25+
| + ++++
26+
27+
error: aborting due to 2 previous errors
1528

16-
For more information about this error, try `rustc --explain E0599`.
29+
Some errors have detailed explanations: E0599, E0609.
30+
For more information about an error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)