Skip to content

Commit d3fc23a

Browse files
Rollup merge of rust-lang#136869 - chenyukang:yukang-fix-133713-let-binding, r=estebank
Fix diagnostic when using = instead of : in let binding Fixes rust-lang#133713 r? `@estebank`
2 parents fb41f2d + a917fd5 commit d3fc23a

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -2347,9 +2347,14 @@ 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 ident_span.from_expansion() {
2351+
return false;
2352+
}
2353+
2354+
// only suggest when the code is a assignment without prefix code
23502355
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) = self.diag_metadata.in_assignment
23512356
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
2352-
&& !ident_span.from_expansion()
2357+
&& self.r.tcx.sess.source_map().is_line_before_span_empty(ident_span)
23532358
{
23542359
let (span, text) = match path.segments.first() {
23552360
Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
@@ -2368,6 +2373,22 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
23682373
);
23692374
return true;
23702375
}
2376+
2377+
// a special case for #133713
2378+
// '=' maybe a typo of `:`, which is a type annotation instead of assignment
2379+
if err.code == Some(E0423)
2380+
&& let Some((let_span, None, Some(val_span))) = self.diag_metadata.current_let_binding
2381+
&& val_span.contains(ident_span)
2382+
&& val_span.lo() == ident_span.lo()
2383+
{
2384+
err.span_suggestion_verbose(
2385+
let_span.shrink_to_hi().to(val_span.shrink_to_lo()),
2386+
"you might have meant to use `:` for type annotation",
2387+
": ",
2388+
Applicability::MaybeIncorrect,
2389+
);
2390+
return true;
2391+
}
23712392
false
23722393
}
23732394

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)