Skip to content

Commit

Permalink
Separate bindings from other patterns in HIR
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed May 27, 2016
1 parent ab7c35f commit 216f5fb
Show file tree
Hide file tree
Showing 29 changed files with 237 additions and 307 deletions.
4 changes: 2 additions & 2 deletions src/librustc/cfg/construct.rs
Expand Up @@ -99,7 +99,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {

fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {
match pat.node {
PatKind::Ident(_, _, None) |
PatKind::Binding(_, _, None) |
PatKind::Path(..) |
PatKind::QPath(..) |
PatKind::Lit(..) |
Expand All @@ -110,7 +110,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {

PatKind::Box(ref subpat) |
PatKind::Ref(ref subpat, _) |
PatKind::Ident(_, _, Some(ref subpat)) => {
PatKind::Binding(_, _, Some(ref subpat)) => {
let subpat_exit = self.pat(&subpat, pred);
self.add_ast_node(pat.id, &[subpat_exit])
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/fold.rs
Expand Up @@ -914,8 +914,8 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
id: folder.new_id(id),
node: match node {
PatKind::Wild => PatKind::Wild,
PatKind::Ident(binding_mode, pth1, sub) => {
PatKind::Ident(binding_mode,
PatKind::Binding(binding_mode, pth1, sub) => {
PatKind::Binding(binding_mode,
Spanned {
span: folder.new_span(pth1.span),
node: folder.fold_name(pth1.node),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/intravisit.rs
Expand Up @@ -485,7 +485,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
PatKind::Ref(ref subpattern, _) => {
visitor.visit_pat(subpattern)
}
PatKind::Ident(_, ref pth1, ref optional_subpattern) => {
PatKind::Binding(_, ref pth1, ref optional_subpattern) => {
visitor.visit_name(pth1.span, pth1.node);
walk_list!(visitor, visit_pat, optional_subpattern);
}
Expand Down
20 changes: 11 additions & 9 deletions src/librustc/hir/lowering.rs
Expand Up @@ -866,14 +866,16 @@ impl<'a> LoweringContext<'a> {
PatKind::Wild => hir::PatKind::Wild,
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
self.with_parent_def(p.id, |this| {
let name = match this.resolver.get_resolution(p.id).map(|d| d.full_def()) {
// Only pattern bindings are renamed
None | Some(Def::Local(..)) => this.lower_ident(pth1.node),
_ => pth1.node.name,
};
hir::PatKind::Ident(this.lower_binding_mode(binding_mode),
respan(pth1.span, name),
sub.as_ref().map(|x| this.lower_pat(x)))
match this.resolver.get_resolution(p.id).map(|d| d.full_def()) {
// `None` can occur in body-less function signatures
None | Some(Def::Local(..)) => {
hir::PatKind::Binding(this.lower_binding_mode(binding_mode),
respan(pth1.span,
this.lower_ident(pth1.node)),
sub.as_ref().map(|x| this.lower_pat(x)))
}
_ => hir::PatKind::Path(hir::Path::from_name(pth1.span, pth1.node.name))
}
})
}
PatKind::Lit(ref e) => hir::PatKind::Lit(self.lower_expr(e)),
Expand Down Expand Up @@ -1868,7 +1870,7 @@ impl<'a> LoweringContext<'a> {

fn pat_ident_binding_mode(&mut self, span: Span, name: Name, bm: hir::BindingMode)
-> P<hir::Pat> {
let pat_ident = hir::PatKind::Ident(bm,
let pat_ident = hir::PatKind::Binding(bm,
Spanned {
span: span,
node: name,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/collector.rs
Expand Up @@ -165,7 +165,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
}

fn visit_pat(&mut self, pat: &'ast Pat) {
let node = if let PatKind::Ident(..) = pat.node {
let node = if let PatKind::Binding(..) = pat.node {
NodeLocal(pat)
} else {
NodePat(pat)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/def_collector.rs
Expand Up @@ -396,7 +396,7 @@ impl<'ast> intravisit::Visitor<'ast> for DefCollector<'ast> {
fn visit_pat(&mut self, pat: &'ast hir::Pat) {
let parent_def = self.parent_def;

if let hir::PatKind::Ident(_, name, _) = pat.node {
if let hir::PatKind::Binding(_, name, _) = pat.node {
let def = self.create_def(pat.id, DefPathData::Binding(name.node));
self.parent_def = Some(def);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Expand Up @@ -561,7 +561,7 @@ impl<'ast> Map<'ast> {
NodeVariant(v) => v.node.name,
NodeLifetime(lt) => lt.name,
NodeTyParam(tp) => tp.name,
NodeLocal(&Pat { node: PatKind::Ident(_,l,_), .. }) => l.node,
NodeLocal(&Pat { node: PatKind::Binding(_,l,_), .. }) => l.node,
NodeStructCtor(_) => self.name(self.get_parent(id)),
_ => bug!("no name for {}", self.node_to_string(id))
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/hir/mod.rs
Expand Up @@ -466,7 +466,7 @@ impl Pat {
}

match self.node {
PatKind::Ident(_, _, Some(ref p)) => p.walk_(it),
PatKind::Binding(_, _, Some(ref p)) => p.walk_(it),
PatKind::Struct(_, ref fields, _) => {
fields.iter().all(|field| field.node.pat.walk_(it))
}
Expand All @@ -484,7 +484,7 @@ impl Pat {
PatKind::Wild |
PatKind::Lit(_) |
PatKind::Range(_, _) |
PatKind::Ident(_, _, _) |
PatKind::Binding(..) |
PatKind::Path(..) |
PatKind::QPath(_, _) => {
true
Expand Down Expand Up @@ -532,7 +532,7 @@ pub enum PatKind {
/// which it is. The resolver determines this, and
/// records this pattern's `NodeId` in an auxiliary
/// set (of "PatIdents that refer to unit patterns or constants").
Ident(BindingMode, Spanned<Name>, Option<P<Pat>>),
Binding(BindingMode, Spanned<Name>, Option<P<Pat>>),

/// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
/// The `bool` is `true` in the presence of a `..`.
Expand Down Expand Up @@ -1144,7 +1144,7 @@ pub type ExplicitSelf = Spanned<SelfKind>;

impl Arg {
pub fn to_self(&self) -> Option<ExplicitSelf> {
if let PatKind::Ident(BindByValue(mutbl), name, _) = self.pat.node {
if let PatKind::Binding(BindByValue(mutbl), name, _) = self.pat.node {
if name.node.unhygienize() == keywords::SelfValue.name() {
return match self.ty.node {
TyInfer => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
Expand All @@ -1160,7 +1160,7 @@ impl Arg {
}

pub fn is_self(&self) -> bool {
if let PatKind::Ident(_, name, _) = self.pat.node {
if let PatKind::Binding(_, name, _) = self.pat.node {
name.node.unhygienize() == keywords::SelfValue.name()
} else {
false
Expand Down
25 changes: 9 additions & 16 deletions src/librustc/hir/pat_util.rs
Expand Up @@ -70,7 +70,6 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
PatKind::Lit(_) | PatKind::Range(_, _) | PatKind::QPath(..) => true,
PatKind::TupleStruct(..) |
PatKind::Path(..) |
PatKind::Ident(_, _, None) |
PatKind::Struct(..) => {
match dm.get(&pat.id).map(|d| d.full_def()) {
Some(Def::Variant(..)) => true,
Expand All @@ -86,7 +85,6 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
PatKind::TupleStruct(..) |
PatKind::Path(..) |
PatKind::Ident(_, _, None) |
PatKind::Struct(..) => {
match dm.get(&pat.id).map(|d| d.full_def()) {
Some(Def::Variant(..)) | Some(Def::Struct(..)) | Some(Def::TyAlias(..)) => true,
Expand All @@ -99,7 +97,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {

pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
PatKind::Ident(_, _, None) | PatKind::Path(..) | PatKind::QPath(..) => {
PatKind::Path(..) | PatKind::QPath(..) => {
match dm.get(&pat.id).map(|d| d.full_def()) {
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
_ => false
Expand All @@ -113,7 +111,7 @@ pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
// returned instead of a panic.
pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
PatKind::Ident(_, _, None) | PatKind::Path(..) | PatKind::QPath(..) => {
PatKind::Path(..) | PatKind::QPath(..) => {
match dm.get(&pat.id)
.and_then(|d| if d.depth == 0 { Some(d.base_def) }
else { None } ) {
Expand All @@ -125,32 +123,28 @@ pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
}
}

pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
pub fn pat_is_binding(_: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
PatKind::Ident(..) => {
!pat_is_variant_or_struct(dm, pat) &&
!pat_is_const(dm, pat)
}
PatKind::Binding(..) => true,
_ => false
}
}

pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
pub fn pat_is_binding_or_wild(_: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
PatKind::Ident(..) => pat_is_binding(dm, pat),
PatKind::Wild => true,
PatKind::Binding(..) | PatKind::Wild => true,
_ => false
}
}

/// Call `it` on every "binding" in a pattern, e.g., on `a` in
/// `match foo() { Some(a) => (), None => () }`
pub fn pat_bindings<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
pub fn pat_bindings<I>(_: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
{
pat.walk(|p| {
match p.node {
PatKind::Ident(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
PatKind::Binding(binding_mode, ref pth, _) => {
it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
}
_ => {}
Expand Down Expand Up @@ -221,7 +215,7 @@ pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {

pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
match pat.node {
PatKind::Ident(hir::BindByValue(_), ref path1, None) => {
PatKind::Binding(hir::BindByValue(..), ref path1, None) => {
Some(path1.node)
}
_ => {
Expand All @@ -241,7 +235,6 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
match p.node {
PatKind::TupleStruct(..) |
PatKind::Path(..) |
PatKind::Ident(_, _, None) |
PatKind::Struct(..) => {
match dm.get(&p.id) {
Some(&PathResolution { base_def: Def::Variant(_, id), .. }) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/print.rs
Expand Up @@ -1716,7 +1716,7 @@ impl<'a> State<'a> {
// is that it doesn't matter
match pat.node {
PatKind::Wild => word(&mut self.s, "_")?,
PatKind::Ident(binding_mode, ref path1, ref sub) => {
PatKind::Binding(binding_mode, ref path1, ref sub) => {
match binding_mode {
hir::BindByRef(mutbl) => {
self.word_nbsp("ref")?;
Expand Down Expand Up @@ -2170,7 +2170,7 @@ impl<'a> State<'a> {
if let Some(eself) = input.to_self() {
self.print_explicit_self(&eself)?;
} else {
let invalid = if let PatKind::Ident(_, name, _) = input.pat.node {
let invalid = if let PatKind::Binding(_, name, _) = input.pat.node {
name.node == keywords::Invalid.name()
} else {
false
Expand Down
25 changes: 9 additions & 16 deletions src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -935,9 +935,9 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
let def_map = &self.tcx().def_map;
if pat_util::pat_is_binding(&def_map.borrow(), pat) {
match pat.node {
PatKind::Ident(hir::BindByRef(_), _, _) =>
PatKind::Binding(hir::BindByRef(_), _, _) =>
mode.lub(BorrowingMatch),
PatKind::Ident(hir::BindByValue(_), _, _) => {
PatKind::Binding(hir::BindByValue(_), _, _) => {
match copy_or_move(self.mc.infcx, &cmt_pat, PatBindingMove) {
Copy => mode.lub(CopyingMatch),
Move(_) => mode.lub(MovingMatch),
Expand Down Expand Up @@ -989,14 +989,14 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {

// It is also a borrow or copy/move of the value being matched.
match pat.node {
PatKind::Ident(hir::BindByRef(m), _, _) => {
PatKind::Binding(hir::BindByRef(m), _, _) => {
if let ty::TyRef(&r, _) = pat_ty.sty {
let bk = ty::BorrowKind::from_mutbl(m);
delegate.borrow(pat.id, pat.span, cmt_pat,
r, bk, RefBinding);
}
}
PatKind::Ident(hir::BindByValue(_), _, _) => {
PatKind::Binding(hir::BindByValue(_), _, _) => {
let mode = copy_or_move(infcx, &cmt_pat, PatBindingMove);
debug!("walk_pat binding consuming pat");
delegate.consume_pat(pat, cmt_pat, mode);
Expand Down Expand Up @@ -1057,8 +1057,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
let tcx = infcx.tcx;

match pat.node {
PatKind::TupleStruct(..) | PatKind::Path(..) | PatKind::QPath(..) |
PatKind::Ident(_, _, None) | PatKind::Struct(..) => {
PatKind::Struct(..) | PatKind::TupleStruct(..) |
PatKind::Path(..) | PatKind::QPath(..) => {
match def_map.get(&pat.id).map(|d| d.full_def()) {
None => {
// no definition found: pat is not a
Expand Down Expand Up @@ -1094,8 +1094,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
}

Some(Def::Const(..)) |
Some(Def::AssociatedConst(..)) |
Some(Def::Local(..)) => {
Some(Def::AssociatedConst(..)) => {
// This is a leaf (i.e. identifier binding
// or constant value to match); thus no
// `matched_pat` call.
Expand All @@ -1121,16 +1120,10 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
}
}

PatKind::Ident(_, _, Some(_)) => {
// Do nothing; this is a binding (not an enum
// variant or struct), and the cat_pattern call
// will visit the substructure recursively.
}

PatKind::Wild | PatKind::Tuple(..) | PatKind::Box(..) |
PatKind::Ref(..) | PatKind::Lit(..) | PatKind::Range(..) |
PatKind::Vec(..) => {
// Similarly, each of these cases does not
PatKind::Vec(..) | PatKind::Binding(..) => {
// Each of these cases does not
// correspond to an enum variant or struct, so we
// do not do any `matched_pat` calls for these
// cases either.
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -306,7 +306,7 @@ impl MutabilityCategory {
fn from_local(tcx: TyCtxt, id: ast::NodeId) -> MutabilityCategory {
let ret = match tcx.map.get(id) {
ast_map::NodeLocal(p) => match p.node {
PatKind::Ident(bind_mode, _, _) => {
PatKind::Binding(bind_mode, _, _) => {
if bind_mode == hir::BindByValue(hir::MutMutable) {
McDeclared
} else {
Expand Down Expand Up @@ -398,7 +398,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
// *being borrowed* is. But ideally we would put in a more
// fundamental fix to this conflated use of the node id.
let ret_ty = match pat.node {
PatKind::Ident(hir::BindByRef(_), _, _) => {
PatKind::Binding(hir::BindByRef(_), _, _) => {
// a bind-by-ref means that the base_ty will be the type of the ident itself,
// but what we want here is the type of the underlying value being borrowed.
// So peel off one-level, turning the &T into T.
Expand Down Expand Up @@ -1276,11 +1276,11 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
}
}

PatKind::Path(..) | PatKind::QPath(..) | PatKind::Ident(_, _, None) => {
PatKind::Path(..) | PatKind::QPath(..) | PatKind::Binding(_, _, None) => {
// Lone constant, or unit variant or identifier: ignore
}

PatKind::Ident(_, _, Some(ref subpat)) => {
PatKind::Binding(_, _, Some(ref subpat)) => {
self.cat_pattern_(cmt, &subpat, op)?;
}

Expand Down
12 changes: 4 additions & 8 deletions src/librustc/middle/region.rs
Expand Up @@ -752,13 +752,9 @@ fn resolve_arm(visitor: &mut RegionResolutionVisitor, arm: &hir::Arm) {
fn resolve_pat(visitor: &mut RegionResolutionVisitor, pat: &hir::Pat) {
visitor.new_node_extent(pat.id);

// If this is a binding (or maybe a binding, I'm too lazy to check
// the def map) then record the lifetime of that binding.
match pat.node {
PatKind::Ident(..) => {
record_var_lifetime(visitor, pat.id, pat.span);
}
_ => { }
// If this is a binding then record the lifetime of that binding.
if let PatKind::Binding(..) = pat.node {
record_var_lifetime(visitor, pat.id, pat.span);
}

intravisit::walk_pat(visitor, pat);
Expand Down Expand Up @@ -958,7 +954,7 @@ fn resolve_local(visitor: &mut RegionResolutionVisitor, local: &hir::Local) {
/// | box P&
fn is_binding_pat(pat: &hir::Pat) -> bool {
match pat.node {
PatKind::Ident(hir::BindByRef(_), _, _) => true,
PatKind::Binding(hir::BindByRef(_), _, _) => true,

PatKind::Struct(_, ref field_pats, _) => {
field_pats.iter().any(|fp| is_binding_pat(&fp.node.pat))
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -2216,7 +2216,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
match self.map.find(id) {
Some(ast_map::NodeLocal(pat)) => {
match pat.node {
PatKind::Ident(_, ref path1, _) => path1.node.as_str(),
PatKind::Binding(_, ref path1, _) => path1.node.as_str(),
_ => {
bug!("Variable id {} maps to {:?}, not local", id, pat);
},
Expand Down
Expand Up @@ -98,7 +98,7 @@ pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
move_pat: &hir::Pat,
cmt: mc::cmt<'tcx>) {
let pat_span_path_opt = match move_pat.node {
PatKind::Ident(_, ref path1, _) => {
PatKind::Binding(_, ref path1, _) => {
Some(MoveSpanAndPath{span: move_pat.span,
name: path1.node})
},
Expand Down

0 comments on commit 216f5fb

Please sign in to comment.