Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 14 additions & 7 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1810,18 +1810,23 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
false
}

fn let_binding_suggestion(&self, err: &mut Diagnostic, ident_span: Span) -> bool {
fn let_binding_suggestion(&mut 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 &&
let ast::ExprKind::Path(None, _) = lhs.kind {
if let Some(Expr { kind: ExprKind::Assign(lhs, rhs, _), .. }) =
self.diagnostic_metadata.in_assignment
{
Comment thread
oli-obk marked this conversation as resolved.
Outdated
let is_rhs_assign = matches!(rhs.kind, ExprKind::Assign(..));
Comment thread
chenyukang marked this conversation as resolved.
Outdated
if let ast::ExprKind::Path(None, _) = lhs.kind && !is_rhs_assign {
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))
// HACK(chenyukang): make sure ident_name is at the starting of the line to protect against macros,
// and avoid some special cases like `x = x = x`
if let Ok(line) = sm.span_to_snippet(line_span) &&
let stripped = line.split_whitespace().collect::<String>() &&
stripped.trim().starts_with(&ident_name) &&
stripped.matches(&format!("{}=", &ident_name)).count() == 1
Comment thread
chenyukang marked this conversation as resolved.
Outdated
{
err.span_suggestion_verbose(
ident_span.shrink_to_lo(),
Expand All @@ -1832,6 +1837,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
added_suggestion = true;
}
}
self.diagnostic_metadata.in_assignment = None;
}
added_suggestion
}

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
}
115 changes: 115 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,115 @@
error[E0425]: cannot find value `x` in this scope
--> $DIR/issue-104086-suggest-let.rs:2:5
|
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: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;
| ^ not found in this scope

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;
| ^ not found in this scope

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;
| ^ not found in this scope

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`.