Skip to content

Commit 4ebdd5c

Browse files
committed
Disallow setting built-in cfgs via set the command-line
1 parent ff63c14 commit 4ebdd5c

35 files changed

+374
-2
lines changed

compiler/rustc_lint/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,10 @@ lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::Manual
741741
.label = argument has type `{$arg_ty}`
742742
.suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value
743743
744+
lint_unexpected_builtin_cfg = unexpected `--cfg {$cfg}` flag
745+
.controlled_by = config `{$cfg_name}` is only supposed to be controlled by `{$controlled_by}`
746+
.issue = see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
747+
744748
lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_println}` to the top of the `build.rs`
745749
lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead
746750
lint_unexpected_cfg_add_cargo_toml_lint_cfg = or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:{$cargo_toml_lint_cfg}

compiler/rustc_lint/src/context/diagnostics.rs

+3
Original file line numberDiff line numberDiff line change
@@ -424,5 +424,8 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
424424
lints::InnerAttributeUnstable::CustomInnerAttribute
425425
}
426426
.decorate_lint(diag),
427+
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
428+
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
429+
}
427430
}
428431
}

compiler/rustc_lint/src/lints.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,16 @@ pub mod unexpected_cfg_value {
23192319
}
23202320
}
23212321

2322+
#[derive(LintDiagnostic)]
2323+
#[diag(lint_unexpected_builtin_cfg)]
2324+
#[note(lint_controlled_by)]
2325+
#[note(lint_issue)]
2326+
pub struct UnexpectedBuiltinCfg {
2327+
pub(crate) cfg: String,
2328+
pub(crate) cfg_name: Symbol,
2329+
pub(crate) controlled_by: &'static str,
2330+
}
2331+
23222332
#[derive(LintDiagnostic)]
23232333
#[diag(lint_macro_use_deprecated)]
23242334
#[help]

compiler/rustc_lint_defs/src/builtin.rs

+34
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ declare_lint_pass! {
105105
UNCONDITIONAL_RECURSION,
106106
UNCOVERED_PARAM_IN_PROJECTION,
107107
UNDEFINED_NAKED_FUNCTION_ABI,
108+
UNEXPECTED_BUILTIN_CFGS,
108109
UNEXPECTED_CFGS,
109110
UNFULFILLED_LINT_EXPECTATIONS,
110111
UNINHABITED_STATIC,
@@ -3268,6 +3269,39 @@ declare_lint! {
32683269
"detects unexpected names and values in `#[cfg]` conditions",
32693270
}
32703271

3272+
declare_lint! {
3273+
/// The `unexpected_builtin_cfgs` lint detects builtin cfgs set via the CLI, `--cfg`.
3274+
///
3275+
/// ### Example
3276+
///
3277+
/// ```text
3278+
/// rustc --cfg unix
3279+
/// ```
3280+
///
3281+
/// ```rust,ignore (needs command line option)
3282+
/// fn main() {}
3283+
/// ```
3284+
///
3285+
/// This will produce:
3286+
///
3287+
/// ```text
3288+
/// error: unexpected `--cfg unix` flag
3289+
/// |
3290+
/// = note: config `unix` is only supposed to be controlled by `--target`
3291+
/// = note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
3292+
/// = note: `#[deny(unexpected_builtin_cfgs)]` on by default
3293+
/// ```
3294+
///
3295+
/// ### Explanation
3296+
///
3297+
/// Setting builtin cfgs can and does produce incoherent behavior, it's better to the use
3298+
/// the appropriate `rustc` flag that controls the config. For example setting the `windows`
3299+
/// cfg but on Linux based target.
3300+
pub UNEXPECTED_BUILTIN_CFGS,
3301+
Deny,
3302+
"detects builtin cfgs set via the `--cfg`"
3303+
}
3304+
32713305
declare_lint! {
32723306
/// The `repr_transparent_external_private_fields` lint
32733307
/// detects types marked `#[repr(transparent)]` that (transitively)

compiler/rustc_lint_defs/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,11 @@ pub enum BuiltinLintDiag {
742742
InnerAttributeUnstable {
743743
is_macro: bool,
744744
},
745+
UnexpectedBuiltinCfg {
746+
cfg: String,
747+
cfg_name: Symbol,
748+
controlled_by: &'static str,
749+
},
745750
}
746751

747752
/// Lints that are buffered up early on in the `Session` before the

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

+73
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
//! - Add the activation logic in [`default_configuration`]
1818
//! - Add the cfg to [`CheckCfg::fill_well_known`] (and related files),
1919
//! so that the compiler can know the cfg is expected
20+
//! - Add the cfg in [`disallow_cfgs`] to disallow users from setting it via `--cfg`
2021
//! - Add the feature gating in `compiler/rustc_feature/src/builtin_attrs.rs`
2122
23+
use rustc_ast::ast;
2224
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
25+
use rustc_lint_defs::builtin::UNEXPECTED_BUILTIN_CFGS;
26+
use rustc_lint_defs::BuiltinLintDiag;
2327
use rustc_span::symbol::{sym, Symbol};
2428
use rustc_target::abi::Align;
2529
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet};
@@ -83,6 +87,75 @@ impl<'a, T: Eq + Hash + Copy + 'a> Extend<&'a T> for ExpectedValues<T> {
8387
}
8488
}
8589

90+
/// Disallow "builtin" cfgs from the CLI as they produce incoherent behavior
91+
pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
92+
let disallow = |cfg: &(Symbol, Option<Symbol>), controlled_by| {
93+
let cfg_name = cfg.0;
94+
let cfg = if let Some(value) = cfg.1 {
95+
format!(r#"{}="{}""#, cfg_name, value)
96+
} else {
97+
format!("{}", cfg_name)
98+
};
99+
sess.psess.opt_span_buffer_lint(
100+
UNEXPECTED_BUILTIN_CFGS,
101+
None,
102+
ast::CRATE_NODE_ID,
103+
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by },
104+
)
105+
};
106+
107+
// We want to restrict setting cfgs that will produce "incoherent" behavior between
108+
// the cfg and the "real" flag that sets it.
109+
//
110+
// The tests are in tests/ui/cfg/disallowed-cli-cfgs.rs.
111+
//
112+
// Not all "well known cfgs" can be disallowed, as they are set by external tools via the
113+
// CLI, we therefore don't lint on them:
114+
//
115+
// - clippy
116+
// - doc
117+
// - doctest
118+
// - miri
119+
// - rustfmt
120+
//
121+
// doc/doctest: https://github.com/rust-lang/rust/blob/d03d6c0fead582c98c6446ec92456ca8fd03ff65/src/librustdoc/doctest.rs#L134-L135
122+
// clippy: https://github.com/rust-lang/rust/blob/f1b0d54ca9a5ea43950c279985a6e12dcf387de8/src/tools/clippy/src/driver.rs#L276
123+
// miri: https://github.com/rust-lang/rust/blob/d03d6c0fead582c98c6446ec92456ca8fd03ff65/src/tools/miri/src/lib.rs#L161
124+
125+
for cfg in user_cfgs {
126+
match cfg {
127+
(sym::test, None) => disallow(cfg, "--test"),
128+
(sym::overflow_checks, None) => disallow(cfg, "-C overflow-checks"),
129+
(sym::debug_assertions, None) => disallow(cfg, "-C debug-assertions"),
130+
(sym::ub_checks, None) => disallow(cfg, "-Z ub-checks"),
131+
(sym::sanitize, None | Some(_)) => disallow(cfg, "-Z sanitizer"),
132+
(
133+
sym::sanitizer_cfi_generalize_pointers | sym::sanitizer_cfi_normalize_integers,
134+
None | Some(_),
135+
) => disallow(cfg, "-Z sanitizer=cfi"),
136+
(sym::proc_macro, None) => disallow(cfg, "--crate-type proc-macro"),
137+
(sym::panic, Some(sym::abort | sym::unwind)) => disallow(cfg, "-C panic"),
138+
(sym::target_feature, Some(_)) => disallow(cfg, "-C target-feature"),
139+
(sym::unix, None)
140+
| (sym::windows, None)
141+
| (sym::relocation_model, Some(_))
142+
| (sym::target_abi, None | Some(_))
143+
| (sym::target_arch, Some(_))
144+
| (sym::target_endian, Some(_))
145+
| (sym::target_env, None | Some(_))
146+
| (sym::target_family, Some(_))
147+
| (sym::target_os, Some(_))
148+
| (sym::target_pointer_width, Some(_))
149+
| (sym::target_vendor, None | Some(_))
150+
| (sym::target_has_atomic, Some(_))
151+
| (sym::target_has_atomic_equal_alignment, Some(_))
152+
| (sym::target_has_atomic_load_store, Some(_))
153+
| (sym::target_thread_local, None) => disallow(cfg, "--target"),
154+
_ => {}
155+
}
156+
}
157+
}
158+
86159
/// Generate the default configs for a given session
87160
pub(crate) fn default_configuration(sess: &Session) -> Cfg {
88161
let mut ret = Cfg::default();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@ check-pass
2+
//@ compile-flags: --cfg unix -Aunexpected_builtin_cfgs
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `--cfg overflow_checks` flag
2+
|
3+
= note: config `overflow_checks` is only supposed to be controlled by `-C overflow-checks`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+

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

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ check-fail
2+
//@ revisions: test_ overflow_checks_ debug_assertions_ ub_checks_ sanitize_
3+
//@ revisions: sanitizer_cfi_generalize_pointers_ sanitizer_cfi_normalize_integers_
4+
//@ revisions: proc_macro_ panic_ target_feature_ unix_ windows_ target_abi_
5+
//@ revisions: target_arch_ target_endian_ target_env_ target_family_ target_os_
6+
//@ revisions: target_pointer_width_ target_vendor_ target_has_atomic_
7+
//@ revisions: target_has_atomic_equal_alignment_ target_has_atomic_load_store_
8+
//@ revisions: target_thread_local_ relocation_model_
9+
10+
//@ [test_]compile-flags: --cfg test
11+
//@ [overflow_checks_]compile-flags: --cfg overflow_checks
12+
//@ [debug_assertions_]compile-flags: --cfg debug_assertions
13+
//@ [ub_checks_]compile-flags: --cfg ub_checks
14+
//@ [sanitize_]compile-flags: --cfg sanitize="cfi"
15+
//@ [sanitizer_cfi_generalize_pointers_]compile-flags: --cfg sanitizer_cfi_generalize_pointers
16+
//@ [sanitizer_cfi_normalize_integers_]compile-flags: --cfg sanitizer_cfi_normalize_integers
17+
//@ [proc_macro_]compile-flags: --cfg proc_macro
18+
//@ [panic_]compile-flags: --cfg panic="abort"
19+
//@ [target_feature_]compile-flags: --cfg target_feature="sse3"
20+
//@ [unix_]compile-flags: --cfg unix
21+
//@ [windows_]compile-flags: --cfg windows
22+
//@ [target_abi_]compile-flags: --cfg target_abi="gnu"
23+
//@ [target_arch_]compile-flags: --cfg target_arch="arm"
24+
//@ [target_endian_]compile-flags: --cfg target_endian="little"
25+
//@ [target_env_]compile-flags: --cfg target_env
26+
//@ [target_family_]compile-flags: --cfg target_family="unix"
27+
//@ [target_os_]compile-flags: --cfg target_os="linux"
28+
//@ [target_pointer_width_]compile-flags: --cfg target_pointer_width="32"
29+
//@ [target_vendor_]compile-flags: --cfg target_vendor
30+
//@ [target_has_atomic_]compile-flags: --cfg target_has_atomic="32"
31+
//@ [target_has_atomic_equal_alignment_]compile-flags: --cfg target_has_atomic_equal_alignment="32"
32+
//@ [target_has_atomic_load_store_]compile-flags: --cfg target_has_atomic_load_store="32"
33+
//@ [target_thread_local_]compile-flags: --cfg target_thread_local
34+
//@ [relocation_model_]compile-flags: --cfg relocation_model="a"
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `--cfg sanitize="cfi"` flag
2+
|
3+
= note: config `sanitize` is only supposed to be controlled by `-Z sanitizer`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `--cfg sanitizer_cfi_generalize_pointers` flag
2+
|
3+
= note: config `sanitizer_cfi_generalize_pointers` is only supposed to be controlled by `-Z sanitizer=cfi`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `--cfg sanitizer_cfi_normalize_integers` flag
2+
|
3+
= note: config `sanitizer_cfi_normalize_integers` is only supposed to be controlled by `-Z sanitizer=cfi`
4+
= note: see <https://github.com/rust-lang/rust/issues/xxxxx> for more information
5+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `--cfg target_abi="gnu"` 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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
= note: `#[deny(unexpected_builtin_cfgs)]` on by default
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)