about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-08-18 19:54:59 +0200
committerGitHub <noreply@github.com>2021-08-18 19:54:59 +0200
commit016f691068fee70a99d2c0024ebe26b3e10f02d5 (patch)
treeb9fb7f2e677c1ec818ee22c98abb7c7126fc4a6d
parent9b7c771713ec2678f9dbd7a398f3759b253d960e (diff)
parente62ecdc5a7cbd44a9ade5061ddad4d8a4e1d6599 (diff)
downloadrust-016f691068fee70a99d2c0024ebe26b3e10f02d5.tar.gz
rust-016f691068fee70a99d2c0024ebe26b3e10f02d5.zip
Rollup merge of #88036 - nbdd0121:const3, r=petrochenkov
Fix dead code warning when inline const is used in pattern

Fixes #78171
-rw-r--r--compiler/rustc_passes/src/dead.rs7
-rw-r--r--src/test/ui/lint/dead-code/anon-const-in-pat.rs45
2 files changed, 52 insertions, 0 deletions
diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs
index 4e157b0a574..ae65222f3f2 100644
--- a/compiler/rustc_passes/src/dead.rs
+++ b/compiler/rustc_passes/src/dead.rs
@@ -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
@@ -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;
     }
 }
 
diff --git a/src/test/ui/lint/dead-code/anon-const-in-pat.rs b/src/test/ui/lint/dead-code/anon-const-in-pat.rs
new file mode 100644
index 00000000000..4c6211a279a
--- /dev/null
+++ b/src/test/ui/lint/dead-code/anon-const-in-pat.rs
@@ -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();
+}