diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 3aceec7002ab9..61b22ccc3bf5e 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -385,8 +385,8 @@ pub enum GenericParamKind { }, Const { ty: P, - /// Span of the `const` keyword. - kw_span: Span, + /// Span of the whole parameter definition, including default. + span: Span, /// Optional default value for the const generic param. default: Option, }, @@ -410,10 +410,7 @@ impl GenericParam { self.ident.span } GenericParamKind::Type { default: Some(ty) } => self.ident.span.to(ty.span), - GenericParamKind::Const { kw_span, default: Some(default), .. } => { - kw_span.to(default.value.span) - } - GenericParamKind::Const { kw_span, default: None, ty } => kw_span.to(ty.span), + GenericParamKind::Const { span, .. } => *span, } } } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 867ab7d947898..ad6fa4ac216df 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -1350,9 +1350,10 @@ macro_rules! common_visitor_and_walkers { match kind { GenericParamKind::Lifetime => (), GenericParamKind::Type { default } => visit_opt!(vis, visit_ty, default), - GenericParamKind::Const { ty, default, kw_span: _ } => { + GenericParamKind::Const { ty, default, span } => { try_visit!(vis.visit_ty(ty)); visit_opt!(vis, visit_anon_const, default); + try_visit!(visit_span(vis, span)); } } if let Some(sp) = colon_span { diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d14e27982ef50..0706bdb119f01 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1960,7 +1960,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { (hir::ParamName::Plain(self.lower_ident(param.ident)), kind) } - GenericParamKind::Const { ty, kw_span: _, default } => { + GenericParamKind::Const { ty, span: _, default } => { let ty = self .lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault)); diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index da24825120376..53e2a1c695a8f 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -909,11 +909,11 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara } GenericParamKind::Type { default: None } => (), GenericParamKind::Lifetime => (), - GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => { + GenericParamKind::Const { ty: _, span: _, default: Some(default) } => { ordered_params += " = "; ordered_params += &pprust::expr_to_string(&default.value); } - GenericParamKind::Const { ty: _, kw_span: _, default: None } => (), + GenericParamKind::Const { ty: _, span: _, default: None } => (), } first = false; } diff --git a/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs b/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs index 3a20b39798d7b..6082e376435a7 100644 --- a/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs +++ b/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs @@ -124,7 +124,7 @@ pub(crate) fn expand_deriving_coerce_pointee( GenericParamKind::Type { default: _ } => { cx.typaram(p.span(), p.ident, p.bounds.clone(), None) } - GenericParamKind::Const { ty, kw_span: _, default: _ } => cx + GenericParamKind::Const { ty, span: _, default: _ } => cx .const_param( p.span(), p.ident, diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index d642851bb77bf..d201ca196d694 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -664,10 +664,10 @@ impl<'a> TraitDef<'a> { cx.typaram(param.ident.span.with_ctxt(ctxt), param.ident, bounds, None) } - GenericParamKind::Const { ty, kw_span, .. } => { + GenericParamKind::Const { ty, span, .. } => { let const_nodefault_kind = GenericParamKind::Const { ty: ty.clone(), - kw_span: kw_span.with_ctxt(ctxt), + span: span.with_ctxt(ctxt), // We can't have default values inside impl block default: None, diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index a333f2c7cb787..85683c1a03ff2 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -172,7 +172,7 @@ impl<'a> ExtCtxt<'a> { attrs: AttrVec::new(), bounds, is_placeholder: false, - kind: ast::GenericParamKind::Const { ty, kw_span: DUMMY_SP, default }, + kind: ast::GenericParamKind::Const { ty, span: DUMMY_SP, default }, colon_span: None, } } diff --git a/compiler/rustc_parse/src/parser/generics.rs b/compiler/rustc_parse/src/parser/generics.rs index af1d1a1ec6697..86326341a75ae 100644 --- a/compiler/rustc_parse/src/parser/generics.rs +++ b/compiler/rustc_parse/src/parser/generics.rs @@ -114,13 +114,18 @@ impl<'a> Parser<'a> { // Parse optional const generics default value. let default = if self.eat(exp!(Eq)) { Some(self.parse_const_arg()?) } else { None }; + let span = if let Some(ref default) = default { + const_span.to(default.value.span) + } else { + const_span.to(ty.span) + }; Ok(GenericParam { ident, id: ast::DUMMY_NODE_ID, attrs: preceding_attrs, bounds: Vec::new(), - kind: GenericParamKind::Const { ty, kw_span: const_span, default }, + kind: GenericParamKind::Const { ty, span, default }, is_placeholder: false, colon_span: None, }) @@ -137,6 +142,11 @@ impl<'a> Parser<'a> { // Parse optional const generics default value. let default = if self.eat(exp!(Eq)) { Some(self.parse_const_arg()?) } else { None }; + let span = if let Some(ref default) = default { + mistyped_const_ident.span.to(default.value.span) + } else { + mistyped_const_ident.span.to(ty.span) + }; self.dcx() .struct_span_err( @@ -156,7 +166,7 @@ impl<'a> Parser<'a> { id: ast::DUMMY_NODE_ID, attrs: preceding_attrs, bounds: Vec::new(), - kind: GenericParamKind::Const { ty, kw_span: mistyped_const_ident.span, default }, + kind: GenericParamKind::Const { ty, span, default }, is_placeholder: false, colon_span: None, }) diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index ac7bdda41954e..4e8316bcd4338 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -1656,7 +1656,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { forward_ty_ban_rib.bindings.swap_remove(i); forward_ty_ban_rib_const_param_ty.bindings.swap_remove(i); } - GenericParamKind::Const { ref ty, kw_span: _, ref default } => { + GenericParamKind::Const { ref ty, span: _, ref default } => { // Const parameters can't have param bounds. assert!(param.bounds.is_empty()); diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 6230b8cfbec7d..a4022691995e7 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2940,7 +2940,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { let span = if let [.., bound] = ¶m.bounds[..] { bound.span() } else if let GenericParam { - kind: GenericParamKind::Const { ty, kw_span: _, default }, .. + kind: GenericParamKind::Const { ty, span: _, default }, .. } = param { default.as_ref().map(|def| def.value.span).unwrap_or(ty.span) } else { diff --git a/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs b/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs index e65914b9b5ee5..e6396987cc6d9 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs @@ -886,13 +886,13 @@ pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool { ( Const { ty: lt, - kw_span: _, default: ld, + span: _, }, Const { ty: rt, - kw_span: _, default: rd, + span: _, }, ) => eq_ty(lt, rt) && both(ld.as_ref(), rd.as_ref(), eq_anon_const), _ => false, diff --git a/src/tools/rustfmt/src/spanned.rs b/src/tools/rustfmt/src/spanned.rs index 507647566d417..ac132999b62eb 100644 --- a/src/tools/rustfmt/src/spanned.rs +++ b/src/tools/rustfmt/src/spanned.rs @@ -122,7 +122,7 @@ impl Spanned for ast::GenericParam { fn span(&self) -> Span { let lo = match self.kind { _ if !self.attrs.is_empty() => self.attrs[0].span.lo(), - ast::GenericParamKind::Const { kw_span, .. } => kw_span.lo(), + ast::GenericParamKind::Const { span, .. } => span.lo(), _ => self.ident.span.lo(), }; let hi = if self.bounds.is_empty() { diff --git a/src/tools/rustfmt/src/types.rs b/src/tools/rustfmt/src/types.rs index dd1515805e54b..c0df01edd6dee 100644 --- a/src/tools/rustfmt/src/types.rs +++ b/src/tools/rustfmt/src/types.rs @@ -689,7 +689,7 @@ impl Rewrite for ast::GenericParam { let param_start = if let ast::GenericParamKind::Const { ref ty, - kw_span, + span, default, } = &self.kind { @@ -711,7 +711,7 @@ impl Rewrite for ast::GenericParam { default.rewrite_result(context, Shape::legacy(budget, shape.indent))?; param.push_str(&rewrite); } - kw_span.lo() + span.lo() } else { param.push_str(rewrite_ident(context, self.ident)); self.ident.span.lo()