catch never loops through diverging functions

This commit is contained in:
Mario Carneiro 2023-09-02 07:51:34 -04:00
parent 39b316db61
commit b3980d8497
3 changed files with 26 additions and 3 deletions

View file

@ -148,7 +148,7 @@ fn never_loop_expr<'tcx>(
local_labels: &mut Vec<(HirId, bool)>,
main_loop_id: HirId,
) -> NeverLoopResult {
match expr.kind {
let result = match expr.kind {
ExprKind::Unary(_, e)
| ExprKind::Cast(e, _)
| ExprKind::Type(e, _)
@ -262,7 +262,14 @@ fn never_loop_expr<'tcx>(
| ExprKind::ConstBlock(_)
| ExprKind::Lit(_)
| ExprKind::Err(_) => NeverLoopResult::Normal,
}
};
combine_seq(result, || {
if cx.typeck_results().expr_ty(expr).is_never() {
NeverLoopResult::Diverging
} else {
NeverLoopResult::Normal
}
})
}
fn never_loop_expr_all<'tcx, T: Iterator<Item = &'tcx Expr<'tcx>>>(

View file

@ -385,6 +385,13 @@ pub fn test31(b: bool) {
}
}
pub fn test32(b: bool) {
loop {
//~^ ERROR: this loop never actually loops
panic!("oh no");
}
}
fn main() {
test1();
test2();

View file

@ -161,5 +161,14 @@ LL | | if b { break 'c } else { break 'b }
LL | | }
| |_____________^
error: aborting due to 14 previous errors
error: this loop never actually loops
--> $DIR/never_loop.rs:389:5
|
LL | / loop {
LL | |
LL | | panic!("oh no");
LL | | }
| |_____^
error: aborting due to 15 previous errors