Skip to content

Commit 9d98390

Browse files
committed
Auto merge of #31581 - petrochenkov:patrefact, r=Manishearth
cc #31487 (comment) plugin-[breaking-change] The first commit renames `ast::Pat_` to `ast::PatKind` and uses its variants in enum qualified form. I've also taken the opportunity and renamed `PatKind::Region` into `PatKind::Ref`. The second commit splits `PatKind::Enum` into `PatKind::TupleStruct` and `PatKind::UnitStruct`. So, pattern kinds now correspond to their struct/variant kinds - `Struct`, `TupleStruct` and `UnitStruct`. @nikomatsakis @nrc @arielb1 Are you okay with this naming scheme? An alternative possible naming scheme is `PatKind::StructVariant`, `PatKind::TupleVariant`, `PatKind::UnitVariant` (it's probably closer to the common use, but I like it less). I intend to apply these changes to HIR later, they should not necessarily go in the same nightly with #31487 r? @Manishearth
2 parents 09395bb + 9f414a4 commit 9d98390

File tree

16 files changed

+179
-158
lines changed

16 files changed

+179
-158
lines changed

src/librustc_front/lowering.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -913,26 +913,29 @@ pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
913913
P(hir::Pat {
914914
id: p.id,
915915
node: match p.node {
916-
PatWild => hir::PatWild,
917-
PatIdent(ref binding_mode, pth1, ref sub) => {
916+
PatKind::Wild => hir::PatWild,
917+
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
918918
hir::PatIdent(lower_binding_mode(lctx, binding_mode),
919919
respan(pth1.span, lower_ident(lctx, pth1.node)),
920920
sub.as_ref().map(|x| lower_pat(lctx, x)))
921921
}
922-
PatLit(ref e) => hir::PatLit(lower_expr(lctx, e)),
923-
PatEnum(ref pth, ref pats) => {
922+
PatKind::Lit(ref e) => hir::PatLit(lower_expr(lctx, e)),
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-
PatQPath(ref qself, ref pth) => {
928+
PatKind::Path(ref pth) => {
929+
hir::PatEnum(lower_path(lctx, pth), Some(hir::HirVec::new()))
930+
}
931+
PatKind::QPath(ref qself, ref pth) => {
929932
let qself = hir::QSelf {
930933
ty: lower_ty(lctx, &qself.ty),
931934
position: qself.position,
932935
};
933936
hir::PatQPath(qself, lower_path(lctx, pth))
934937
}
935-
PatStruct(ref pth, ref fields, etc) => {
938+
PatKind::Struct(ref pth, ref fields, etc) => {
936939
let pth = lower_path(lctx, pth);
937940
let fs = fields.iter()
938941
.map(|f| {
@@ -948,20 +951,22 @@ pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
948951
.collect();
949952
hir::PatStruct(pth, fs, etc)
950953
}
951-
PatTup(ref elts) => hir::PatTup(elts.iter().map(|x| lower_pat(lctx, x)).collect()),
952-
PatBox(ref inner) => hir::PatBox(lower_pat(lctx, inner)),
953-
PatRegion(ref inner, mutbl) => {
954+
PatKind::Tup(ref elts) => {
955+
hir::PatTup(elts.iter().map(|x| lower_pat(lctx, x)).collect())
956+
}
957+
PatKind::Box(ref inner) => hir::PatBox(lower_pat(lctx, inner)),
958+
PatKind::Ref(ref inner, mutbl) => {
954959
hir::PatRegion(lower_pat(lctx, inner), lower_mutability(lctx, mutbl))
955960
}
956-
PatRange(ref e1, ref e2) => {
961+
PatKind::Range(ref e1, ref e2) => {
957962
hir::PatRange(lower_expr(lctx, e1), lower_expr(lctx, e2))
958963
}
959-
PatVec(ref before, ref slice, ref after) => {
964+
PatKind::Vec(ref before, ref slice, ref after) => {
960965
hir::PatVec(before.iter().map(|x| lower_pat(lctx, x)).collect(),
961966
slice.as_ref().map(|x| lower_pat(lctx, x)),
962967
after.iter().map(|x| lower_pat(lctx, x)).collect())
963968
}
964-
PatMac(_) => panic!("Shouldn't exist here"),
969+
PatKind::Mac(_) => panic!("Shouldn't exist here"),
965970
},
966971
span: p.span,
967972
})

src/librustc_passes/const_fn.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
use rustc::session::{Session, CompileResult};
1515

16-
use syntax::ast;
16+
use syntax::ast::{self, PatKind};
1717
use syntax::visit::{self, Visitor, FnKind};
1818
use syntax::codemap::Span;
1919

@@ -104,8 +104,8 @@ impl<'a, 'v> Visitor<'v> for CheckConstFn<'a> {
104104
// Ensure the arguments are simple, not mutable/by-ref or patterns.
105105
for arg in &fd.inputs {
106106
match arg.pat.node {
107-
ast::PatWild => {}
108-
ast::PatIdent(ast::BindingMode::ByValue(ast::Mutability::Immutable), _, None) => {}
107+
PatKind::Wild => {}
108+
PatKind::Ident(ast::BindingMode::ByValue(ast::Mutability::Immutable), _, None) => {}
109109
_ => {
110110
span_err!(self.sess, arg.pat.span, E0022,
111111
"arguments of constant functions can only \

src/librustc_trans/save/dump_csv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use std::fs::File;
4040
use std::hash::*;
4141
use std::collections::HashSet;
4242

43-
use syntax::ast::{self, NodeId};
43+
use syntax::ast::{self, NodeId, PatKind};
4444
use syntax::codemap::*;
4545
use syntax::parse::token::{self, keywords};
4646
use syntax::visit::{self, Visitor};
@@ -780,7 +780,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
780780

781781
fn process_pat(&mut self, p: &ast::Pat) {
782782
match p.node {
783-
ast::PatStruct(ref path, ref fields, _) => {
783+
PatKind::Struct(ref path, ref fields, _) => {
784784
visit::walk_path(self, path);
785785
let adt = self.tcx.node_id_to_type(p.id).ty_adt_def().unwrap();
786786
let def = self.tcx.def_map.borrow()[&p.id].full_def();

src/librustc_trans/save/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_front::{hir, lowering};
2121
use rustc::front::map::NodeItem;
2222
use rustc::session::config::CrateType::CrateTypeExecutable;
2323

24-
use syntax::ast::{self, NodeId};
24+
use syntax::ast::{self, NodeId, PatKind};
2525
use syntax::ast_util;
2626
use syntax::codemap::*;
2727
use syntax::parse::token::{self, keywords};
@@ -758,16 +758,17 @@ impl PathCollector {
758758
impl<'v> Visitor<'v> for PathCollector {
759759
fn visit_pat(&mut self, p: &ast::Pat) {
760760
match p.node {
761-
ast::PatStruct(ref path, _, _) => {
761+
PatKind::Struct(ref path, _, _) => {
762762
self.collected_paths.push((p.id, path.clone(),
763763
ast::Mutability::Mutable, recorder::TypeRef));
764764
}
765-
ast::PatEnum(ref path, _) |
766-
ast::PatQPath(_, ref path) => {
765+
PatKind::TupleStruct(ref path, _) |
766+
PatKind::Path(ref path) |
767+
PatKind::QPath(_, ref path) => {
767768
self.collected_paths.push((p.id, path.clone(),
768769
ast::Mutability::Mutable, recorder::VarRef));
769770
}
770-
ast::PatIdent(bm, ref path1, _) => {
771+
PatKind::Ident(bm, ref path1, _) => {
771772
debug!("PathCollector, visit ident in pat {}: {:?} {:?}",
772773
path1.node,
773774
p.span,

src/libsyntax/ast.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// The Rust abstract syntax tree.
1212

13-
pub use self::Pat_::*;
1413
pub use self::StructFieldKind::*;
1514
pub use self::TyParamBound::*;
1615
pub use self::UnsafeSource::*;
@@ -521,7 +520,7 @@ pub struct Block {
521520
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
522521
pub struct Pat {
523522
pub id: NodeId,
524-
pub node: Pat_,
523+
pub node: PatKind,
525524
pub span: Span,
526525
}
527526

@@ -552,47 +551,53 @@ pub enum BindingMode {
552551
}
553552

554553
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
555-
pub enum Pat_ {
554+
pub enum PatKind {
556555
/// Represents a wildcard pattern (`_`)
557-
PatWild,
556+
Wild,
558557

559-
/// A PatIdent may either be a new bound variable,
560-
/// or a nullary enum (in which case the third field
561-
/// 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`).
562561
///
563-
/// In the nullary enum case, the parser can't determine
562+
/// In the unit or const pattern case, the parser can't determine
564563
/// which it is. The resolver determines this, and
565-
/// records this pattern's NodeId in an auxiliary
566-
/// set (of "PatIdents that refer to nullary enums")
567-
PatIdent(BindingMode, SpannedIdent, Option<P<Pat>>),
564+
/// records this pattern's `NodeId` in an auxiliary
565+
/// set (of "PatIdents that refer to unit patterns or constants").
566+
Ident(BindingMode, SpannedIdent, Option<P<Pat>>),
568567

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)`.
569573
/// "None" means a `Variant(..)` pattern where we don't bind the fields to names.
570-
PatEnum(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),
571579

572580
/// An associated const named using the qualified path `<T>::CONST` or
573581
/// `<T as Trait>::CONST`. Associated consts from inherent impls can be
574582
/// referred to as simply `T::CONST`, in which case they will end up as
575-
/// PatEnum, and the resolver will have to sort that out.
576-
PatQPath(QSelf, Path),
583+
/// PatKind::Enum, and the resolver will have to sort that out.
584+
QPath(QSelf, Path),
577585

578-
/// Destructuring of a struct, e.g. `Foo {x, y, ..}`
579-
/// The `bool` is `true` in the presence of a `..`
580-
PatStruct(Path, Vec<Spanned<FieldPat>>, bool),
581586
/// A tuple pattern `(a, b)`
582-
PatTup(Vec<P<Pat>>),
587+
Tup(Vec<P<Pat>>),
583588
/// A `box` pattern
584-
PatBox(P<Pat>),
589+
Box(P<Pat>),
585590
/// A reference pattern, e.g. `&mut (a, b)`
586-
PatRegion(P<Pat>, Mutability),
591+
Ref(P<Pat>, Mutability),
587592
/// A literal
588-
PatLit(P<Expr>),
593+
Lit(P<Expr>),
589594
/// A range pattern, e.g. `1...2`
590-
PatRange(P<Expr>, P<Expr>),
595+
Range(P<Expr>, P<Expr>),
591596
/// `[a, b, ..i, y, z]` is represented as:
592-
/// `PatVec(box [a, b], Some(i), box [y, z])`
593-
PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
597+
/// `PatKind::Vec(box [a, b], Some(i), box [y, z])`
598+
Vec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
594599
/// A macro pattern; pre-expansion
595-
PatMac(Mac),
600+
Mac(Mac),
596601
}
597602

598603
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
@@ -1609,7 +1614,7 @@ impl Arg {
16091614
}),
16101615
pat: P(Pat {
16111616
id: DUMMY_NODE_ID,
1612-
node: PatIdent(BindingMode::ByValue(mutability), path, None),
1617+
node: PatKind::Ident(BindingMode::ByValue(mutability), path, None),
16131618
span: span
16141619
}),
16151620
id: DUMMY_NODE_ID

src/libsyntax/ast_util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> P<Pat> {
6969
let spanned = codemap::Spanned{ span: s, node: i };
7070
P(Pat {
7171
id: id,
72-
node: PatIdent(BindingMode::ByValue(Mutability::Immutable), spanned, None),
72+
node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), spanned, None),
7373
span: s
7474
})
7575
}
@@ -348,7 +348,7 @@ pub fn compute_id_range_for_fn_body(fk: FnKind,
348348
/// and false otherwise.
349349
pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
350350
match pat.node {
351-
ast::PatIdent(..) => true,
351+
PatKind::Ident(..) => true,
352352
_ => false,
353353
}
354354
}

src/libsyntax/ext/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
pub use self::SyntaxExtension::*;
1212

1313
use ast;
14-
use ast::Name;
14+
use ast::{Name, PatKind};
1515
use codemap;
1616
use codemap::{CodeMap, Span, ExpnId, ExpnInfo, NO_EXPANSION};
1717
use errors::DiagnosticBuilder;
@@ -307,7 +307,7 @@ impl MacResult for MacEager {
307307
return Some(P(ast::Pat {
308308
id: ast::DUMMY_NODE_ID,
309309
span: e.span,
310-
node: ast::PatLit(e),
310+
node: PatKind::Lit(e),
311311
}));
312312
}
313313
}
@@ -359,7 +359,7 @@ impl DummyResult {
359359
pub fn raw_pat(sp: Span) -> ast::Pat {
360360
ast::Pat {
361361
id: ast::DUMMY_NODE_ID,
362-
node: ast::PatWild,
362+
node: PatKind::Wild,
363363
span: sp,
364364
}
365365
}

src/libsyntax/ext/build.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use abi::Abi;
12-
use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp};
12+
use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
1313
use attr;
1414
use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
1515
use ext::base::ExtCtxt;
@@ -166,7 +166,7 @@ pub trait AstBuilder {
166166
fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
167167
fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
168168

169-
fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
169+
fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
170170
fn pat_wild(&self, span: Span) -> P<ast::Pat>;
171171
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
172172
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
@@ -805,14 +805,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
805805
}
806806

807807

808-
fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
808+
fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
809809
P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
810810
}
811811
fn pat_wild(&self, span: Span) -> P<ast::Pat> {
812-
self.pat(span, ast::PatWild)
812+
self.pat(span, PatKind::Wild)
813813
}
814814
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
815-
self.pat(span, ast::PatLit(expr))
815+
self.pat(span, PatKind::Lit(expr))
816816
}
817817
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
818818
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
@@ -823,20 +823,24 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
823823
span: Span,
824824
ident: ast::Ident,
825825
bm: ast::BindingMode) -> P<ast::Pat> {
826-
let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
826+
let pat = PatKind::Ident(bm, Spanned{span: span, node: ident}, None);
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 = ast::PatEnum(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,
834838
path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
835-
let pat = ast::PatStruct(path, field_pats, false);
839+
let pat = PatKind::Struct(path, field_pats, false);
836840
self.pat(span, pat)
837841
}
838842
fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
839-
self.pat(span, ast::PatTup(pats))
843+
self.pat(span, PatKind::Tup(pats))
840844
}
841845

842846
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {

0 commit comments

Comments
 (0)