Skip to content

Commit ce136f8

Browse files
committed
Fix incorrect suggestion for uninitialize binding in destructuring pattern
1 parent b11fbfb commit ce136f8

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
613613
if self.sugg_span.is_some() {
614614
return;
615615
}
616-
if let hir::StmtKind::Local(hir::Local { span, ty, init: None, .. }) = &ex.kind
616+
617+
// FIXME: We make sure that this is a normal binding and not a destructuring assignment,
618+
// but we could suggest `todo!()` for all uninitalized bindings in the destructuring
619+
if let hir::StmtKind::Local(hir::Local { span, ty, init: None, pat, .. }) =
620+
&ex.kind
621+
&& let hir::PatKind::Binding(..) = pat.kind
617622
&& span.contains(self.decl_span)
618623
{
619624
self.sugg_span = ty.map_or(Some(self.decl_span), |ty| Some(ty.span));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// regression test for #120634
2+
3+
struct A(u8);
4+
struct B { d: u8 }
5+
6+
fn main() {
7+
let (a, );
8+
let [b, ];
9+
let A(c);
10+
let B { d };
11+
let _: (u8, u8, u8, u8) = (a, b, c, d);
12+
//~^ ERROR used binding `a`
13+
//~| ERROR used binding `b`
14+
//~| ERROR used binding `c`
15+
//~| ERROR used binding `d`
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0381]: used binding `a` isn't initialized
2+
--> $DIR/borrowck-uninit-binding-suggestion.rs:11:32
3+
|
4+
LL | let (a, );
5+
| - binding declared here but left uninitialized
6+
...
7+
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
8+
| ^ `a` used here but it isn't initialized
9+
10+
error[E0381]: used binding `b` isn't initialized
11+
--> $DIR/borrowck-uninit-binding-suggestion.rs:11:35
12+
|
13+
LL | let [b, ];
14+
| - binding declared here but left uninitialized
15+
...
16+
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
17+
| ^ `b` used here but it isn't initialized
18+
19+
error[E0381]: used binding `c` isn't initialized
20+
--> $DIR/borrowck-uninit-binding-suggestion.rs:11:38
21+
|
22+
LL | let A(c);
23+
| - binding declared here but left uninitialized
24+
LL | let B { d };
25+
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
26+
| ^ `c` used here but it isn't initialized
27+
28+
error[E0381]: used binding `d` isn't initialized
29+
--> $DIR/borrowck-uninit-binding-suggestion.rs:11:41
30+
|
31+
LL | let B { d };
32+
| - binding declared here but left uninitialized
33+
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
34+
| ^ `d` used here but it isn't initialized
35+
36+
error: aborting due to 4 previous errors
37+
38+
For more information about this error, try `rustc --explain E0381`.

0 commit comments

Comments
 (0)