Skip to content

Commit 82c472a

Browse files
committed
add span for struct pattern rest (..) in hir
1 parent aae0707 commit 82c472a

File tree

12 files changed

+33
-23
lines changed

12 files changed

+33
-23
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,10 +1434,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
14341434
self.dcx().emit_err(FunctionalRecordUpdateDestructuringAssignment {
14351435
span: e.span,
14361436
});
1437-
true
1437+
Some(e.span)
14381438
}
1439-
StructRest::Rest(_) => true,
1440-
StructRest::None => false,
1439+
StructRest::Rest(span) => Some(*span),
1440+
StructRest::None => None,
14411441
};
14421442
let struct_pat = hir::PatKind::Struct(qpath, field_pats, fields_omitted);
14431443
return self.pat_without_dbm(lhs.span, struct_pat);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2508,7 +2508,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25082508
fields: &'hir [hir::PatField<'hir>],
25092509
) -> &'hir hir::Pat<'hir> {
25102510
let qpath = hir::QPath::LangItem(lang_item, self.lower_span(span));
2511-
self.pat(span, hir::PatKind::Struct(qpath, fields, false))
2511+
self.pat(span, hir::PatKind::Struct(qpath, fields, None))
25122512
}
25132513

25142514
fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, HirId) {

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
106106
break hir::PatKind::Struct(
107107
qpath,
108108
fs,
109-
matches!(
110-
etc,
111-
ast::PatFieldsRest::Rest(_) | ast::PatFieldsRest::Recovered(_)
112-
),
109+
match etc {
110+
ast::PatFieldsRest::Rest(sp) => Some(*sp),
111+
ast::PatFieldsRest::Recovered(_) => Some(Span::default()),
112+
_ => None,
113+
},
113114
);
114115
}
115116
PatKind::Tuple(pats) => {

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,8 +1884,8 @@ pub enum PatKind<'hir> {
18841884
Binding(BindingMode, HirId, Ident, Option<&'hir Pat<'hir>>),
18851885

18861886
/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
1887-
/// The `bool` is `true` in the presence of a `..`.
1888-
Struct(QPath<'hir>, &'hir [PatField<'hir>], bool),
1887+
/// The `Option` contains the span a possible `..`.
1888+
Struct(QPath<'hir>, &'hir [PatField<'hir>], Option<Span>),
18891889

18901890
/// A tuple struct/variant pattern `Variant(x, y, .., z)`.
18911891
/// If the `..` pattern fragment is present, then `DotDotPos` denotes its position.
@@ -4979,8 +4979,8 @@ mod size_asserts {
49794979
static_assert_size!(ItemKind<'_>, 64);
49804980
static_assert_size!(LetStmt<'_>, 72);
49814981
static_assert_size!(Param<'_>, 32);
4982-
static_assert_size!(Pat<'_>, 72);
4983-
static_assert_size!(PatKind<'_>, 48);
4982+
static_assert_size!(Pat<'_>, 80);
4983+
static_assert_size!(PatKind<'_>, 56);
49844984
static_assert_size!(Path<'_>, 40);
49854985
static_assert_size!(PathSegment<'_>, 48);
49864986
static_assert_size!(QPath<'_>, 24);

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,12 +1958,12 @@ impl<'a> State<'a> {
19581958
self.print_qpath(qpath, true);
19591959
self.nbsp();
19601960
self.word("{");
1961-
let empty = fields.is_empty() && !etc;
1961+
let empty = fields.is_empty() && etc.is_none();
19621962
if !empty {
19631963
self.space();
19641964
}
19651965
self.commasep_cmnt(Consistent, fields, |s, f| s.print_patfield(f), |f| f.pat.span);
1966-
if etc {
1966+
if etc.is_some() {
19671967
if !fields.is_empty() {
19681968
self.word_space(",");
19691969
}

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
605605
},
606606
PatKind::Struct(_, fields, has_rest_pat) => match opt_path_res.unwrap() {
607607
Ok(ResolvedPat { ty, kind: ResolvedPatKind::Struct { variant } }) => self
608-
.check_pat_struct(pat, fields, has_rest_pat, ty, variant, expected, pat_info),
608+
.check_pat_struct(
609+
pat,
610+
fields,
611+
has_rest_pat.is_some(),
612+
ty,
613+
variant,
614+
expected,
615+
pat_info,
616+
),
609617
Err(guar) => {
610618
let ty_err = Ty::new_error(self.tcx, guar);
611619
for field in fields {
@@ -2428,7 +2436,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24282436
let len = unmentioned_fields.len();
24292437
let (prefix, postfix, sp) = match fields {
24302438
[] => match &pat.kind {
2431-
PatKind::Struct(path, [], false) => {
2439+
PatKind::Struct(path, [], None) => {
24322440
(" { ", " }", path.span().shrink_to_hi().until(pat.span.shrink_to_hi()))
24332441
}
24342442
_ => return err,

compiler/rustc_passes/src/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ impl<'tcx> Liveness<'_, 'tcx> {
15831583
});
15841584

15851585
let can_remove = match pat.kind {
1586-
hir::PatKind::Struct(_, fields, true) => {
1586+
hir::PatKind::Struct(_, fields, Some(_)) => {
15871587
// if all fields are shorthand, remove the struct field, otherwise, mark with _ as prefix
15881588
fields.iter().all(|f| f.is_shorthand)
15891589
}

src/tools/clippy/clippy_lints/src/equatable_if_let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn unary_pattern(pat: &Pat<'_>) -> bool {
5353
| PatKind::Never
5454
| PatKind::Or(_)
5555
| PatKind::Err(_) => false,
56-
PatKind::Struct(_, a, etc) => !etc && a.iter().all(|x| unary_pattern(x.pat)),
56+
PatKind::Struct(_, a, etc) => etc.is_none() && a.iter().all(|x| unary_pattern(x.pat)),
5757
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => etc.as_opt_usize().is_none() && array_rec(a),
5858
PatKind::Ref(x, _) | PatKind::Box(x) | PatKind::Deref(x) | PatKind::Guard(x, _) => unary_pattern(x),
5959
PatKind::Expr(_) => true,

src/tools/clippy/clippy_lints/src/manual_let_else.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fn replace_in_pattern(
287287
}
288288
return or_pat;
289289
},
290-
PatKind::Struct(path, fields, has_dot_dot) => {
290+
PatKind::Struct(path, fields, dot_dot) => {
291291
let fields = fields
292292
.iter()
293293
.map(|fld| {
@@ -311,7 +311,7 @@ fn replace_in_pattern(
311311
.collect::<Vec<_>>();
312312
let fields_string = fields.join(", ");
313313

314-
let dot_dot_str = if has_dot_dot { " .." } else { "" };
314+
let dot_dot_str = if dot_dot.is_some() { " .." } else { "" };
315315
let (sn_pth, _) = snippet_with_context(cx, path.span(), span.ctxt(), "", app);
316316
return format!("{sn_pth} {{ {fields_string}{dot_dot_str} }}");
317317
},

src/tools/clippy/clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::REST_PAT_IN_FULLY_BOUND_STRUCTS;
77

88
pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) {
99
if !pat.span.from_expansion()
10-
&& let PatKind::Struct(QPath::Resolved(_, path), fields, true) = pat.kind
10+
&& let PatKind::Struct(QPath::Resolved(_, path), fields, Some(_)) = pat.kind
1111
&& let Some(def_id) = path.res.opt_def_id()
1212
&& let ty = cx.tcx.type_of(def_id).instantiate_identity()
1313
&& let ty::Adt(def, _) = ty.kind()

0 commit comments

Comments
 (0)