Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
sess: users cannot set target modifier cfgs
As with all other builtin cfgs, target modifier cfgs cannot be set by
users.
  • Loading branch information
davidtwco committed Feb 3, 2026
commit 280a4b9914bd5ba85a49aa8d585a129898b30da1
47 changes: 34 additions & 13 deletions compiler/rustc_session/src/config/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//! - Add the cfg in [`disallow_cfgs`] to disallow users from setting it via `--cfg`
//! - Add the feature gating in `compiler/rustc_feature/src/builtin_attrs.rs`

use std::borrow::Cow;
use std::hash::Hash;
use std::iter;

Expand All @@ -30,7 +31,7 @@ use rustc_lint_defs::builtin::EXPLICIT_BUILTIN_CFGS_IN_FLAGS;
use rustc_span::{Symbol, sym};
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, Target};

use crate::config::{CrateType, FmtDebug};
use crate::config::{CG_OPTIONS, CrateType, FmtDebug, OptionDesc, OptionPrefix, Z_OPTIONS};
use crate::{Session, errors};

/// The parsed `--cfg` options that define the compilation environment of the
Expand Down Expand Up @@ -115,20 +116,20 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {

for cfg in user_cfgs {
match cfg {
(sym::overflow_checks, None) => disallow(cfg, "-C overflow-checks"),
(sym::debug_assertions, None) => disallow(cfg, "-C debug-assertions"),
(sym::ub_checks, None) => disallow(cfg, "-Z ub-checks"),
(sym::contract_checks, None) => disallow(cfg, "-Z contract-checks"),
(sym::sanitize, None | Some(_)) => disallow(cfg, "-Z sanitizer"),
(sym::overflow_checks, None) => disallow(cfg, Cow::Borrowed("-C overflow-checks")),
(sym::debug_assertions, None) => disallow(cfg, Cow::Borrowed("-C debug-assertions")),
(sym::ub_checks, None) => disallow(cfg, Cow::Borrowed("-Z ub-checks")),
(sym::contract_checks, None) => disallow(cfg, Cow::Borrowed("-Z contract-checks")),
(sym::sanitize, None | Some(_)) => disallow(cfg, Cow::Borrowed("-Z sanitizer")),
(
sym::sanitizer_cfi_generalize_pointers | sym::sanitizer_cfi_normalize_integers,
None | Some(_),
) => disallow(cfg, "-Z sanitizer=cfi"),
(sym::proc_macro, None) => disallow(cfg, "--crate-type proc-macro"),
) => disallow(cfg, Cow::Borrowed("-Z sanitizer=cfi")),
(sym::proc_macro, None) => disallow(cfg, Cow::Borrowed("--crate-type proc-macro")),
(sym::panic, Some(sym::abort | sym::unwind | sym::immediate_abort)) => {
disallow(cfg, "-C panic")
disallow(cfg, Cow::Borrowed("-C panic"))
}
(sym::target_feature, Some(_)) => disallow(cfg, "-C target-feature"),
(sym::target_feature, Some(_)) => disallow(cfg, Cow::Borrowed("-C target-feature")),
(sym::unix, None)
| (sym::windows, None)
| (sym::relocation_model, Some(_))
Expand All @@ -147,12 +148,32 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
| (sym::target_has_reliable_f16_math, None | Some(_))
| (sym::target_has_reliable_f128, None | Some(_))
| (sym::target_has_reliable_f128_math, None | Some(_))
| (sym::target_thread_local, None) => disallow(cfg, "--target"),
(sym::fmt_debug, None | Some(_)) => disallow(cfg, "-Z fmt-debug"),
(sym::emscripten_wasm_eh, None | Some(_)) => disallow(cfg, "-Z emscripten_wasm_eh"),
| (sym::target_thread_local, None) => disallow(cfg, Cow::Borrowed("--target")),
(sym::fmt_debug, None | Some(_)) => disallow(cfg, Cow::Borrowed("-Z fmt-debug")),
(sym::emscripten_wasm_eh, None | Some(_)) => {
disallow(cfg, Cow::Borrowed("-Z emscripten_wasm_eh"))
}
_ => {}
}
}

// Also prohibit setting any target modifier cfgs, these all have corresponding flags.
fn target_modifiers_with_cfgs(
desc: &OptionDesc<impl OptionPrefix>,
) -> Option<(String, Symbol)> {
desc.target_modifier_cfg().map(|sym| (desc.flag(), sym))
}
let check_target_modifier_with_cfg = |flag, flag_cfg| {
if let Some(matching_cfg) = user_cfgs.iter().find(|(cfg, _)| *cfg == flag_cfg) {
disallow(matching_cfg, Cow::Owned(flag));
}
};
for (flag, cfg) in CG_OPTIONS.iter().filter_map(target_modifiers_with_cfgs) {
check_target_modifier_with_cfg(flag, cfg);
}
for (flag, cfg) in Z_OPTIONS.iter().filter_map(target_modifiers_with_cfgs) {
check_target_modifier_with_cfg(flag, cfg);
}
}

/// Generate the default configs for a given session
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::num::{NonZero, ParseIntError};

use rustc_ast::token;
Expand Down Expand Up @@ -514,5 +515,5 @@ pub(crate) struct SoftFloatDeprecated;
pub(crate) struct UnexpectedBuiltinCfg {
pub(crate) cfg: String,
pub(crate) cfg_name: Symbol,
pub(crate) controlled_by: &'static str,
pub(crate) controlled_by: Cow<'static, str>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_modifier_branch_protection="bti"` flag
|
= note: config `target_modifier_branch_protection` is only supposed to be controlled by `-Z branch-protection`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

8 changes: 8 additions & 0 deletions tests/ui/cfg/target-modifiers/user-sets.fixed_x18.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_modifier_fixed_x18` flag
|
= note: config `target_modifier_fixed_x18` is only supposed to be controlled by `-Z fixed-x18`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_modifier_indirect_branch_cs_prefix` flag
|
= note: config `target_modifier_indirect_branch_cs_prefix` is only supposed to be controlled by `-Z indirect-branch-cs-prefix`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_modifier_reg_struct_return` flag
|
= note: config `target_modifier_reg_struct_return` is only supposed to be controlled by `-Z reg-struct-return`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

8 changes: 8 additions & 0 deletions tests/ui/cfg/target-modifiers/user-sets.regparm.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_modifier_regparm="0"` flag
|
= note: config `target_modifier_regparm` is only supposed to be controlled by `-Z regparm`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

8 changes: 8 additions & 0 deletions tests/ui/cfg/target-modifiers/user-sets.retpoline.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_modifier_retpoline` flag
|
= note: config `target_modifier_retpoline` is only supposed to be controlled by `-Z retpoline`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_modifier_retpoline_external_thunk` flag
|
= note: config `target_modifier_retpoline_external_thunk` is only supposed to be controlled by `-Z retpoline-external-thunk`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

14 changes: 14 additions & 0 deletions tests/ui/cfg/target-modifiers/user-sets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ check-fail
//@ compile-flags: --crate-type=lib
//@ revisions: fixed_x18 indirect_branch_cs_prefix regparm
//@ revisions: reg_struct_return retpoline retpoline_external_thunk
//@[fixed_x18] compile-flags: --cfg target_modifier_fixed_x18
//@[indirect_branch_cs_prefix] compile-flags: --cfg target_modifier_indirect_branch_cs_prefix
//@[regparm] compile-flags: --cfg target_modifier_regparm="0"
//@[reg_struct_return] compile-flags: --cfg target_modifier_reg_struct_return
//@[retpoline] compile-flags: --cfg target_modifier_retpoline
//@[retpoline_external_thunk] compile-flags: --cfg target_modifier_retpoline_external_thunk

fn main() {}

//~? ERROR unexpected `--cfg