Skip to content

Commit

Permalink
Rollup merge of rust-lang#73597 - ayazhafiz:i/const-span, r=ecstatic-…
Browse files Browse the repository at this point in the history
…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 🙂
  • Loading branch information
Manishearth committed Jun 24, 2020
2 parents dba2ae4 + 86f6c0e commit 1911553
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/librustc_ast/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ pub enum GenericParamKind {
},
Const {
ty: P<Ty>,
/// Span of the `const` keyword.
kw_span: Span,
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
GenericParamKind::Type { default } => {
visit_opt(default, |default| vis.visit_ty(default));
}
GenericParamKind::Const { ty } => {
GenericParamKind::Const { ty, kw_span: _ } => {
vis.visit_ty(ty);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_lowering/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2230,7 +2230,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

(hir::ParamName::Plain(param.ident), kind)
}
GenericParamKind::Const { ref ty } => {
GenericParamKind::Const { ref ty, kw_span: _ } => {
let ty = self
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
this.lower_ty(&ty, ImplTraitContext::disallowed())
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,9 +1135,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
generics.params.iter().map(|param| {
let ident = Some(param.ident.to_string());
let (kind, ident) = match &param.kind {
GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
GenericParamKind::Const { ref ty } => {
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident),
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
GenericParamKind::Const { ref ty, kw_span: _ } => {
let ty = pprust::ty_to_string(ty);
(ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2578,7 +2578,7 @@ impl<'a> State<'a> {
s.print_type(default)
}
}
ast::GenericParamKind::Const { ref ty } => {
ast::GenericParamKind::Const { ref ty, kw_span: _ } => {
s.word_space("const");
s.print_ident(param.ident);
s.s.space();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn inject_impl_of_structural_trait(
*default = None;
ast::GenericArg::Type(cx.ty_ident(span, param.ident))
}
ast::GenericParamKind::Const { ty: _ } => {
ast::GenericParamKind::Const { ty: _, kw_span: _ } => {
ast::GenericArg::Const(cx.const_ident(span, param.ident))
}
})
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_parse/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ impl<'a> Parser<'a> {
}

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

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

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

Ok(GenericParam {
ident,
id: ast::DUMMY_NODE_ID,
attrs: preceding_attrs.into(),
bounds: Vec::new(),
kind: GenericParamKind::Const { ty },
kind: GenericParamKind::Const { ty, kw_span: const_span },
is_placeholder: false,
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {

for param in &generics.params {
match param.kind {
GenericParamKind::Lifetime { .. } => self.visit_generic_param(param),
GenericParamKind::Type { ref default, .. } => {
GenericParamKind::Lifetime => self.visit_generic_param(param),
GenericParamKind::Type { ref default } => {
for bound in &param.bounds {
self.visit_param_bound(bound);
}
Expand All @@ -551,7 +551,7 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
// Allow all following defaults to refer to this type parameter.
default_ban_rib.bindings.remove(&Ident::with_dummy_span(param.ident.name));
}
GenericParamKind::Const { ref ty } => {
GenericParamKind::Const { ref ty, kw_span: _ } => {
for bound in &param.bounds {
self.visit_param_bound(bound);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/utils/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
&& match (&l.kind, &r.kind) {
(Lifetime, Lifetime) => true,
(Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
(Const { ty: l }, Const { ty: r }) => eq_ty(l, r),
(Const { ty: l, kw_span: _ }, Const { ty: r, kw_span: _ }) => eq_ty(l, r),
_ => false,
}
&& over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
Expand Down

0 comments on commit 1911553

Please sign in to comment.