Skip to content

Commit 216f5fb

Browse files
committed
Separate bindings from other patterns in HIR
1 parent ab7c35f commit 216f5fb

File tree

29 files changed

+237
-307
lines changed

29 files changed

+237
-307
lines changed

src/librustc/cfg/construct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
9999

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

111111
PatKind::Box(ref subpat) |
112112
PatKind::Ref(ref subpat, _) |
113-
PatKind::Ident(_, _, Some(ref subpat)) => {
113+
PatKind::Binding(_, _, Some(ref subpat)) => {
114114
let subpat_exit = self.pat(&subpat, pred);
115115
self.add_ast_node(pat.id, &[subpat_exit])
116116
}

src/librustc/hir/fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,8 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
914914
id: folder.new_id(id),
915915
node: match node {
916916
PatKind::Wild => PatKind::Wild,
917-
PatKind::Ident(binding_mode, pth1, sub) => {
918-
PatKind::Ident(binding_mode,
917+
PatKind::Binding(binding_mode, pth1, sub) => {
918+
PatKind::Binding(binding_mode,
919919
Spanned {
920920
span: folder.new_span(pth1.span),
921921
node: folder.fold_name(pth1.node),

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
485485
PatKind::Ref(ref subpattern, _) => {
486486
visitor.visit_pat(subpattern)
487487
}
488-
PatKind::Ident(_, ref pth1, ref optional_subpattern) => {
488+
PatKind::Binding(_, ref pth1, ref optional_subpattern) => {
489489
visitor.visit_name(pth1.span, pth1.node);
490490
walk_list!(visitor, visit_pat, optional_subpattern);
491491
}

src/librustc/hir/lowering.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -866,14 +866,16 @@ impl<'a> LoweringContext<'a> {
866866
PatKind::Wild => hir::PatKind::Wild,
867867
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
868868
self.with_parent_def(p.id, |this| {
869-
let name = match this.resolver.get_resolution(p.id).map(|d| d.full_def()) {
870-
// Only pattern bindings are renamed
871-
None | Some(Def::Local(..)) => this.lower_ident(pth1.node),
872-
_ => pth1.node.name,
873-
};
874-
hir::PatKind::Ident(this.lower_binding_mode(binding_mode),
875-
respan(pth1.span, name),
876-
sub.as_ref().map(|x| this.lower_pat(x)))
869+
match this.resolver.get_resolution(p.id).map(|d| d.full_def()) {
870+
// `None` can occur in body-less function signatures
871+
None | Some(Def::Local(..)) => {
872+
hir::PatKind::Binding(this.lower_binding_mode(binding_mode),
873+
respan(pth1.span,
874+
this.lower_ident(pth1.node)),
875+
sub.as_ref().map(|x| this.lower_pat(x)))
876+
}
877+
_ => hir::PatKind::Path(hir::Path::from_name(pth1.span, pth1.node.name))
878+
}
877879
})
878880
}
879881
PatKind::Lit(ref e) => hir::PatKind::Lit(self.lower_expr(e)),
@@ -1868,7 +1870,7 @@ impl<'a> LoweringContext<'a> {
18681870

18691871
fn pat_ident_binding_mode(&mut self, span: Span, name: Name, bm: hir::BindingMode)
18701872
-> P<hir::Pat> {
1871-
let pat_ident = hir::PatKind::Ident(bm,
1873+
let pat_ident = hir::PatKind::Binding(bm,
18721874
Spanned {
18731875
span: span,
18741876
node: name,

src/librustc/hir/map/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
165165
}
166166

167167
fn visit_pat(&mut self, pat: &'ast Pat) {
168-
let node = if let PatKind::Ident(..) = pat.node {
168+
let node = if let PatKind::Binding(..) = pat.node {
169169
NodeLocal(pat)
170170
} else {
171171
NodePat(pat)

src/librustc/hir/map/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl<'ast> intravisit::Visitor<'ast> for DefCollector<'ast> {
396396
fn visit_pat(&mut self, pat: &'ast hir::Pat) {
397397
let parent_def = self.parent_def;
398398

399-
if let hir::PatKind::Ident(_, name, _) = pat.node {
399+
if let hir::PatKind::Binding(_, name, _) = pat.node {
400400
let def = self.create_def(pat.id, DefPathData::Binding(name.node));
401401
self.parent_def = Some(def);
402402
}

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'ast> Map<'ast> {
561561
NodeVariant(v) => v.node.name,
562562
NodeLifetime(lt) => lt.name,
563563
NodeTyParam(tp) => tp.name,
564-
NodeLocal(&Pat { node: PatKind::Ident(_,l,_), .. }) => l.node,
564+
NodeLocal(&Pat { node: PatKind::Binding(_,l,_), .. }) => l.node,
565565
NodeStructCtor(_) => self.name(self.get_parent(id)),
566566
_ => bug!("no name for {}", self.node_to_string(id))
567567
}

src/librustc/hir/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ impl Pat {
466466
}
467467

468468
match self.node {
469-
PatKind::Ident(_, _, Some(ref p)) => p.walk_(it),
469+
PatKind::Binding(_, _, Some(ref p)) => p.walk_(it),
470470
PatKind::Struct(_, ref fields, _) => {
471471
fields.iter().all(|field| field.node.pat.walk_(it))
472472
}
@@ -484,7 +484,7 @@ impl Pat {
484484
PatKind::Wild |
485485
PatKind::Lit(_) |
486486
PatKind::Range(_, _) |
487-
PatKind::Ident(_, _, _) |
487+
PatKind::Binding(..) |
488488
PatKind::Path(..) |
489489
PatKind::QPath(_, _) => {
490490
true
@@ -532,7 +532,7 @@ pub enum PatKind {
532532
/// which it is. The resolver determines this, and
533533
/// records this pattern's `NodeId` in an auxiliary
534534
/// set (of "PatIdents that refer to unit patterns or constants").
535-
Ident(BindingMode, Spanned<Name>, Option<P<Pat>>),
535+
Binding(BindingMode, Spanned<Name>, Option<P<Pat>>),
536536

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

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

11621162
pub fn is_self(&self) -> bool {
1163-
if let PatKind::Ident(_, name, _) = self.pat.node {
1163+
if let PatKind::Binding(_, name, _) = self.pat.node {
11641164
name.node.unhygienize() == keywords::SelfValue.name()
11651165
} else {
11661166
false

src/librustc/hir/pat_util.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
7070
PatKind::Lit(_) | PatKind::Range(_, _) | PatKind::QPath(..) => true,
7171
PatKind::TupleStruct(..) |
7272
PatKind::Path(..) |
73-
PatKind::Ident(_, _, None) |
7473
PatKind::Struct(..) => {
7574
match dm.get(&pat.id).map(|d| d.full_def()) {
7675
Some(Def::Variant(..)) => true,
@@ -86,7 +85,6 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
8685
match pat.node {
8786
PatKind::TupleStruct(..) |
8887
PatKind::Path(..) |
89-
PatKind::Ident(_, _, None) |
9088
PatKind::Struct(..) => {
9189
match dm.get(&pat.id).map(|d| d.full_def()) {
9290
Some(Def::Variant(..)) | Some(Def::Struct(..)) | Some(Def::TyAlias(..)) => true,
@@ -99,7 +97,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
9997

10098
pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
10199
match pat.node {
102-
PatKind::Ident(_, _, None) | PatKind::Path(..) | PatKind::QPath(..) => {
100+
PatKind::Path(..) | PatKind::QPath(..) => {
103101
match dm.get(&pat.id).map(|d| d.full_def()) {
104102
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
105103
_ => false
@@ -113,7 +111,7 @@ pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
113111
// returned instead of a panic.
114112
pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
115113
match pat.node {
116-
PatKind::Ident(_, _, None) | PatKind::Path(..) | PatKind::QPath(..) => {
114+
PatKind::Path(..) | PatKind::QPath(..) => {
117115
match dm.get(&pat.id)
118116
.and_then(|d| if d.depth == 0 { Some(d.base_def) }
119117
else { None } ) {
@@ -125,32 +123,28 @@ pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
125123
}
126124
}
127125

128-
pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
126+
pub fn pat_is_binding(_: &DefMap, pat: &hir::Pat) -> bool {
129127
match pat.node {
130-
PatKind::Ident(..) => {
131-
!pat_is_variant_or_struct(dm, pat) &&
132-
!pat_is_const(dm, pat)
133-
}
128+
PatKind::Binding(..) => true,
134129
_ => false
135130
}
136131
}
137132

138-
pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
133+
pub fn pat_is_binding_or_wild(_: &DefMap, pat: &hir::Pat) -> bool {
139134
match pat.node {
140-
PatKind::Ident(..) => pat_is_binding(dm, pat),
141-
PatKind::Wild => true,
135+
PatKind::Binding(..) | PatKind::Wild => true,
142136
_ => false
143137
}
144138
}
145139

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

222216
pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
223217
match pat.node {
224-
PatKind::Ident(hir::BindByValue(_), ref path1, None) => {
218+
PatKind::Binding(hir::BindByValue(..), ref path1, None) => {
225219
Some(path1.node)
226220
}
227221
_ => {
@@ -241,7 +235,6 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
241235
match p.node {
242236
PatKind::TupleStruct(..) |
243237
PatKind::Path(..) |
244-
PatKind::Ident(_, _, None) |
245238
PatKind::Struct(..) => {
246239
match dm.get(&p.id) {
247240
Some(&PathResolution { base_def: Def::Variant(_, id), .. }) => {

src/librustc/hir/print.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ impl<'a> State<'a> {
17161716
// is that it doesn't matter
17171717
match pat.node {
17181718
PatKind::Wild => word(&mut self.s, "_")?,
1719-
PatKind::Ident(binding_mode, ref path1, ref sub) => {
1719+
PatKind::Binding(binding_mode, ref path1, ref sub) => {
17201720
match binding_mode {
17211721
hir::BindByRef(mutbl) => {
17221722
self.word_nbsp("ref")?;
@@ -2170,7 +2170,7 @@ impl<'a> State<'a> {
21702170
if let Some(eself) = input.to_self() {
21712171
self.print_explicit_self(&eself)?;
21722172
} else {
2173-
let invalid = if let PatKind::Ident(_, name, _) = input.pat.node {
2173+
let invalid = if let PatKind::Binding(_, name, _) = input.pat.node {
21742174
name.node == keywords::Invalid.name()
21752175
} else {
21762176
false

src/librustc/middle/expr_use_visitor.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -935,9 +935,9 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
935935
let def_map = &self.tcx().def_map;
936936
if pat_util::pat_is_binding(&def_map.borrow(), pat) {
937937
match pat.node {
938-
PatKind::Ident(hir::BindByRef(_), _, _) =>
938+
PatKind::Binding(hir::BindByRef(_), _, _) =>
939939
mode.lub(BorrowingMatch),
940-
PatKind::Ident(hir::BindByValue(_), _, _) => {
940+
PatKind::Binding(hir::BindByValue(_), _, _) => {
941941
match copy_or_move(self.mc.infcx, &cmt_pat, PatBindingMove) {
942942
Copy => mode.lub(CopyingMatch),
943943
Move(_) => mode.lub(MovingMatch),
@@ -989,14 +989,14 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
989989

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

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

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

1124-
PatKind::Ident(_, _, Some(_)) => {
1125-
// Do nothing; this is a binding (not an enum
1126-
// variant or struct), and the cat_pattern call
1127-
// will visit the substructure recursively.
1128-
}
1129-
11301123
PatKind::Wild | PatKind::Tuple(..) | PatKind::Box(..) |
11311124
PatKind::Ref(..) | PatKind::Lit(..) | PatKind::Range(..) |
1132-
PatKind::Vec(..) => {
1133-
// Similarly, each of these cases does not
1125+
PatKind::Vec(..) | PatKind::Binding(..) => {
1126+
// Each of these cases does not
11341127
// correspond to an enum variant or struct, so we
11351128
// do not do any `matched_pat` calls for these
11361129
// cases either.

src/librustc/middle/mem_categorization.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl MutabilityCategory {
306306
fn from_local(tcx: TyCtxt, id: ast::NodeId) -> MutabilityCategory {
307307
let ret = match tcx.map.get(id) {
308308
ast_map::NodeLocal(p) => match p.node {
309-
PatKind::Ident(bind_mode, _, _) => {
309+
PatKind::Binding(bind_mode, _, _) => {
310310
if bind_mode == hir::BindByValue(hir::MutMutable) {
311311
McDeclared
312312
} else {
@@ -398,7 +398,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
398398
// *being borrowed* is. But ideally we would put in a more
399399
// fundamental fix to this conflated use of the node id.
400400
let ret_ty = match pat.node {
401-
PatKind::Ident(hir::BindByRef(_), _, _) => {
401+
PatKind::Binding(hir::BindByRef(_), _, _) => {
402402
// a bind-by-ref means that the base_ty will be the type of the ident itself,
403403
// but what we want here is the type of the underlying value being borrowed.
404404
// So peel off one-level, turning the &T into T.
@@ -1276,11 +1276,11 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
12761276
}
12771277
}
12781278

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

1283-
PatKind::Ident(_, _, Some(ref subpat)) => {
1283+
PatKind::Binding(_, _, Some(ref subpat)) => {
12841284
self.cat_pattern_(cmt, &subpat, op)?;
12851285
}
12861286

src/librustc/middle/region.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -752,13 +752,9 @@ fn resolve_arm(visitor: &mut RegionResolutionVisitor, arm: &hir::Arm) {
752752
fn resolve_pat(visitor: &mut RegionResolutionVisitor, pat: &hir::Pat) {
753753
visitor.new_node_extent(pat.id);
754754

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

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

963959
PatKind::Struct(_, ref field_pats, _) => {
964960
field_pats.iter().any(|fp| is_binding_pat(&fp.node.pat))

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22162216
match self.map.find(id) {
22172217
Some(ast_map::NodeLocal(pat)) => {
22182218
match pat.node {
2219-
PatKind::Ident(_, ref path1, _) => path1.node.as_str(),
2219+
PatKind::Binding(_, ref path1, _) => path1.node.as_str(),
22202220
_ => {
22212221
bug!("Variable id {} maps to {:?}, not local", id, pat);
22222222
},

src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
9898
move_pat: &hir::Pat,
9999
cmt: mc::cmt<'tcx>) {
100100
let pat_span_path_opt = match move_pat.node {
101-
PatKind::Ident(_, ref path1, _) => {
101+
PatKind::Binding(_, ref path1, _) => {
102102
Some(MoveSpanAndPath{span: move_pat.span,
103103
name: path1.node})
104104
},

0 commit comments

Comments
 (0)