Skip to content

Commit

Permalink
Rollup merge of #96746 - JohnTitor:issue-96738, r=petrochenkov
Browse files Browse the repository at this point in the history
Fix an ICE on #96738

In the block we don't know if the method actually exists thus `expect_local` panics.
Fixes #96738
Fixes #96583
  • Loading branch information
compiler-errors committed May 7, 2022
2 parents 102bbc9 + 35d77c1 commit 825dc80
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
29 changes: 19 additions & 10 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,24 +368,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if self.is_fn_ty(rcvr_ty, span) {
if let SelfSource::MethodCall(expr) = source {
let suggest = if let ty::FnDef(def_id, _) = rcvr_ty.kind() {
let local_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(local_id);
let node = tcx.hir().get(hir_id);
let fields = node.tuple_fields();

if let Some(fields) = fields
&& let Some(DefKind::Ctor(of, _)) = self.tcx.opt_def_kind(local_id) {
Some((fields, of))
if let Some(local_id) = def_id.as_local() {
let hir_id = tcx.hir().local_def_id_to_hir_id(local_id);
let node = tcx.hir().get(hir_id);
let fields = node.tuple_fields();
if let Some(fields) = fields
&& let Some(DefKind::Ctor(of, _)) = self.tcx.opt_def_kind(local_id) {
Some((fields.len(), of))
} else {
None
}
} else {
None
// The logic here isn't smart but `associated_item_def_ids`
// doesn't work nicely on local.
if let DefKind::Ctor(of, _) = tcx.def_kind(def_id) {
let parent_def_id = tcx.parent(*def_id);
Some((tcx.associated_item_def_ids(parent_def_id).len(), of))
} else {
None
}
}
} else {
None
};

// If the function is a tuple constructor, we recommend that they call it
if let Some((fields, kind)) = suggest {
suggest_call_constructor(expr.span, kind, fields.len(), &mut err);
suggest_call_constructor(expr.span, kind, fields, &mut err);
} else {
// General case
err.span_label(
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/typeck/issue-96738.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
Some.nonexistent_method(); //~ ERROR: no method named `nonexistent_method` found
}
16 changes: 16 additions & 0 deletions src/test/ui/typeck/issue-96738.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0599]: no method named `nonexistent_method` found for fn item `fn(_) -> Option<_> {Option::<_>::Some}` in the current scope
--> $DIR/issue-96738.rs:2:10
|
LL | Some.nonexistent_method();
| ---- ^^^^^^^^^^^^^^^^^^ method not found in `fn(_) -> Option<_> {Option::<_>::Some}`
| |
| this is the constructor of an enum variant
|
help: call the constructor
|
LL | (Some)(_).nonexistent_method();
| + ++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.

0 comments on commit 825dc80

Please sign in to comment.