Skip to content

Commit e4f02e4

Browse files
committed
fix guard patterns interaction with never type
1 parent bf0970f commit e4f02e4

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,8 +1807,9 @@ impl<'hir> Pat<'hir> {
18071807
// Does not constitute a read.
18081808
PatKind::Wild => false,
18091809

1810-
// Might not constitute a read, since the condition might be false.
1811-
PatKind::Guard(_, _) => true,
1810+
// The guard cannot affect if we make a read or not (it runs after the inner pattern
1811+
// has matched), therefore it's irrelevant.
1812+
PatKind::Guard(pat, _) => pat.is_guaranteed_to_constitute_read_for_never(),
18121813

18131814
// This is unnecessarily restrictive when the pattern that doesn't
18141815
// constitute a read is unreachable.

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ impl<'tcx> TyCtxt<'tcx> {
259259
expr.hir_id != lhs.hir_id
260260
}
261261

262-
// See note on `PatKind::Or` below for why this is `all`.
262+
// See note on `PatKind::Or` in `Pat::is_guaranteed_to_constitute_read_for_never`
263+
// for why this is `all`.
263264
ExprKind::Match(scrutinee, arms, _) => {
264265
assert_eq!(scrutinee.hir_id, expr.hir_id);
265266
arms.iter().all(|arm| arm.pat.is_guaranteed_to_constitute_read_for_never())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for <https://github.com/rust-lang/rust/pull/149545#discussion_r2585205872>
2+
//
3+
//@ check-pass
4+
#![feature(guard_patterns, never_type)]
5+
#![expect(incomplete_features, unused_parens)]
6+
#![deny(unreachable_code)]
7+
8+
fn main() {
9+
unsafe {
10+
let x = std::ptr::null::<!>();
11+
12+
// This should not constitute a read for never, therefore no code here is unreachable
13+
let (_ if false): ! = *x;
14+
();
15+
}
16+
}

0 commit comments

Comments
 (0)