Skip to content

Commit 732ae69

Browse files
committed
Remove span from hir::Pat.
1 parent afc6e69 commit 732ae69

File tree

40 files changed

+285
-228
lines changed

40 files changed

+285
-228
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11141114
// parameters (c.f. rust-lang/rust#64512).
11151115
for (index, parameter) in decl.inputs.iter().enumerate() {
11161116
let parameter = this.lower_param(parameter);
1117-
let span = parameter.pat.span;
1117+
let span = this.spans[parameter.pat.hir_id];
11181118

11191119
// Check if this is a binding pattern, if so, we can optimize and avoid adding a
11201120
// `let <pat> = __argN;` statement. In this case, we do not rename the parameter.

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,7 +2583,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25832583
self.arena.alloc(hir::Pat {
25842584
hir_id,
25852585
kind: hir::PatKind::Binding(bm, hir_id, ident.with_span_pos(span), None),
2586-
span,
25872586
default_binding_modes: true,
25882587
}),
25892588
hir_id,
@@ -2595,19 +2594,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25952594
}
25962595

25972596
fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
2598-
self.arena.alloc(hir::Pat {
2599-
hir_id: self.next_id(span),
2600-
kind,
2601-
span,
2602-
default_binding_modes: true,
2603-
})
2597+
self.arena.alloc(hir::Pat { hir_id: self.next_id(span), kind, default_binding_modes: true })
26042598
}
26052599

26062600
fn pat_without_dbm(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
26072601
self.arena.alloc(hir::Pat {
26082602
hir_id: self.next_id(span),
26092603
kind,
2610-
span,
26112604
default_binding_modes: false,
26122605
})
26132606
}

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
275275
self.arena.alloc(hir::Pat {
276276
hir_id: self.lower_node_id(p.id, p.span),
277277
kind,
278-
span: p.span,
279278
default_binding_modes: true,
280279
})
281280
}

compiler/rustc_hir/src/hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,6 @@ pub struct Pat<'hir> {
757757
#[stable_hasher(ignore)]
758758
pub hir_id: HirId,
759759
pub kind: PatKind<'hir>,
760-
pub span: Span,
761760
// Whether to use default binding modes.
762761
// At present, this is false only for destructuring assignment.
763762
pub default_binding_modes: bool,

compiler/rustc_hir/src/pat_util.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::def::{CtorOf, DefKind, Res};
22
use crate::def_id::DefId;
33
use crate::hir::{self, HirId, PatKind};
44
use rustc_span::symbol::Ident;
5-
use rustc_span::Span;
65

76
use std::iter::{Enumerate, ExactSizeIterator};
87

@@ -60,10 +59,10 @@ impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
6059
impl hir::Pat<'_> {
6160
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
6261
/// `match foo() { Some(a) => (), None => () }`
63-
pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Span, Ident)) {
62+
pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Ident)) {
6463
self.walk_always(|p| {
6564
if let PatKind::Binding(binding_mode, _, ident, _) = p.kind {
66-
f(binding_mode, p.hir_id, p.span, ident);
65+
f(binding_mode, p.hir_id, ident);
6766
}
6867
});
6968
}
@@ -72,17 +71,14 @@ impl hir::Pat<'_> {
7271
/// `match foo() { Some(a) => (), None => () }`.
7372
///
7473
/// When encountering an or-pattern `p_0 | ... | p_n` only `p_0` will be visited.
75-
pub fn each_binding_or_first(
76-
&self,
77-
f: &mut impl FnMut(hir::BindingAnnotation, HirId, Span, Ident),
78-
) {
74+
pub fn each_binding_or_first(&self, f: &mut impl FnMut(hir::BindingAnnotation, HirId, Ident)) {
7975
self.walk(|p| match &p.kind {
8076
PatKind::Or(ps) => {
8177
ps[0].each_binding_or_first(f);
8278
false
8379
}
8480
PatKind::Binding(bm, _, ident, _) => {
85-
f(*bm, p.hir_id, p.span, *ident);
81+
f(*bm, p.hir_id, *ident);
8682
true
8783
}
8884
_ => true,
@@ -150,7 +146,7 @@ impl hir::Pat<'_> {
150146
// ref bindings are be implicit after #42640 (default match binding modes). See issue #44848.
151147
pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
152148
let mut result = None;
153-
self.each_binding(|annotation, _, _, _| match annotation {
149+
self.each_binding(|annotation, _, _| match annotation {
154150
hir::BindingAnnotation::Ref => match result {
155151
None | Some(hir::Mutability::Not) => result = Some(hir::Mutability::Not),
156152
_ => {}

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,29 @@ impl<'a> State<'a> {
335335
pub fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
336336
where
337337
F: FnMut(&mut State<'_>, &T),
338-
G: FnMut(&T) -> rustc_span::Span,
338+
G: FnMut(&State<'_>, &T) -> rustc_span::Span,
339339
{
340340
self.rbox(0, b);
341341
let len = elts.len();
342342
let mut i = 0;
343343
for elt in elts {
344-
self.maybe_print_comment(get_span(elt).hi());
344+
self.maybe_print_comment(get_span(self, elt).hi());
345345
op(self, elt);
346346
i += 1;
347347
if i < len {
348348
self.s.word(",");
349-
self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
349+
self.maybe_print_trailing_comment(
350+
get_span(self, elt),
351+
Some(get_span(self, &elts[i]).hi()),
352+
);
350353
self.space_if_not_bol();
351354
}
352355
}
353356
self.end();
354357
}
355358

356359
pub fn commasep_exprs(&mut self, b: Breaks, exprs: &[hir::Expr<'_>]) {
357-
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |e| e.span)
360+
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |_, e| e.span)
358361
}
359362

360363
pub fn print_mod(&mut self, _mod: &hir::Mod<'_>, attrs: &[ast::Attribute]) {
@@ -1186,7 +1189,7 @@ impl<'a> State<'a> {
11861189
s.print_expr(&field.expr);
11871190
s.end()
11881191
},
1189-
|f| f.span,
1192+
|_, f| f.span,
11901193
);
11911194
match *wth {
11921195
Some(ref expr) => {
@@ -1836,7 +1839,8 @@ impl<'a> State<'a> {
18361839
}
18371840

18381841
pub fn print_pat(&mut self, pat: &hir::Pat<'_>) {
1839-
self.maybe_print_comment(pat.span.lo());
1842+
let span = self.span(pat.hir_id);
1843+
self.maybe_print_comment(span.lo());
18401844
self.ann.pre(self, AnnNode::Pat(pat));
18411845
// Pat isn't normalized, but the beauty of it
18421846
// is that it doesn't matter
@@ -1900,7 +1904,7 @@ impl<'a> State<'a> {
19001904
s.print_pat(&f.pat);
19011905
s.end()
19021906
},
1903-
|f| f.pat.span,
1907+
|s, f| s.span(f.pat.hir_id),
19041908
);
19051909
if etc {
19061910
if !fields.is_empty() {

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
370370
local_visitor.visit_expr(expr);
371371
}
372372
let err_span = if let Some(pattern) = local_visitor.found_arg_pattern {
373-
pattern.span
373+
self.tcx.hir().span(pattern.hir_id)
374374
} else if let Some(span) = arg_data.span {
375375
// `span` here lets us point at `sum` instead of the entire right hand side expr:
376376
// error[E0282]: type annotations needed
@@ -537,12 +537,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
537537
// with the type parameter `_` specified
538538
// ```
539539
err.span_label(
540-
pattern.span,
540+
self.tcx.hir().span(pattern.hir_id),
541541
format!("consider giving this closure parameter {}", suffix),
542542
);
543543
} else if let Some(pattern) = local_visitor.found_local_pattern {
544+
let pattern_span = self.tcx.hir().span(pattern.hir_id);
544545
let msg = if let Some(simple_ident) = pattern.simple_ident() {
545-
match pattern.span.desugaring_kind() {
546+
match pattern_span.desugaring_kind() {
546547
None => format!("consider giving `{}` {}", simple_ident, suffix),
547548
Some(DesugaringKind::ForLoop(_)) => {
548549
"the element type for this iterator is not specified".to_string()
@@ -552,7 +553,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
552553
} else {
553554
format!("consider giving this pattern {}", suffix)
554555
};
555-
err.span_label(pattern.span, msg);
556+
err.span_label(pattern_span, msg);
556557
} else if let Some(e) = local_visitor.found_method_call {
557558
if let ExprKind::MethodCall(segment, ..) = &e.kind {
558559
// Suggest specifying type params or point out the return type of the call:

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
798798
argument_scope: region::Scope,
799799
ast_body: &'tcx hir::Expr<'tcx>,
800800
) -> BlockAnd<()> {
801+
let tcx = self.hir.tcx();
802+
801803
// Allocate locals for the function arguments
802804
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
803805
let source_info =
804-
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
806+
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| tcx.hir().span(arg.pat.hir_id)));
805807
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));
806808

807809
// If this is a simple binding pattern, give debuginfo a nice name.
@@ -859,8 +861,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
859861
if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
860862
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
861863
name = ident.name;
864+
let pat_span = tcx_hir.span(pat.hir_id);
862865
match hir_typeck_results
863-
.extract_binding_mode(tcx.sess, pat.hir_id, pat.span)
866+
.extract_binding_mode(tcx.sess, pat.hir_id, pat_span)
864867
{
865868
Some(ty::BindByValue(hir::Mutability::Mut)) => {
866869
mutability = Mutability::Mut;
@@ -904,7 +907,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
904907

905908
// Make sure we drop (parts of) the argument even when not matched on.
906909
self.schedule_drop(
907-
arg_opt.as_ref().map_or(ast_body.span, |arg| arg.pat.span),
910+
arg_opt.as_ref().map_or(ast_body.span, |arg| tcx_hir.span(arg.pat.hir_id)),
908911
argument_scope,
909912
local,
910913
DropKind::Value,

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
141141
let pattern: &_ = cx.pattern_arena.alloc(expand_pattern(pattern));
142142
if !patcx.errors.is_empty() {
143143
*have_errors = true;
144-
patcx.report_inlining_errors(pat.span);
144+
let pat_span = self.tcx.hir().span(pat.hir_id);
145+
patcx.report_inlining_errors(pat_span);
145146
}
146147
(pattern, pattern_ty)
147148
}
@@ -226,9 +227,10 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
226227
}
227228

228229
let joined_patterns = joined_uncovered_patterns(&witnesses);
230+
let pat_span = self.tcx.hir().span(pat.hir_id);
229231
let mut err = struct_span_err!(
230232
self.tcx.sess,
231-
pat.span,
233+
pat_span,
232234
E0005,
233235
"refutable pattern in {}: {} not covered",
234236
origin,
@@ -242,7 +244,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
242244
false
243245
}
244246
_ => {
245-
err.span_label(pat.span, pattern_not_covered_label(&witnesses, &joined_patterns));
247+
err.span_label(pat_span, pattern_not_covered_label(&witnesses, &joined_patterns));
246248
true
247249
}
248250
};
@@ -281,13 +283,14 @@ fn const_not_var(
281283
path: &hir::Path<'_>,
282284
) {
283285
let descr = path.res.descr();
286+
let pat_span = tcx.hir().span(pat.hir_id);
284287
err.span_label(
285-
pat.span,
288+
pat_span,
286289
format!("interpreted as {} {} pattern, not a new variable", path.res.article(), descr,),
287290
);
288291

289292
err.span_suggestion(
290-
pat.span,
293+
pat_span,
291294
"introduce a variable instead",
292295
format!("{}_var", path.segments[0].ident).to_lowercase(),
293296
// Cannot use `MachineApplicable` as it's not really *always* correct
@@ -304,8 +307,9 @@ fn const_not_var(
304307
fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_>) {
305308
pat.walk_always(|p| {
306309
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
310+
let span = cx.tcx.hir().span(p.hir_id);
307311
if let Some(ty::BindByValue(hir::Mutability::Not)) =
308-
cx.typeck_results.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
312+
cx.typeck_results.extract_binding_mode(cx.tcx.sess, p.hir_id, span)
309313
{
310314
let pat_ty = cx.typeck_results.pat_ty(p).peel_refs();
311315
if let ty::Adt(edef, _) = pat_ty.kind() {
@@ -317,7 +321,7 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
317321
cx.tcx.struct_span_lint_hir(
318322
BINDINGS_WITH_VARIANT_NAME,
319323
p.hir_id,
320-
p.span,
324+
span,
321325
|lint| {
322326
let ty_path = cx.tcx.def_path_str(edef.did);
323327
lint.build(&format!(
@@ -327,7 +331,7 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
327331
))
328332
.code(error_code!(E0170))
329333
.span_suggestion(
330-
p.span,
334+
span,
331335
"to match on the variant, qualify the path",
332336
format!("{}::{}", ty_path, ident),
333337
Applicability::MachineApplicable,
@@ -616,17 +620,19 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
616620
hir::PatKind::Binding(.., name, Some(sub)) => (*name, sub),
617621
_ => return,
618622
};
619-
let binding_span = pat.span.with_hi(name.span.hi());
623+
let pat_span = cx.tcx.hir().span(pat.hir_id);
624+
let binding_span = pat_span.with_hi(name.span.hi());
620625

621626
let typeck_results = cx.typeck_results;
622627
let sess = cx.tcx.sess;
623628

624629
// Get the binding move, extract the mutability if by-ref.
625-
let mut_outer = match typeck_results.extract_binding_mode(sess, pat.hir_id, pat.span) {
626-
Some(ty::BindByValue(_)) if is_binding_by_move(cx, pat.hir_id, pat.span) => {
630+
let mut_outer = match typeck_results.extract_binding_mode(sess, pat.hir_id, pat_span) {
631+
Some(ty::BindByValue(_)) if is_binding_by_move(cx, pat.hir_id, pat_span) => {
627632
// We have `x @ pat` where `x` is by-move. Reject all borrows in `pat`.
628633
let mut conflicts_ref = Vec::new();
629-
sub.each_binding(|_, hir_id, span, _| {
634+
sub.each_binding(|_, hir_id, _| {
635+
let span = cx.tcx.hir().span(hir_id);
630636
match typeck_results.extract_binding_mode(sess, hir_id, span) {
631637
Some(ty::BindByValue(_)) | None => {}
632638
Some(ty::BindByReference(_)) => conflicts_ref.push(span),
@@ -638,7 +644,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
638644
name,
639645
typeck_results.node_type(pat.hir_id),
640646
);
641-
sess.struct_span_err(pat.span, "borrow of moved value")
647+
sess.struct_span_err(pat_span, "borrow of moved value")
642648
.span_label(binding_span, format!("value moved into `{}` here", name))
643649
.span_label(binding_span, occurs_because)
644650
.span_labels(conflicts_ref, "value borrowed here after move")
@@ -655,7 +661,8 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
655661
let mut conflicts_move = Vec::new();
656662
let mut conflicts_mut_mut = Vec::new();
657663
let mut conflicts_mut_ref = Vec::new();
658-
sub.each_binding(|_, hir_id, span, name| {
664+
sub.each_binding(|_, hir_id, name| {
665+
let span = cx.tcx.hir().span(hir_id);
659666
match typeck_results.extract_binding_mode(sess, hir_id, span) {
660667
Some(ty::BindByReference(mut_inner)) => match (mut_outer, mut_inner) {
661668
(Mutability::Not, Mutability::Not) => {} // Both sides are `ref`.
@@ -673,7 +680,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
673680
if !conflicts_mut_mut.is_empty() {
674681
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
675682
let mut err = sess
676-
.struct_span_err(pat.span, "cannot borrow value as mutable more than once at a time");
683+
.struct_span_err(pat_span, "cannot borrow value as mutable more than once at a time");
677684
err.span_label(binding_span, format!("first mutable borrow, by `{}`, occurs here", name));
678685
for (span, name) in conflicts_mut_mut {
679686
err.span_label(span, format!("another mutable borrow, by `{}`, occurs here", name));
@@ -693,7 +700,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
693700
};
694701
let msg =
695702
format!("cannot borrow value as {} because it is also borrowed as {}", also, primary);
696-
let mut err = sess.struct_span_err(pat.span, &msg);
703+
let mut err = sess.struct_span_err(pat_span, &msg);
697704
err.span_label(binding_span, format!("{} borrow, by `{}`, occurs here", primary, name));
698705
for (span, name) in conflicts_mut_ref {
699706
err.span_label(span, format!("{} borrow, by `{}`, occurs here", also, name));
@@ -705,7 +712,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
705712
} else if !conflicts_move.is_empty() {
706713
// Report by-ref and by-move conflicts, e.g. `ref x @ y`.
707714
let mut err =
708-
sess.struct_span_err(pat.span, "cannot move out of value because it is borrowed");
715+
sess.struct_span_err(pat_span, "cannot move out of value because it is borrowed");
709716
err.span_label(binding_span, format!("value borrowed, by `{}`, here", name));
710717
for (span, name) in conflicts_move {
711718
err.span_label(span, format!("value moved into `{}` here", name));
@@ -735,10 +742,11 @@ fn check_legality_of_bindings_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pa
735742
match pat.kind {
736743
hir::PatKind::Binding(.., ref subpat) => {
737744
if !self.bindings_allowed {
745+
let pat_span = self.cx.tcx.hir().span(pat.hir_id);
738746
feature_err(
739747
&self.cx.tcx.sess.parse_sess,
740748
sym::bindings_after_at,
741-
pat.span,
749+
pat_span,
742750
"pattern bindings after an `@` are unstable",
743751
)
744752
.emit();

0 commit comments

Comments
 (0)