Skip to content

Commit 2119976

Browse files
committed
Auto merge of #87140 - camsteffen:pat-slice-refs, r=oli-obk
Remove refs from Pat slices Changes `PatKind::Or(&'hir [&'hir Pat<'hir>])` to `PatKind::Or(&'hir [Pat<'hir>])` and others. This is more consistent with `ExprKind`, saves a little memory, and is a little easier to use.
2 parents 59d92bd + 1537cd4 commit 2119976

File tree

12 files changed

+85
-68
lines changed

12 files changed

+85
-68
lines changed

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
10671067
eq_sign_span: Span,
10681068
assignments: &mut Vec<hir::Stmt<'hir>>,
10691069
) -> &'hir hir::Pat<'hir> {
1070+
self.arena.alloc(self.destructure_assign_mut(lhs, eq_sign_span, assignments))
1071+
}
1072+
1073+
fn destructure_assign_mut(
1074+
&mut self,
1075+
lhs: &Expr,
1076+
eq_sign_span: Span,
1077+
assignments: &mut Vec<hir::Stmt<'hir>>,
1078+
) -> hir::Pat<'hir> {
10701079
match &lhs.kind {
10711080
// Underscore pattern.
10721081
ExprKind::Underscore => {
@@ -1080,7 +1089,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10801089
let (before, after) = pats.split_at(i);
10811090
hir::PatKind::Slice(
10821091
before,
1083-
Some(self.pat_without_dbm(span, hir::PatKind::Wild)),
1092+
Some(self.arena.alloc(self.pat_without_dbm(span, hir::PatKind::Wild))),
10841093
after,
10851094
)
10861095
} else {
@@ -1165,14 +1174,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
11651174
let tuple_pat = hir::PatKind::Tuple(&[], Some(0));
11661175
return self.pat_without_dbm(lhs.span, tuple_pat);
11671176
} else {
1168-
return self.destructure_assign(e, eq_sign_span, assignments);
1177+
return self.destructure_assign_mut(e, eq_sign_span, assignments);
11691178
}
11701179
}
11711180
_ => {}
11721181
}
11731182
// Treat all other cases as normal lvalue.
11741183
let ident = Ident::new(sym::lhs, lhs.span);
1175-
let (pat, binding) = self.pat_ident(lhs.span, ident);
1184+
let (pat, binding) = self.pat_ident_mut(lhs.span, ident);
11761185
let ident = self.expr_ident(lhs.span, ident, binding);
11771186
let assign = hir::ExprKind::Assign(self.lower_expr(lhs), ident, eq_sign_span);
11781187
let expr = self.expr(lhs.span, assign, ThinVec::new());
@@ -1191,7 +1200,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11911200
ctx: &str,
11921201
eq_sign_span: Span,
11931202
assignments: &mut Vec<hir::Stmt<'hir>>,
1194-
) -> (&'hir [&'hir hir::Pat<'hir>], Option<(usize, Span)>) {
1203+
) -> (&'hir [hir::Pat<'hir>], Option<(usize, Span)>) {
11951204
let mut rest = None;
11961205
let elements =
11971206
self.arena.alloc_from_iter(elements.iter().enumerate().filter_map(|(i, e)| {
@@ -1204,7 +1213,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12041213
}
12051214
None
12061215
} else {
1207-
Some(self.destructure_assign(e, eq_sign_span, assignments))
1216+
Some(self.destructure_assign_mut(e, eq_sign_span, assignments))
12081217
}
12091218
}));
12101219
(elements, rest)

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -2577,21 +2577,35 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25772577
self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::Unannotated)
25782578
}
25792579

2580+
fn pat_ident_mut(&mut self, span: Span, ident: Ident) -> (hir::Pat<'hir>, hir::HirId) {
2581+
self.pat_ident_binding_mode_mut(span, ident, hir::BindingAnnotation::Unannotated)
2582+
}
2583+
25802584
fn pat_ident_binding_mode(
25812585
&mut self,
25822586
span: Span,
25832587
ident: Ident,
25842588
bm: hir::BindingAnnotation,
25852589
) -> (&'hir hir::Pat<'hir>, hir::HirId) {
2590+
let (pat, hir_id) = self.pat_ident_binding_mode_mut(span, ident, bm);
2591+
(self.arena.alloc(pat), hir_id)
2592+
}
2593+
2594+
fn pat_ident_binding_mode_mut(
2595+
&mut self,
2596+
span: Span,
2597+
ident: Ident,
2598+
bm: hir::BindingAnnotation,
2599+
) -> (hir::Pat<'hir>, hir::HirId) {
25862600
let hir_id = self.next_id();
25872601

25882602
(
2589-
self.arena.alloc(hir::Pat {
2603+
hir::Pat {
25902604
hir_id,
25912605
kind: hir::PatKind::Binding(bm, hir_id, ident.with_span_pos(span), None),
25922606
span,
25932607
default_binding_modes: true,
2594-
}),
2608+
},
25952609
hir_id,
25962610
)
25972611
}
@@ -2609,13 +2623,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26092623
})
26102624
}
26112625

2612-
fn pat_without_dbm(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
2613-
self.arena.alloc(hir::Pat {
2614-
hir_id: self.next_id(),
2615-
kind,
2616-
span,
2617-
default_binding_modes: false,
2618-
})
2626+
fn pat_without_dbm(&mut self, span: Span, kind: hir::PatKind<'hir>) -> hir::Pat<'hir> {
2627+
hir::Pat { hir_id: self.next_id(), kind, span, default_binding_modes: false }
26192628
}
26202629

26212630
fn ty_path(

Diff for: compiler/rustc_ast_lowering/src/pat.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ use rustc_span::symbol::Ident;
1010
use rustc_span::{source_map::Spanned, Span};
1111

1212
impl<'a, 'hir> LoweringContext<'a, 'hir> {
13-
crate fn lower_pat(&mut self, mut pattern: &Pat) -> &'hir hir::Pat<'hir> {
13+
crate fn lower_pat(&mut self, pattern: &Pat) -> &'hir hir::Pat<'hir> {
14+
self.arena.alloc(self.lower_pat_mut(pattern))
15+
}
16+
17+
crate fn lower_pat_mut(&mut self, mut pattern: &Pat) -> hir::Pat<'hir> {
1418
ensure_sufficient_stack(|| {
1519
// loop here to avoid recursion
1620
let node = loop {
@@ -34,7 +38,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3438
}
3539
PatKind::Or(ref pats) => {
3640
break hir::PatKind::Or(
37-
self.arena.alloc_from_iter(pats.iter().map(|x| self.lower_pat(x))),
41+
self.arena.alloc_from_iter(pats.iter().map(|x| self.lower_pat_mut(x))),
3842
);
3943
}
4044
PatKind::Path(ref qself, ref path) => {
@@ -101,7 +105,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
101105
&mut self,
102106
pats: &[P<Pat>],
103107
ctx: &str,
104-
) -> (&'hir [&'hir hir::Pat<'hir>], Option<usize>) {
108+
) -> (&'hir [hir::Pat<'hir>], Option<usize>) {
105109
let mut elems = Vec::with_capacity(pats.len());
106110
let mut rest = None;
107111

@@ -140,7 +144,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
140144
}
141145

142146
// It was not a sub-tuple pattern so lower it normally.
143-
elems.push(self.lower_pat(pat));
147+
elems.push(self.lower_pat_mut(pat));
144148
}
145149

146150
for (_, pat) in iter {
@@ -149,7 +153,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
149153
// ...but there was one again, so error.
150154
self.ban_extra_rest_pat(pat.span, rest.unwrap().1, ctx);
151155
} else {
152-
elems.push(self.lower_pat(pat));
156+
elems.push(self.lower_pat_mut(pat));
153157
}
154158
}
155159

@@ -189,11 +193,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
189193
// Record, lower it to `$binding_mode $ident @ _`, and stop here.
190194
PatKind::Ident(ref bm, ident, Some(ref sub)) if sub.is_rest() => {
191195
prev_rest_span = Some(sub.span);
192-
slice = Some(lower_rest_sub(self, pat, bm, ident, sub));
196+
slice = Some(self.arena.alloc(lower_rest_sub(self, pat, bm, ident, sub)));
193197
break;
194198
}
195199
// It was not a subslice pattern so lower it normally.
196-
_ => before.push(self.lower_pat(pat)),
200+
_ => before.push(self.lower_pat_mut(pat)),
197201
}
198202
}
199203

@@ -214,7 +218,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
214218
self.ban_extra_rest_pat(rest_span, prev_rest_span.unwrap(), "slice");
215219
} else {
216220
// Lower the pattern normally.
217-
after.push(self.lower_pat(pat));
221+
after.push(self.lower_pat_mut(pat));
218222
}
219223
}
220224

@@ -268,17 +272,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
268272
}
269273

270274
fn pat_wild_with_node_id_of(&mut self, p: &Pat) -> &'hir hir::Pat<'hir> {
271-
self.pat_with_node_id_of(p, hir::PatKind::Wild)
275+
self.arena.alloc(self.pat_with_node_id_of(p, hir::PatKind::Wild))
272276
}
273277

274278
/// Construct a `Pat` with the `HirId` of `p.id` lowered.
275-
fn pat_with_node_id_of(&mut self, p: &Pat, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
276-
self.arena.alloc(hir::Pat {
279+
fn pat_with_node_id_of(&mut self, p: &Pat, kind: hir::PatKind<'hir>) -> hir::Pat<'hir> {
280+
hir::Pat {
277281
hir_id: self.lower_node_id(p.id),
278282
kind,
279283
span: p.span,
280284
default_binding_modes: true,
281-
})
285+
}
282286
}
283287

284288
/// Emit a friendly error for extra `..` patterns in a tuple/tuple struct/slice pattern.

Diff for: compiler/rustc_hir/src/hir.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -808,13 +808,13 @@ impl<'hir> Pat<'hir> {
808808
}
809809

810810
use PatKind::*;
811-
match &self.kind {
811+
match self.kind {
812812
Wild | Lit(_) | Range(..) | Binding(.., None) | Path(_) => true,
813813
Box(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_short_(it),
814814
Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk_short_(it)),
815815
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().all(|p| p.walk_short_(it)),
816816
Slice(before, slice, after) => {
817-
before.iter().chain(slice.iter()).chain(after.iter()).all(|p| p.walk_short_(it))
817+
before.iter().chain(slice).chain(after.iter()).all(|p| p.walk_short_(it))
818818
}
819819
}
820820
}
@@ -836,13 +836,13 @@ impl<'hir> Pat<'hir> {
836836
}
837837

838838
use PatKind::*;
839-
match &self.kind {
839+
match self.kind {
840840
Wild | Lit(_) | Range(..) | Binding(.., None) | Path(_) => {}
841841
Box(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_(it),
842842
Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk_(it)),
843843
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().for_each(|p| p.walk_(it)),
844844
Slice(before, slice, after) => {
845-
before.iter().chain(slice.iter()).chain(after.iter()).for_each(|p| p.walk_(it))
845+
before.iter().chain(slice).chain(after.iter()).for_each(|p| p.walk_(it))
846846
}
847847
}
848848
}
@@ -940,19 +940,19 @@ pub enum PatKind<'hir> {
940940
/// A tuple struct/variant pattern `Variant(x, y, .., z)`.
941941
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
942942
/// `0 <= position <= subpats.len()`
943-
TupleStruct(QPath<'hir>, &'hir [&'hir Pat<'hir>], Option<usize>),
943+
TupleStruct(QPath<'hir>, &'hir [Pat<'hir>], Option<usize>),
944944

945945
/// An or-pattern `A | B | C`.
946946
/// Invariant: `pats.len() >= 2`.
947-
Or(&'hir [&'hir Pat<'hir>]),
947+
Or(&'hir [Pat<'hir>]),
948948

949949
/// A path pattern for an unit struct/variant or a (maybe-associated) constant.
950950
Path(QPath<'hir>),
951951

952952
/// A tuple pattern (e.g., `(a, b)`).
953953
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
954954
/// `0 <= position <= subpats.len()`
955-
Tuple(&'hir [&'hir Pat<'hir>], Option<usize>),
955+
Tuple(&'hir [Pat<'hir>], Option<usize>),
956956

957957
/// A `box` pattern.
958958
Box(&'hir Pat<'hir>),
@@ -975,7 +975,7 @@ pub enum PatKind<'hir> {
975975
/// ```
976976
/// PatKind::Slice([Binding(a), Binding(b)], Some(Wild), [Binding(c), Binding(d)])
977977
/// ```
978-
Slice(&'hir [&'hir Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [&'hir Pat<'hir>]),
978+
Slice(&'hir [Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [Pat<'hir>]),
979979
}
980980

981981
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)]

Diff for: compiler/rustc_mir_build/src/thir/pattern/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
325325

326326
fn lower_tuple_subpats(
327327
&mut self,
328-
pats: &'tcx [&'tcx hir::Pat<'tcx>],
328+
pats: &'tcx [hir::Pat<'tcx>],
329329
expected_len: usize,
330330
gap_pos: Option<usize>,
331331
) -> Vec<FieldPat<'tcx>> {
@@ -338,7 +338,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
338338
.collect()
339339
}
340340

341-
fn lower_patterns(&mut self, pats: &'tcx [&'tcx hir::Pat<'tcx>]) -> Vec<Pat<'tcx>> {
341+
fn lower_patterns(&mut self, pats: &'tcx [hir::Pat<'tcx>]) -> Vec<Pat<'tcx>> {
342342
pats.iter().map(|p| self.lower_pattern(p)).collect()
343343
}
344344

@@ -350,9 +350,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
350350
&mut self,
351351
span: Span,
352352
ty: Ty<'tcx>,
353-
prefix: &'tcx [&'tcx hir::Pat<'tcx>],
353+
prefix: &'tcx [hir::Pat<'tcx>],
354354
slice: &'tcx Option<&'tcx hir::Pat<'tcx>>,
355-
suffix: &'tcx [&'tcx hir::Pat<'tcx>],
355+
suffix: &'tcx [hir::Pat<'tcx>],
356356
) -> PatKind<'tcx> {
357357
let prefix = self.lower_patterns(prefix);
358358
let slice = self.lower_opt_pattern(slice);

Diff for: compiler/rustc_typeck/src/check/pat.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
864864
&self,
865865
pat: &'tcx Pat<'tcx>,
866866
qpath: &'tcx hir::QPath<'tcx>,
867-
subpats: &'tcx [&'tcx Pat<'tcx>],
867+
subpats: &'tcx [Pat<'tcx>],
868868
ddpos: Option<usize>,
869869
expected: Ty<'tcx>,
870870
def_bm: BindingMode,
@@ -982,7 +982,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
982982
pat_span: Span,
983983
res: Res,
984984
qpath: &hir::QPath<'_>,
985-
subpats: &'tcx [&'tcx Pat<'tcx>],
985+
subpats: &'tcx [Pat<'tcx>],
986986
fields: &'tcx [ty::FieldDef],
987987
expected: Ty<'tcx>,
988988
had_err: bool,
@@ -1112,7 +1112,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11121112
fn check_pat_tuple(
11131113
&self,
11141114
span: Span,
1115-
elements: &'tcx [&'tcx Pat<'tcx>],
1115+
elements: &'tcx [Pat<'tcx>],
11161116
ddpos: Option<usize>,
11171117
expected: Ty<'tcx>,
11181118
def_bm: BindingMode,
@@ -1746,9 +1746,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17461746
fn check_pat_slice(
17471747
&self,
17481748
span: Span,
1749-
before: &'tcx [&'tcx Pat<'tcx>],
1749+
before: &'tcx [Pat<'tcx>],
17501750
slice: Option<&'tcx Pat<'tcx>>,
1751-
after: &'tcx [&'tcx Pat<'tcx>],
1751+
after: &'tcx [Pat<'tcx>],
17521752
expected: Ty<'tcx>,
17531753
def_bm: BindingMode,
17541754
ti: TopInfo<'tcx>,

Diff for: src/librustdoc/clean/utils.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,12 @@ crate fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
260260
PatKind::Wild | PatKind::Struct(..) => return kw::Underscore,
261261
PatKind::Binding(_, _, ident, _) => return ident.name,
262262
PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
263-
PatKind::Or(ref pats) => pats
264-
.iter()
265-
.map(|p| name_from_pat(&**p).to_string())
266-
.collect::<Vec<String>>()
267-
.join(" | "),
263+
PatKind::Or(ref pats) => {
264+
pats.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(" | ")
265+
}
268266
PatKind::Tuple(ref elts, _) => format!(
269267
"({})",
270-
elts.iter()
271-
.map(|p| name_from_pat(&**p).to_string())
272-
.collect::<Vec<String>>()
273-
.join(", ")
268+
elts.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(", ")
274269
),
275270
PatKind::Box(ref p) => return name_from_pat(&**p),
276271
PatKind::Ref(ref p, _) => return name_from_pat(&**p),
@@ -282,9 +277,9 @@ crate fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
282277
}
283278
PatKind::Range(..) => return kw::Underscore,
284279
PatKind::Slice(ref begin, ref mid, ref end) => {
285-
let begin = begin.iter().map(|p| name_from_pat(&**p).to_string());
280+
let begin = begin.iter().map(|p| name_from_pat(p).to_string());
286281
let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
287-
let end = end.iter().map(|p| name_from_pat(&**p).to_string());
282+
let end = end.iter().map(|p| name_from_pat(p).to_string());
288283
format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
289284
}
290285
})

Diff for: src/tools/clippy/clippy_lints/src/manual_unwrap_or.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
6161
if let Some((idx, or_arm)) = arms.iter().enumerate().find(|(_, arm)| {
6262
match arm.pat.kind {
6363
PatKind::Path(ref qpath) => is_lang_ctor(cx, qpath, OptionNone),
64-
PatKind::TupleStruct(ref qpath, &[pat], _) =>
64+
PatKind::TupleStruct(ref qpath, [pat], _) =>
6565
matches!(pat.kind, PatKind::Wild) && is_lang_ctor(cx, qpath, ResultErr),
6666
_ => false,
6767
}
6868
});
6969
let unwrap_arm = &arms[1 - idx];
70-
if let PatKind::TupleStruct(ref qpath, &[unwrap_pat], _) = unwrap_arm.pat.kind;
70+
if let PatKind::TupleStruct(ref qpath, [unwrap_pat], _) = unwrap_arm.pat.kind;
7171
if is_lang_ctor(cx, qpath, OptionSome) || is_lang_ctor(cx, qpath, ResultOk);
7272
if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
7373
if path_to_local_id(unwrap_arm.body, binding_hir_id);

Diff for: src/tools/clippy/clippy_lints/src/matches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
625625
if let PatKind::TupleStruct(
626626
QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind;
627627
if args.len() == 1;
628-
if let PatKind::Binding(_, arg, ..) = strip_pat_refs(args[0]).kind;
628+
if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind;
629629
let body = remove_blocks(arms[0].body);
630630
if path_to_local_id(body, arg);
631631

0 commit comments

Comments
 (0)