Skip to content

Commit 9f414a4

Browse files
committed
Split ast::PatKind::Enum into tuple struct and path patterns
1 parent 14adc9b commit 9f414a4

File tree

8 files changed

+47
-26
lines changed

8 files changed

+47
-26
lines changed

src/librustc_front/lowering.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,11 +920,14 @@ pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
920920
sub.as_ref().map(|x| lower_pat(lctx, x)))
921921
}
922922
PatKind::Lit(ref e) => hir::PatLit(lower_expr(lctx, e)),
923-
PatKind::Enum(ref pth, ref pats) => {
923+
PatKind::TupleStruct(ref pth, ref pats) => {
924924
hir::PatEnum(lower_path(lctx, pth),
925925
pats.as_ref()
926926
.map(|pats| pats.iter().map(|x| lower_pat(lctx, x)).collect()))
927927
}
928+
PatKind::Path(ref pth) => {
929+
hir::PatEnum(lower_path(lctx, pth), Some(hir::HirVec::new()))
930+
}
928931
PatKind::QPath(ref qself, ref pth) => {
929932
let qself = hir::QSelf {
930933
ty: lower_ty(lctx, &qself.ty),

src/librustc_trans/save/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,8 @@ impl<'v> Visitor<'v> for PathCollector {
762762
self.collected_paths.push((p.id, path.clone(),
763763
ast::Mutability::Mutable, recorder::TypeRef));
764764
}
765-
PatKind::Enum(ref path, _) |
765+
PatKind::TupleStruct(ref path, _) |
766+
PatKind::Path(ref path) |
766767
PatKind::QPath(_, ref path) => {
767768
self.collected_paths.push((p.id, path.clone(),
768769
ast::Mutability::Mutable, recorder::VarRef));

src/libsyntax/ast.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -555,28 +555,34 @@ pub enum PatKind {
555555
/// Represents a wildcard pattern (`_`)
556556
Wild,
557557

558-
/// A PatKind::Ident may either be a new bound variable,
559-
/// or a nullary enum (in which case the third field
560-
/// is None).
558+
/// A `PatKind::Ident` may either be a new bound variable,
559+
/// or a unit struct/variant pattern, or a const pattern (in the last two cases
560+
/// the third field must be `None`).
561561
///
562-
/// In the nullary enum case, the parser can't determine
562+
/// In the unit or const pattern case, the parser can't determine
563563
/// which it is. The resolver determines this, and
564-
/// records this pattern's NodeId in an auxiliary
565-
/// set (of "PatIdents that refer to nullary enums")
564+
/// records this pattern's `NodeId` in an auxiliary
565+
/// set (of "PatIdents that refer to unit patterns or constants").
566566
Ident(BindingMode, SpannedIdent, Option<P<Pat>>),
567567

568+
/// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
569+
/// The `bool` is `true` in the presence of a `..`.
570+
Struct(Path, Vec<Spanned<FieldPat>>, bool),
571+
572+
/// A tuple struct/variant pattern `Variant(x, y, z)`.
568573
/// "None" means a `Variant(..)` pattern where we don't bind the fields to names.
569-
Enum(Path, Option<Vec<P<Pat>>>),
574+
TupleStruct(Path, Option<Vec<P<Pat>>>),
575+
576+
/// A path pattern.
577+
/// Such pattern can be resolved to a unit struct/variant or a constant.
578+
Path(Path),
570579

571580
/// An associated const named using the qualified path `<T>::CONST` or
572581
/// `<T as Trait>::CONST`. Associated consts from inherent impls can be
573582
/// referred to as simply `T::CONST`, in which case they will end up as
574583
/// PatKind::Enum, and the resolver will have to sort that out.
575584
QPath(QSelf, Path),
576585

577-
/// Destructuring of a struct, e.g. `Foo {x, y, ..}`
578-
/// The `bool` is `true` in the presence of a `..`
579-
Struct(Path, Vec<Spanned<FieldPat>>, bool),
580586
/// A tuple pattern `(a, b)`
581587
Tup(Vec<P<Pat>>),
582588
/// A `box` pattern

src/libsyntax/ext/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,11 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
827827
self.pat(span, pat)
828828
}
829829
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
830-
let pat = PatKind::Enum(path, Some(subpats));
830+
let pat = if subpats.is_empty() {
831+
PatKind::Path(path)
832+
} else {
833+
PatKind::TupleStruct(path, Some(subpats))
834+
};
831835
self.pat(span, pat)
832836
}
833837
fn pat_struct(&self, span: Span,

src/libsyntax/fold.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,10 +1127,13 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
11271127
sub.map(|x| folder.fold_pat(x)))
11281128
}
11291129
PatKind::Lit(e) => PatKind::Lit(folder.fold_expr(e)),
1130-
PatKind::Enum(pth, pats) => {
1131-
PatKind::Enum(folder.fold_path(pth),
1130+
PatKind::TupleStruct(pth, pats) => {
1131+
PatKind::TupleStruct(folder.fold_path(pth),
11321132
pats.map(|pats| pats.move_map(|x| folder.fold_pat(x))))
11331133
}
1134+
PatKind::Path(pth) => {
1135+
PatKind::Path(folder.fold_path(pth))
1136+
}
11341137
PatKind::QPath(qself, pth) => {
11351138
let qself = QSelf {ty: folder.fold_ty(qself.ty), .. qself};
11361139
PatKind::QPath(qself, folder.fold_path(pth))

src/libsyntax/parse/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,22 +3534,22 @@ impl<'a> Parser<'a> {
35343534
self.bump();
35353535
self.bump();
35363536
try!(self.expect(&token::CloseDelim(token::Paren)));
3537-
pat = PatKind::Enum(path, None);
3537+
pat = PatKind::TupleStruct(path, None);
35383538
} else {
35393539
let args = try!(self.parse_enum_variant_seq(
35403540
&token::OpenDelim(token::Paren),
35413541
&token::CloseDelim(token::Paren),
35423542
seq_sep_trailing_allowed(token::Comma),
35433543
|p| p.parse_pat()));
3544-
pat = PatKind::Enum(path, Some(args));
3544+
pat = PatKind::TupleStruct(path, Some(args));
35453545
}
35463546
}
35473547
_ => {
35483548
pat = match qself {
35493549
// Parse qualified path
35503550
Some(qself) => PatKind::QPath(qself, path),
35513551
// Parse nullary enum
3552-
None => PatKind::Enum(path, Some(vec![]))
3552+
None => PatKind::Path(path)
35533553
};
35543554
}
35553555
}

src/libsyntax/print/pprust.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,20 +2478,21 @@ impl<'a> State<'a> {
24782478
None => ()
24792479
}
24802480
}
2481-
PatKind::Enum(ref path, ref args_) => {
2481+
PatKind::TupleStruct(ref path, ref args_) => {
24822482
try!(self.print_path(path, true, 0));
24832483
match *args_ {
24842484
None => try!(word(&mut self.s, "(..)")),
24852485
Some(ref args) => {
2486-
if !args.is_empty() {
2487-
try!(self.popen());
2488-
try!(self.commasep(Inconsistent, &args[..],
2489-
|s, p| s.print_pat(&p)));
2490-
try!(self.pclose());
2491-
}
2486+
try!(self.popen());
2487+
try!(self.commasep(Inconsistent, &args[..],
2488+
|s, p| s.print_pat(&p)));
2489+
try!(self.pclose());
24922490
}
24932491
}
24942492
}
2493+
PatKind::Path(ref path) => {
2494+
try!(self.print_path(path, true, 0));
2495+
}
24952496
PatKind::QPath(ref qself, ref path) => {
24962497
try!(self.print_qpath(path, qself, false));
24972498
}

src/libsyntax/visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,15 @@ pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
419419

420420
pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
421421
match pattern.node {
422-
PatKind::Enum(ref path, ref opt_children) => {
422+
PatKind::TupleStruct(ref path, ref opt_children) => {
423423
visitor.visit_path(path, pattern.id);
424424
if let Some(ref children) = *opt_children {
425425
walk_list!(visitor, visit_pat, children);
426426
}
427427
}
428+
PatKind::Path(ref path) => {
429+
visitor.visit_path(path, pattern.id);
430+
}
428431
PatKind::QPath(ref qself, ref path) => {
429432
visitor.visit_ty(&qself.ty);
430433
visitor.visit_path(path, pattern.id)

0 commit comments

Comments
 (0)