Skip to content

Commit e43afbc

Browse files
committed
Get associated consts working in match patterns.
1 parent a38ff54 commit e43afbc

23 files changed

+596
-209
lines changed

src/librustc/middle/cfg/construct.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
105105
match pat.node {
106106
ast::PatIdent(_, _, None) |
107107
ast::PatEnum(_, None) |
108+
ast::PatQPath(..) |
108109
ast::PatLit(..) |
109110
ast::PatRange(..) |
110111
ast::PatWild(_) => {

src/librustc/middle/check_match.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'map> ast_util::IdVisitingOperation for RenamingRecorder<'map> {
439439
impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
440440
fn fold_pat(&mut self, pat: P<Pat>) -> P<Pat> {
441441
return match pat.node {
442-
ast::PatIdent(..) | ast::PatEnum(..) => {
442+
ast::PatIdent(..) | ast::PatEnum(..) | ast::PatQPath(..) => {
443443
let def = self.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def());
444444
match def {
445445
Some(DefAssociatedConst(did, _)) |
@@ -762,6 +762,9 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
762762
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
763763
_ => vec!(Single)
764764
},
765+
ast::PatQPath(..) =>
766+
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
767+
been rewritten"),
765768
ast::PatStruct(..) =>
766769
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
767770
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
@@ -891,6 +894,11 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
891894
}
892895
}
893896

897+
ast::PatQPath(_, _) => {
898+
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
899+
been rewritten")
900+
}
901+
894902
ast::PatStruct(_, ref pattern_fields, _) => {
895903
// Is this a struct or an enum variant?
896904
let def = cx.tcx.def_map.borrow().get(&pat_id).unwrap().full_def();

src/librustc/middle/expr_use_visitor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,8 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
10911091
let tcx = typer.tcx();
10921092

10931093
match pat.node {
1094-
ast::PatEnum(_, _) | ast::PatIdent(_, _, None) | ast::PatStruct(..) => {
1094+
ast::PatEnum(_, _) | ast::PatQPath(..) |
1095+
ast::PatIdent(_, _, None) | ast::PatStruct(..) => {
10951096
match def_map.get(&pat.id).map(|d| d.full_def()) {
10961097
None => {
10971098
// no definition found: pat is not a

src/librustc/middle/mem_categorization.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,10 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
12601260
}
12611261
}
12621262

1263+
ast::PatQPath(..) => {
1264+
// Lone constant: ignore
1265+
}
1266+
12631267
ast::PatIdent(_, _, Some(ref subpat)) => {
12641268
try!(self.cat_pattern_(cmt, &**subpat, op));
12651269
}

src/librustc/middle/pat_util.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn pat_id_map(dm: &DefMap, pat: &ast::Pat) -> PatIdMap {
3030

3131
pub fn pat_is_refutable(dm: &DefMap, pat: &ast::Pat) -> bool {
3232
match pat.node {
33-
ast::PatLit(_) | ast::PatRange(_, _) => true,
33+
ast::PatLit(_) | ast::PatRange(_, _) | ast::PatQPath(..) => true,
3434
ast::PatEnum(_, _) |
3535
ast::PatIdent(_, _, None) |
3636
ast::PatStruct(..) => {
@@ -60,7 +60,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &ast::Pat) -> bool {
6060

6161
pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
6262
match pat.node {
63-
ast::PatIdent(_, _, None) | ast::PatEnum(..) => {
63+
ast::PatIdent(_, _, None) | ast::PatEnum(..) | ast::PatQPath(..) => {
6464
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
6565
Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
6666
_ => false
@@ -70,6 +70,22 @@ pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
7070
}
7171
}
7272

73+
// Same as above, except that partially-resolved defs cause `false` to be
74+
// returned instead of a panic.
75+
pub fn pat_is_resolved_const(dm: &DefMap, pat: &ast::Pat) -> bool {
76+
match pat.node {
77+
ast::PatIdent(_, _, None) | ast::PatEnum(..) | ast::PatQPath(..) => {
78+
match dm.borrow().get(&pat.id)
79+
.and_then(|d| if d.depth == 0 { Some(d.base_def) }
80+
else { None } ) {
81+
Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
82+
_ => false
83+
}
84+
}
85+
_ => false
86+
}
87+
}
88+
7389
pub fn pat_is_binding(dm: &DefMap, pat: &ast::Pat) -> bool {
7490
match pat.node {
7591
ast::PatIdent(..) => {

0 commit comments

Comments
 (0)