Skip to content

Commit

Permalink
Use diverges instead of !-type
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Oct 22, 2020
1 parent d415fae commit d1c2815
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// If we encountered a `break`, then (no surprise) it may be possible to break from the
// loop... unless the value being returned from the loop diverges itself, e.g.
// `break return 5` or `break loop {}`.
ctxt.may_break |= !e_ty.is_never();
ctxt.may_break |= !self.diverges.get().is_always();

// the type of a `break` is always `!`, since it diverges
tcx.types.never
Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/break-diverging-value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ fn loop_break_break() -> i32 { //~ ERROR mismatched types
let loop_value = loop { break break };
}

fn loop_break_return_2() -> i32 { //~ ERROR mismatched types
let loop_value = loop { break { return; () } };
//~^ ERROR `return;` in a function whose return type is not `()`
fn loop_break_return_2() -> i32 {
let loop_value = loop { break { return 0; () } }; // ok
}

enum Void {}
Expand Down
21 changes: 3 additions & 18 deletions src/test/ui/break-diverging-value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,14 @@ LL | fn loop_break_break() -> i32 {
| |
| implicitly returns `()` as its body has no tail or `return` expression

error[E0069]: `return;` in a function whose return type is not `()`
--> $DIR/break-diverging-value.rs:16:37
|
LL | let loop_value = loop { break { return; () } };
| ^^^^^^ return type is not `()`

error[E0308]: mismatched types
--> $DIR/break-diverging-value.rs:15:29
|
LL | fn loop_break_return_2() -> i32 {
| ------------------- ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression

error[E0308]: mismatched types
--> $DIR/break-diverging-value.rs:26:25
--> $DIR/break-diverging-value.rs:25:25
|
LL | fn loop_break_void() -> i32 {
| --------------- ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0069, E0308.
For more information about an error, try `rustc --explain E0069`.
For more information about this error, try `rustc --explain E0308`.

0 comments on commit d1c2815

Please sign in to comment.