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

Tighten the 'introduce new binding' suggestion #104186

Merged
merged 3 commits into from
Nov 10, 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
14 changes: 10 additions & 4 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ struct DiagnosticMetadata<'ast> {

/// Used to detect possible new binding written without `let` and to provide structured suggestion.
in_assignment: Option<&'ast Expr>,
is_assign_rhs: bool,

/// If we are currently in a trait object definition. Used to point at the bounds when
/// encountering a struct or enum.
Expand Down Expand Up @@ -3963,10 +3964,15 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.resolve_expr(elem, Some(expr));
self.visit_expr(idx);
}
ExprKind::Assign(..) => {
let old = self.diagnostic_metadata.in_assignment.replace(expr);
visit::walk_expr(self, expr);
self.diagnostic_metadata.in_assignment = old;
ExprKind::Assign(ref lhs, ref rhs, _) => {
if !self.diagnostic_metadata.is_assign_rhs {
self.diagnostic_metadata.in_assignment = Some(expr);
}
self.visit_expr(lhs);
self.diagnostic_metadata.is_assign_rhs = true;
self.diagnostic_metadata.in_assignment = None;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we reinstate the overwritten value, instead of putting None in all cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

we only care about the lhs of assignment.

Copy link
Member Author

Choose a reason for hiding this comment

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

And the backup state is not important, the check logic is invoked by visit_expr.

self.visit_expr(rhs);
self.diagnostic_metadata.is_assign_rhs = false;
}
_ => {
visit::walk_expr(self, expr);
Expand Down
21 changes: 7 additions & 14 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1810,29 +1810,22 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
false
}

fn let_binding_suggestion(&self, err: &mut Diagnostic, ident_span: Span) -> bool {
// try to give a suggestion for this pattern: `name = 1`, which is common in other languages
let mut added_suggestion = false;
if let Some(Expr { kind: ExprKind::Assign(lhs, _rhs, _), .. }) = self.diagnostic_metadata.in_assignment &&
// try to give a suggestion for this pattern: `name = blah`, which is common in other languages
// suggest `let name = blah` to introduce a new binding
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
if let Some(Expr { kind: ExprKind::Assign(lhs, .. ), .. }) = self.diagnostic_metadata.in_assignment &&
let ast::ExprKind::Path(None, _) = lhs.kind {
let sm = self.r.session.source_map();
let line_span = sm.span_extend_to_line(ident_span);
let ident_name = sm.span_to_snippet(ident_span).unwrap();
// HACK(chenyukang): make sure ident_name is at the starting of the line to protect against macros
if sm
.span_to_snippet(line_span)
.map_or(false, |s| s.trim().starts_with(&ident_name))
{
if !ident_span.from_expansion() {
err.span_suggestion_verbose(
ident_span.shrink_to_lo(),
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
added_suggestion = true;
return true;
}
}
added_suggestion
false
}

fn find_module(&mut self, def_id: DefId) -> Option<(Module<'a>, ImportSuggestion)> {
Expand Down
30 changes: 30 additions & 0 deletions src/test/ui/suggestions/issue-104086-suggest-let.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
fn main() {
x = x = x;
//~^ ERROR cannot find value `x` in this scope
//~| ERROR cannot find value `x` in this scope
//~| ERROR cannot find value `x` in this scope

x = y = y = y;
//~^ ERROR cannot find value `y` in this scope
//~| ERROR cannot find value `y` in this scope
//~| ERROR cannot find value `y` in this scope
//~| ERROR cannot find value `x` in this scope

x = y = y;
//~^ ERROR cannot find value `x` in this scope
//~| ERROR cannot find value `y` in this scope
//~| ERROR cannot find value `y` in this scope

x = x = y;
//~^ ERROR cannot find value `x` in this scope
//~| ERROR cannot find value `x` in this scope
//~| ERROR cannot find value `y` in this scope

x = x; // will suggest add `let`
//~^ ERROR cannot find value `x` in this scope
//~| ERROR cannot find value `x` in this scope

x = y // will suggest add `let`
//~^ ERROR cannot find value `x` in this scope
//~| ERROR cannot find value `y` in this scope
}
135 changes: 135 additions & 0 deletions src/test/ui/suggestions/issue-104086-suggest-let.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:2:5
|
LL | x = x = x;
| ^
|
help: you might have meant to introduce a new binding
|
LL | let x = x = x;
| +++

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:2:9
|
LL | x = x = x;
| ^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:2:13
|
LL | x = x = x;
| ^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:7:5
|
LL | x = y = y = y;
| ^
|
help: you might have meant to introduce a new binding
|
LL | let x = y = y = y;
| +++

error[E0425]: cannot find value `y` in this scope
--> $DIR/issue-104086-suggest-let.rs:7:9
|
LL | x = y = y = y;
| ^ not found in this scope

error[E0425]: cannot find value `y` in this scope
--> $DIR/issue-104086-suggest-let.rs:7:13
|
LL | x = y = y = y;
| ^ not found in this scope

error[E0425]: cannot find value `y` in this scope
--> $DIR/issue-104086-suggest-let.rs:7:17
|
LL | x = y = y = y;
| ^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:13:5
|
LL | x = y = y;
| ^
|
help: you might have meant to introduce a new binding
|
LL | let x = y = y;
| +++

error[E0425]: cannot find value `y` in this scope
--> $DIR/issue-104086-suggest-let.rs:13:9
|
LL | x = y = y;
| ^ not found in this scope

error[E0425]: cannot find value `y` in this scope
--> $DIR/issue-104086-suggest-let.rs:13:13
|
LL | x = y = y;
| ^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:18:5
|
LL | x = x = y;
| ^
|
help: you might have meant to introduce a new binding
|
LL | let x = x = y;
| +++

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:18:9
|
LL | x = x = y;
| ^ not found in this scope

error[E0425]: cannot find value `y` in this scope
--> $DIR/issue-104086-suggest-let.rs:18:13
|
LL | x = x = y;
| ^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:23:5
|
LL | x = x; // will suggest add `let`
| ^
|
help: you might have meant to introduce a new binding
|
LL | let x = x; // will suggest add `let`
| +++

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:23:9
|
LL | x = x; // will suggest add `let`
| ^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:27:5
|
LL | x = y // will suggest add `let`
| ^
|
help: you might have meant to introduce a new binding
|
LL | let x = y // will suggest add `let`
| +++

error[E0425]: cannot find value `y` in this scope
--> $DIR/issue-104086-suggest-let.rs:27:9
|
LL | x = y // will suggest add `let`
| ^ not found in this scope

error: aborting due to 17 previous errors

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