Skip to content

Commit

Permalink
Auto merge of #95956 - yaahc:stable-in-unstable, r=cjgillot
Browse files Browse the repository at this point in the history
Support unstable moves via stable in unstable items

part of https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/moving.20items.20to.20core.20unstably and a blocker of #90328.

The libs-api team needs the ability to move an already stable item to a new location unstably, in this case for Error in core. Otherwise these changes are insta-stable making them much harder to merge.

This PR attempts to solve the problem by checking the stability of path segments as well as the last item in the path itself, which is currently the only thing checked.
  • Loading branch information
bors committed Jul 14, 2022
2 parents f1a8854 + 3e2c5b5 commit 24699bc
Show file tree
Hide file tree
Showing 34 changed files with 304 additions and 33 deletions.
29 changes: 27 additions & 2 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl ConstStability {
pub enum StabilityLevel {
// Reason for the current stability level and the relevant rust-lang issue
Unstable { reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool },
Stable { since: Symbol },
Stable { since: Symbol, allowed_through_unstable_modules: bool },
}

impl StabilityLevel {
Expand Down Expand Up @@ -172,6 +172,7 @@ where
let mut stab: Option<(Stability, Span)> = None;
let mut const_stab: Option<(ConstStability, Span)> = None;
let mut promotable = false;
let mut allowed_through_unstable_modules = false;

let diagnostic = &sess.parse_sess.span_diagnostic;

Expand All @@ -182,6 +183,7 @@ where
sym::unstable,
sym::stable,
sym::rustc_promotable,
sym::rustc_allowed_through_unstable_modules,
]
.iter()
.any(|&s| attr.has_name(s))
Expand All @@ -193,6 +195,8 @@ where

if attr.has_name(sym::rustc_promotable) {
promotable = true;
} else if attr.has_name(sym::rustc_allowed_through_unstable_modules) {
allowed_through_unstable_modules = true;
}
// attributes with data
else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta {
Expand Down Expand Up @@ -406,7 +410,7 @@ where

match (feature, since) {
(Some(feature), Some(since)) => {
let level = Stable { since };
let level = Stable { since, allowed_through_unstable_modules: false };
if sym::stable == meta_name {
stab = Some((Stability { level, feature }, attr.span));
} else {
Expand Down Expand Up @@ -447,6 +451,27 @@ where
}
}

if allowed_through_unstable_modules {
if let Some((
Stability {
level: StabilityLevel::Stable { ref mut allowed_through_unstable_modules, .. },
..
},
_,
)) = stab
{
*allowed_through_unstable_modules = true;
} else {
struct_span_err!(
diagnostic,
item_sp,
E0789,
"`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute"
)
.emit();
}
}

(stab, const_stab)
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,5 @@ E0788: include_str!("./error_codes/E0788.md"),
// E0721, // `await` keyword
// E0723, // unstable feature in `const` context
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
E0789, // rustc_allowed_through_unstable_modules without stability attribute
}
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
"allow_internal_unsafe side-steps the unsafe_code lint",
),
rustc_attr!(rustc_allowed_through_unstable_modules, Normal, template!(Word), WarnFollowing,
"rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
through unstable paths"),

// ==========================================================================
// Internal attributes: Type system related:
Expand Down
19 changes: 15 additions & 4 deletions compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,15 @@ impl<'tcx> TyCtxt<'tcx> {
///
/// This function will also check if the item is deprecated.
/// If so, and `id` is not `None`, a deprecated lint attached to `id` will be emitted.
///
/// Returns `true` if item is allowed aka, stable or unstable under an enabled feature.
pub fn check_stability(
self,
def_id: DefId,
id: Option<HirId>,
span: Span,
method_span: Option<Span>,
) {
) -> bool {
self.check_stability_allow_unstable(def_id, id, span, method_span, AllowUnstable::No)
}

Expand All @@ -490,14 +492,16 @@ impl<'tcx> TyCtxt<'tcx> {
/// If so, and `id` is not `None`, a deprecated lint attached to `id` will be emitted.
///
/// Pass `AllowUnstable::Yes` to `allow_unstable` to force an unstable item to be allowed. Deprecation warnings will be emitted normally.
///
/// Returns `true` if item is allowed aka, stable or unstable under an enabled feature.
pub fn check_stability_allow_unstable(
self,
def_id: DefId,
id: Option<HirId>,
span: Span,
method_span: Option<Span>,
allow_unstable: AllowUnstable,
) {
) -> bool {
self.check_optional_stability(
def_id,
id,
Expand All @@ -516,6 +520,8 @@ impl<'tcx> TyCtxt<'tcx> {
/// missing stability attributes (not necessarily just emit a `bug!`). This is necessary
/// for default generic parameters, which only have stability attributes if they were
/// added after the type on which they're defined.
///
/// Returns `true` if item is allowed aka, stable or unstable under an enabled feature.
pub fn check_optional_stability(
self,
def_id: DefId,
Expand All @@ -524,13 +530,16 @@ impl<'tcx> TyCtxt<'tcx> {
method_span: Option<Span>,
allow_unstable: AllowUnstable,
unmarked: impl FnOnce(Span, DefId),
) {
) -> bool {
let soft_handler = |lint, span, msg: &_| {
self.struct_span_lint_hir(lint, id.unwrap_or(hir::CRATE_HIR_ID), span, |lint| {
lint.build(msg).emit();
})
};
match self.eval_stability_allow_unstable(def_id, id, span, method_span, allow_unstable) {
let eval_result =
self.eval_stability_allow_unstable(def_id, id, span, method_span, allow_unstable);
let is_allowed = matches!(eval_result, EvalResult::Allow);
match eval_result {
EvalResult::Allow => {}
EvalResult::Deny { feature, reason, issue, suggestion, is_soft } => report_unstable(
self.sess,
Expand All @@ -544,6 +553,8 @@ impl<'tcx> TyCtxt<'tcx> {
),
EvalResult::Unmarked => unmarked(span, def_id),
}

is_allowed
}

pub fn lookup_deprecation(self, id: DefId) -> Option<Deprecation> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl CheckAttrVisitor<'_> {
| sym::rustc_const_stable
| sym::unstable
| sym::stable
| sym::rustc_allowed_through_unstable_modules
| sym::rustc_promotable => self.check_stability_promotable(&attr, span, target),
_ => true,
};
Expand Down
51 changes: 48 additions & 3 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A pass that annotates every item and method with its stability level,
//! propagating default levels lexically from parent to children ast nodes.

use attr::StabilityLevel;
use rustc_attr::{self as attr, ConstStability, Stability};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_errors::struct_span_err;
Expand Down Expand Up @@ -223,7 +224,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {

// Check if deprecated_since < stable_since. If it is,
// this is *almost surely* an accident.
if let (&Some(dep_since), &attr::Stable { since: stab_since }) =
if let (&Some(dep_since), &attr::Stable { since: stab_since, .. }) =
(&depr.as_ref().and_then(|(d, _)| d.since), &stab.level)
{
// Explicit version of iter::order::lt to handle parse errors properly
Expand Down Expand Up @@ -773,7 +774,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, id: hir::HirId) {
if let Some(def_id) = path.res.opt_def_id() {
let method_span = path.segments.last().map(|s| s.ident.span);
self.tcx.check_stability_allow_unstable(
let item_is_allowed = self.tcx.check_stability_allow_unstable(
def_id,
Some(id),
path.span,
Expand All @@ -783,8 +784,52 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
} else {
AllowUnstable::No
},
)
);

let is_allowed_through_unstable_modules = |def_id| {
self.tcx
.lookup_stability(def_id)
.map(|stab| match stab.level {
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
allowed_through_unstable_modules
}
_ => false,
})
.unwrap_or(false)
};

if item_is_allowed && !is_allowed_through_unstable_modules(def_id) {
// Check parent modules stability as well if the item the path refers to is itself
// stable. We only emit warnings for unstable path segments if the item is stable
// or allowed because stability is often inherited, so the most common case is that
// both the segments and the item are unstable behind the same feature flag.
//
// We check here rather than in `visit_path_segment` to prevent visiting the last
// path segment twice
//
// We include special cases via #[rustc_allowed_through_unstable_modules] for items
// that were accidentally stabilized through unstable paths before this check was
// added, such as `core::intrinsics::transmute`
let parents = path.segments.iter().rev().skip(1);
for path_segment in parents {
if let Some(def_id) = path_segment.res.as_ref().and_then(Res::opt_def_id) {
// use `None` for id to prevent deprecation check
self.tcx.check_stability_allow_unstable(
def_id,
None,
path.span,
None,
if is_unstable_reexport(self.tcx, id) {
AllowUnstable::Yes
} else {
AllowUnstable::No
},
);
}
}
}
}

intravisit::walk_path(self, path)
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ symbols! {
rustc_allocator_nounwind,
rustc_allow_const_fn_unstable,
rustc_allow_incoherent_impl,
rustc_allowed_through_unstable_modules,
rustc_attrs,
rustc_box,
rustc_builtin_macro,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// as the rest of the type. As such, we ignore missing
// stability attributes.
},
)
);
}
if let (hir::TyKind::Infer, false) = (&ty.kind, self.astconv.allow_ty_infer()) {
self.inferred_params.push(ty.span);
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,7 @@ extern "rust-intrinsic" {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_allowed_through_unstable_modules)]
#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
#[rustc_diagnostic_item = "transmute"]
pub fn transmute<T, U>(e: T) -> U;
Expand Down Expand Up @@ -2649,6 +2650,7 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
/// Here is an example of how this could cause a problem:
/// ```no_run
/// #![feature(const_eval_select)]
/// #![feature(core_intrinsics)]
/// use std::hint::unreachable_unchecked;
/// use std::intrinsics::const_eval_select;
///
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ pub(crate) mod builtin {
/// Unstable implementation detail of the `rustc` compiler, do not use.
#[rustc_builtin_macro]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable(core_intrinsics, libstd_sys_internals)]
#[allow_internal_unstable(core_intrinsics, libstd_sys_internals, rt)]
#[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")]
#[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it.
pub macro RustcDecodable($item:item) {
Expand All @@ -1547,7 +1547,7 @@ pub(crate) mod builtin {
/// Unstable implementation detail of the `rustc` compiler, do not use.
#[rustc_builtin_macro]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable(core_intrinsics)]
#[allow_internal_unstable(core_intrinsics, rt)]
#[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")]
#[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it.
pub macro RustcEncodable($item:item) {
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/unicode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[test]
pub fn version() {
let (major, _minor, _update) = core::unicode::UNICODE_VERSION;
let (major, _minor, _update) = core::char::UNICODE_VERSION;
assert!(major >= 10);
}
3 changes: 2 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
#![cfg_attr(not(bootstrap), deny(ffi_unwind_calls))]
// std may use features in a platform-specific way
#![allow(unused_features)]
#![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count))]
#![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count, rt))]
#![cfg_attr(
all(target_vendor = "fortanix", target_env = "sgx"),
feature(slice_index_methods, coerce_unsized, sgx_platform)
Expand Down Expand Up @@ -297,6 +297,7 @@
// Library features (alloc):
#![feature(alloc_layout_extra)]
#![feature(alloc_c_string)]
#![feature(alloc_ffi)]
#![feature(allocator_api)]
#![feature(get_mut_unchecked)]
#![feature(map_try_insert)]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::thread::Result;

#[doc(hidden)]
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
#[allow_internal_unstable(libstd_sys_internals, const_format_args, core_panic)]
#[allow_internal_unstable(libstd_sys_internals, const_format_args, core_panic, rt)]
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
#[rustc_macro_transparency = "semitransparent"]
pub macro panic_2015 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ fn render_stability_since_raw(
}

let const_title_and_stability = match const_stability {
Some(ConstStability { level: StabilityLevel::Stable { since }, .. })
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. })
if Some(since) != containing_const_ver =>
{
Some((format!("const since {}", since), format!("const: {}", since)))
Expand Down
1 change: 1 addition & 0 deletions src/test/codegen/intrinsics/const_eval_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#![crate_type = "lib"]
#![feature(const_eval_select)]
#![feature(core_intrinsics)]

use std::intrinsics::const_eval_select;

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/intrinsics/const-eval-select-bad.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(const_eval_select)]
#![feature(core_intrinsics)]

use std::intrinsics::const_eval_select;

Expand Down
Loading

0 comments on commit 24699bc

Please sign in to comment.