Skip to content

rmeta: avoid embedding StabilityLevel::Unstable reason multiple times into .rmeta\.rlib files #98770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1527,6 +1527,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