Skip to content

Commit 5af2270

Browse files
Fix types being marked as dead when they are inferred generic arguments
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent 292db4a commit 5af2270

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
125125
) => {
126126
self.check_def_id(def_id);
127127
}
128-
_ if self.in_pat => {}
129128
Res::PrimTy(..) | Res::SelfCtor(..) | Res::Local(..) => {}
130129
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), ctor_def_id) => {
130+
// Using a variant in patterns should not make the variant live,
131+
// since we can just remove the match arm that matches the pattern
132+
if self.in_pat {
133+
return;
134+
}
131135
let variant_id = self.tcx.parent(ctor_def_id);
132136
let enum_id = self.tcx.parent(variant_id);
133137
self.check_def_id(enum_id);
@@ -136,6 +140,11 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
136140
}
137141
}
138142
Res::Def(DefKind::Variant, variant_id) => {
143+
// Using a variant in patterns should not make the variant live,
144+
// since we can just remove the match arm that matches the pattern
145+
if self.in_pat {
146+
return;
147+
}
139148
let enum_id = self.tcx.parent(variant_id);
140149
self.check_def_id(enum_id);
141150
if !self.ignore_variant_stack.contains(&variant_id) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
3+
#![deny(dead_code)]
4+
5+
#[derive(Default)]
6+
struct Test {
7+
8+
}
9+
10+
fn main() {
11+
if let Some::<Test>(test) = magic::<Test>() {
12+
13+
}
14+
}
15+
16+
17+
fn magic<T: Default>() -> Option<T> {
18+
Some(T::default())
19+
}

0 commit comments

Comments
 (0)