Skip to content
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

Make rustc consider itself a stable compiler when RUSTC_BOOTSTRAP=-1 #132993

Merged
merged 2 commits into from
Nov 18, 2024
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
19 changes: 12 additions & 7 deletions compiler/rustc_feature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ impl UnstableFeatures {
// Returns whether `krate` should be counted as unstable
let is_unstable_crate =
|var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));
// `true` if we should enable unstable features for bootstrapping.
let bootstrap =
std::env::var("RUSTC_BOOTSTRAP").is_ok_and(|var| var == "1" || is_unstable_crate(&var));
match (disable_unstable_features, bootstrap) {
(_, true) => UnstableFeatures::Cheat,
(true, _) => UnstableFeatures::Disallow,
(false, _) => UnstableFeatures::Allow,

let bootstrap = std::env::var("RUSTC_BOOTSTRAP").ok();
if let Some(val) = bootstrap.as_deref() {
match val {
val if val == "1" || is_unstable_crate(val) => return UnstableFeatures::Cheat,
// Hypnotize ourselves so that we think we are a stable compiler and thus don't
// allow any unstable features.
"-1" => return UnstableFeatures::Disallow,
_ => {}
}
}

if disable_unstable_features { UnstableFeatures::Disallow } else { UnstableFeatures::Allow }
}

pub fn is_nightly_build(&self) -> bool {
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_feature/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ fn rustc_bootstrap_parsing() {
assert!(!is_bootstrap("x,y,z", Some("a")));
assert!(!is_bootstrap("x,y,z", None));

// this is technically a breaking change, but there are no stability guarantees for RUSTC_BOOTSTRAP
// `RUSTC_BOOTSTRAP=0` is not recognized.
assert!(!is_bootstrap("0", None));

// `RUSTC_BOOTSTRAP=-1` is force-stable, no unstable features allowed.
let is_force_stable = |krate| {
std::env::set_var("RUSTC_BOOTSTRAP", "-1");
matches!(UnstableFeatures::from_environment(krate), UnstableFeatures::Disallow)
};
assert!(is_force_stable(None));
// Does not support specifying any crate.
assert!(is_force_stable(Some("x")));
assert!(is_force_stable(Some("x,y,z")));
}
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct EnabledLangFeature {
pub stable_since: Option<Symbol>,
}

/// Information abhout an enabled library feature.
/// Information about an enabled library feature.
#[derive(Debug, Copy, Clone)]
pub struct EnabledLibFeature {
pub gate_name: Symbol,
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/bootstrap/rustc_bootstap.force_stable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: the option `Z` is only accepted on the nightly compiler

help: consider switching to a nightly toolchain: `rustup default nightly`

note: selecting a toolchain with `+toolchain` arguments require a rustup proxy; see <https://rust-lang.github.io/rustup/concepts/index.html>

note: for more information about Rust's stability policy, see <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html#unstable-features>

error: 1 nightly option were parsed

47 changes: 47 additions & 0 deletions tests/ui/bootstrap/rustc_bootstap.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(there's a typo on rustc_bootstap.rs ^^)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Check `RUSTC_BOOTSTRAP`'s behavior in relation to feature stability and what rustc considers
//! itself to be (stable vs non-stable ).
//!
//! `RUSTC_BOOTSTRAP` accepts:
//!
//! - `1`: cheat, allow usage of unstable features even if rustc thinks it is a stable compiler.
//! - `x,y,z`: comma-delimited list of crates.
//! - `-1`: force rustc to think it is a stable compiler.

// ignore-tidy-linelength

//@ revisions: default_nightly cheat cheat_single_crate cheat_multi_crate force_stable invalid_zero invalid_junk
//@ only-nightly

//@[default_nightly] unset-rustc-env:RUSTC_BOOTSTRAP
//@[default_nightly] check-pass

// For a nightly compiler, this is same as `default_nightly` as if `RUSTC_BOOTSTRAP` was unset.
//@[invalid_zero] rustc-env:RUSTC_BOOTSTRAP=0
//@[invalid_zero] check-pass

// Invalid values are silently discarded, same as `default_nightly`, i.e. as if `RUSTC_BOOTSTRAP`
// was unset.
//@[invalid_junk] rustc-env:RUSTC_BOOTSTRAP=*
//@[invalid_junk] check-pass

//@[cheat] rustc-env:RUSTC_BOOTSTRAP=1
//@[cheat] check-pass

//@[cheat_single_crate] rustc-env:RUSTC_BOOTSTRAP=x
//@[cheat_single_crate] check-pass

//@[cheat_multi_crate] rustc-env:RUSTC_BOOTSTRAP=x,y,z
//@[cheat_multi_crate] check-pass

// Note: compiletest passes some `-Z` flags to the compiler for ui testing purposes, so here we
// instead abuse the fact that `-Z unstable-options` is also part of rustc's stability story and is
// also affected by `RUSTC_BOOTSTRAP`.
//@[force_stable] rustc-env:RUSTC_BOOTSTRAP=-1
//@[force_stable] compile-flags: -Z unstable-options
//@[force_stable] regex-error-pattern: error: the option `Z` is only accepted on the nightly compiler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N.B.: this doesn't check in a snapshot because the exact error message will be different between beta/stable/dev depending on channel info.


#![crate_type = "lib"]

// Note: `rustc_attrs` is a perma-unstable internal feature that is unlikely to change, which is
// used as a proxy to check `RUSTC_BOOTSTRAP` versus stability checking logic.
#![feature(rustc_attrs)]
Loading