Skip to content

Commit fc05d76

Browse files
committed
Fix diagnostic when using = instead of : in let bindings
1 parent ffa9afe commit fc05d76

File tree

4 files changed

+91
-19
lines changed

4 files changed

+91
-19
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+40-19
Original file line numberDiff line numberDiff line change
@@ -2347,26 +2347,47 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
23472347
// try to give a suggestion for this pattern: `name = blah`, which is common in other languages
23482348
// suggest `let name = blah` to introduce a new binding
23492349
fn let_binding_suggestion(&mut self, err: &mut Diag<'_>, ident_span: Span) -> bool {
2350-
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) = self.diag_metadata.in_assignment
2351-
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
2352-
&& !ident_span.from_expansion()
2353-
{
2354-
let (span, text) = match path.segments.first() {
2355-
Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
2356-
// a special case for #117894
2357-
let name = name.strip_prefix('_').unwrap_or(name);
2358-
(ident_span, format!("let {name}"))
2359-
}
2360-
_ => (ident_span.shrink_to_lo(), "let ".to_string()),
2361-
};
2350+
if !ident_span.from_expansion() {
2351+
// only suggest when the code is a assignment without prefix code
2352+
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) =
2353+
self.diag_metadata.in_assignment
2354+
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
2355+
&& self.r.tcx.sess.source_map().is_line_before_span_empty(ident_span)
2356+
{
2357+
let (span, text) = match path.segments.first() {
2358+
Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
2359+
// a special case for #117894
2360+
let name = name.strip_prefix('_').unwrap_or(name);
2361+
(ident_span, format!("let {name}"))
2362+
}
2363+
_ => (ident_span.shrink_to_lo(), "let ".to_string()),
2364+
};
23622365

2363-
err.span_suggestion_verbose(
2364-
span,
2365-
"you might have meant to introduce a new binding",
2366-
text,
2367-
Applicability::MaybeIncorrect,
2368-
);
2369-
return true;
2366+
err.span_suggestion_verbose(
2367+
span,
2368+
"you might have meant to introduce a new binding",
2369+
text,
2370+
Applicability::MaybeIncorrect,
2371+
);
2372+
return true;
2373+
} else {
2374+
// a special case for #133713
2375+
// '=' maybe a typo of `:`, which is a type annotation instead of assignment
2376+
if err.code == Some(E0423)
2377+
&& let Some((let_span, _, _)) = self.diag_metadata.current_let_binding
2378+
&& let span = let_span.shrink_to_hi().to(ident_span.shrink_to_lo())
2379+
&& let Ok(code) = self.r.tcx.sess.source_map().span_to_snippet(span)
2380+
&& code.trim() == "="
2381+
{
2382+
err.span_suggestion_verbose(
2383+
span,
2384+
"you might have meant to use `:` for type annotation",
2385+
": ",
2386+
Applicability::MaybeIncorrect,
2387+
);
2388+
return true;
2389+
}
2390+
}
23702391
}
23712392
false
23722393
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ run-rustfix
2+
#![allow(dead_code)]
3+
4+
fn demo1() {
5+
let _last: u64 = 0; //~ ERROR expected value, found builtin type `u64`
6+
}
7+
8+
fn demo2() {
9+
let _val: u64; //~ ERROR expected value, found builtin type `u64`
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ run-rustfix
2+
#![allow(dead_code)]
3+
4+
fn demo1() {
5+
let _last = u64 = 0; //~ ERROR expected value, found builtin type `u64`
6+
}
7+
8+
fn demo2() {
9+
let _val = u64; //~ ERROR expected value, found builtin type `u64`
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0423]: expected value, found builtin type `u64`
2+
--> $DIR/let-binding-suggest-issue-133713.rs:5:17
3+
|
4+
LL | let _last = u64 = 0;
5+
| ^^^
6+
|
7+
help: you might have meant to use `:` for type annotation
8+
|
9+
LL - let _last = u64 = 0;
10+
LL + let _last: u64 = 0;
11+
|
12+
13+
error[E0423]: expected value, found builtin type `u64`
14+
--> $DIR/let-binding-suggest-issue-133713.rs:9:16
15+
|
16+
LL | let _val = u64;
17+
| ^^^
18+
|
19+
help: you might have meant to use `:` for type annotation
20+
|
21+
LL - let _val = u64;
22+
LL + let _val: u64;
23+
|
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)