Skip to content

Commit 4b1e14f

Browse files
committed
Disallow setting some built-in cfg via set the command-line
1 parent 1be24d7 commit 4b1e14f

25 files changed

+198
-1
lines changed

compiler/rustc_session/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ session_crate_name_empty = crate name must not be empty
1414
1515
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
1616
17+
session_disallow_cli_cfg = unexpected `--cfg {$cfg}` flag
18+
.controlled_by = config `{$cfg_name}` is only supposed to be controlled by `{$controlled_by}`
19+
.issue = see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
20+
1721
session_expr_parentheses_needed = parentheses are required to parse this as an expression
1822
1923
session_failed_to_create_profiler = failed to create profiler: {$err}

compiler/rustc_session/src/config.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,10 @@ pub(crate) const fn default_lib_output() -> CrateType {
12921292
}
12931293

12941294
pub fn build_configuration(sess: &Session, mut user_cfg: Cfg) -> Cfg {
1295-
// Combine the configuration requested by the session (command line) with
1295+
// First disallow some configuration given on the command line
1296+
cfg::disallow_cfgs(sess, &user_cfg);
1297+
1298+
// Then combine the configuration requested by the session (command line) with
12961299
// some default and generated configuration items.
12971300
user_cfg.extend(cfg::default_configuration(sess));
12981301
user_cfg

compiler/rustc_session/src/config/cfg.rs

+52
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet};
2626
use rustc_target::spec::{Target, TargetTriple, TARGETS};
2727

2828
use crate::config::CrateType;
29+
use crate::errors::DisallowConfig;
2930
use crate::Session;
3031

3132
use std::hash::Hash;
@@ -379,3 +380,54 @@ impl CheckCfg {
379380
ins!(sym::windows, no_values);
380381
}
381382
}
383+
384+
pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
385+
let disallow = |cfg: &(Symbol, Option<Symbol>), controlled_by| {
386+
let cfg_name = cfg.0;
387+
let cfg = if let Some(value) = cfg.1 {
388+
format!(r#"{}="{}""#, cfg_name, value)
389+
} else {
390+
format!("{}", cfg_name)
391+
};
392+
sess.dcx().emit_fatal(DisallowConfig { cfg, cfg_name, controlled_by })
393+
};
394+
395+
// We want to restrict setting cfgs that will produce "incoherent" behavior between
396+
// the cfg and the "real" flag that sets it, but not all cfgs produce incoherent
397+
// behavior, we therefore exclude those cfgs:
398+
//
399+
// - test
400+
// - clippy
401+
// - doc
402+
// - doctest
403+
// - miri
404+
// - rustfmt
405+
// - overflow_checks
406+
// - ub_checks
407+
408+
for cfg in user_cfgs {
409+
match cfg {
410+
(sym::debug_assertions, None) => disallow(cfg, "-C debug_assertions"),
411+
(sym::proc_macro, None) => disallow(cfg, "--crate-type proc-macro"),
412+
(sym::panic, Some(sym::abort)) => disallow(cfg, "-C panic"),
413+
(sym::panic, Some(sym::unwind)) => disallow(cfg, "-C panic"),
414+
(sym::target_feature, Some(_)) => disallow(cfg, "-C target-feature"),
415+
(sym::unix, None)
416+
| (sym::windows, None)
417+
| (sym::relocation_model, Some(_))
418+
| (sym::target_abi, None | Some(_))
419+
| (sym::target_arch, Some(_))
420+
| (sym::target_endian, Some(_))
421+
| (sym::target_env, None | Some(_))
422+
| (sym::target_family, Some(_))
423+
| (sym::target_os, Some(_))
424+
| (sym::target_pointer_width, Some(_))
425+
| (sym::target_vendor, None | Some(_))
426+
| (sym::target_has_atomic, Some(_))
427+
| (sym::target_has_atomic_equal_alignment, Some(_))
428+
| (sym::target_has_atomic_load_store, Some(_))
429+
| (sym::target_thread_local, None) => disallow(cfg, "--target"),
430+
_ => {}
431+
}
432+
}
433+
}

compiler/rustc_session/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,13 @@ pub(crate) struct FunctionReturnThunkExternRequiresNonLargeCodeModel;
472472
pub(crate) struct FailedToCreateProfiler {
473473
pub(crate) err: String,
474474
}
475+
476+
#[derive(Diagnostic)]
477+
#[diag(session_disallow_cli_cfg)]
478+
#[note(session_controlled_by)]
479+
#[note(session_issue)]
480+
pub(crate) struct DisallowConfig {
481+
pub(crate) cfg: String,
482+
pub(crate) cfg_name: Symbol,
483+
pub(crate) controlled_by: &'static str,
484+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg debug_assertions` flag
2+
|
3+
= note: config `debug_assertions` is only supposed to be controlled by `-C debug_assertions`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg panic="abort"` flag
2+
|
3+
= note: config `panic` is only supposed to be controlled by `-C panic`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg proc_macro` flag
2+
|
3+
= note: config `proc_macro` is only supposed to be controlled by `--crate-type proc-macro`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg relocation_model="a"` flag
2+
|
3+
= note: config `relocation_model` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+

tests/ui/cfg/disallowed-cli-cfgs.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ check-fail
2+
//@ revisions: debug_assertions proc_macro panic target_feature unix windows target_abi
3+
//@ revisions: target_arch target_endian target_env target_family target_os target_pointer_width
4+
//@ revisions: target_vendor target_has_atomic target_has_atomic_equal_alignment
5+
//@ revisions: target_has_atomic_load_store target_thread_local relocation_model
6+
7+
//@ [debug_assertions]compile-flags: --cfg debug_assertions
8+
//@ [proc_macro]compile-flags: --cfg proc_macro
9+
//@ [panic]compile-flags: --cfg panic="abort"
10+
//@ [target_feature]compile-flags: --cfg target_feature="sse3"
11+
//@ [unix]compile-flags: --cfg unix
12+
//@ [windows]compile-flags: --cfg windows
13+
//@ [target_abi]compile-flags: --cfg target_abi="gnu"
14+
//@ [target_arch]compile-flags: --cfg target_arch="arm"
15+
//@ [target_endian]compile-flags: --cfg target_endian="little"
16+
//@ [target_env]compile-flags: --cfg target_env
17+
//@ [target_family]compile-flags: --cfg target_family="unix"
18+
//@ [target_os]compile-flags: --cfg target_os="linux"
19+
//@ [target_pointer_width]compile-flags: --cfg target_pointer_width="32"
20+
//@ [target_vendor]compile-flags: --cfg target_vendor
21+
//@ [target_has_atomic]compile-flags: --cfg target_has_atomic="32"
22+
//@ [target_has_atomic_equal_alignment]compile-flags: --cfg target_has_atomic_equal_alignment="32"
23+
//@ [target_has_atomic_load_store]compile-flags: --cfg target_has_atomic_load_store="32"
24+
//@ [target_thread_local]compile-flags: --cfg target_thread_local
25+
//@ [relocation_model]compile-flags: --cfg relocation_model="a"
26+
27+
28+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_abi` flag
2+
|
3+
= note: config `target_abi` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_arch="arm"` flag
2+
|
3+
= note: config `target_arch` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_endian="little"` flag
2+
|
3+
= note: config `target_endian` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_env` flag
2+
|
3+
= note: config `target_env` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_family="unix"` flag
2+
|
3+
= note: config `target_family` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_feature="sse3"` flag
2+
|
3+
= note: config `target_feature` is only supposed to be controlled by `-C target-feature`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_has_atomic="32"` flag
2+
|
3+
= note: config `target_has_atomic` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_has_atomic_equal_alignment="32"` flag
2+
|
3+
= note: config `target_has_atomic_equal_alignment` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_has_atomic_load_store="32"` flag
2+
|
3+
= note: config `target_has_atomic_load_store` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_os="linux"` flag
2+
|
3+
= note: config `target_os` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_pointer_width="32"` flag
2+
|
3+
= note: config `target_pointer_width` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_thread_local` flag
2+
|
3+
= note: config `target_thread_local` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg target_vendor` flag
2+
|
3+
= note: config `target_vendor` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg test` flag
2+
|
3+
= note: config `test` is only supposed to be controlled by `--test`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg unix` flag
2+
|
3+
= note: config `unix` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unexpected `--cfg windows` flag
2+
|
3+
= note: config `windows` is only supposed to be controlled by `--target`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+

0 commit comments

Comments
 (0)