Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually fix ICE from #96583 #96844

Merged
merged 1 commit into from
May 9, 2022
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
31 changes: 20 additions & 11 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
use rustc_middle::ty::error::ExpectedFound;
use rustc_middle::ty::error::TypeError::{FieldMisMatch, Sorts};
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AdtKind, Ty, TypeFoldable};
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TypeFoldable};
use rustc_session::parse::feature_err;
use rustc_span::hygiene::DesugaringKind;
use rustc_span::lev_distance::find_best_match_for_name;
Expand Down Expand Up @@ -2034,17 +2034,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
base: &'tcx hir::Expr<'tcx>,
def_id: DefId,
) {
let local_id = def_id.expect_local();
let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_id);
let node = self.tcx.hir().get(hir_id);

if let Some(fields) = node.tuple_fields() {
let kind = match self.tcx.opt_def_kind(local_id) {
Some(DefKind::Ctor(of, _)) => of,
_ => return,
};
if let Some(local_id) = def_id.as_local() {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_id);
let node = self.tcx.hir().get(hir_id);

if let Some(fields) = node.tuple_fields() {
let kind = match self.tcx.opt_def_kind(local_id) {
Some(DefKind::Ctor(of, _)) => of,
_ => return,
};

suggest_call_constructor(base.span, kind, fields.len(), err);
suggest_call_constructor(base.span, kind, fields.len(), err);
}
} else {
// The logic here isn't smart but `associated_item_def_ids`
// doesn't work nicely on local.
Comment on lines +2050 to +2051
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I even copied the comments from #96746 so I hope this makes sense in this context.

if let DefKind::Ctor(of, _) = self.tcx.def_kind(def_id) {
let parent_def_id = self.tcx.parent(def_id);
let fields = self.tcx.associated_item_def_ids(parent_def_id);
suggest_call_constructor(base.span, of, fields.len(), err);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/typeck/issue-96738.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
Some.nonexistent_method(); //~ ERROR: no method named `nonexistent_method` found
Some.nonexistent_field; //~ ERROR: no field `nonexistent_field`
}
18 changes: 16 additions & 2 deletions src/test/ui/typeck/issue-96738.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ help: call the constructor
LL | (Some)(_).nonexistent_method();
| + ++++

error: aborting due to previous error
error[E0609]: no field `nonexistent_field` on type `fn(_) -> Option<_> {Option::<_>::Some}`
--> $DIR/issue-96738.rs:3:10
|
LL | Some.nonexistent_field;
| ---- ^^^^^^^^^^^^^^^^^
| |
| this is the constructor of an enum variant
|
help: call the constructor
|
LL | (Some)(_).nonexistent_field;
| + ++++

error: aborting due to 2 previous errors

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