Skip to content

Commit

Permalink
Disallow setting some built-in cfg via set the command-line
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Jun 8, 2024
1 parent 1be24d7 commit 37aa414
Show file tree
Hide file tree
Showing 25 changed files with 197 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/rustc_session/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ session_crate_name_empty = crate name must not be empty
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
session_disallow_cli_cfg = unexpected `--cfg {$cfg}` flag
.controlled_by = config `{$cfg_name}` is only supposed to be controlled by `{$controlled_by}`
.issue = see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
session_expr_parentheses_needed = parentheses are required to parse this as an expression
session_failed_to_create_profiler = failed to create profiler: {$err}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,10 @@ pub(crate) const fn default_lib_output() -> CrateType {
}

pub fn build_configuration(sess: &Session, mut user_cfg: Cfg) -> Cfg {
// Combine the configuration requested by the session (command line) with
// First disallow some configuration given on the command line
cfg::disallow_cfgs(sess, &user_cfg);

// Then combine the configuration requested by the session (command line) with
// some default and generated configuration items.
user_cfg.extend(cfg::default_configuration(sess));
user_cfg
Expand Down
52 changes: 52 additions & 0 deletions compiler/rustc_session/src/config/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet};
use rustc_target::spec::{Target, TargetTriple, TARGETS};

use crate::config::CrateType;
use crate::errors::DisallowConfig;
use crate::Session;

use std::hash::Hash;
Expand Down Expand Up @@ -379,3 +380,54 @@ impl CheckCfg {
ins!(sym::windows, no_values);
}
}

pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
let disallow = |cfg: &(Symbol, Option<Symbol>), controlled_by| {
let cfg_name = cfg.0;
let cfg = if let Some(value) = cfg.1 {
format!(r#"{}="{}""#, cfg_name, value)
} else {
format!("{}", cfg_name)
};
sess.dcx().emit_fatal(DisallowConfig { cfg, cfg_name, controlled_by })
};

// We want to restrict setting cfgs that will produce "incoherent" behavior between
// the cfg and the "real" flag that sets it, but not all cfgs produce incoherent
// behavior, we therefore exclude those cfgs:
//
// - test
// - clippy
// - doc
// - doctest
// - miri
// - rustfmt
// - overflow_checks
// - debug_assertions
// - ub_checks

for cfg in user_cfgs {
match cfg {
(sym::proc_macro, None) => disallow(cfg, "--crate-type proc-macro"),
(sym::panic, Some(sym::abort)) => disallow(cfg, "-C panic"),
(sym::panic, Some(sym::unwind)) => disallow(cfg, "-C panic"),
(sym::target_feature, Some(_)) => disallow(cfg, "-C target-feature"),
(sym::unix, None)
| (sym::windows, None)
| (sym::relocation_model, Some(_))
| (sym::target_abi, None | Some(_))
| (sym::target_arch, Some(_))
| (sym::target_endian, Some(_))
| (sym::target_env, None | Some(_))
| (sym::target_family, Some(_))
| (sym::target_os, Some(_))
| (sym::target_pointer_width, Some(_))
| (sym::target_vendor, None | Some(_))
| (sym::target_has_atomic, Some(_))
| (sym::target_has_atomic_equal_alignment, Some(_))
| (sym::target_has_atomic_load_store, Some(_))
| (sym::target_thread_local, None) => disallow(cfg, "--target"),
_ => {}
}
}
}
10 changes: 10 additions & 0 deletions compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,13 @@ pub(crate) struct FunctionReturnThunkExternRequiresNonLargeCodeModel;
pub(crate) struct FailedToCreateProfiler {
pub(crate) err: String,
}

#[derive(Diagnostic)]
#[diag(session_disallow_cli_cfg)]
#[note(session_controlled_by)]
#[note(session_issue)]
pub(crate) struct DisallowConfig {
pub(crate) cfg: String,
pub(crate) cfg_name: Symbol,
pub(crate) controlled_by: &'static str,
}
5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.debug_assertions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg debug_assertions` flag
|
= note: config `debug_assertions` is only supposed to be controlled by `-C debug_assertions`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.panic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg panic="abort"` flag
|
= note: config `panic` is only supposed to be controlled by `-C panic`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.proc_macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg proc_macro` flag
|
= note: config `proc_macro` is only supposed to be controlled by `--crate-type proc-macro`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.relocation_model.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg relocation_model="a"` flag
|
= note: config `relocation_model` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

27 changes: 27 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//@ check-fail
//@ revisions: proc_macro panic target_feature unix windows target_abi
//@ revisions: target_arch target_endian target_env target_family target_os target_pointer_width
//@ revisions: target_vendor target_has_atomic target_has_atomic_equal_alignment
//@ revisions: target_has_atomic_load_store target_thread_local relocation_model

//@ [proc_macro]compile-flags: --cfg proc_macro
//@ [panic]compile-flags: --cfg panic="abort"
//@ [target_feature]compile-flags: --cfg target_feature="sse3"
//@ [unix]compile-flags: --cfg unix
//@ [windows]compile-flags: --cfg windows
//@ [target_abi]compile-flags: --cfg target_abi="gnu"
//@ [target_arch]compile-flags: --cfg target_arch="arm"
//@ [target_endian]compile-flags: --cfg target_endian="little"
//@ [target_env]compile-flags: --cfg target_env
//@ [target_family]compile-flags: --cfg target_family="unix"
//@ [target_os]compile-flags: --cfg target_os="linux"
//@ [target_pointer_width]compile-flags: --cfg target_pointer_width="32"
//@ [target_vendor]compile-flags: --cfg target_vendor
//@ [target_has_atomic]compile-flags: --cfg target_has_atomic="32"
//@ [target_has_atomic_equal_alignment]compile-flags: --cfg target_has_atomic_equal_alignment="32"
//@ [target_has_atomic_load_store]compile-flags: --cfg target_has_atomic_load_store="32"
//@ [target_thread_local]compile-flags: --cfg target_thread_local
//@ [relocation_model]compile-flags: --cfg relocation_model="a"


fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_abi.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_abi` flag
|
= note: config `target_abi` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_arch.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_arch="arm"` flag
|
= note: config `target_arch` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_endian.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_endian="little"` flag
|
= note: config `target_endian` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_env.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_env` flag
|
= note: config `target_env` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_family.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_family="unix"` flag
|
= note: config `target_family` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_feature.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_feature="sse3"` flag
|
= note: config `target_feature` is only supposed to be controlled by `-C target-feature`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_has_atomic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_has_atomic="32"` flag
|
= note: config `target_has_atomic` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_has_atomic_equal_alignment="32"` flag
|
= note: config `target_has_atomic_equal_alignment` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_has_atomic_load_store="32"` flag
|
= note: config `target_has_atomic_load_store` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_os.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_os="linux"` flag
|
= note: config `target_os` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_pointer_width.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_pointer_width="32"` flag
|
= note: config `target_pointer_width` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_thread_local.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_thread_local` flag
|
= note: config `target_thread_local` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_vendor.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg target_vendor` flag
|
= note: config `target_vendor` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.test.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg test` flag
|
= note: config `test` is only supposed to be controlled by `--test`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.unix.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg unix` flag
|
= note: config `unix` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

5 changes: 5 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.windows.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unexpected `--cfg windows` flag
|
= note: config `windows` is only supposed to be controlled by `--target`
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information

0 comments on commit 37aa414

Please sign in to comment.