Skip to content

Commit ffc0e2e

Browse files
authored
Unrolled build for rust-lang#128443
Rollup merge of rust-lang#128443 - compiler-errors:async-unreachable, r=fmease Properly mark loop as diverging if it has no breaks Due to specifics about the desugaring of the `.await` operator, HIR typeck doesn't recognize that `.await`ing an `impl Future<Output = !>` will diverge in the same way as calling a `fn() -> !`. This is because the await operator desugars to approximately: ```rust loop { match future.poll(...) { Poll::Ready(x) => break x, Poll::Pending => {} } } ``` We know that the value of `x` is `!`, however since `break` is a coercion site, we coerce `!` to some `?0` (the type of the loop expression). Then since the type of the `loop {...}` expression is `?0`, we will not detect the loop as diverging like we do with other expressions that evaluate to `!`: https://github.com/rust-lang/rust/blob/0b5eb7ba7bd796fb39c8bb6acd9ef6c140f28b65/compiler/rustc_hir_typeck/src/expr.rs#L240-L243 We can technically fix this in two ways: 1. Make coercion of loop exprs more eagerly result in a type of `!` when the only break expressions have type `!`. 2. Make loops understand that all of that if they have only diverging break values, then the loop diverges as well. (1.) likely has negative effects on inference, and seems like a weird special case to drill into coercion. However, it turns out that (2.) is very easy to implement, we already record whether a loop has any break expressions, and when we do so, we actually skip over any break expressions with diverging values!: https://github.com/rust-lang/rust/blob/0b5eb7ba7bd796fb39c8bb6acd9ef6c140f28b65/compiler/rustc_hir_typeck/src/expr.rs#L713-L716 Thus, we can consider the loop as diverging if we see that it has no breaks, which is the change implemented in this PR. This is not usually a problem in regular code for two reasons: 1. In regular code, we already mark `break diverging()` as unreachable if `diverging()` is unreachable. We don't do this for `.await`, since we suppress unreachable errors within `.await` (rust-lang#64930). Un-suppressing this code will result in spurious unreachable expression errors pointing to internal await machinery. 3. In loops that truly have no breaks (e.g. `loop {}`), we already evaluate the type of the loop to `!`, so this special case is kinda moot. This only affects loops that have `break`s with values of type `!`. Thus, this seems like a change that may affect more code than just `.await`, but it likely does not in meaningful ways; if it does, it's certainly correct to apply. Fixes rust-lang#128434
2 parents 28a58f2 + 74754b8 commit ffc0e2e

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13061306
// No way to know whether it's diverging because
13071307
// of a `break` or an outer `break` or `return`.
13081308
self.diverges.set(Diverges::Maybe);
1309+
} else {
1310+
self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
13091311
}
13101312

13111313
// If we permit break with a value, then result type is

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+33-21
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4848
/// Produces warning on the given node, if the current point in the
4949
/// function is unreachable, and there hasn't been another warning.
5050
pub(crate) fn warn_if_unreachable(&self, id: HirId, span: Span, kind: &str) {
51-
// FIXME: Combine these two 'if' expressions into one once
52-
// let chains are implemented
53-
if let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() {
54-
// If span arose from a desugaring of `if` or `while`, then it is the condition itself,
55-
// which diverges, that we are about to lint on. This gives suboptimal diagnostics.
56-
// Instead, stop here so that the `if`- or `while`-expression's block is linted instead.
57-
if !span.is_desugaring(DesugaringKind::CondTemporary)
58-
&& !span.is_desugaring(DesugaringKind::Async)
59-
&& !orig_span.is_desugaring(DesugaringKind::Await)
60-
{
61-
self.diverges.set(Diverges::WarnedAlways);
51+
// If span arose from a desugaring of `if` or `while`, then it is the condition itself,
52+
// which diverges, that we are about to lint on. This gives suboptimal diagnostics.
53+
// Instead, stop here so that the `if`- or `while`-expression's block is linted instead.
54+
if span.is_desugaring(DesugaringKind::CondTemporary) {
55+
return;
56+
}
6257

63-
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
58+
// Don't lint if the result of an async block or async function is `!`.
59+
// This does not affect the unreachable lints *within* the body.
60+
if span.is_desugaring(DesugaringKind::Async) {
61+
return;
62+
}
6463

65-
let msg = format!("unreachable {kind}");
66-
self.tcx().node_span_lint(lint::builtin::UNREACHABLE_CODE, id, span, |lint| {
67-
lint.primary_message(msg.clone());
68-
lint.span_label(span, msg).span_label(
69-
orig_span,
70-
custom_note.unwrap_or("any code following this expression is unreachable"),
71-
);
72-
})
73-
}
64+
// Don't lint *within* the `.await` operator, since that's all just desugaring junk.
65+
// We only want to lint if there is a subsequent expression after the `.await`.
66+
if span.is_desugaring(DesugaringKind::Await) {
67+
return;
7468
}
69+
70+
let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() else {
71+
return;
72+
};
73+
74+
// Don't warn twice.
75+
self.diverges.set(Diverges::WarnedAlways);
76+
77+
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
78+
79+
let msg = format!("unreachable {kind}");
80+
self.tcx().node_span_lint(lint::builtin::UNREACHABLE_CODE, id, span, |lint| {
81+
lint.primary_message(msg.clone());
82+
lint.span_label(span, msg).span_label(
83+
orig_span,
84+
custom_note.unwrap_or("any code following this expression is unreachable"),
85+
);
86+
})
7587
}
7688

7789
/// Resolves type and const variables in `ty` if possible. Unlike the infcx
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ edition:2018
2+
3+
#![deny(unreachable_code)]
4+
5+
async fn foo() {
6+
endless().await;
7+
println!("this is unreachable!");
8+
//~^ ERROR unreachable statement
9+
}
10+
11+
async fn endless() -> ! {
12+
loop {}
13+
}
14+
15+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: unreachable statement
2+
--> $DIR/unreachable-lint-2.rs:7:5
3+
|
4+
LL | endless().await;
5+
| ----- any code following this expression is unreachable
6+
LL | println!("this is unreachable!");
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/unreachable-lint-2.rs:3:9
11+
|
12+
LL | #![deny(unreachable_code)]
13+
| ^^^^^^^^^^^^^^^^
14+
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
16+
error: aborting due to 1 previous error
17+

0 commit comments

Comments
 (0)