diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-09-27 10:42:35 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-27 10:42:35 +0200 |
| commit | a6f066596b9802ba5c9859c962c79fc50e26367f (patch) | |
| tree | 39895c1cc568d518eb6f8e6670f4bf0dc00664da | |
| parent | 243ce35b9fbad1fb024c85a96ef79ef9755935af (diff) | |
| parent | 4b15959218a4e856d95329d7752b1c55d5922de5 (diff) | |
| download | rust-a6f066596b9802ba5c9859c962c79fc50e26367f.tar.gz rust-a6f066596b9802ba5c9859c962c79fc50e26367f.zip | |
Rollup merge of #116187 - estebank:small-tweak, r=compiler-errors
Add context to `let: Ty = loop { break };`
We weren't accounting for the case where `break` was immediately within the `loop` block.
| -rw-r--r-- | compiler/rustc_hir_typeck/src/demand.rs | 1 | ||||
| -rw-r--r-- | tests/ui/issues/issue-27042.stderr | 7 | ||||
| -rw-r--r-- | tests/ui/loops/loop-labeled-break-value.stderr | 15 | ||||
| -rw-r--r-- | tests/ui/loops/loop-properly-diverging-2.stderr | 5 |
4 files changed, 23 insertions, 5 deletions
diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 31e773585d1..256a4bf9449 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -545,6 +545,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Climb the HIR tree to see if the current `Expr` is part of a `break;` statement. let Some( hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Semi(&ref p), .. }) + | hir::Node::Block(hir::Block { expr: Some(&ref p), .. }) | hir::Node::Expr(&ref p), ) = self.tcx.hir().find(parent_id) else { diff --git a/tests/ui/issues/issue-27042.stderr b/tests/ui/issues/issue-27042.stderr index 69eedeb8025..01532de999e 100644 --- a/tests/ui/issues/issue-27042.stderr +++ b/tests/ui/issues/issue-27042.stderr @@ -11,8 +11,13 @@ LL | | while true { break }; // but here we cite the whole loop error[E0308]: mismatched types --> $DIR/issue-27042.rs:6:16 | +LL | let _: i32 = + | - expected because of this assignment +LL | 'a: // in this case, the citation is just the `break`: LL | loop { break }; - | ^^^^^ expected `i32`, found `()` + | ---- ^^^^^ expected `i32`, found `()` + | | + | this loop is expected to be of type `i32` | help: give it a value of the expected type | diff --git a/tests/ui/loops/loop-labeled-break-value.stderr b/tests/ui/loops/loop-labeled-break-value.stderr index f233670e8c4..694d6c306f6 100644 --- a/tests/ui/loops/loop-labeled-break-value.stderr +++ b/tests/ui/loops/loop-labeled-break-value.stderr @@ -2,7 +2,10 @@ error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:3:29 | LL | let _: i32 = loop { break }; - | ^^^^^ expected `i32`, found `()` + | - ---- ^^^^^ expected `i32`, found `()` + | | | + | | this loop is expected to be of type `i32` + | expected because of this assignment | help: give it a value of the expected type | @@ -13,7 +16,10 @@ error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:6:37 | LL | let _: i32 = 'inner: loop { break 'inner }; - | ^^^^^^^^^^^^ expected `i32`, found `()` + | - ---- ^^^^^^^^^^^^ expected `i32`, found `()` + | | | + | | this loop is expected to be of type `i32` + | expected because of this assignment | help: give it a value of the expected type | @@ -24,7 +30,10 @@ error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:9:45 | LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } }; - | ^^^^^^^^^^^^^ expected `i32`, found `()` + | - ---- ^^^^^^^^^^^^^ expected `i32`, found `()` + | | | + | | this loop is expected to be of type `i32` + | expected because of this assignment | help: give it a value of the expected type | diff --git a/tests/ui/loops/loop-properly-diverging-2.stderr b/tests/ui/loops/loop-properly-diverging-2.stderr index dc60657ee14..1d1ae60cda1 100644 --- a/tests/ui/loops/loop-properly-diverging-2.stderr +++ b/tests/ui/loops/loop-properly-diverging-2.stderr @@ -2,7 +2,10 @@ error[E0308]: mismatched types --> $DIR/loop-properly-diverging-2.rs:2:23 | LL | let x: i32 = loop { break }; - | ^^^^^ expected `i32`, found `()` + | - ---- ^^^^^ expected `i32`, found `()` + | | | + | | this loop is expected to be of type `i32` + | expected because of this assignment | help: give it a value of the expected type | |
