Skip to content

Commit f903c97

Browse files
committed
Merge PatKind::QPath into PatKind::Path in AST
1 parent d06f1dc commit f903c97

File tree

10 files changed

+24
-46
lines changed

10 files changed

+24
-46
lines changed

src/librustc/hir/lowering.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -866,10 +866,10 @@ impl<'a> LoweringContext<'a> {
866866
pats.iter().map(|x| self.lower_pat(x)).collect(),
867867
ddpos)
868868
}
869-
PatKind::Path(ref pth) => {
869+
PatKind::Path(None, ref pth) => {
870870
hir::PatKind::Path(self.lower_path(pth))
871871
}
872-
PatKind::QPath(ref qself, ref pth) => {
872+
PatKind::Path(Some(ref qself), ref pth) => {
873873
let qself = hir::QSelf {
874874
ty: self.lower_ty(&qself.ty),
875875
position: qself.position,

src/librustc_resolve/lib.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -2332,8 +2332,8 @@ impl<'a> Resolver<'a> {
23322332
}, "variant or struct");
23332333
}
23342334

2335-
PatKind::Path(ref path) => {
2336-
self.resolve_pattern_path(pat.id, None, path, ValueNS, |def| {
2335+
PatKind::Path(ref qself, ref path) => {
2336+
self.resolve_pattern_path(pat.id, qself.as_ref(), path, ValueNS, |def| {
23372337
match def {
23382338
Def::Struct(..) | Def::Variant(..) |
23392339
Def::Const(..) | Def::AssociatedConst(..) | Def::Err => true,
@@ -2342,15 +2342,6 @@ impl<'a> Resolver<'a> {
23422342
}, "variant, struct or constant");
23432343
}
23442344

2345-
PatKind::QPath(ref qself, ref path) => {
2346-
self.resolve_pattern_path(pat.id, Some(qself), path, ValueNS, |def| {
2347-
match def {
2348-
Def::AssociatedConst(..) | Def::Err => true,
2349-
_ => false,
2350-
}
2351-
}, "associated constant");
2352-
}
2353-
23542345
PatKind::Struct(ref path, _, _) => {
23552346
self.resolve_pattern_path(pat.id, None, path, TypeNS, |def| {
23562347
match def {

src/librustc_save_analysis/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,7 @@ impl<'v> Visitor<'v> for PathCollector {
699699
ast::Mutability::Mutable, recorder::TypeRef));
700700
}
701701
PatKind::TupleStruct(ref path, _, _) |
702-
PatKind::Path(ref path) |
703-
PatKind::QPath(_, ref path) => {
702+
PatKind::Path(_, ref path) => {
704703
self.collected_paths.push((p.id, path.clone(),
705704
ast::Mutability::Mutable, recorder::VarRef));
706705
}

src/libsyntax/ast.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,6 @@ impl Pat {
579579
PatKind::Range(_, _) |
580580
PatKind::Ident(_, _, _) |
581581
PatKind::Path(..) |
582-
PatKind::QPath(_, _) |
583582
PatKind::Mac(_) => {
584583
true
585584
}
@@ -627,15 +626,11 @@ pub enum PatKind {
627626
/// 0 <= position <= subpats.len()
628627
TupleStruct(Path, Vec<P<Pat>>, Option<usize>),
629628

630-
/// A path pattern.
631-
/// Such pattern can be resolved to a unit struct/variant or a constant.
632-
Path(Path),
633-
634-
/// An associated const named using the qualified path `<T>::CONST` or
635-
/// `<T as Trait>::CONST`. Associated consts from inherent impls can be
636-
/// referred to as simply `T::CONST`, in which case they will end up as
637-
/// PatKind::Path, and the resolver will have to sort that out.
638-
QPath(QSelf, Path),
629+
/// A possibly qualified path pattern.
630+
/// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
631+
/// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
632+
/// only legally refer to associated constants.
633+
Path(Option<QSelf>, Path),
639634

640635
/// A tuple pattern `(a, b)`.
641636
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.

src/libsyntax/ext/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
830830
}
831831
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
832832
let pat = if subpats.is_empty() {
833-
PatKind::Path(path)
833+
PatKind::Path(None, path)
834834
} else {
835835
PatKind::TupleStruct(path, subpats, None)
836836
};

src/libsyntax/fold.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,11 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
10881088
PatKind::TupleStruct(folder.fold_path(pth),
10891089
pats.move_map(|x| folder.fold_pat(x)), ddpos)
10901090
}
1091-
PatKind::Path(pth) => {
1092-
PatKind::Path(folder.fold_path(pth))
1093-
}
1094-
PatKind::QPath(qself, pth) => {
1095-
let qself = QSelf {ty: folder.fold_ty(qself.ty), .. qself};
1096-
PatKind::QPath(qself, folder.fold_path(pth))
1091+
PatKind::Path(opt_qself, pth) => {
1092+
let opt_qself = opt_qself.map(|qself| {
1093+
QSelf { ty: folder.fold_ty(qself.ty), position: qself.position }
1094+
});
1095+
PatKind::Path(opt_qself, folder.fold_path(pth))
10971096
}
10981097
PatKind::Struct(pth, fields, etc) => {
10991098
let pth = folder.fold_path(pth);

src/libsyntax/parse/parser.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -3715,12 +3715,7 @@ impl<'a> Parser<'a> {
37153715
pat = PatKind::TupleStruct(path, fields, ddpos)
37163716
}
37173717
_ => {
3718-
pat = match qself {
3719-
// Parse qualified path
3720-
Some(qself) => PatKind::QPath(qself, path),
3721-
// Parse nullary enum
3722-
None => PatKind::Path(path)
3723-
};
3718+
pat = PatKind::Path(qself, path);
37243719
}
37253720
}
37263721
}

src/libsyntax/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2483,10 +2483,10 @@ impl<'a> State<'a> {
24832483
}
24842484
try!(self.pclose());
24852485
}
2486-
PatKind::Path(ref path) => {
2486+
PatKind::Path(None, ref path) => {
24872487
try!(self.print_path(path, true, 0));
24882488
}
2489-
PatKind::QPath(ref qself, ref path) => {
2489+
PatKind::Path(Some(ref qself), ref path) => {
24902490
try!(self.print_qpath(path, qself, false));
24912491
}
24922492
PatKind::Struct(ref path, ref fields, etc) => {

src/libsyntax/visit.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,10 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
409409
visitor.visit_path(path, pattern.id);
410410
walk_list!(visitor, visit_pat, children);
411411
}
412-
PatKind::Path(ref path) => {
413-
visitor.visit_path(path, pattern.id);
414-
}
415-
PatKind::QPath(ref qself, ref path) => {
416-
visitor.visit_ty(&qself.ty);
412+
PatKind::Path(ref opt_qself, ref path) => {
413+
if let Some(ref qself) = *opt_qself {
414+
visitor.visit_ty(&qself.ty);
415+
}
417416
visitor.visit_path(path, pattern.id)
418417
}
419418
PatKind::Struct(ref path, ref fields, _) => {

src/test/compile-fail/method-resolvable-path-in-pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ impl MyTrait for Foo {}
1919
fn main() {
2020
match 0u32 {
2121
<Foo as MyTrait>::trait_bar => {}
22-
//~^ ERROR expected associated constant, found method `trait_bar`
22+
//~^ ERROR expected variant, struct or constant, found method `trait_bar`
2323
}
2424
}

0 commit comments

Comments
 (0)