Skip to content

Commit 3d5fb7d

Browse files
authored
Unrolled build for #156497
Rollup merge of #156497 - lokirithm:fix-155516, r=cjgillot fix-155516: Don't suggest wrong unwrap expect Fixes #155516 r? Kivooeo
2 parents 485ec3f + 9633071 commit 3d5fb7d

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22722272
expected: Ty<'tcx>,
22732273
found: Ty<'tcx>,
22742274
) -> bool {
2275+
// don't suggest missing `.expect()` or `?` in destructuring assignments LHS.
2276+
// If the immediate parent is an Assign Expr, and the LHS and the RHS of that Expr
2277+
// overlap with each other, it's guaranteed that the expression came from desugaring
2278+
// a destructuring assignment.
2279+
let parent_node = self.tcx.parent_hir_node(expr.hir_id);
2280+
if let hir::Node::Expr(e) = parent_node
2281+
&& let hir::ExprKind::Assign(lhs, rhs, _) = e.kind
2282+
&& rhs.hir_id == expr.hir_id
2283+
&& lhs.span.overlaps(rhs.span)
2284+
{
2285+
return false;
2286+
}
2287+
22752288
let ty::Adt(adt, args) = found.kind() else {
22762289
return false;
22772290
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! regression test for <https://github.com/rust-lang/rust/issues/155516>
2+
3+
struct NamedStruct {
4+
opt_field: Option<usize>,
5+
res_field: Result<usize, ()>,
6+
}
7+
8+
fn main() {
9+
let a: usize;
10+
let b: usize;
11+
12+
NamedStruct {
13+
opt_field: a, //~ ERROR mismatched types
14+
res_field: b, //~ ERROR mismatched types
15+
} = NamedStruct {
16+
opt_field: Some(0),
17+
res_field: Ok(42_usize),
18+
};
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/dont-suggest-wrong-expect-suggestion.rs:13:20
3+
|
4+
LL | let a: usize;
5+
| ----- expected due to this type
6+
...
7+
LL | opt_field: a,
8+
| ^ expected `usize`, found `Option<usize>`
9+
|
10+
= note: expected type `usize`
11+
found enum `Option<usize>`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/dont-suggest-wrong-expect-suggestion.rs:14:20
15+
|
16+
LL | let b: usize;
17+
| ----- expected due to this type
18+
...
19+
LL | res_field: b,
20+
| ^ expected `usize`, found `Result<usize, ()>`
21+
|
22+
= note: expected type `usize`
23+
found enum `Result<usize, ()>`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)