Skip to content

Commit 472bc0e

Browse files
authored
Rollup merge of rust-lang#137742 - mu001999-contrib:fix-137708, r=compiler-errors
unconditionally lower match arm even if it's unneeded for never pattern in match fixes rust-lang#137708 Lowering arm body is skipped when lowering match arm with never pattern, but we may need the HirId for DefId in the body in later passes. And then we got the ICE `No HirId for DefId`. Fixes this by lowering the arm body even if it's unneeded for never pattern in match, so that we can generate HirId and use it then. r? `@compiler-errors`
2 parents ecfb7d2 + d504f70 commit 472bc0e

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
@@ -671,10 +671,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
671671
let span = self.lower_span(arm.span);
672672
self.lower_attrs(hir_id, &arm.attrs, arm.span);
673673
let is_never_pattern = pat.is_never_pattern();
674-
let body = if let Some(body) = &arm.body
674+
// We need to lower the body even if it's unneeded for never pattern in match,
675+
// ensure that we can get HirId for DefId if need (issue #137708).
676+
let body = arm.body.as_ref().map(|x| self.lower_expr(x));
677+
let body = if let Some(body) = body
675678
&& !is_never_pattern
676679
{
677-
self.lower_expr(body)
680+
body
678681
} else {
679682
// Either `body.is_none()` or `is_never_pattern` here.
680683
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)