From 8bf4b2283e390b844b12c8979e1343eaffd222ba Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 3 Nov 2024 07:43:03 +0100 Subject: [PATCH] allow rustc_private feature in force-unstable-if-unmarked crates --- .../src/check_consts/check.rs | 21 +++++++- library/std/src/lib.rs | 5 -- ...rsive_const_stab_unmarked_crate_imports.rs | 8 ++- ...e_const_stab_unmarked_crate_imports.stderr | 49 +++++++++++++++---- 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/check.rs b/compiler/rustc_const_eval/src/check_consts/check.rs index c026e02ff4e02..fbc91e35a6a7e 100644 --- a/compiler/rustc_const_eval/src/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/check_consts/check.rs @@ -3,6 +3,7 @@ use std::assert_matches::assert_matches; use std::borrow::Cow; use std::mem; +use std::num::NonZero; use std::ops::Deref; use rustc_attr::{ConstStability, StabilityLevel}; @@ -780,7 +781,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { } } Some(ConstStability { - level: StabilityLevel::Unstable { implied_by: implied_feature, .. }, + level: StabilityLevel::Unstable { implied_by: implied_feature, issue, .. }, feature, .. }) => { @@ -803,7 +804,23 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { // to allow this. let feature_enabled = callee.is_local() || tcx.features().enabled(feature) - || implied_feature.is_some_and(|f| tcx.features().enabled(f)); + || implied_feature.is_some_and(|f| tcx.features().enabled(f)) + || { + // When we're compiling the compiler itself we may pull in + // crates from crates.io, but those crates may depend on other + // crates also pulled in from crates.io. We want to ideally be + // able to compile everything without requiring upstream + // modifications, so in the case that this looks like a + // `rustc_private` crate (e.g., a compiler crate) and we also have + // the `-Z force-unstable-if-unmarked` flag present (we're + // compiling a compiler crate), then let this missing feature + // annotation slide. + // This matches what we do in `eval_stability_allow_unstable` for + // regular stability. + feature == sym::rustc_private + && issue == NonZero::new(27812) + && self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked + }; // We do *not* honor this if we are in the "danger zone": we have to enforce // recursive const-stability and the callee is not safe-to-expose. In that // case we need `check_op` to do the check. diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index a8755df78c6b0..1de52eb7b21b2 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -392,11 +392,6 @@ #![feature(stdarch_internal)] // tidy-alphabetical-end // -// Library features (crates without staged_api): -// tidy-alphabetical-start -#![feature(rustc_private)] -// tidy-alphabetical-end -// // Only for re-exporting: // tidy-alphabetical-start #![feature(assert_matches)] diff --git a/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.rs b/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.rs index f03bfb81a1462..06ce406fd8994 100644 --- a/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.rs +++ b/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.rs @@ -1,10 +1,10 @@ //@ aux-build:unstable_if_unmarked_const_fn_crate.rs //@ aux-build:unmarked_const_fn_crate.rs #![feature(staged_api, rustc_private)] -#![stable(since="1.0.0", feature = "stable")] +#![stable(since = "1.0.0", feature = "stable")] -extern crate unstable_if_unmarked_const_fn_crate; extern crate unmarked_const_fn_crate; +extern crate unstable_if_unmarked_const_fn_crate; #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "rust1", since = "1.0.0")] @@ -18,6 +18,4 @@ const fn stable_fn() { //~^ERROR: cannot be (indirectly) exposed to stable } -fn main() { - -} +fn main() {} diff --git a/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.stderr b/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.stderr index a655c0faab629..b9223a6775fab 100644 --- a/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.stderr +++ b/tests/ui/consts/min_const_fn/recursive_const_stab_unmarked_crate_imports.stderr @@ -1,20 +1,48 @@ -error: const function that might be (indirectly) exposed to stable cannot use `#[feature(rustc_private)]` +error[E0658]: use of unstable library feature 'rustc_private': 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? + --> $DIR/recursive_const_stab_unmarked_crate_imports.rs:6:1 + | +LL | extern crate unstable_if_unmarked_const_fn_crate; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #27812 for more information + = help: add `#![feature(rustc_private)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: use of unstable library feature 'rustc_private': 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? + --> $DIR/recursive_const_stab_unmarked_crate_imports.rs:13:5 + | +LL | unstable_if_unmarked_const_fn_crate::expose_on_stable(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #27812 for more information + = help: add `#![feature(rustc_private)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: use of unstable library feature 'rustc_private': 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? --> $DIR/recursive_const_stab_unmarked_crate_imports.rs:15:5 | LL | unstable_if_unmarked_const_fn_crate::not_stably_const(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: mark the callee as `#[rustc_const_stable_indirect]` if it does not itself require any unsafe features -help: if the caller is not (yet) meant to be exposed to stable, add `#[rustc_const_unstable]` (this is what you probably want to do) + = note: see issue #27812 for more information + = help: add `#![feature(rustc_private)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error: `expose_on_stable` is not yet stable as a const fn + --> $DIR/recursive_const_stab_unmarked_crate_imports.rs:13:5 | -LL + #[rustc_const_unstable(feature = "...", issue = "...")] -LL | const fn stable_fn() { +LL | unstable_if_unmarked_const_fn_crate::expose_on_stable(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: otherwise, as a last resort `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks (this requires team approval) + = help: add `#![feature(rustc_private)]` to the crate attributes to enable + +error: `not_stably_const` is not yet stable as a const fn + --> $DIR/recursive_const_stab_unmarked_crate_imports.rs:15:5 | -LL + #[rustc_allow_const_fn_unstable(rustc_private)] -LL | const fn stable_fn() { +LL | unstable_if_unmarked_const_fn_crate::not_stably_const(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | + = help: add `#![feature(rustc_private)]` to the crate attributes to enable error: `just_a_fn` cannot be (indirectly) exposed to stable --> $DIR/recursive_const_stab_unmarked_crate_imports.rs:17:5 @@ -24,5 +52,6 @@ LL | unmarked_const_fn_crate::just_a_fn(); | = help: either mark the callee as `#[rustc_const_stable_indirect]`, or the caller as `#[rustc_const_unstable]` -error: aborting due to 2 previous errors +error: aborting due to 6 previous errors +For more information about this error, try `rustc --explain E0658`.