Skip to content

Commit

Permalink
Auto merge of #31581 - petrochenkov:patrefact, r=Manishearth
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Feb 14, 2016
2 parents 09395bb + 9f414a4 commit 9d98390
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 158 deletions.
29 changes: 17 additions & 12 deletions src/librustc_front/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,26 +913,29 @@ pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
P(hir::Pat {
id: p.id,
node: match p.node {
PatWild => hir::PatWild,
PatIdent(ref binding_mode, pth1, ref sub) => {
PatKind::Wild => hir::PatWild,
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
hir::PatIdent(lower_binding_mode(lctx, binding_mode),
respan(pth1.span, lower_ident(lctx, pth1.node)),
sub.as_ref().map(|x| lower_pat(lctx, x)))
}
PatLit(ref e) => hir::PatLit(lower_expr(lctx, e)),
PatEnum(ref pth, ref pats) => {
PatKind::Lit(ref e) => hir::PatLit(lower_expr(lctx, e)),
PatKind::TupleStruct(ref pth, ref pats) => {
hir::PatEnum(lower_path(lctx, pth),
pats.as_ref()
.map(|pats| pats.iter().map(|x| lower_pat(lctx, x)).collect()))
}
PatQPath(ref qself, ref pth) => {
PatKind::Path(ref pth) => {
hir::PatEnum(lower_path(lctx, pth), Some(hir::HirVec::new()))
}
PatKind::QPath(ref qself, ref pth) => {
let qself = hir::QSelf {
ty: lower_ty(lctx, &qself.ty),
position: qself.position,
};
hir::PatQPath(qself, lower_path(lctx, pth))
}
PatStruct(ref pth, ref fields, etc) => {
PatKind::Struct(ref pth, ref fields, etc) => {
let pth = lower_path(lctx, pth);
let fs = fields.iter()
.map(|f| {
Expand All @@ -948,20 +951,22 @@ pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
.collect();
hir::PatStruct(pth, fs, etc)
}
PatTup(ref elts) => hir::PatTup(elts.iter().map(|x| lower_pat(lctx, x)).collect()),
PatBox(ref inner) => hir::PatBox(lower_pat(lctx, inner)),
PatRegion(ref inner, mutbl) => {
PatKind::Tup(ref elts) => {
hir::PatTup(elts.iter().map(|x| lower_pat(lctx, x)).collect())
}
PatKind::Box(ref inner) => hir::PatBox(lower_pat(lctx, inner)),
PatKind::Ref(ref inner, mutbl) => {
hir::PatRegion(lower_pat(lctx, inner), lower_mutability(lctx, mutbl))
}
PatRange(ref e1, ref e2) => {
PatKind::Range(ref e1, ref e2) => {
hir::PatRange(lower_expr(lctx, e1), lower_expr(lctx, e2))
}
PatVec(ref before, ref slice, ref after) => {
PatKind::Vec(ref before, ref slice, ref after) => {
hir::PatVec(before.iter().map(|x| lower_pat(lctx, x)).collect(),
slice.as_ref().map(|x| lower_pat(lctx, x)),
after.iter().map(|x| lower_pat(lctx, x)).collect())
}
PatMac(_) => panic!("Shouldn't exist here"),
PatKind::Mac(_) => panic!("Shouldn't exist here"),
},
span: p.span,
})
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_passes/const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use rustc::session::{Session, CompileResult};

use syntax::ast;
use syntax::ast::{self, PatKind};
use syntax::visit::{self, Visitor, FnKind};
use syntax::codemap::Span;

Expand Down Expand Up @@ -104,8 +104,8 @@ impl<'a, 'v> Visitor<'v> for CheckConstFn<'a> {
// Ensure the arguments are simple, not mutable/by-ref or patterns.
for arg in &fd.inputs {
match arg.pat.node {
ast::PatWild => {}
ast::PatIdent(ast::BindingMode::ByValue(ast::Mutability::Immutable), _, None) => {}
PatKind::Wild => {}
PatKind::Ident(ast::BindingMode::ByValue(ast::Mutability::Immutable), _, None) => {}
_ => {
span_err!(self.sess, arg.pat.span, E0022,
"arguments of constant functions can only \
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/save/dump_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::fs::File;
use std::hash::*;
use std::collections::HashSet;

use syntax::ast::{self, NodeId};
use syntax::ast::{self, NodeId, PatKind};
use syntax::codemap::*;
use syntax::parse::token::{self, keywords};
use syntax::visit::{self, Visitor};
Expand Down Expand Up @@ -780,7 +780,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {

fn process_pat(&mut self, p: &ast::Pat) {
match p.node {
ast::PatStruct(ref path, ref fields, _) => {
PatKind::Struct(ref path, ref fields, _) => {
visit::walk_path(self, path);
let adt = self.tcx.node_id_to_type(p.id).ty_adt_def().unwrap();
let def = self.tcx.def_map.borrow()[&p.id].full_def();
Expand Down
11 changes: 6 additions & 5 deletions src/librustc_trans/save/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_front::{hir, lowering};
use rustc::front::map::NodeItem;
use rustc::session::config::CrateType::CrateTypeExecutable;

use syntax::ast::{self, NodeId};
use syntax::ast::{self, NodeId, PatKind};
use syntax::ast_util;
use syntax::codemap::*;
use syntax::parse::token::{self, keywords};
Expand Down Expand Up @@ -758,16 +758,17 @@ impl PathCollector {
impl<'v> Visitor<'v> for PathCollector {
fn visit_pat(&mut self, p: &ast::Pat) {
match p.node {
ast::PatStruct(ref path, _, _) => {
PatKind::Struct(ref path, _, _) => {
self.collected_paths.push((p.id, path.clone(),
ast::Mutability::Mutable, recorder::TypeRef));
}
ast::PatEnum(ref path, _) |
ast::PatQPath(_, ref path) => {
PatKind::TupleStruct(ref path, _) |
PatKind::Path(ref path) |
PatKind::QPath(_, ref path) => {
self.collected_paths.push((p.id, path.clone(),
ast::Mutability::Mutable, recorder::VarRef));
}
ast::PatIdent(bm, ref path1, _) => {
PatKind::Ident(bm, ref path1, _) => {
debug!("PathCollector, visit ident in pat {}: {:?} {:?}",
path1.node,
p.span,
Expand Down
57 changes: 31 additions & 26 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

// The Rust abstract syntax tree.

pub use self::Pat_::*;
pub use self::StructFieldKind::*;
pub use self::TyParamBound::*;
pub use self::UnsafeSource::*;
Expand Down Expand Up @@ -521,7 +520,7 @@ pub struct Block {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Pat {
pub id: NodeId,
pub node: Pat_,
pub node: PatKind,
pub span: Span,
}

Expand Down Expand Up @@ -552,47 +551,53 @@ pub enum BindingMode {
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Pat_ {
pub enum PatKind {
/// Represents a wildcard pattern (`_`)
PatWild,
Wild,

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

/// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
/// The `bool` is `true` in the presence of a `..`.
Struct(Path, Vec<Spanned<FieldPat>>, bool),

/// A tuple struct/variant pattern `Variant(x, y, z)`.
/// "None" means a `Variant(..)` pattern where we don't bind the fields to names.
PatEnum(Path, Option<Vec<P<Pat>>>),
TupleStruct(Path, Option<Vec<P<Pat>>>),

/// A path pattern.
/// Such pattern can be resolved to a unit struct/variant or a constant.
Path(Path),

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

/// Destructuring of a struct, e.g. `Foo {x, y, ..}`
/// The `bool` is `true` in the presence of a `..`
PatStruct(Path, Vec<Spanned<FieldPat>>, bool),
/// A tuple pattern `(a, b)`
PatTup(Vec<P<Pat>>),
Tup(Vec<P<Pat>>),
/// A `box` pattern
PatBox(P<Pat>),
Box(P<Pat>),
/// A reference pattern, e.g. `&mut (a, b)`
PatRegion(P<Pat>, Mutability),
Ref(P<Pat>, Mutability),
/// A literal
PatLit(P<Expr>),
Lit(P<Expr>),
/// A range pattern, e.g. `1...2`
PatRange(P<Expr>, P<Expr>),
Range(P<Expr>, P<Expr>),
/// `[a, b, ..i, y, z]` is represented as:
/// `PatVec(box [a, b], Some(i), box [y, z])`
PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
/// `PatKind::Vec(box [a, b], Some(i), box [y, z])`
Vec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
/// A macro pattern; pre-expansion
PatMac(Mac),
Mac(Mac),
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
Expand Down Expand Up @@ -1609,7 +1614,7 @@ impl Arg {
}),
pat: P(Pat {
id: DUMMY_NODE_ID,
node: PatIdent(BindingMode::ByValue(mutability), path, None),
node: PatKind::Ident(BindingMode::ByValue(mutability), path, None),
span: span
}),
id: DUMMY_NODE_ID
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> P<Pat> {
let spanned = codemap::Spanned{ span: s, node: i };
P(Pat {
id: id,
node: PatIdent(BindingMode::ByValue(Mutability::Immutable), spanned, None),
node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), spanned, None),
span: s
})
}
Expand Down Expand Up @@ -348,7 +348,7 @@ pub fn compute_id_range_for_fn_body(fk: FnKind,
/// and false otherwise.
pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
match pat.node {
ast::PatIdent(..) => true,
PatKind::Ident(..) => true,
_ => false,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
pub use self::SyntaxExtension::*;

use ast;
use ast::Name;
use ast::{Name, PatKind};
use codemap;
use codemap::{CodeMap, Span, ExpnId, ExpnInfo, NO_EXPANSION};
use errors::DiagnosticBuilder;
Expand Down Expand Up @@ -307,7 +307,7 @@ impl MacResult for MacEager {
return Some(P(ast::Pat {
id: ast::DUMMY_NODE_ID,
span: e.span,
node: ast::PatLit(e),
node: PatKind::Lit(e),
}));
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ impl DummyResult {
pub fn raw_pat(sp: Span) -> ast::Pat {
ast::Pat {
id: ast::DUMMY_NODE_ID,
node: ast::PatWild,
node: PatKind::Wild,
span: sp,
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

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

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


fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
}
fn pat_wild(&self, span: Span) -> P<ast::Pat> {
self.pat(span, ast::PatWild)
self.pat(span, PatKind::Wild)
}
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
self.pat(span, ast::PatLit(expr))
self.pat(span, PatKind::Lit(expr))
}
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
Expand All @@ -823,20 +823,24 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
span: Span,
ident: ast::Ident,
bm: ast::BindingMode) -> P<ast::Pat> {
let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
let pat = PatKind::Ident(bm, Spanned{span: span, node: ident}, None);
self.pat(span, pat)
}
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
let pat = ast::PatEnum(path, Some(subpats));
let pat = if subpats.is_empty() {
PatKind::Path(path)
} else {
PatKind::TupleStruct(path, Some(subpats))
};
self.pat(span, pat)
}
fn pat_struct(&self, span: Span,
path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
let pat = ast::PatStruct(path, field_pats, false);
let pat = PatKind::Struct(path, field_pats, false);
self.pat(span, pat)
}
fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
self.pat(span, ast::PatTup(pats))
self.pat(span, PatKind::Tup(pats))
}

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

0 comments on commit 9d98390

Please sign in to comment.