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.
This commit is contained in:
Matthias Krüger 2023-09-27 10:42:35 +02:00 committed by GitHub
commit a6f066596b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 5 deletions

View File

@ -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. // Climb the HIR tree to see if the current `Expr` is part of a `break;` statement.
let Some( let Some(
hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Semi(&ref p), .. }) 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), | hir::Node::Expr(&ref p),
) = self.tcx.hir().find(parent_id) ) = self.tcx.hir().find(parent_id)
else { else {

View File

@ -11,8 +11,13 @@ LL | | while true { break }; // but here we cite the whole loop
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-27042.rs:6:16 --> $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 }; 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 help: give it a value of the expected type
| |

View File

@ -2,7 +2,10 @@ error[E0308]: mismatched types
--> $DIR/loop-labeled-break-value.rs:3:29 --> $DIR/loop-labeled-break-value.rs:3:29
| |
LL | let _: i32 = loop { break }; 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 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 --> $DIR/loop-labeled-break-value.rs:6:37
| |
LL | let _: i32 = 'inner: loop { break 'inner }; 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 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 --> $DIR/loop-labeled-break-value.rs:9:45
| |
LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } }; 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 help: give it a value of the expected type
| |

View File

@ -2,7 +2,10 @@ error[E0308]: mismatched types
--> $DIR/loop-properly-diverging-2.rs:2:23 --> $DIR/loop-properly-diverging-2.rs:2:23
| |
LL | let x: i32 = loop { break }; 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 help: give it a value of the expected type
| |