Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow test revisions that conflict with built in cfgs #131930

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/tools/compiletest/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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() })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a FWIW, this parsing is quite primitive, it doesn't handle the --print=check-cfg specifics, fortunately this is only collecting names and shouldn't be in any specific case so it should always work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is only querying for the cfg name, otherwise this can't be used

.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));
Expand Down
1 change: 1 addition & 0 deletions src/tools/compiletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

Expand Down
3 changes: 3 additions & 0 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/lint/lint-missing-doc-expect.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Expand Down
Loading