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

Be more careful about unresolved exprs in suggestion #105310

Merged
merged 1 commit into from
Dec 6, 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
23 changes: 13 additions & 10 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,25 +1482,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ident_name: Symbol,
}

// FIXME: This really should be taking scoping, etc into account.
impl<'v> Visitor<'v> for LetVisitor<'v> {
fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) {
if let hir::StmtKind::Local(hir::Local { pat, init, .. }) = &ex.kind {
if let Binding(_, _, ident, ..) = pat.kind &&
ident.name == self.ident_name {
self.result = *init;
}
if let hir::StmtKind::Local(hir::Local { pat, init, .. }) = &ex.kind
&& let Binding(_, _, ident, ..) = pat.kind
&& ident.name == self.ident_name
{
self.result = *init;
} else {
hir::intravisit::walk_stmt(self, ex);
}
hir::intravisit::walk_stmt(self, ex);
}
}

let mut visitor = LetVisitor { result: None, ident_name: seg1.ident.name };
visitor.visit_body(&body);

let parent = self.tcx.hir().get_parent_node(seg1.hir_id);
if let Some(Node::Expr(call_expr)) = self.tcx.hir().find(parent) &&
let Some(expr) = visitor.result {
let self_ty = self.node_ty(expr.hir_id);
if let Some(Node::Expr(call_expr)) = self.tcx.hir().find(parent)
&& let Some(expr) = visitor.result
&& let Some(self_ty) = self.node_ty_opt(expr.hir_id)
{
let probe = self.lookup_probe(
seg2.ident,
self_ty,
Expand All @@ -1513,7 +1516,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':').unwrap(),
"you may have meant to call an instance method",
".".to_string(),
Applicability::MaybeIncorrect
Applicability::MaybeIncorrect,
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/typeck/path-to-method-sugg-unresolved-expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let page_size = page_size::get();
//~^ ERROR failed to resolve: use of undeclared crate or module `page_size`
}
9 changes: 9 additions & 0 deletions src/test/ui/typeck/path-to-method-sugg-unresolved-expr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0433]: failed to resolve: use of undeclared crate or module `page_size`
--> $DIR/path-to-method-sugg-unresolved-expr.rs:2:21
|
LL | let page_size = page_size::get();
| ^^^^^^^^^ use of undeclared crate or module `page_size`

error: aborting due to previous error

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