about summary refs log tree commit diff
diff options
context:
space:
mode:
authorclubby789 <jamie@hill-daniel.co.uk>2024-10-19 14:02:40 +0000
committerclubby789 <jamie@hill-daniel.co.uk>2024-10-23 18:05:27 +0000
commit2e3091d66ccd2e474ac63beaace3813c0f085cfa (patch)
tree2cd6f52d950c36519a4c331c6c6ae5e94ee94047
parentbe01dabfefd2daa4574b974f571c7852085d60cb (diff)
downloadrust-2e3091d66ccd2e474ac63beaace3813c0f085cfa.tar.gz
rust-2e3091d66ccd2e474ac63beaace3813c0f085cfa.zip
Don't allow test revisions that conflict with built in cfgs
-rw-r--r--src/tools/compiletest/src/common.rs18
-rw-r--r--src/tools/compiletest/src/lib.rs1
-rw-r--r--src/tools/compiletest/src/runtest.rs3
-rw-r--r--tests/ui/lint/lint-missing-doc-expect.rs4
4 files changed, 24 insertions, 2 deletions
diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs
index ff059940f7c..3e6423e474a 100644
--- a/src/tools/compiletest/src/common.rs
+++ b/src/tools/compiletest/src/common.rs
@@ -376,6 +376,7 @@ pub struct Config {
     pub only_modified: bool,
 
     pub target_cfgs: OnceLock<TargetCfgs>,
+    pub builtin_cfg_names: OnceLock<HashSet<String>>,
 
     pub nocapture: bool,
 
@@ -440,6 +441,11 @@ impl Config {
         self.target_cfg().panic == PanicStrategy::Unwind
     }
 
+    /// Get the list of builtin, 'well known' cfg names
+    pub fn builtin_cfg_names(&self) -> &HashSet<String> {
+        self.builtin_cfg_names.get_or_init(|| builtin_cfg_names(self))
+    }
+
     pub fn has_threads(&self) -> bool {
         // Wasm targets don't have threads unless `-threads` is in the target
         // name, such as `wasm32-wasip1-threads`.
@@ -651,6 +657,18 @@ pub enum Endian {
     Big,
 }
 
+fn builtin_cfg_names(config: &Config) -> HashSet<String> {
+    rustc_output(
+        config,
+        &["--print=check-cfg", "-Zunstable-options", "--check-cfg=cfg()"],
+        Default::default(),
+    )
+    .lines()
+    .map(|l| if let Some((name, _)) = l.split_once('=') { name.to_string() } else { l.to_string() })
+    .chain(std::iter::once(String::from("test")))
+    .collect()
+}
+
 fn rustc_output(config: &Config, args: &[&str], envs: HashMap<String, String>) -> String {
     let mut command = Command::new(&config.rustc_path);
     add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
diff --git a/src/tools/compiletest/src/lib.rs b/src/tools/compiletest/src/lib.rs
index 7d6ede9bcda..54ba98ffcc4 100644
--- a/src/tools/compiletest/src/lib.rs
+++ b/src/tools/compiletest/src/lib.rs
@@ -356,6 +356,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
         force_rerun: matches.opt_present("force-rerun"),
 
         target_cfgs: OnceLock::new(),
+        builtin_cfg_names: OnceLock::new(),
 
         nocapture: matches.opt_present("nocapture"),
 
diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs
index 36c5106ddad..fc409ba3762 100644
--- a/src/tools/compiletest/src/runtest.rs
+++ b/src/tools/compiletest/src/runtest.rs
@@ -478,6 +478,9 @@ impl<'test> TestCx<'test> {
                     "error: redundant cfg argument `{normalized_revision}` is already created by the revision"
                 );
             }
+            if self.config.builtin_cfg_names().contains(&normalized_revision) {
+                panic!("error: revision `{normalized_revision}` collides with a builtin cfg");
+            }
             cmd.args(cfg_arg);
         }
 
diff --git a/tests/ui/lint/lint-missing-doc-expect.rs b/tests/ui/lint/lint-missing-doc-expect.rs
index 991f65003dc..b1abf8b6962 100644
--- a/tests/ui/lint/lint-missing-doc-expect.rs
+++ b/tests/ui/lint/lint-missing-doc-expect.rs
@@ -1,10 +1,10 @@
 // Make sure that `#[expect(missing_docs)]` is always correctly fulfilled.
 
 //@ check-pass
-//@ revisions: lib bin test
+//@ revisions: lib bin test_
 //@ [lib]compile-flags: --crate-type lib
 //@ [bin]compile-flags: --crate-type bin
-//@ [test]compile-flags: --test
+//@ [test_]compile-flags: --test
 
 #[expect(missing_docs)]
 pub fn foo() {}