From 3455d66041eb1d766814b57608d7ae8f20c8b04e Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 9 Mar 2023 21:10:08 +0100 Subject: [PATCH] Honor current target when checking conditional compilation values This is fixed by simply using the currently registered target in the current session. We need to use it because of target json that are not by design included in the rustc list of targets. --- compiler/rustc_interface/src/util.rs | 2 +- compiler/rustc_session/src/config.rs | 7 ++++--- tests/ui/check-cfg/my-awesome-platform.json | 12 +++++++++++ tests/ui/check-cfg/values-target-json.rs | 21 ++++++++++++++++++++ tests/ui/check-cfg/values-target-json.stderr | 13 ++++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/ui/check-cfg/my-awesome-platform.json create mode 100644 tests/ui/check-cfg/values-target-json.rs create mode 100644 tests/ui/check-cfg/values-target-json.stderr diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index e5d2fb2ea2807..043892410ceaa 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -110,7 +110,7 @@ pub fn create_session( add_configuration(&mut cfg, &mut sess, &*codegen_backend); let mut check_cfg = config::to_crate_check_config(check_cfg); - check_cfg.fill_well_known(); + check_cfg.fill_well_known(&sess.target); sess.parse_sess.config = cfg; sess.parse_sess.check_config = check_cfg; diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index d4e4ace889b03..485c3f554625a 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1137,7 +1137,7 @@ impl CrateCheckConfig { } /// Fills a `CrateCheckConfig` with well-known configuration values. - fn fill_well_known_values(&mut self) { + fn fill_well_known_values(&mut self, current_target: &Target) { if !self.well_known_values { return; } @@ -1229,6 +1229,7 @@ impl CrateCheckConfig { for target in TARGETS .iter() .map(|target| Target::expect_builtin(&TargetTriple::from_triple(target))) + .chain(iter::once(current_target.clone())) { values_target_os.insert(Symbol::intern(&target.options.os)); values_target_family @@ -1243,9 +1244,9 @@ impl CrateCheckConfig { } } - pub fn fill_well_known(&mut self) { + pub fn fill_well_known(&mut self, current_target: &Target) { self.fill_well_known_names(); - self.fill_well_known_values(); + self.fill_well_known_values(current_target); } } diff --git a/tests/ui/check-cfg/my-awesome-platform.json b/tests/ui/check-cfg/my-awesome-platform.json new file mode 100644 index 0000000000000..5e9ab8f1a2d00 --- /dev/null +++ b/tests/ui/check-cfg/my-awesome-platform.json @@ -0,0 +1,12 @@ +{ + "llvm-target": "x86_64-unknown-none-gnu", + "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", + "arch": "x86_64", + "target-endian": "little", + "target-pointer-width": "64", + "target-c-int-width": "32", + "os": "ericos", + "linker-flavor": "ld.lld", + "linker": "rust-lld", + "executables": true +} diff --git a/tests/ui/check-cfg/values-target-json.rs b/tests/ui/check-cfg/values-target-json.rs new file mode 100644 index 0000000000000..2ef5a44592b81 --- /dev/null +++ b/tests/ui/check-cfg/values-target-json.rs @@ -0,0 +1,21 @@ +// This test checks that we don't lint values defined by a custom target (target json) +// +// check-pass +// needs-llvm-components: x86 +// compile-flags: --crate-type=lib --check-cfg=values() --target={{src-base}}/check-cfg/my-awesome-platform.json -Z unstable-options + +#![feature(lang_items, no_core, auto_traits)] +#![no_core] + +#[lang = "sized"] +trait Sized {} + +#[cfg(target_os = "linuz")] +//~^ WARNING unexpected `cfg` condition value +fn target_os_linux_misspell() {} + +#[cfg(target_os = "linux")] +fn target_os_linux() {} + +#[cfg(target_os = "ericos")] +fn target_os_ericos() {} diff --git a/tests/ui/check-cfg/values-target-json.stderr b/tests/ui/check-cfg/values-target-json.stderr new file mode 100644 index 0000000000000..b58d2970773b8 --- /dev/null +++ b/tests/ui/check-cfg/values-target-json.stderr @@ -0,0 +1,13 @@ +warning: unexpected `cfg` condition value + --> $DIR/values-target-json.rs:13:7 + | +LL | #[cfg(target_os = "linuz")] + | ^^^^^^^^^^^^------- + | | + | help: did you mean: `"linux"` + | + = note: expected values for `target_os` are: aix, android, cuda, dragonfly, emscripten, ericos, espidf, freebsd, fuchsia, haiku, hermit, horizon, illumos, ios, l4re, linux, macos, netbsd, none, nto, openbsd, psp, redox, solaris, solid_asp3, tvos, uefi, unknown, vita, vxworks, wasi, watchos, windows, xous + = note: `#[warn(unexpected_cfgs)]` on by default + +warning: 1 warning emitted +