Skip to content

Commit

Permalink
Auto merge of rust-lang#98770 - klensy:no-string-dupes-ugly, r=cjgillot
Browse files Browse the repository at this point in the history
rmeta: avoid embedding `StabilityLevel::Unstable` reason multiple times into .rmeta\.rlib files

Avoids bloating size of some rmeta\rlib files by not placing default string for `StabilityLevel::Unstable` reason multiple times, affects only stdlib\rustc artifacts. For stdlib cuts about 3% (diff of total size for patched\unpatched *.rmeta files of stage1-std) of file size, depending on crates.

fixes rust-lang#88180
  • Loading branch information
bors committed Jul 25, 2022
2 parents 530c0a8 + b38c948 commit 7f93d4a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
35 changes: 33 additions & 2 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub enum StabilityLevel {
/// `#[unstable]`
Unstable {
/// Reason for the current stability level.
reason: Option<Symbol>,
reason: UnstableReason,
/// Relevant `rust-lang/rust` issue.
issue: Option<NonZeroU32>,
is_soft: bool,
Expand Down Expand Up @@ -182,6 +182,32 @@ impl StabilityLevel {
}
}

#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
#[derive(HashStable_Generic)]
pub enum UnstableReason {
None,
Default,
Some(Symbol),
}

impl UnstableReason {
fn from_opt_reason(reason: Option<Symbol>) -> Self {
// UnstableReason::Default constructed manually
match reason {
Some(r) => Self::Some(r),
None => Self::None,
}
}

pub fn to_opt_reason(&self) -> Option<Symbol> {
match self {
Self::None => None,
Self::Default => Some(sym::unstable_location_reason_default),
Self::Some(r) => Some(*r),
}
}
}

/// Collects stability info from all stability attributes in `attrs`.
/// Returns `None` if no stability attributes are found.
pub fn find_stability(
Expand Down Expand Up @@ -371,7 +397,12 @@ where
);
continue;
}
let level = Unstable { reason, issue: issue_num, is_soft, implied_by };
let level = Unstable {
reason: UnstableReason::from_opt_reason(reason),
issue: issue_num,
is_soft,
implied_by,
};
if sym::unstable == meta_name {
stab = Some((Stability { level, feature }, attr.span));
} else {
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,13 @@ impl<'tcx> TyCtxt<'tcx> {
}

let suggestion = suggestion_for_allocator_api(self, def_id, span, feature);
EvalResult::Deny { feature, reason, issue, suggestion, is_soft }
EvalResult::Deny {
feature,
reason: reason.to_opt_reason(),
issue,
suggestion,
is_soft,
}
}
Some(_) => {
// Stable APIs are always ok to call and deprecated APIs are
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! propagating default levels lexically from parent to children ast nodes.
use attr::StabilityLevel;
use rustc_attr::{self as attr, ConstStability, Stability, Unstable};
use rustc_attr::{self as attr, ConstStability, Stability, Unstable, UnstableReason};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir;
Expand Down Expand Up @@ -634,12 +634,9 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
// while maintaining the invariant that all sysroot crates are unstable
// by default and are unable to be used.
if tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
let reason = "this crate is being loaded from the sysroot, an \
unstable location; did you mean to load this crate \
from crates.io via `Cargo.toml` instead?";
let stability = Stability {
level: attr::StabilityLevel::Unstable {
reason: Some(Symbol::intern(reason)),
reason: UnstableReason::Default,
issue: NonZeroU32::new(27812),
is_soft: false,
implied_by: None,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ impl<'a> Resolver<'a> {
stability::report_unstable(
self.session,
feature,
reason,
reason.to_opt_reason(),
issue,
None,
is_soft,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,9 @@ symbols! {
unsized_locals,
unsized_tuple_coercion,
unstable,
unstable_location_reason_default: "this crate is being loaded from the sysroot, an \
unstable location; did you mean to load this crate \
from crates.io via `Cargo.toml` instead?",
untagged_unions,
unused_imports,
unused_qualifications,
Expand Down

0 comments on commit 7f93d4a

Please sign in to comment.