Skip to content

Commit ec5cd74

Browse files
committed
Remove GenericArg::span.
1 parent ec56b9f commit ec5cd74

File tree

7 files changed

+39
-38
lines changed

7 files changed

+39
-38
lines changed

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
266266
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
267267
let first_generic_span = generic_args
268268
.args
269-
.iter()
270-
.map(|a| a.span())
271-
.chain(generic_args.bindings.iter().map(|b| b.span))
272-
.next();
269+
.first()
270+
.map(|a| self.spans[a.id()])
271+
.or_else(|| generic_args.bindings.first().map(|b| b.span));
273272
if !generic_args.parenthesized && !has_lifetimes {
274273
generic_args.args = self
275274
.elided_path_lifetimes(

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,6 @@ pub enum GenericArg<'hir> {
261261
}
262262

263263
impl GenericArg<'_> {
264-
pub fn span(&self) -> Span {
265-
match self {
266-
GenericArg::Lifetime(l) => l.span,
267-
GenericArg::Type(t) => t.span,
268-
GenericArg::Const(c) => c.span,
269-
}
270-
}
271-
272264
pub fn id(&self) -> HirId {
273265
match self {
274266
GenericArg::Lifetime(l) => l.hir_id,
@@ -355,17 +347,17 @@ impl GenericArgs<'_> {
355347
own_counts
356348
}
357349

358-
pub fn span(&self) -> Option<Span> {
350+
pub fn span(&self, get_span: impl Fn(HirId) -> Span) -> Option<Span> {
359351
self.args
360352
.iter()
361353
.filter(|arg| !arg.is_synthetic())
362-
.map(|arg| arg.span())
354+
.map(|arg| get_span(arg.id()))
363355
.reduce(|span1, span2| span1.to(span2))
364356
}
365357

366358
/// Returns span encompassing arguments and their surrounding `<>` or `()`
367-
pub fn span_ext(&self, sm: &SourceMap) -> Option<Span> {
368-
let mut span = self.span()?;
359+
pub fn span_ext(&self, sm: &SourceMap, get_span: impl Fn(HirId) -> Span) -> Option<Span> {
360+
let mut span = self.span(get_span)?;
369361

370362
let (o, c) = if self.parenthesized { ('(', ')') } else { ('<', '>') };
371363

compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
637637
// HIR lowering sometimes doesn't catch this in erroneous
638638
// programs, so we need to use delay_span_bug here. See #82126.
639639
self.infcx.tcx.sess.delay_span_bug(
640-
hir_arg.span(),
640+
self.infcx.tcx.hir().span(hir_arg.id()),
641641
&format!("unmatched subst and hir arg: found {:?} vs {:?}", kind, hir_arg),
642642
);
643643
}

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2929
help: Option<&str>,
3030
) {
3131
let sess = tcx.sess;
32+
let arg_span = tcx.hir().span(arg.id());
3233
let mut err = struct_span_err!(
3334
sess,
34-
arg.span(),
35+
arg_span,
3536
E0747,
3637
"{} provided when a {} was expected",
3738
arg.descr(),
@@ -46,8 +47,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
4647

4748
let add_braces_suggestion = |arg: &GenericArg<'_>, err: &mut DiagnosticBuilder<'_>| {
4849
let suggestions = vec![
49-
(arg.span().shrink_to_lo(), String::from("{ ")),
50-
(arg.span().shrink_to_hi(), String::from(" }")),
50+
(tcx.hir().span(arg.id()).shrink_to_lo(), String::from("{ ")),
51+
(tcx.hir().span(arg.id()).shrink_to_hi(), String::from(" }")),
5152
];
5253
err.multipart_suggestion(
5354
"if this generic argument was intended as a const parameter, \
@@ -102,7 +103,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
102103
let snippet = sess.source_map().span_to_snippet(tcx.hir().span(len.hir_id));
103104
if let Ok(snippet) = snippet {
104105
err.span_suggestion(
105-
arg.span(),
106+
arg_span,
106107
"array type provided where a `usize` was expected, try",
107108
format!("{{ {} }}", snippet),
108109
Applicability::MaybeIncorrect,
@@ -453,7 +454,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
453454
invalid_args.extend(
454455
gen_args.args[args_offset + expected_max..args_offset + provided]
455456
.iter()
456-
.map(|arg| arg.span()),
457+
.map(|arg| tcx.hir().span(arg.id())),
457458
);
458459
};
459460

@@ -548,7 +549,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
548549
.args
549550
.iter()
550551
.filter_map(|arg| match arg {
551-
GenericArg::Type(_) | GenericArg::Const(_) => Some(arg.span()),
552+
GenericArg::Type(_) | GenericArg::Const(_) => Some(tcx.hir().span(arg.id())),
552553
_ => None,
553554
})
554555
.collect::<Vec<_>>();
@@ -596,7 +597,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
596597
let msg = "cannot specify lifetime arguments explicitly \
597598
if late bound lifetime parameters are present";
598599
let note = "the late bound lifetime parameter is introduced here";
599-
let span = args.args[0].span();
600+
let span = tcx.hir().span(args.args[0].id());
600601

601602
if position == GenericArgPosition::Value
602603
&& arg_counts.lifetimes != param_counts.lifetimes

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
421421
}
422422
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => {
423423
if has_default {
424+
let arg_id = arg.id();
424425
tcx.check_optional_stability(
425426
param.def_id,
426-
Some(arg.id()),
427-
arg.span(),
427+
Some(arg_id),
428+
tcx.hir().span(arg_id),
428429
|_, _| {
429430
// Default generic parameters may not be marked
430431
// with stability attributes, i.e. when the

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
129129
.and_then(|args| args.args.iter().last())
130130
// Account for `foo.bar::<T>()`.
131131
.map(|arg| {
132+
let arg_span = tcx.hir().span(arg.id());
132133
// Skip the closing `>`.
133134
tcx.sess
134135
.source_map()
135-
.next_point(tcx.sess.source_map().next_point(arg.span()))
136+
.next_point(tcx.sess.source_map().next_point(arg_span))
136137
})
137138
.unwrap_or(*span),
138139
&args[1..], // Skip the receiver.

compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx> WrongNumberOfGenericArgs<'_, 'tcx> {
6565
let def_kind = self.tcx.def_kind(self.def_id).descr(self.def_id);
6666
let (quantifier, bound) = self.quantifier_and_bound();
6767

68-
if self.gen_args.span().is_some() {
68+
if self.gen_args.span(|id| self.tcx.hir().span(id)).is_some() {
6969
format!(
7070
"this {} takes {}{} {} argument{} but {}{} {} argument{} {} supplied",
7171
def_kind,
@@ -122,7 +122,7 @@ impl<'tcx> WrongNumberOfGenericArgs<'_, 'tcx> {
122122

123123
for (i, arg) in args {
124124
err.span_label(
125-
arg.span(),
125+
self.tcx.hir().span(arg.id()),
126126
if i + 1 == self.provided {
127127
format!(
128128
"supplied {} {} argument{}",
@@ -139,7 +139,7 @@ impl<'tcx> WrongNumberOfGenericArgs<'_, 'tcx> {
139139

140140
fn suggest(&self, err: &mut DiagnosticBuilder<'_>) {
141141
if self.provided == 0 {
142-
if self.gen_args.span().is_some() {
142+
if self.gen_args.span(|id| self.tcx.hir().span(id)).is_some() {
143143
self.suggest_adding_args(err);
144144
} else {
145145
self.suggest_creating_generics(err);
@@ -206,11 +206,14 @@ impl<'tcx> WrongNumberOfGenericArgs<'_, 'tcx> {
206206
let missing_arg_count = self.expected_min - self.provided;
207207

208208
let (span, sugg_prefix) = if self.args_offset + self.provided == 0 {
209-
let span = self.gen_args.args[0].span().shrink_to_lo();
209+
let span = self.tcx.hir().span(self.gen_args.args[0].id()).shrink_to_lo();
210210
(span, "")
211211
} else {
212-
let span =
213-
self.gen_args.args[self.args_offset + self.provided - 1].span().shrink_to_hi();
212+
let span = self
213+
.tcx
214+
.hir()
215+
.span(self.gen_args.args[self.args_offset + self.provided - 1].id())
216+
.shrink_to_hi();
214217
(span, ", ")
215218
};
216219

@@ -249,7 +252,7 @@ impl<'tcx> WrongNumberOfGenericArgs<'_, 'tcx> {
249252
.path_segment
250253
.args
251254
.unwrap()
252-
.span_ext(sm)
255+
.span_ext(sm, |id| self.tcx.hir().span(id))
253256
.unwrap()
254257
.with_lo(self.path_segment.ident.span.hi());
255258

@@ -293,19 +296,23 @@ impl<'tcx> WrongNumberOfGenericArgs<'_, 'tcx> {
293296
let last_argument_ends_generics = to_idx + 1 == self.gen_args.args.len();
294297

295298
if !first_argument_starts_generics && last_argument_ends_generics {
296-
(self.gen_args.args[from_idx - 1].span().hi(), true)
299+
(self.tcx.hir().span(self.gen_args.args[from_idx - 1].id()).hi(), true)
297300
} else {
298-
(self.gen_args.args[from_idx].span().lo(), false)
301+
(self.tcx.hir().span(self.gen_args.args[from_idx].id()).lo(), false)
299302
}
300303
};
301304

302305
let to = {
303-
let hi = self.gen_args.args[to_idx].span().hi();
306+
let hi = self.tcx.hir().span(self.gen_args.args[to_idx].id()).hi();
304307

305308
if comma_eaten {
306309
hi
307310
} else {
308-
self.gen_args.args.get(to_idx + 1).map(|arg| arg.span().lo()).unwrap_or(hi)
311+
self.gen_args
312+
.args
313+
.get(to_idx + 1)
314+
.map(|arg| self.tcx.hir().span(arg.id()).lo())
315+
.unwrap_or(hi)
309316
}
310317
};
311318

0 commit comments

Comments
 (0)