Skip to content

Commit bab4731

Browse files
authored
Rollup merge of rust-lang#73597 - ayazhafiz:i/const-span, r=ecstatic-morse
Record span of `const` kw in GenericParamKind Context: this is needed for a fix of rust-lang/rustfmt#4263, which currently records the span of a const generic param incorrectly because the location of the `const` kw is not known. I am not sure how to add tests for this; any guidance in how to do so would be appreciated 🙂
2 parents 3fc8f8e + 86f6c0e commit bab4731

File tree

9 files changed

+16
-14
lines changed

9 files changed

+16
-14
lines changed

src/librustc_ast/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ pub enum GenericParamKind {
335335
},
336336
Const {
337337
ty: P<Ty>,
338+
/// Span of the `const` keyword.
339+
kw_span: Span,
338340
},
339341
}
340342

src/librustc_ast/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
762762
GenericParamKind::Type { default } => {
763763
visit_opt(default, |default| vis.visit_ty(default));
764764
}
765-
GenericParamKind::Const { ty } => {
765+
GenericParamKind::Const { ty, kw_span: _ } => {
766766
vis.visit_ty(ty);
767767
}
768768
}

src/librustc_ast_lowering/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22302230

22312231
(hir::ParamName::Plain(param.ident), kind)
22322232
}
2233-
GenericParamKind::Const { ref ty } => {
2233+
GenericParamKind::Const { ref ty, kw_span: _ } => {
22342234
let ty = self
22352235
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
22362236
this.lower_ty(&ty, ImplTraitContext::disallowed())

src/librustc_ast_passes/ast_validation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1135,9 +1135,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11351135
generics.params.iter().map(|param| {
11361136
let ident = Some(param.ident.to_string());
11371137
let (kind, ident) = match &param.kind {
1138-
GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
1139-
GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
1140-
GenericParamKind::Const { ref ty } => {
1138+
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident),
1139+
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
1140+
GenericParamKind::Const { ref ty, kw_span: _ } => {
11411141
let ty = pprust::ty_to_string(ty);
11421142
(ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
11431143
}

src/librustc_ast_pretty/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2578,7 +2578,7 @@ impl<'a> State<'a> {
25782578
s.print_type(default)
25792579
}
25802580
}
2581-
ast::GenericParamKind::Const { ref ty } => {
2581+
ast::GenericParamKind::Const { ref ty, kw_span: _ } => {
25822582
s.word_space("const");
25832583
s.print_ident(param.ident);
25842584
s.s.space();

src/librustc_builtin_macros/deriving/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn inject_impl_of_structural_trait(
123123
*default = None;
124124
ast::GenericArg::Type(cx.ty_ident(span, param.ident))
125125
}
126-
ast::GenericParamKind::Const { ty: _ } => {
126+
ast::GenericParamKind::Const { ty: _, kw_span: _ } => {
127127
ast::GenericArg::Const(cx.const_ident(span, param.ident))
128128
}
129129
})

src/librustc_parse/parser/generics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ impl<'a> Parser<'a> {
4747
}
4848

4949
fn parse_const_param(&mut self, preceding_attrs: Vec<Attribute>) -> PResult<'a, GenericParam> {
50-
let lo = self.token.span;
50+
let const_span = self.token.span;
5151

5252
self.expect_keyword(kw::Const)?;
5353
let ident = self.parse_ident()?;
5454
self.expect(&token::Colon)?;
5555
let ty = self.parse_ty()?;
5656

57-
self.sess.gated_spans.gate(sym::const_generics, lo.to(self.prev_token.span));
57+
self.sess.gated_spans.gate(sym::const_generics, const_span.to(self.prev_token.span));
5858

5959
Ok(GenericParam {
6060
ident,
6161
id: ast::DUMMY_NODE_ID,
6262
attrs: preceding_attrs.into(),
6363
bounds: Vec::new(),
64-
kind: GenericParamKind::Const { ty },
64+
kind: GenericParamKind::Const { ty, kw_span: const_span },
6565
is_placeholder: false,
6666
})
6767
}

src/librustc_resolve/late.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
536536

537537
for param in &generics.params {
538538
match param.kind {
539-
GenericParamKind::Lifetime { .. } => self.visit_generic_param(param),
540-
GenericParamKind::Type { ref default, .. } => {
539+
GenericParamKind::Lifetime => self.visit_generic_param(param),
540+
GenericParamKind::Type { ref default } => {
541541
for bound in &param.bounds {
542542
self.visit_param_bound(bound);
543543
}
@@ -551,7 +551,7 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
551551
// Allow all following defaults to refer to this type parameter.
552552
default_ban_rib.bindings.remove(&Ident::with_dummy_span(param.ident.name));
553553
}
554-
GenericParamKind::Const { ref ty } => {
554+
GenericParamKind::Const { ref ty, kw_span: _ } => {
555555
for bound in &param.bounds {
556556
self.visit_param_bound(bound);
557557
}

src/tools/clippy/clippy_lints/src/utils/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
476476
&& match (&l.kind, &r.kind) {
477477
(Lifetime, Lifetime) => true,
478478
(Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
479-
(Const { ty: l }, Const { ty: r }) => eq_ty(l, r),
479+
(Const { ty: l, kw_span: _ }, Const { ty: r, kw_span: _ }) => eq_ty(l, r),
480480
_ => false,
481481
}
482482
&& over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))

0 commit comments

Comments
 (0)