Skip to content

Commit c56c9fc

Browse files
committed
rustc: Remove the dummy hack from check_match
Turns out you can create &'static T quite easily in a constant, I just forgot about this!
1 parent 18e4129 commit c56c9fc

File tree

4 files changed

+25
-42
lines changed

4 files changed

+25
-42
lines changed

src/librustc/middle/check_match.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use syntax::ptr::P;
3232
use syntax::visit::{mod, Visitor, FnKind};
3333
use util::ppaux::ty_to_string;
3434

35-
pub const DUMMY_WILD_PAT: Pat = Pat {
35+
pub const DUMMY_WILD_PAT: &'static Pat = &Pat {
3636
id: DUMMY_NODE_ID,
3737
node: PatWild(PatWildSingle),
3838
span: DUMMY_SP
@@ -297,12 +297,11 @@ fn raw_pat<'a>(p: &'a Pat) -> &'a Pat {
297297
}
298298

299299
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix) {
300-
match is_useful(cx, matrix, &[&DUMMY_WILD_PAT], ConstructWitness) {
300+
match is_useful(cx, matrix, &[DUMMY_WILD_PAT], ConstructWitness) {
301301
UsefulWithWitness(pats) => {
302-
let dummy = DUMMY_WILD_PAT.clone();
303302
let witness = match pats.as_slice() {
304303
[ref witness] => &**witness,
305-
[] => &dummy,
304+
[] => DUMMY_WILD_PAT,
306305
_ => unreachable!()
307306
};
308307
span_err!(cx.tcx.sess, sp, E0004,
@@ -556,9 +555,8 @@ fn is_useful(cx: &MatchCheckCtxt,
556555
let arity = constructor_arity(cx, &c, left_ty);
557556
let mut result = {
558557
let pat_slice = pats.as_slice();
559-
let dummy = DUMMY_WILD_PAT.clone();
560558
let subpats = Vec::from_fn(arity, |i| {
561-
pat_slice.get(i).map_or(&dummy, |p| &**p)
559+
pat_slice.get(i).map_or(DUMMY_WILD_PAT, |p| &**p)
562560
});
563561
vec![construct_witness(cx, &c, subpats, left_ty)]
564562
};
@@ -580,9 +578,8 @@ fn is_useful(cx: &MatchCheckCtxt,
580578
}).collect();
581579
match is_useful(cx, &matrix, v.tail(), witness) {
582580
UsefulWithWitness(pats) => {
583-
let dummy = DUMMY_WILD_PAT.clone();
584581
let arity = constructor_arity(cx, &constructor, left_ty);
585-
let wild_pats = Vec::from_elem(arity, &dummy);
582+
let wild_pats = Vec::from_elem(arity, DUMMY_WILD_PAT);
586583
let enum_pat = construct_witness(cx, &constructor, wild_pats, left_ty);
587584
let mut new_pats = vec![enum_pat];
588585
new_pats.extend(pats.into_iter());
@@ -603,11 +600,10 @@ fn is_useful_specialized(cx: &MatchCheckCtxt, &Matrix(ref m): &Matrix,
603600
v: &[&Pat], ctor: Constructor, lty: ty::t,
604601
witness: WitnessPreference) -> Usefulness {
605602
let arity = constructor_arity(cx, &ctor, lty);
606-
let dummy = DUMMY_WILD_PAT.clone();
607603
let matrix = Matrix(m.iter().filter_map(|r| {
608-
specialize(cx, r.as_slice(), &dummy, &ctor, 0u, arity)
604+
specialize(cx, r.as_slice(), &ctor, 0u, arity)
609605
}).collect());
610-
match specialize(cx, v, &dummy, &ctor, 0u, arity) {
606+
match specialize(cx, v, &ctor, 0u, arity) {
611607
Some(v) => is_useful(cx, &matrix, v.as_slice(), witness),
612608
None => NotUseful
613609
}
@@ -729,15 +725,15 @@ fn range_covered_by_constructor(ctor: &Constructor,
729725
/// different patterns.
730726
/// Structure patterns with a partial wild pattern (Foo { a: 42, .. }) have their missing
731727
/// fields filled with wild patterns.
732-
pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
728+
pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
733729
constructor: &Constructor, col: uint, arity: uint) -> Option<Vec<&'a Pat>> {
734730
let &Pat {
735731
id: pat_id, node: ref node, span: pat_span
736732
} = raw_pat(r[col]);
737733
let head: Option<Vec<&Pat>> = match node {
738734

739735
&PatWild(_) =>
740-
Some(Vec::from_elem(arity, dummy)),
736+
Some(Vec::from_elem(arity, DUMMY_WILD_PAT)),
741737

742738
&PatIdent(_, _, _) => {
743739
let opt_def = cx.tcx.def_map.borrow().find_copy(&pat_id);
@@ -750,7 +746,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
750746
} else {
751747
None
752748
},
753-
_ => Some(Vec::from_elem(arity, dummy))
749+
_ => Some(Vec::from_elem(arity, DUMMY_WILD_PAT))
754750
}
755751
}
756752

@@ -764,7 +760,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
764760
DefVariant(..) | DefStruct(..) => {
765761
Some(match args {
766762
&Some(ref args) => args.iter().map(|p| &**p).collect(),
767-
&None => Vec::from_elem(arity, dummy)
763+
&None => Vec::from_elem(arity, DUMMY_WILD_PAT)
768764
})
769765
}
770766
_ => None
@@ -800,7 +796,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
800796
let args = struct_fields.iter().map(|sf| {
801797
match pattern_fields.iter().find(|f| f.ident.name == sf.name) {
802798
Some(ref f) => &*f.pat,
803-
_ => dummy
799+
_ => DUMMY_WILD_PAT
804800
}
805801
}).collect();
806802
args
@@ -843,13 +839,13 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
843839
// Fixed-length vectors.
844840
Single => {
845841
let mut pats: Vec<&Pat> = before.iter().map(|p| &**p).collect();
846-
pats.grow_fn(arity - before.len() - after.len(), |_| dummy);
842+
pats.grow_fn(arity - before.len() - after.len(), |_| DUMMY_WILD_PAT);
847843
pats.extend(after.iter().map(|p| &**p));
848844
Some(pats)
849845
},
850846
Slice(length) if before.len() + after.len() <= length && slice.is_some() => {
851847
let mut pats: Vec<&Pat> = before.iter().map(|p| &**p).collect();
852-
pats.grow_fn(arity - before.len() - after.len(), |_| dummy);
848+
pats.grow_fn(arity - before.len() - after.len(), |_| DUMMY_WILD_PAT);
853849
pats.extend(after.iter().map(|p| &**p));
854850
Some(pats)
855851
},
@@ -919,7 +915,7 @@ fn check_fn(cx: &mut MatchCheckCtxt,
919915

920916
fn is_refutable<A>(cx: &MatchCheckCtxt, pat: &Pat, refutable: |&Pat| -> A) -> Option<A> {
921917
let pats = Matrix(vec!(vec!(pat)));
922-
match is_useful(cx, &pats, [&DUMMY_WILD_PAT], ConstructWitness) {
918+
match is_useful(cx, &pats, [DUMMY_WILD_PAT], ConstructWitness) {
923919
UsefulWithWitness(pats) => {
924920
assert_eq!(pats.len(), 1);
925921
Some(refutable(&*pats[0]))

src/librustc/middle/trans/_match.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,6 @@ struct Match<'a, 'p: 'a, 'blk: 'a, 'tcx: 'blk> {
351351
pats: Vec<&'p ast::Pat>,
352352
data: &'a ArmData<'p, 'blk, 'tcx>,
353353
bound_ptrs: Vec<(Ident, ValueRef)>,
354-
355-
// This is a pointer to an instance of check_match::DUMMY_WILD_PAT. The
356-
// check_match code requires that we pass this in (with the same lifetime as
357-
// the patterns passed in). Unfortunately this is required to be propagated
358-
// into this structure in order to get the lifetimes to work.
359-
//
360-
// Lots of the `check_match` code will deal with &DUMMY_WILD_PAT when
361-
// returning references, which used to have the `'static` lifetime before
362-
// const was added to the language. The DUMMY_WILD_PAT does not implement
363-
// Sync, however, so it must be a const, which longer has a static lifetime,
364-
// hence we're passing it in here. This certainly isn't crucial, and if it
365-
// can be removed, please do!
366-
dummy: &'p ast::Pat,
367354
}
368355

369356
impl<'a, 'p, 'blk, 'tcx> Repr for Match<'a, 'p, 'blk, 'tcx> {
@@ -416,7 +403,6 @@ fn expand_nested_bindings<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
416403
*pats.get_mut(col) = pat;
417404
Match {
418405
pats: pats,
419-
dummy: br.dummy,
420406
data: &*br.data,
421407
bound_ptrs: bound_ptrs
422408
}
@@ -464,7 +450,6 @@ fn enter_match<'a, 'b, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
464450
}
465451
Match {
466452
pats: pats,
467-
dummy: br.dummy,
468453
data: br.data,
469454
bound_ptrs: bound_ptrs
470455
}
@@ -559,8 +544,7 @@ fn enter_opt<'a, 'p, 'blk, 'tcx>(
559544

560545
let mcx = check_match::MatchCheckCtxt { tcx: bcx.tcx() };
561546
enter_match(bcx, dm, m, col, val, |pats|
562-
check_match::specialize(&mcx, pats.as_slice(), m[0].dummy, &ctor, col,
563-
variant_size)
547+
check_match::specialize(&mcx, pats.as_slice(), &ctor, col, variant_size)
564548
)
565549
}
566550

@@ -1041,7 +1025,7 @@ fn compile_submatch_continue<'a, 'p, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
10411025
match adt_vals {
10421026
Some(field_vals) => {
10431027
let pats = enter_match(bcx, dm, m, col, val, |pats|
1044-
check_match::specialize(&mcx, pats, m[0].dummy,
1028+
check_match::specialize(&mcx, pats,
10451029
&check_match::Single, col,
10461030
field_vals.len())
10471031
);
@@ -1365,7 +1349,6 @@ fn trans_match_inner<'blk, 'tcx>(scope_cx: Block<'blk, 'tcx>,
13651349
bindings_map: create_bindings_map(bcx, &**arm.pats.get(0), discr_expr, &*arm.body)
13661350
}).collect();
13671351

1368-
let dummy = check_match::DUMMY_WILD_PAT.clone();
13691352
let mut static_inliner = StaticInliner::new(scope_cx.tcx());
13701353
let arm_pats: Vec<Vec<P<ast::Pat>>> = arm_datas.iter().map(|arm_data| {
13711354
arm_data.arm.pats.iter().map(|p| static_inliner.fold_pat((*p).clone())).collect()
@@ -1374,7 +1357,6 @@ fn trans_match_inner<'blk, 'tcx>(scope_cx: Block<'blk, 'tcx>,
13741357
for (arm_data, pats) in arm_datas.iter().zip(arm_pats.iter()) {
13751358
matches.extend(pats.iter().map(|p| Match {
13761359
pats: vec![&**p],
1377-
dummy: &dummy,
13781360
data: arm_data,
13791361
bound_ptrs: Vec::new(),
13801362
}));

src/test/compile-fail/issue-17718-const-naming.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212

1313
const foo: int = 3;
1414
//~^ ERROR: should have an uppercase name such as
15+
//~^^ ERROR: constant item is never used
1516

1617
fn main() {}

src/test/compile-fail/lint-dead-code-1.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ pub const pub_const: int = 0;
3939
const priv_const: int = 0; //~ ERROR: constant item is never used
4040
const used_const: int = 0;
4141
pub const used_const2: int = used_const;
42-
const USED_CONST: int = 0;
43-
const CONST_USED_IN_ENUM_DISCRIMINANT: int = 10;
42+
const USED_CONST: int = 1;
43+
const CONST_USED_IN_ENUM_DISCRIMINANT: int = 11;
4444

4545
pub type typ = *const UsedStruct4;
4646
pub struct PubStruct;
@@ -68,7 +68,10 @@ pub struct PubStruct2 {
6868

6969
pub enum pub_enum { foo1, bar1 }
7070
pub enum pub_enum2 { a(*const StructUsedInEnum) }
71-
pub enum pub_enum3 { Foo = STATIC_USED_IN_ENUM_DISCRIMINANT }
71+
pub enum pub_enum3 {
72+
Foo = STATIC_USED_IN_ENUM_DISCRIMINANT,
73+
Bar = CONST_USED_IN_ENUM_DISCRIMINANT,
74+
}
7275

7376
enum priv_enum { foo2, bar2 } //~ ERROR: enum is never used
7477
enum used_enum {
@@ -89,6 +92,7 @@ pub fn pub_fn() {
8992
let i = 1i;
9093
match i {
9194
USED_STATIC => (),
95+
USED_CONST => (),
9296
_ => ()
9397
}
9498
f::<StructUsedInGeneric>();

0 commit comments

Comments
 (0)