Skip to content

Commit 325f701

Browse files
committed
Eliminate PatKind::Path
1 parent ccc9ba5 commit 325f701

File tree

38 files changed

+260
-170
lines changed

38 files changed

+260
-170
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
13911391
None,
13921392
);
13931393
// Destructure like a unit struct.
1394-
let unit_struct_pat = hir::PatKind::Path(qpath);
1394+
let unit_struct_pat = hir::PatKind::Expr(self.arena.alloc(hir::PatExpr {
1395+
kind: hir::PatExprKind::Path(qpath),
1396+
hir_id: self.next_id(),
1397+
span: self.lower_span(lhs.span),
1398+
}));
13951399
return self.pat_without_dbm(lhs.span, unit_struct_pat);
13961400
}
13971401
}

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6969
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
7070
None,
7171
);
72-
break hir::PatKind::Path(qpath);
72+
let kind = hir::PatExprKind::Path(qpath);
73+
let span = self.lower_span(pattern.span);
74+
let expr = hir::PatExpr { hir_id: pat_hir_id, span, kind };
75+
let expr = self.arena.alloc(expr);
76+
return hir::Pat {
77+
hir_id: self.next_id(),
78+
kind: hir::PatKind::Expr(expr),
79+
span,
80+
default_binding_modes: true,
81+
};
7382
}
7483
PatKind::Struct(qself, path, fields, etc) => {
7584
let qpath = self.lower_qpath(
@@ -304,16 +313,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
304313
)
305314
}
306315
Some(res) => {
307-
let hir_id = self.next_id();
308316
let res = self.lower_res(res);
309-
hir::PatKind::Path(hir::QPath::Resolved(
310-
None,
311-
self.arena.alloc(hir::Path {
312-
span: self.lower_span(ident.span),
313-
res,
314-
segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
315-
}),
316-
))
317+
let span = self.lower_span(ident.span);
318+
hir::PatKind::Expr(self.arena.alloc(hir::PatExpr {
319+
kind: hir::PatExprKind::Path(hir::QPath::Resolved(
320+
None,
321+
self.arena.alloc(hir::Path {
322+
span,
323+
res,
324+
segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), self.next_id(), res)],
325+
}),
326+
)),
327+
hir_id: self.next_id(),
328+
span,
329+
}))
317330
}
318331
}
319332
}

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ impl<'hir> Pat<'hir> {
14371437

14381438
use PatKind::*;
14391439
match self.kind {
1440-
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => true,
1440+
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => true,
14411441
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_short_(it),
14421442
Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk_short_(it)),
14431443
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().all(|p| p.walk_short_(it)),
@@ -1464,7 +1464,7 @@ impl<'hir> Pat<'hir> {
14641464

14651465
use PatKind::*;
14661466
match self.kind {
1467-
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => {}
1467+
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => {}
14681468
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_(it),
14691469
Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk_(it)),
14701470
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().for_each(|p| p.walk_(it)),
@@ -1618,9 +1618,6 @@ pub enum PatKind<'hir> {
16181618
/// A never pattern `!`.
16191619
Never,
16201620

1621-
/// A path pattern for a unit struct/variant or a (maybe-associated) constant.
1622-
Path(QPath<'hir>),
1623-
16241621
/// A tuple pattern (e.g., `(a, b)`).
16251622
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
16261623
/// `0 <= position <= subpats.len()`

compiler/rustc_hir/src/intravisit.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,6 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V:
709709
try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
710710
walk_list!(visitor, visit_pat, children);
711711
}
712-
PatKind::Path(ref qpath) => {
713-
try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
714-
}
715712
PatKind::Struct(ref qpath, fields, _) => {
716713
try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
717714
walk_list!(visitor, visit_pat_field, fields);

compiler/rustc_hir/src/pat_util.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ impl hir::Pat<'_> {
105105
let mut variants = vec![];
106106
self.walk(|p| match &p.kind {
107107
PatKind::Or(_) => false,
108-
PatKind::Path(hir::QPath::Resolved(_, path))
108+
PatKind::Expr(hir::PatExpr {
109+
kind: hir::PatExprKind::Path(hir::QPath::Resolved(_, path)),
110+
..
111+
})
109112
| PatKind::TupleStruct(hir::QPath::Resolved(_, path), ..)
110113
| PatKind::Struct(hir::QPath::Resolved(_, path), ..) => {
111114
if let Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), id) =

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,6 @@ fn resolve_local<'tcx>(
703703
| PatKind::Binding(hir::BindingMode(hir::ByRef::No, _), ..)
704704
| PatKind::Wild
705705
| PatKind::Never
706-
| PatKind::Path(_)
707706
| PatKind::Expr(_)
708707
| PatKind::Range(_, _, _)
709708
| PatKind::Err(_) => false,

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
4141
kind: hir::ExprKind::Path(hir::QPath::TypeRelative(qself, _)),
4242
..
4343
})
44-
| hir::Node::Pat(hir::Pat {
45-
kind: hir::PatKind::Path(hir::QPath::TypeRelative(qself, _)),
44+
| hir::Node::PatExpr(hir::PatExpr {
45+
kind: hir::PatExprKind::Path(hir::QPath::TypeRelative(qself, _)),
4646
..
4747
}) if qself.hir_id == self_ty.hir_id => true,
4848
_ => false,

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,9 +1906,6 @@ impl<'a> State<'a> {
19061906
}
19071907
self.pclose();
19081908
}
1909-
PatKind::Path(ref qpath) => {
1910-
self.print_qpath(qpath, true);
1911-
}
19121909
PatKind::Struct(ref qpath, fields, etc) => {
19131910
self.print_qpath(qpath, true);
19141911
self.nbsp();

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
480480
hir::PatKind::Binding(_, _, _, _)
481481
| hir::PatKind::Struct(_, _, _)
482482
| hir::PatKind::TupleStruct(_, _, _)
483-
| hir::PatKind::Path(_)
484483
| hir::PatKind::Tuple(_, _)
485484
| hir::PatKind::Box(_)
486485
| hir::PatKind::Ref(_, _)

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ use hir::def::DefKind;
1111
use hir::pat_util::EnumerateAndAdjustIterator as _;
1212
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
1313
use rustc_data_structures::fx::FxIndexMap;
14-
use rustc_hir as hir;
1514
use rustc_hir::def::{CtorOf, Res};
1615
use rustc_hir::def_id::LocalDefId;
17-
use rustc_hir::{HirId, PatKind};
16+
use rustc_hir::{self as hir, HirId, PatExpr, PatExprKind, PatKind};
1817
use rustc_lint::LateContext;
1918
use rustc_middle::hir::place::ProjectionKind;
2019
// Export these here so that Clippy can use them.
@@ -564,11 +563,11 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
564563
// FIXME(never_patterns): does this do what I expect?
565564
needs_to_be_read = true;
566565
}
567-
PatKind::Path(qpath) => {
566+
PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, span }) => {
568567
// A `Path` pattern is just a name like `Foo`. This is either a
569568
// named constant or else it refers to an ADT variant
570569

571-
let res = self.cx.typeck_results().qpath_res(qpath, pat.hir_id);
570+
let res = self.cx.typeck_results().qpath_res(qpath, *hir_id);
572571
match res {
573572
Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => {
574573
// Named constants have to be equated with the value
@@ -581,7 +580,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
581580
// Otherwise, this is a struct/enum variant, and so it's
582581
// only a read if we need to read the discriminant.
583582
needs_to_be_read |=
584-
self.is_multivariant_adt(place.place.ty(), pat.span);
583+
self.is_multivariant_adt(place.place.ty(), *span);
585584
}
586585
}
587586
}
@@ -1801,8 +1800,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
18011800
}
18021801
}
18031802

1804-
PatKind::Path(_)
1805-
| PatKind::Binding(.., None)
1803+
PatKind::Binding(.., None)
18061804
| PatKind::Expr(..)
18071805
| PatKind::Range(..)
18081806
| PatKind::Never

0 commit comments

Comments
 (0)