Skip to content

Commit c68aab3

Browse files
authored
Unrolled build for #148262
Rollup merge of #148262 - JonathanBrouwer:dead-code-inference, r=nnethercote Fix types being marked as dead when they are inferred generic arguments Previously usages of a type in a pattern were ignored. This is incorrect, since if the type is in a pattern we're clearly producing it in the expression we're matching against. I think this `in_pat` check was meant to be only for variants, which we should indeed ignore since we can just remove the match arm that matches the pattern. Please double check my logic here since this is my first time touching the dead-code pass and I'm not 100% sure this is what the `self.in_pat` check was for. Fixes #148144
2 parents ab4960e + a09c4fc commit c68aab3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
fn magic<T: Default>() -> Option<T> {
15+
Some(T::default())
16+
}

0 commit comments

Comments
 (0)