Skip to content

Commit

Permalink
Rollup merge of rust-lang#88036 - nbdd0121:const3, r=petrochenkov
Browse files Browse the repository at this point in the history
Fix dead code warning when inline const is used in pattern

Fixes rust-lang#78171
  • Loading branch information
camsteffen committed Aug 15, 2021
2 parents 7859dbd + e62ecdc commit 34deae2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc_middle::middle::privacy;
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_session::lint;
use rustc_span::symbol::{sym, Symbol};
use std::mem;

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

fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
// When inline const blocks are used in pattern position, paths
// referenced by it should be considered as used.
let in_pat = mem::replace(&mut self.in_pat, false);

self.live_symbols.insert(self.tcx.hir().local_def_id(c.hir_id));
intravisit::walk_anon_const(self, c);

self.in_pat = in_pat;
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/test/ui/lint/dead-code/anon-const-in-pat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// check-pass
#![feature(inline_const)]
#![allow(incomplete_features)]
#![deny(dead_code)]

const fn one() -> i32 {
1
}

const fn two() -> i32 {
2
}

const fn three() -> i32 {
3
}

fn inline_const() {
// rust-lang/rust#78171: dead_code lint triggers even though function is used in const pattern
match 1 {
const { one() } => {}
_ => {}
}
}

fn inline_const_range() {
match 1 {
1 ..= const { two() } => {}
_ => {}
}
}

struct S<const C: i32>;

fn const_generic_arg() {
match S::<3> {
S::<{three()}> => {}
}
}

fn main() {
inline_const();
inline_const_range();
const_generic_arg();
}

0 comments on commit 34deae2

Please sign in to comment.