Skip to content

Commit 412f4d1

Browse files
committed
rollup merge of rust-lang#17927 : alexcrichton/more-const
2 parents e9f241b + c56c9fc commit 412f4d1

File tree

6 files changed

+34
-41
lines changed

6 files changed

+34
-41
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
@@ -309,12 +309,11 @@ fn raw_pat<'a>(p: &'a Pat) -> &'a Pat {
309309
}
310310

311311
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix) {
312-
match is_useful(cx, matrix, &[&DUMMY_WILD_PAT], ConstructWitness) {
312+
match is_useful(cx, matrix, &[DUMMY_WILD_PAT], ConstructWitness) {
313313
UsefulWithWitness(pats) => {
314-
let dummy = DUMMY_WILD_PAT.clone();
315314
let witness = match pats.as_slice() {
316315
[ref witness] => &**witness,
317-
[] => &dummy,
316+
[] => DUMMY_WILD_PAT,
318317
_ => unreachable!()
319318
};
320319
span_err!(cx.tcx.sess, sp, E0004,
@@ -568,9 +567,8 @@ fn is_useful(cx: &MatchCheckCtxt,
568567
let arity = constructor_arity(cx, &c, left_ty);
569568
let mut result = {
570569
let pat_slice = pats.as_slice();
571-
let dummy = DUMMY_WILD_PAT.clone();
572570
let subpats = Vec::from_fn(arity, |i| {
573-
pat_slice.get(i).map_or(&dummy, |p| &**p)
571+
pat_slice.get(i).map_or(DUMMY_WILD_PAT, |p| &**p)
574572
});
575573
vec![construct_witness(cx, &c, subpats, left_ty)]
576574
};
@@ -592,9 +590,8 @@ fn is_useful(cx: &MatchCheckCtxt,
592590
}).collect();
593591
match is_useful(cx, &matrix, v.tail(), witness) {
594592
UsefulWithWitness(pats) => {
595-
let dummy = DUMMY_WILD_PAT.clone();
596593
let arity = constructor_arity(cx, &constructor, left_ty);
597-
let wild_pats = Vec::from_elem(arity, &dummy);
594+
let wild_pats = Vec::from_elem(arity, DUMMY_WILD_PAT);
598595
let enum_pat = construct_witness(cx, &constructor, wild_pats, left_ty);
599596
let mut new_pats = vec![enum_pat];
600597
new_pats.extend(pats.into_iter());
@@ -615,11 +612,10 @@ fn is_useful_specialized(cx: &MatchCheckCtxt, &Matrix(ref m): &Matrix,
615612
v: &[&Pat], ctor: Constructor, lty: ty::t,
616613
witness: WitnessPreference) -> Usefulness {
617614
let arity = constructor_arity(cx, &ctor, lty);
618-
let dummy = DUMMY_WILD_PAT.clone();
619615
let matrix = Matrix(m.iter().filter_map(|r| {
620-
specialize(cx, r.as_slice(), &dummy, &ctor, 0u, arity)
616+
specialize(cx, r.as_slice(), &ctor, 0u, arity)
621617
}).collect());
622-
match specialize(cx, v, &dummy, &ctor, 0u, arity) {
618+
match specialize(cx, v, &ctor, 0u, arity) {
623619
Some(v) => is_useful(cx, &matrix, v.as_slice(), witness),
624620
None => NotUseful
625621
}
@@ -741,15 +737,15 @@ fn range_covered_by_constructor(ctor: &Constructor,
741737
/// different patterns.
742738
/// Structure patterns with a partial wild pattern (Foo { a: 42, .. }) have their missing
743739
/// fields filled with wild patterns.
744-
pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
740+
pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
745741
constructor: &Constructor, col: uint, arity: uint) -> Option<Vec<&'a Pat>> {
746742
let &Pat {
747743
id: pat_id, node: ref node, span: pat_span
748744
} = raw_pat(r[col]);
749745
let head: Option<Vec<&Pat>> = match node {
750746

751747
&PatWild(_) =>
752-
Some(Vec::from_elem(arity, dummy)),
748+
Some(Vec::from_elem(arity, DUMMY_WILD_PAT)),
753749

754750
&PatIdent(_, _, _) => {
755751
let opt_def = cx.tcx.def_map.borrow().find_copy(&pat_id);
@@ -762,7 +758,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
762758
} else {
763759
None
764760
},
765-
_ => Some(Vec::from_elem(arity, dummy))
761+
_ => Some(Vec::from_elem(arity, DUMMY_WILD_PAT))
766762
}
767763
}
768764

@@ -776,7 +772,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
776772
DefVariant(..) | DefStruct(..) => {
777773
Some(match args {
778774
&Some(ref args) => args.iter().map(|p| &**p).collect(),
779-
&None => Vec::from_elem(arity, dummy)
775+
&None => Vec::from_elem(arity, DUMMY_WILD_PAT)
780776
})
781777
}
782778
_ => None
@@ -812,7 +808,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
812808
let args = struct_fields.iter().map(|sf| {
813809
match pattern_fields.iter().find(|f| f.ident.name == sf.name) {
814810
Some(ref f) => &*f.pat,
815-
_ => dummy
811+
_ => DUMMY_WILD_PAT
816812
}
817813
}).collect();
818814
args
@@ -855,13 +851,13 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat], dummy: &'a Pat,
855851
// Fixed-length vectors.
856852
Single => {
857853
let mut pats: Vec<&Pat> = before.iter().map(|p| &**p).collect();
858-
pats.grow_fn(arity - before.len() - after.len(), |_| dummy);
854+
pats.grow_fn(arity - before.len() - after.len(), |_| DUMMY_WILD_PAT);
859855
pats.extend(after.iter().map(|p| &**p));
860856
Some(pats)
861857
},
862858
Slice(length) if before.len() + after.len() <= length && slice.is_some() => {
863859
let mut pats: Vec<&Pat> = before.iter().map(|p| &**p).collect();
864-
pats.grow_fn(arity - before.len() - after.len(), |_| dummy);
860+
pats.grow_fn(arity - before.len() - after.len(), |_| DUMMY_WILD_PAT);
865861
pats.extend(after.iter().map(|p| &**p));
866862
Some(pats)
867863
},
@@ -931,7 +927,7 @@ fn check_fn(cx: &mut MatchCheckCtxt,
931927

932928
fn is_refutable<A>(cx: &MatchCheckCtxt, pat: &Pat, refutable: |&Pat| -> A) -> Option<A> {
933929
let pats = Matrix(vec!(vec!(pat)));
934-
match is_useful(cx, &pats, [&DUMMY_WILD_PAT], ConstructWitness) {
930+
match is_useful(cx, &pats, [DUMMY_WILD_PAT], ConstructWitness) {
935931
UsefulWithWitness(pats) => {
936932
assert_eq!(pats.len(), 1);
937933
Some(refutable(&*pats[0]))

src/librustc/middle/dead.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
441441
fn should_warn_about_item(&mut self, item: &ast::Item) -> bool {
442442
let should_warn = match item.node {
443443
ast::ItemStatic(..)
444+
| ast::ItemConst(..)
444445
| ast::ItemFn(..)
445446
| ast::ItemEnum(..)
446447
| ast::ItemStruct(..) => true,

src/librustc/middle/trans/_match.rs

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

370357
impl<'a, 'p, 'blk, 'tcx> Repr for Match<'a, 'p, 'blk, 'tcx> {
@@ -417,7 +404,6 @@ fn expand_nested_bindings<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
417404
*pats.get_mut(col) = pat;
418405
Match {
419406
pats: pats,
420-
dummy: br.dummy,
421407
data: &*br.data,
422408
bound_ptrs: bound_ptrs
423409
}
@@ -465,7 +451,6 @@ fn enter_match<'a, 'b, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
465451
}
466452
Match {
467453
pats: pats,
468-
dummy: br.dummy,
469454
data: br.data,
470455
bound_ptrs: bound_ptrs
471456
}
@@ -560,8 +545,7 @@ fn enter_opt<'a, 'p, 'blk, 'tcx>(
560545

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

@@ -1051,7 +1035,7 @@ fn compile_submatch_continue<'a, 'p, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
10511035
match adt_vals {
10521036
Some(field_vals) => {
10531037
let pats = enter_match(bcx, dm, m, col, val, |pats|
1054-
check_match::specialize(&mcx, pats, m[0].dummy,
1038+
check_match::specialize(&mcx, pats,
10551039
&check_match::Single, col,
10561040
field_vals.len())
10571041
);
@@ -1375,7 +1359,6 @@ fn trans_match_inner<'blk, 'tcx>(scope_cx: Block<'blk, 'tcx>,
13751359
bindings_map: create_bindings_map(bcx, &**arm.pats.get(0), discr_expr, &*arm.body)
13761360
}).collect();
13771361

1378-
let dummy = check_match::DUMMY_WILD_PAT.clone();
13791362
let mut static_inliner = StaticInliner::new(scope_cx.tcx());
13801363
let arm_pats: Vec<Vec<P<ast::Pat>>> = arm_datas.iter().map(|arm_data| {
13811364
arm_data.arm.pats.iter().map(|p| static_inliner.fold_pat((*p).clone())).collect()
@@ -1384,7 +1367,6 @@ fn trans_match_inner<'blk, 'tcx>(scope_cx: Block<'blk, 'tcx>,
13841367
for (arm_data, pats) in arm_datas.iter().zip(arm_pats.iter()) {
13851368
matches.extend(pats.iter().map(|p| Match {
13861369
pats: vec![&**p],
1387-
dummy: &dummy,
13881370
data: arm_data,
13891371
bound_ptrs: Vec::new(),
13901372
}));

src/libsyntax/ast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,14 +1333,16 @@ impl Item_ {
13331333
pub fn descriptive_variant(&self) -> &str {
13341334
match *self {
13351335
ItemStatic(..) => "static item",
1336+
ItemConst(..) => "constant item",
13361337
ItemFn(..) => "function",
13371338
ItemMod(..) => "module",
13381339
ItemForeignMod(..) => "foreign module",
13391340
ItemTy(..) => "type alias",
13401341
ItemEnum(..) => "enum",
13411342
ItemStruct(..) => "struct",
13421343
ItemTrait(..) => "trait",
1343-
_ => "item"
1344+
ItemMac(..) |
1345+
ItemImpl(..) => "item"
13441346
}
13451347
}
13461348
}

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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ pub static used_static2: int = used_static;
3535
const USED_STATIC: int = 0;
3636
const STATIC_USED_IN_ENUM_DISCRIMINANT: int = 10;
3737

38+
pub const pub_const: int = 0;
39+
const priv_const: int = 0; //~ ERROR: constant item is never used
40+
const used_const: int = 0;
41+
pub const used_const2: int = used_const;
42+
const USED_CONST: int = 1;
43+
const CONST_USED_IN_ENUM_DISCRIMINANT: int = 11;
44+
3845
pub type typ = *const UsedStruct4;
3946
pub struct PubStruct;
4047
struct PrivStruct; //~ ERROR: struct is never used
@@ -61,7 +68,10 @@ pub struct PubStruct2 {
6168

6269
pub enum pub_enum { foo1, bar1 }
6370
pub enum pub_enum2 { a(*const StructUsedInEnum) }
64-
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+
}
6575

6676
enum priv_enum { foo2, bar2 } //~ ERROR: enum is never used
6777
enum used_enum {
@@ -82,6 +92,7 @@ pub fn pub_fn() {
8292
let i = 1i;
8393
match i {
8494
USED_STATIC => (),
95+
USED_CONST => (),
8596
_ => ()
8697
}
8798
f::<StructUsedInGeneric>();

0 commit comments

Comments
 (0)