Skip to content

Commit 016f691

Browse files
Rollup merge of #88036 - nbdd0121:const3, r=petrochenkov
Fix dead code warning when inline const is used in pattern Fixes #78171
2 parents 9b7c771 + e62ecdc commit 016f691

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

compiler/rustc_passes/src/dead.rs

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_middle::middle::privacy;
1515
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
1616
use rustc_session::lint;
1717
use rustc_span::symbol::{sym, Symbol};
18+
use std::mem;
1819

1920
// Any local node that may call something in its body block should be
2021
// explored. For example, if it's a live Node::Item that is a
@@ -395,8 +396,14 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
395396
}
396397

397398
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
399+
// When inline const blocks are used in pattern position, paths
400+
// referenced by it should be considered as used.
401+
let in_pat = mem::replace(&mut self.in_pat, false);
402+
398403
self.live_symbols.insert(self.tcx.hir().local_def_id(c.hir_id));
399404
intravisit::walk_anon_const(self, c);
405+
406+
self.in_pat = in_pat;
400407
}
401408
}
402409

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// check-pass
2+
#![feature(inline_const)]
3+
#![allow(incomplete_features)]
4+
#![deny(dead_code)]
5+
6+
const fn one() -> i32 {
7+
1
8+
}
9+
10+
const fn two() -> i32 {
11+
2
12+
}
13+
14+
const fn three() -> i32 {
15+
3
16+
}
17+
18+
fn inline_const() {
19+
// rust-lang/rust#78171: dead_code lint triggers even though function is used in const pattern
20+
match 1 {
21+
const { one() } => {}
22+
_ => {}
23+
}
24+
}
25+
26+
fn inline_const_range() {
27+
match 1 {
28+
1 ..= const { two() } => {}
29+
_ => {}
30+
}
31+
}
32+
33+
struct S<const C: i32>;
34+
35+
fn const_generic_arg() {
36+
match S::<3> {
37+
S::<{three()}> => {}
38+
}
39+
}
40+
41+
fn main() {
42+
inline_const();
43+
inline_const_range();
44+
const_generic_arg();
45+
}

0 commit comments

Comments
 (0)