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

0 commit comments

Comments
 (0)