Skip to content

Commit d504f70

Browse files
committed
Unconditionally lower match arm even if it's unneeded for never pattern in match
1 parent a18bd8a commit d504f70

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
672672
let span = self.lower_span(arm.span);
673673
self.lower_attrs(hir_id, &arm.attrs);
674674
let is_never_pattern = pat.is_never_pattern();
675-
let body = if let Some(body) = &arm.body
675+
// We need to lower the body even if it's unneeded for never pattern in match,
676+
// ensure that we can get HirId for DefId if need (issue #137708).
677+
let body = arm.body.as_ref().map(|x| self.lower_expr(x));
678+
let body = if let Some(body) = body
676679
&& !is_never_pattern
677680
{
678-
self.lower_expr(body)
681+
body
679682
} else {
680683
// Either `body.is_none()` or `is_never_pattern` here.
681684
if !is_never_pattern {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn a() {
2+
match 0 {
3+
! => || { //~ ERROR `!` patterns are experimental
4+
//~^ ERROR a never pattern is always unreachable
5+
//~^^ ERROR mismatched types
6+
use std::ops::Add;
7+
0.add(1)
8+
},
9+
}
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0658]: `!` patterns are experimental
2+
--> $DIR/unused_trait_in_never_pattern_body.rs:3:9
3+
|
4+
LL | ! => || {
5+
| ^
6+
|
7+
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
8+
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: a never pattern is always unreachable
12+
--> $DIR/unused_trait_in_never_pattern_body.rs:3:14
13+
|
14+
LL | ! => || {
15+
| ______________^
16+
LL | |
17+
LL | |
18+
LL | | use std::ops::Add;
19+
LL | | 0.add(1)
20+
LL | | },
21+
| | ^
22+
| | |
23+
| |_________this will never be executed
24+
| help: remove this expression
25+
26+
error: mismatched types
27+
--> $DIR/unused_trait_in_never_pattern_body.rs:3:9
28+
|
29+
LL | ! => || {
30+
| ^ a never pattern must be used on an uninhabited type
31+
|
32+
= note: the matched value is of type `i32`
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)