From eb9c3ee34df46e990ffaa3392ceae66fd66b68d0 Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Sat, 25 Apr 2026 21:25:01 +0200 Subject: [PATCH 1/2] Reduce AttributeKind from 40 to 32 bytes --- .../src/attributes/crate_level.rs | 1 - .../src/attributes/deprecation.rs | 2 +- .../src/attributes/stability.rs | 4 ++-- compiler/rustc_expand/src/base.rs | 2 +- compiler/rustc_hir/src/attrs/data_structures.rs | 7 +++---- compiler/rustc_passes/src/errors.rs | 2 -- compiler/rustc_passes/src/lib_features.rs | 4 ++-- compiler/rustc_passes/src/stability.rs | 15 ++++++++------- .../rustc-const-stability-require-const.stderr | 4 ---- .../const-stability-attribute-implies-missing.rs | 3 +-- ...nst-stability-attribute-implies-missing.stderr | 4 ---- tests/ui/stats/input-stats.stderr | 4 ++-- 12 files changed, 20 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/crate_level.rs b/compiler/rustc_attr_parsing/src/attributes/crate_level.rs index 3739461c2004..70b4ddb9527f 100644 --- a/compiler/rustc_attr_parsing/src/attributes/crate_level.rs +++ b/compiler/rustc_attr_parsing/src/attributes/crate_level.rs @@ -171,7 +171,6 @@ fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option, args: &ArgParser) -> Option) -> Option } } - let (stability, span) = self.stability?; + let (stability, _) = self.stability?; - Some(AttributeKind::RustcConstStability { stability, span }) + Some(AttributeKind::RustcConstStability { stability }) } } diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index b8325f7ba398..15ec07f5da7e 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -969,7 +969,7 @@ pub fn new( stability, deprecation: find_attr!( attrs, - Deprecated { deprecation, .. } => *deprecation + Deprecated { deprecation, .. } => **deprecation ), helper_attrs, edition, diff --git a/compiler/rustc_hir/src/attrs/data_structures.rs b/compiler/rustc_hir/src/attrs/data_structures.rs index 78ce2f019a39..3653a836dbcc 100644 --- a/compiler/rustc_hir/src/attrs/data_structures.rs +++ b/compiler/rustc_hir/src/attrs/data_structures.rs @@ -1013,7 +1013,7 @@ pub enum AttributeKind { /// Represents [`#[deprecated]`](https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html#the-deprecated-attribute). Deprecated { - deprecation: Deprecation, + deprecation: Box, span: Span, }, @@ -1233,7 +1233,6 @@ pub enum AttributeKind { /// Represents `#[pattern_complexity_limit]` PatternComplexityLimit { attr_span: Span, - limit_span: Span, limit: Limit, }, @@ -1348,8 +1347,6 @@ pub enum AttributeKind { /// Represents `#[rustc_const_stable]` and `#[rustc_const_unstable]`. RustcConstStability { stability: PartialConstStability, - /// Span of the `#[rustc_const_stable(...)]` or `#[rustc_const_unstable(...)]` attribute - span: Span, }, /// Represents `#[rustc_const_stable_indirect]`. @@ -1674,3 +1671,5 @@ pub enum AttributeKind { WindowsSubsystem(WindowsSubsystemKind, Span), // tidy-alphabetical-end } + +const _: () = assert!(std::mem::size_of::() == 32); diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 5300267573e6..a49199666d71 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -898,8 +898,6 @@ pub(crate) struct MissingConstErr { pub(crate) struct ConstStableNotStable { #[primary_span] pub fn_sig_span: Span, - #[label("attribute specified here")] - pub const_span: Span, } #[derive(Diagnostic)] diff --git a/compiler/rustc_passes/src/lib_features.rs b/compiler/rustc_passes/src/lib_features.rs index 1f92643815fd..342450c38cb7 100644 --- a/compiler/rustc_passes/src/lib_features.rs +++ b/compiler/rustc_passes/src/lib_features.rs @@ -30,8 +30,8 @@ fn extract(&self, attr: &Attribute) -> Option<(Symbol, FeatureStability, Span)> Attribute::Parsed(AttributeKind::Stability { stability, span }) => { (stability.feature, stability.level, *span) } - Attribute::Parsed(AttributeKind::RustcConstStability { stability, span }) => { - (stability.feature, stability.level, *span) + Attribute::Parsed(AttributeKind::RustcConstStability { stability }) => { + (stability.feature, stability.level, rustc_span::DUMMY_SP) } Attribute::Parsed(AttributeKind::RustcBodyStability { stability, span }) => { (stability.feature, stability.level, *span) diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index e41df43e34bd..94945542b66b 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -97,7 +97,7 @@ fn annotation_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> AnnotationKind { fn lookup_deprecation_entry(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option { let depr = find_attr!(tcx, def_id, - Deprecated { deprecation, span: _ } => *deprecation + Deprecated { deprecation, .. } => **deprecation ); let Some(depr) = depr else { @@ -218,7 +218,7 @@ fn lookup_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option *stability); + find_attr!(tcx, def_id, RustcConstStability { stability } => *stability); // After checking the immediate attributes, get rid of the span and compute implied // const stability: inherit feature gate from regular stability. @@ -379,6 +379,7 @@ macro_rules! find_attr_span { } } } + let attrs = self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id)); // If the current node is a function with const stability attributes (directly given or // implied), check if the function/method is const or the parent impl block is const. @@ -386,7 +387,7 @@ macro_rules! find_attr_span { if let Some(fn_sig) = fn_sig && !fn_sig.header.is_const() && const_stab.is_some() - && find_attr_span!(RustcConstStability).is_some() + && find_attr!(attrs, RustcConstStability{..}) { self.tcx.dcx().emit_err(errors::MissingConstErr { fn_sig_span: fn_sig.span }); } @@ -396,19 +397,19 @@ macro_rules! find_attr_span { && let Some(fn_sig) = fn_sig && const_stab.is_const_stable() && !stab.is_some_and(|s| s.is_stable()) - && let Some(const_span) = find_attr_span!(RustcConstStability) + && find_attr!( attrs, RustcConstStability {..}) { self.tcx .dcx() - .emit_err(errors::ConstStableNotStable { fn_sig_span: fn_sig.span, const_span }); + .emit_err(errors::ConstStableNotStable { fn_sig_span: fn_sig.span }); } if let Some(stab) = &const_stab && stab.is_const_stable() && stab.const_stable_indirect - && let Some(span) = find_attr_span!(RustcConstStability) + && find_attr!(attrs, RustcConstStability {..}) { - self.tcx.dcx().emit_err(errors::RustcConstStableIndirectPairing { span }); + self.tcx.dcx().emit_err(errors::RustcConstStableIndirectPairing { span: rustc_span::DUMMY_SP }); } } diff --git a/tests/ui/consts/rustc-const-stability-require-const.stderr b/tests/ui/consts/rustc-const-stability-require-const.stderr index 8d10bddaa45f..5eedbb8a036e 100644 --- a/tests/ui/consts/rustc-const-stability-require-const.stderr +++ b/tests/ui/consts/rustc-const-stability-require-const.stderr @@ -49,16 +49,12 @@ LL | pub extern "C" fn foo_c() {} error: attribute `#[rustc_const_stable]` can only be applied to functions that are declared `#[stable]` --> $DIR/rustc-const-stability-require-const.rs:52:1 | -LL | #[rustc_const_stable(feature = "barfoo_const", since = "1.0.0")] - | ---------------------------------------------------------------- attribute specified here LL | const fn barfoo_unmarked() {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: attribute `#[rustc_const_stable]` can only be applied to functions that are declared `#[stable]` --> $DIR/rustc-const-stability-require-const.rs:57:1 | -LL | #[rustc_const_stable(feature = "barfoo_const", since = "1.0.0")] - | ---------------------------------------------------------------- attribute specified here LL | pub const fn barfoo_unstable() {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/stability-attribute/const-stability-attribute-implies-missing.rs b/tests/ui/stability-attribute/const-stability-attribute-implies-missing.rs index 6d6d793c62b7..1b5bba25cec5 100644 --- a/tests/ui/stability-attribute/const-stability-attribute-implies-missing.rs +++ b/tests/ui/stability-attribute/const-stability-attribute-implies-missing.rs @@ -1,4 +1,4 @@ -#![crate_type = "lib"] +#![crate_type = "lib"] //~ ERROR feature `const_bar` implying `const_foobar` does not exist #![feature(staged_api)] #![stable(feature = "stability_attribute_implies", since = "1.0.0")] #![rustc_const_stable(feature = "stability_attribute_implies", since = "1.0.0")] @@ -8,7 +8,6 @@ #[stable(feature = "stability_attribute_implies", since = "1.0.0")] #[rustc_const_unstable(feature = "const_foobar", issue = "1", implied_by = "const_bar")] -//~^ ERROR feature `const_bar` implying `const_foobar` does not exist pub const fn foobar() -> u32 { 0 } diff --git a/tests/ui/stability-attribute/const-stability-attribute-implies-missing.stderr b/tests/ui/stability-attribute/const-stability-attribute-implies-missing.stderr index 232de41c769e..0ae786bf125c 100644 --- a/tests/ui/stability-attribute/const-stability-attribute-implies-missing.stderr +++ b/tests/ui/stability-attribute/const-stability-attribute-implies-missing.stderr @@ -1,8 +1,4 @@ error: feature `const_bar` implying `const_foobar` does not exist - --> $DIR/const-stability-attribute-implies-missing.rs:10:1 - | -LL | #[rustc_const_unstable(feature = "const_foobar", issue = "1", implied_by = "const_bar")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/stats/input-stats.stderr b/tests/ui/stats/input-stats.stderr index a91a15bc63df..3bc4645ccb71 100644 --- a/tests/ui/stats/input-stats.stderr +++ b/tests/ui/stats/input-stats.stderr @@ -93,10 +93,10 @@ hir-stats GenericParam 400 (NN.N%) 5 80 hir-stats Block 288 (NN.N%) 6 48 hir-stats GenericBound 256 (NN.N%) 4 64 hir-stats - Trait 256 (NN.N%) 4 -hir-stats Attribute 160 (NN.N%) 4 40 hir-stats Variant 144 (NN.N%) 2 72 hir-stats GenericArgs 144 (NN.N%) 3 48 hir-stats FieldDef 128 (NN.N%) 2 64 +hir-stats Attribute 128 (NN.N%) 4 32 hir-stats FnDecl 120 (NN.N%) 3 40 hir-stats Stmt 96 (NN.N%) 3 32 hir-stats - Expr 32 (NN.N%) 1 @@ -119,5 +119,5 @@ hir-stats TraitItemId 8 (NN.N%) 2 4 hir-stats ImplItemId 8 (NN.N%) 2 4 hir-stats ForeignItemId 4 (NN.N%) 1 4 hir-stats ---------------------------------------------------------------- -hir-stats Total 8_576 172 +hir-stats Total 8_544 172 hir-stats ================================================================ From 2ba201017086071338bf706e4ad4177cab74f7a7 Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Sun, 26 Apr 2026 11:56:52 +0200 Subject: [PATCH 2/2] Remove span, unbox deprecation --- compiler/rustc_attr_parsing/src/attributes/deprecation.rs | 3 +-- compiler/rustc_expand/src/base.rs | 2 +- compiler/rustc_hir/src/attrs/data_structures.rs | 3 +-- compiler/rustc_hir/src/hir.rs | 1 - compiler/rustc_passes/src/check_attr.rs | 5 +++-- compiler/rustc_passes/src/stability.rs | 8 ++++---- .../stability-attribute/stability-attribute-sanity.stderr | 4 ---- 7 files changed, 10 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/deprecation.rs b/compiler/rustc_attr_parsing/src/attributes/deprecation.rs index fdd1e472d48c..571ca9144655 100644 --- a/compiler/rustc_attr_parsing/src/attributes/deprecation.rs +++ b/compiler/rustc_attr_parsing/src/attributes/deprecation.rs @@ -162,8 +162,7 @@ fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option **deprecation + Deprecated { deprecation, .. } => *deprecation ), helper_attrs, edition, diff --git a/compiler/rustc_hir/src/attrs/data_structures.rs b/compiler/rustc_hir/src/attrs/data_structures.rs index 3653a836dbcc..e8244bdf6cf0 100644 --- a/compiler/rustc_hir/src/attrs/data_structures.rs +++ b/compiler/rustc_hir/src/attrs/data_structures.rs @@ -1013,8 +1013,7 @@ pub enum AttributeKind { /// Represents [`#[deprecated]`](https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html#the-deprecated-attribute). Deprecated { - deprecation: Box, - span: Span, + deprecation: Deprecation, }, /// Represents `#[diagnostic::do_not_recommend]`. diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 5608bd82fdac..ff18952d979c 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1376,7 +1376,6 @@ fn span(&self) -> Span { Attribute::Unparsed(u) => u.span, // FIXME: should not be needed anymore when all attrs are parsed Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span, - Attribute::Parsed(AttributeKind::Deprecated { span, .. }) => *span, Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1, a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"), } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index d11825016baf..445b369260dc 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -150,8 +150,9 @@ fn check_attributes( Attribute::Parsed(AttributeKind::RustcAllowConstFnUnstable(_, first_span)) => { self.check_rustc_allow_const_fn_unstable(hir_id, *first_span, span, target) } - Attribute::Parsed(AttributeKind::Deprecated { span: attr_span, .. }) => { - self.check_deprecated(hir_id, *attr_span, target) + Attribute::Parsed(AttributeKind::Deprecated { .. }) => { + // FIXME move to attr parsing + self.check_deprecated(hir_id, rustc_span::DUMMY_SP, target) } Attribute::Parsed(AttributeKind::TargetFeature{ attr_span, ..}) => { self.check_target_feature(hir_id, *attr_span, target, attrs) diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 94945542b66b..c7263551593d 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -97,7 +97,7 @@ fn annotation_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> AnnotationKind { fn lookup_deprecation_entry(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option { let depr = find_attr!(tcx, def_id, - Deprecated { deprecation, .. } => **deprecation + Deprecated { deprecation, .. } => *deprecation ); let Some(depr) = depr else { @@ -324,6 +324,7 @@ fn check_compatible_stability(&self, def_id: LocalDefId) { let depr = self.tcx.lookup_deprecation_entry(def_id); let stab = self.tcx.lookup_stability(def_id); let const_stab = self.tcx.lookup_const_stability(def_id); + let attrs = self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id)); macro_rules! find_attr_span { ($name:ident) => {{ @@ -334,9 +335,9 @@ macro_rules! find_attr_span { if stab.is_none() && depr.map_or(false, |d| d.attr.is_since_rustc_version()) - && let Some(span) = find_attr_span!(Deprecated) + && find_attr!(attrs, Deprecated { .. }) { - self.tcx.dcx().emit_err(errors::DeprecatedAttribute { span }); + self.tcx.dcx().emit_err(errors::DeprecatedAttribute { span: rustc_span::DUMMY_SP }); } if let Some(stab) = stab { @@ -379,7 +380,6 @@ macro_rules! find_attr_span { } } } - let attrs = self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id)); // If the current node is a function with const stability attributes (directly given or // implied), check if the function/method is const or the parent impl block is const. diff --git a/tests/ui/stability-attribute/stability-attribute-sanity.stderr b/tests/ui/stability-attribute/stability-attribute-sanity.stderr index 55b318c51ab9..3b0ab520623c 100644 --- a/tests/ui/stability-attribute/stability-attribute-sanity.stderr +++ b/tests/ui/stability-attribute/stability-attribute-sanity.stderr @@ -153,10 +153,6 @@ LL | #[deprecated(since = "invalid", note = "text")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0549]: deprecated attribute must be paired with either stable or unstable attribute - --> $DIR/stability-attribute-sanity.rs:71:1 - | -LL | #[deprecated(since = "5.5.5", note = "text")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0711]: feature `a` is declared stable since 1.0.0, but was previously declared stable since 4.4.4 --> $DIR/stability-attribute-sanity.rs:67:1