Skip to content

Commit 995b2b4

Browse files
committed
Auto merge of rust-lang#134248 - oli-obk:patkind-path-removal, r=<try>
Merge `PatKind::Path` into `PatKind::Lit` Follow-up to rust-lang#134228 We always had a duplication where `Path`s could be represented as `PatKind::Path` or `PatKind::Lit(ExprKind::Path)`. We had to handle both everywhere, and still do after rust-lang#134228, so I'm removing it now.
2 parents 0c2c096 + 6a6e2ae commit 995b2b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+320
-285
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ 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(hir::PatExprKind::Path(qpath));
13951395
return self.pat_without_dbm(lhs.span, unit_struct_pat);
13961396
}
13971397
}

compiler/rustc_ast_lowering/src/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
213213
self.insert(expr.span, expr.hir_id, Node::PatExpr(expr));
214214

215215
self.with_parent(expr.hir_id, |this| {
216-
intravisit::walk_pat_expr(this, expr);
216+
intravisit::walk_pat_expr(this, expr.hir_id, expr.span, &expr.kind);
217217
});
218218
}
219219

compiler/rustc_ast_lowering/src/pat.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ 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+
return hir::Pat {
74+
hir_id: pat_hir_id,
75+
kind: hir::PatKind::Expr(kind),
76+
span: pattern.span,
77+
default_binding_modes: true,
78+
};
7379
}
7480
PatKind::Struct(qself, path, fields, etc) => {
7581
let qpath = self.lower_qpath(
@@ -117,9 +123,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
117123
break hir::PatKind::Ref(self.lower_pat(inner), *mutbl);
118124
}
119125
PatKind::Range(e1, e2, Spanned { node: end, .. }) => {
126+
let mut lower_end = |e: &Expr| {
127+
&*self.arena.alloc(hir::PatExpr {
128+
hir_id: self.lower_node_id(e.id),
129+
span: e.span,
130+
kind: self.lower_expr_within_pat(e, true),
131+
})
132+
};
120133
break hir::PatKind::Range(
121-
e1.as_deref().map(|e| self.lower_expr_within_pat(e, true)),
122-
e2.as_deref().map(|e| self.lower_expr_within_pat(e, true)),
134+
e1.as_deref().map(|e| lower_end(e)),
135+
e2.as_deref().map(|e| lower_end(e)),
123136
self.lower_range_end(end, e2.is_some()),
124137
);
125138
}
@@ -306,14 +319,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
306319
Some(res) => {
307320
let hir_id = self.next_id();
308321
let res = self.lower_res(res);
309-
hir::PatKind::Path(hir::QPath::Resolved(
322+
hir::PatKind::Expr(hir::PatExprKind::Path(hir::QPath::Resolved(
310323
None,
311324
self.arena.alloc(hir::Path {
312325
span: self.lower_span(ident.span),
313326
res,
314327
segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
315328
}),
316-
))
329+
)))
317330
}
318331
}
319332
}
@@ -370,16 +383,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
370383
// }
371384
// m!(S);
372385
// ```
373-
fn lower_expr_within_pat(
374-
&mut self,
375-
expr: &Expr,
376-
allow_paths: bool,
377-
) -> &'hir hir::PatExpr<'hir> {
386+
fn lower_expr_within_pat(&mut self, expr: &Expr, allow_paths: bool) -> hir::PatExprKind<'hir> {
378387
let err = |guar| hir::PatExprKind::Lit {
379388
lit: self.arena.alloc(respan(self.lower_span(expr.span), LitKind::Err(guar))),
380389
negated: false,
381390
};
382-
let kind = match &expr.kind {
391+
match &expr.kind {
383392
ExprKind::Lit(lit) => {
384393
hir::PatExprKind::Lit { lit: self.lower_lit(lit, expr.span), negated: false }
385394
}
@@ -413,11 +422,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
413422
});
414423
err(guar)
415424
}
416-
};
417-
self.arena.alloc(hir::PatExpr {
418-
hir_id: self.lower_node_id(expr.id),
419-
span: expr.span,
420-
kind,
421-
})
425+
}
422426
}
423427
}

compiler/rustc_hir/src/hir.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ impl<'hir> Pat<'hir> {
13861386

13871387
use PatKind::*;
13881388
match self.kind {
1389-
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => true,
1389+
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => true,
13901390
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_short_(it),
13911391
Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk_short_(it)),
13921392
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().all(|p| p.walk_short_(it)),
@@ -1413,7 +1413,7 @@ impl<'hir> Pat<'hir> {
14131413

14141414
use PatKind::*;
14151415
match self.kind {
1416-
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => {}
1416+
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => {}
14171417
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_(it),
14181418
Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk_(it)),
14191419
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().for_each(|p| p.walk_(it)),
@@ -1521,6 +1521,7 @@ impl fmt::Debug for DotDotPos {
15211521

15221522
#[derive(Debug, Clone, Copy, HashStable_Generic)]
15231523
pub struct PatExpr<'hir> {
1524+
#[stable_hasher(ignore)]
15241525
pub hir_id: HirId,
15251526
pub span: Span,
15261527
pub kind: PatExprKind<'hir>,
@@ -1566,9 +1567,6 @@ pub enum PatKind<'hir> {
15661567
/// A never pattern `!`.
15671568
Never,
15681569

1569-
/// A path pattern for a unit struct/variant or a (maybe-associated) constant.
1570-
Path(QPath<'hir>),
1571-
15721570
/// A tuple pattern (e.g., `(a, b)`).
15731571
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
15741572
/// `0 <= position <= subpats.len()`
@@ -1584,7 +1582,7 @@ pub enum PatKind<'hir> {
15841582
Ref(&'hir Pat<'hir>, Mutability),
15851583

15861584
/// A literal, const block or path.
1587-
Expr(&'hir PatExpr<'hir>),
1585+
Expr(PatExprKind<'hir>),
15881586

15891587
/// A guard pattern (e.g., `x if guard(x)`).
15901588
Guard(&'hir Pat<'hir>, &'hir Expr<'hir>),

compiler/rustc_hir/src/intravisit.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ pub trait Visitor<'v>: Sized {
343343
walk_pat_field(self, f)
344344
}
345345
fn visit_pat_expr(&mut self, expr: &'v PatExpr<'v>) -> Self::Result {
346-
walk_pat_expr(self, expr)
346+
walk_pat_expr(self, expr.hir_id, expr.span, &expr.kind)
347347
}
348348
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
349349
walk_anon_const(self, c)
@@ -668,9 +668,6 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V:
668668
try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
669669
walk_list!(visitor, visit_pat, children);
670670
}
671-
PatKind::Path(ref qpath) => {
672-
try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
673-
}
674671
PatKind::Struct(ref qpath, fields, _) => {
675672
try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
676673
walk_list!(visitor, visit_pat_field, fields);
@@ -688,7 +685,9 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V:
688685
try_visit!(visitor.visit_ident(ident));
689686
visit_opt!(visitor, visit_pat, optional_subpattern);
690687
}
691-
PatKind::Expr(ref expression) => try_visit!(visitor.visit_pat_expr(expression)),
688+
PatKind::Expr(ref expression) => {
689+
try_visit!(walk_pat_expr(visitor, pattern.hir_id, pattern.span, expression))
690+
}
692691
PatKind::Range(ref lower_bound, ref upper_bound, _) => {
693692
visit_opt!(visitor, visit_pat_expr, lower_bound);
694693
visit_opt!(visitor, visit_pat_expr, upper_bound);
@@ -713,12 +712,17 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'
713712
visitor.visit_pat(field.pat)
714713
}
715714

716-
pub fn walk_pat_expr<'v, V: Visitor<'v>>(visitor: &mut V, expr: &'v PatExpr<'v>) -> V::Result {
717-
try_visit!(visitor.visit_id(expr.hir_id));
718-
match &expr.kind {
715+
pub fn walk_pat_expr<'v, V: Visitor<'v>>(
716+
visitor: &mut V,
717+
hir_id: HirId,
718+
span: Span,
719+
kind: &'v PatExprKind<'v>,
720+
) -> V::Result {
721+
try_visit!(visitor.visit_id(hir_id));
722+
match kind {
719723
PatExprKind::Lit { .. } => V::Result::output(),
720724
PatExprKind::ConstBlock(c) => visitor.visit_inline_const(c),
721-
PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, expr.hir_id, expr.span),
725+
PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, hir_id, span),
722726
}
723727
}
724728

compiler/rustc_hir/src/pat_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ 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::PatExprKind::Path(hir::QPath::Resolved(_, path)))
109109
| PatKind::TupleStruct(hir::QPath::Resolved(_, path), ..)
110110
| PatKind::Struct(hir::QPath::Resolved(_, path), ..) => {
111111
if let Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), id) =

compiler/rustc_hir_analysis/src/check/region.rs

-1
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
3939
..
4040
})
4141
| hir::Node::Pat(hir::Pat {
42-
kind: hir::PatKind::Path(hir::QPath::TypeRelative(qself, _)),
42+
kind: hir::PatKind::Expr(hir::PatExprKind::Path(hir::QPath::TypeRelative(qself, _))),
43+
..
44+
})
45+
| hir::Node::PatExpr(hir::PatExpr {
46+
kind: hir::PatExprKind::Path(hir::QPath::TypeRelative(qself, _)),
4347
..
4448
}) if qself.hir_id == self_ty.hir_id => true,
4549
_ => false,

compiler/rustc_hir_pretty/src/lib.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'a> State<'a> {
199199
Node::OpaqueTy(o) => self.print_opaque_ty(o),
200200
Node::Pat(a) => self.print_pat(a),
201201
Node::PatField(a) => self.print_patfield(a),
202-
Node::PatExpr(a) => self.print_pat_expr(a),
202+
Node::PatExpr(a) => self.print_pat_expr(&a.kind),
203203
Node::Arm(a) => self.print_arm(a),
204204
Node::Infer(_) => self.word("_"),
205205
Node::PreciseCapturingNonLifetimeArg(param) => self.print_ident(param.ident),
@@ -1850,8 +1850,8 @@ impl<'a> State<'a> {
18501850
}
18511851
}
18521852

1853-
fn print_pat_expr(&mut self, expr: &hir::PatExpr<'_>) {
1854-
match &expr.kind {
1853+
fn print_pat_expr(&mut self, kind: &hir::PatExprKind<'_>) {
1854+
match kind {
18551855
hir::PatExprKind::Lit { lit, negated } => {
18561856
if *negated {
18571857
self.word("-");
@@ -1905,9 +1905,6 @@ impl<'a> State<'a> {
19051905
}
19061906
self.pclose();
19071907
}
1908-
PatKind::Path(ref qpath) => {
1909-
self.print_qpath(qpath, true);
1910-
}
19111908
PatKind::Struct(ref qpath, fields, etc) => {
19121909
self.print_qpath(qpath, true);
19131910
self.nbsp();
@@ -1980,17 +1977,17 @@ impl<'a> State<'a> {
19801977
self.pclose();
19811978
}
19821979
}
1983-
PatKind::Expr(e) => self.print_pat_expr(e),
1980+
PatKind::Expr(ref e) => self.print_pat_expr(e),
19841981
PatKind::Range(begin, end, end_kind) => {
19851982
if let Some(expr) = begin {
1986-
self.print_pat_expr(expr);
1983+
self.print_pat_expr(&expr.kind);
19871984
}
19881985
match end_kind {
19891986
RangeEnd::Included => self.word("..."),
19901987
RangeEnd::Excluded => self.word(".."),
19911988
}
19921989
if let Some(expr) = end {
1993-
self.print_pat_expr(expr);
1990+
self.print_pat_expr(&expr.kind);
19941991
}
19951992
}
19961993
PatKind::Slice(before, slice, after) => {

compiler/rustc_hir_typeck/src/expr.rs

-1
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

+3-5
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, 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,7 +563,7 @@ 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(PatExprKind::Path(qpath)) => {
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

@@ -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

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use hir::def_id::LocalDefId;
55
use rustc_ast::util::parser::ExprPrecedence;
66
use rustc_data_structures::packed::Pu128;
77
use rustc_errors::{Applicability, Diag, MultiSpan};
8-
use rustc_hir as hir;
98
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
109
use rustc_hir::lang_items::LangItem;
1110
use rustc_hir::{
12-
Arm, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId,
13-
Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicateKind, expr_needs_parens,
11+
self as hir, Arm, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind,
12+
GenericBound, HirId, Node, PatExprKind, Path, QPath, Stmt, StmtKind, TyKind,
13+
WherePredicateKind, expr_needs_parens,
1414
};
1515
use rustc_hir_analysis::collect::suggest_impl_trait;
1616
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
@@ -1419,8 +1419,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14191419
// since the user probably just misunderstood how `let else`
14201420
// and `&&` work together.
14211421
if let Some((_, hir::Node::LetStmt(local))) = cond_parent
1422-
&& let hir::PatKind::Path(qpath) | hir::PatKind::TupleStruct(qpath, _, _) =
1423-
&local.pat.kind
1422+
&& let hir::PatKind::Expr(PatExprKind::Path(qpath))
1423+
| hir::PatKind::TupleStruct(qpath, _, _) = &local.pat.kind
14241424
&& let hir::QPath::Resolved(None, path) = qpath
14251425
&& let Some(did) = path
14261426
.res

compiler/rustc_hir_typeck/src/method/suggest.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
177177
})
178178
| hir::Node::Pat(&hir::Pat {
179179
kind:
180-
hir::PatKind::Path(QPath::TypeRelative(rcvr, segment))
181-
| hir::PatKind::Struct(QPath::TypeRelative(rcvr, segment), ..)
182-
| hir::PatKind::TupleStruct(QPath::TypeRelative(rcvr, segment), ..),
180+
hir::PatKind::Struct(QPath::TypeRelative(rcvr, segment), ..)
181+
| hir::PatKind::TupleStruct(QPath::TypeRelative(rcvr, segment), ..)
182+
| hir::PatKind::Expr(hir::PatExprKind::Path(QPath::TypeRelative(rcvr, segment))),
183183
span,
184184
..
185185
}) => {

0 commit comments

Comments
 (0)