Skip to content

Commit

Permalink
Rollup merge of #133159 - Zalathar:unstable-options-no-value, r=jieyouxu
Browse files Browse the repository at this point in the history
Don't allow `-Zunstable-options` to take a value

Passing an explicit boolean value (`-Zunstable-options=on`, `off` etc.) sometimes appears to work, but actually puts the compiler into an unintended state where unstable _options_ are still forbidden, but unstable values of _some_ stable options are allowed.

This is a result of `-Zunstable-options` being checked in multiple different places, in slightly different ways. Fixing the checks in `config::nightly_options` to understand boolean values would be non-trivial, so for now it's easier to make things consistent by forbidding values in the `-Z` parser.

---

There were a few uses of this in tests, which happened to work because they were tests of unstable values.
  • Loading branch information
compiler-errors authored Nov 23, 2024
2 parents 7b40a9b + 660246b commit bb92131
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
48 changes: 29 additions & 19 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn build_options<O: Default>(

#[allow(non_upper_case_globals)]
mod desc {
pub(crate) const parse_no_flag: &str = "no value";
pub(crate) const parse_no_value: &str = "no value";
pub(crate) const parse_bool: &str =
"one of: `y`, `yes`, `on`, `true`, `n`, `no`, `off` or `false`";
pub(crate) const parse_opt_bool: &str = parse_bool;
Expand Down Expand Up @@ -462,14 +462,18 @@ pub mod parse {
pub(crate) use super::*;
pub(crate) const MAX_THREADS_CAP: usize = 256;

/// This is for boolean options that don't take a value and start with
/// `no-`. This style of option is deprecated.
pub(crate) fn parse_no_flag(slot: &mut bool, v: Option<&str>) -> bool {
/// This is for boolean options that don't take a value, and are true simply
/// by existing on the command-line.
///
/// This style of option is deprecated, and is mainly used by old options
/// beginning with `no-`.
pub(crate) fn parse_no_value(slot: &mut bool, v: Option<&str>) -> bool {
match v {
None => {
*slot = true;
true
}
// Trying to specify a value is always forbidden.
Some(_) => false,
}
}
Expand Down Expand Up @@ -1609,16 +1613,16 @@ options! {
"perform LLVM link-time optimizations"),
metadata: Vec<String> = (Vec::new(), parse_list, [TRACKED],
"metadata to mangle symbol names with"),
no_prepopulate_passes: bool = (false, parse_no_flag, [TRACKED],
no_prepopulate_passes: bool = (false, parse_no_value, [TRACKED],
"give an empty list of passes to the pass manager"),
no_redzone: Option<bool> = (None, parse_opt_bool, [TRACKED],
"disable the use of the redzone"),
#[rustc_lint_opt_deny_field_access("documented to do nothing")]
no_stack_check: bool = (false, parse_no_flag, [UNTRACKED],
no_stack_check: bool = (false, parse_no_value, [UNTRACKED],
"this option is deprecated and does nothing"),
no_vectorize_loops: bool = (false, parse_no_flag, [TRACKED],
no_vectorize_loops: bool = (false, parse_no_value, [TRACKED],
"disable loop vectorization optimization passes"),
no_vectorize_slp: bool = (false, parse_no_flag, [TRACKED],
no_vectorize_slp: bool = (false, parse_no_value, [TRACKED],
"disable LLVM's SLP vectorization pass"),
opt_level: String = ("0".to_string(), parse_string, [TRACKED],
"optimization level (0-3, s, or z; default: 0)"),
Expand Down Expand Up @@ -1915,25 +1919,25 @@ options! {
"dump facts from NLL analysis into side files (default: no)"),
nll_facts_dir: String = ("nll-facts".to_string(), parse_string, [UNTRACKED],
"the directory the NLL facts are dumped into (default: `nll-facts`)"),
no_analysis: bool = (false, parse_no_flag, [UNTRACKED],
no_analysis: bool = (false, parse_no_value, [UNTRACKED],
"parse and expand the source, but run no analysis"),
no_codegen: bool = (false, parse_no_flag, [TRACKED_NO_CRATE_HASH],
no_codegen: bool = (false, parse_no_value, [TRACKED_NO_CRATE_HASH],
"run all passes except codegen; no output"),
no_generate_arange_section: bool = (false, parse_no_flag, [TRACKED],
no_generate_arange_section: bool = (false, parse_no_value, [TRACKED],
"omit DWARF address ranges that give faster lookups"),
no_implied_bounds_compat: bool = (false, parse_bool, [TRACKED],
"disable the compatibility version of the `implied_bounds_ty` query"),
no_jump_tables: bool = (false, parse_no_flag, [TRACKED],
no_jump_tables: bool = (false, parse_no_value, [TRACKED],
"disable the jump tables and lookup tables that can be generated from a switch case lowering"),
no_leak_check: bool = (false, parse_no_flag, [UNTRACKED],
no_leak_check: bool = (false, parse_no_value, [UNTRACKED],
"disable the 'leak check' for subtyping; unsound, but useful for tests"),
no_link: bool = (false, parse_no_flag, [TRACKED],
no_link: bool = (false, parse_no_value, [TRACKED],
"compile without linking"),
no_parallel_backend: bool = (false, parse_no_flag, [UNTRACKED],
no_parallel_backend: bool = (false, parse_no_value, [UNTRACKED],
"run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO)"),
no_profiler_runtime: bool = (false, parse_no_flag, [TRACKED],
no_profiler_runtime: bool = (false, parse_no_value, [TRACKED],
"prevent automatic injection of the profiler_builtins crate"),
no_trait_vptr: bool = (false, parse_no_flag, [TRACKED],
no_trait_vptr: bool = (false, parse_no_value, [TRACKED],
"disable generation of trait vptr in vtable for upcasting"),
no_unique_section_names: bool = (false, parse_bool, [TRACKED],
"do not use unique names for text and data sections when -Z function-sections is used"),
Expand Down Expand Up @@ -1991,7 +1995,7 @@ options! {
proc_macro_execution_strategy: ProcMacroExecutionStrategy = (ProcMacroExecutionStrategy::SameThread,
parse_proc_macro_execution_strategy, [UNTRACKED],
"how to run proc-macro code (default: same-thread)"),
profile_closures: bool = (false, parse_no_flag, [UNTRACKED],
profile_closures: bool = (false, parse_no_value, [UNTRACKED],
"profile size of closures"),
profile_sample_use: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
"use the given `.prof` file for sampled profile-guided optimization (also known as AutoFDO)"),
Expand Down Expand Up @@ -2165,8 +2169,14 @@ written to standard error output)"),
"enable unsound and buggy MIR optimizations (default: no)"),
/// This name is kind of confusing: Most unstable options enable something themselves, while
/// this just allows "normal" options to be feature-gated.
///
/// The main check for `-Zunstable-options` takes place separately from the
/// usual parsing of `-Z` options (see [`crate::config::nightly_options`]),
/// so this boolean value is mostly used for enabling unstable _values_ of
/// stable options. That separate check doesn't handle boolean values, so
/// to avoid an inconsistent state we also forbid them here.
#[rustc_lint_opt_deny_field_access("use `Session::unstable_options` instead of this field")]
unstable_options: bool = (false, parse_bool, [UNTRACKED],
unstable_options: bool = (false, parse_no_value, [UNTRACKED],
"adds unstable command line options to rustc interface (default: no)"),
use_ctors_section: Option<bool> = (None, parse_opt_bool, [TRACKED],
"use legacy .ctors section for initializers rather than .init_array"),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/codemap_tests/huge_multispan_highlight.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ revisions: ascii unicode
//@ compile-flags: --color=always
//@[ascii] compile-flags: --error-format=human
//@[unicode] compile-flags: -Zunstable-options=yes --error-format=human-unicode
//@[unicode] compile-flags: -Zunstable-options --error-format=human-unicode
//@ ignore-windows
fn main() {
let _ = match true {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/diagnostic-width/E0271.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ revisions: ascii unicode
//@[ascii] compile-flags: --diagnostic-width=40
//@[unicode] compile-flags: -Zunstable-options=yes --error-format=human-unicode --diagnostic-width=40
//@[unicode] compile-flags: -Zunstable-options --error-format=human-unicode --diagnostic-width=40
//@ normalize-stderr-test: "long-type-\d+" -> "long-type-hash"
trait Future {
type Error;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/diagnostic-width/flag-human.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ revisions: ascii unicode
//@[ascii] compile-flags: --diagnostic-width=20
//@[unicode] compile-flags: -Zunstable-options=yes --error-format=human-unicode --diagnostic-width=20
//@[unicode] compile-flags: -Zunstable-options --error-format=human-unicode --diagnostic-width=20

// This test checks that `-Z output-width` effects the human error output by restricting it to an
// arbitrarily low value so that the effect is visible.
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/diagnostic-width/long-E0308.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ revisions: ascii unicode
//@[ascii] compile-flags: --diagnostic-width=60 -Zwrite-long-types-to-disk=yes
//@[unicode] compile-flags: -Zunstable-options=yes --json=diagnostic-unicode --diagnostic-width=60 -Zwrite-long-types-to-disk=yes
//@[unicode] compile-flags: -Zunstable-options --json=diagnostic-unicode --diagnostic-width=60 -Zwrite-long-types-to-disk=yes
//@ normalize-stderr-test: "long-type-\d+" -> "long-type-hash"

mod a {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ revisions: ascii unicode
//@[unicode] compile-flags: -Zunstable-options=yes --error-format=human-unicode
//@[unicode] compile-flags: -Zunstable-options --error-format=human-unicode
// ignore-tidy-linelength

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/diagnostic-width/non-whitespace-trimming-2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ revisions: ascii unicode
//@[unicode] compile-flags: -Zunstable-options=yes --error-format=human-unicode
//@[unicode] compile-flags: -Zunstable-options --error-format=human-unicode
// ignore-tidy-linelength

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-emitter/unicode-output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ compile-flags: -Zunstable-options=yes --error-format=human-unicode --color=always
//@ compile-flags: -Zunstable-options --error-format=human-unicode --color=always
//@ edition:2018
//@ only-linux

Expand Down

0 comments on commit bb92131

Please sign in to comment.