diff --git a/crates/rustc_plugin/src/cli.rs b/crates/rustc_plugin/src/cli.rs index c8d6e8068..bb6abe119 100644 --- a/crates/rustc_plugin/src/cli.rs +++ b/crates/rustc_plugin/src/cli.rs @@ -168,9 +168,13 @@ fn only_run_on_file( // Add compile filter to specify the target corresponding to the given file cmd.arg("-p").arg(format!("{}:{}", pkg.name, pkg.version)); + // See https://doc.rust-lang.org/cargo/commands/cargo-check.html#target-selection for possible compile kinds enum CompileKind { Lib, Bin, + Example, + Test, + Bench, ProcMacro, } @@ -181,6 +185,9 @@ fn only_run_on_file( "lib" | "rlib" | "dylib" | "staticlib" | "cdylib" => CompileKind::Lib, "bin" => CompileKind::Bin, "proc-macro" => CompileKind::ProcMacro, + "example" => CompileKind::Example, + "test" => CompileKind::Test, + "bench" => CompileKind::Bench, _ => unreachable!("unexpected cargo crate type: {kind_str}"), }; @@ -207,10 +214,37 @@ fn only_run_on_file( cmd.args(["--bin", &target.name]); } CompileKind::ProcMacro => {} + CompileKind::Example => { + cmd.args(["--example", &target.name]); + } + CompileKind::Test => { + cmd.args(["--test", &target.name]); + } + CompileKind::Bench => { + cmd.args(["--bench", &target.name]); + } } - cmd.env(SPECIFIC_CRATE, pkg.name.replace('-', "_")); - cmd.env(SPECIFIC_TARGET, kind_str); + cmd.env( + SPECIFIC_CRATE, + match kind { + CompileKind::Lib => &pkg.name, + CompileKind::Bin => &pkg.name, + CompileKind::Example => &target.name, + CompileKind::Test => &target.name, + CompileKind::Bench => &target.name, + CompileKind::ProcMacro => &pkg.name, + } + .replace('-', "_"), + ); + cmd.env( + SPECIFIC_TARGET, + if matches!(kind, CompileKind::Bench | CompileKind::Example) { + "bin" + } else { + kind_str + }, + ); log::debug!( "Package: {}, target kind {}, target name {}", diff --git a/crates/rustc_plugin/src/driver.rs b/crates/rustc_plugin/src/driver.rs index f9ea4dbc6..8cfeb7706 100644 --- a/crates/rustc_plugin/src/driver.rs +++ b/crates/rustc_plugin/src/driver.rs @@ -138,13 +138,7 @@ pub fn driver_main(plugin: T) { let primary_package = env::var("CARGO_PRIMARY_PACKAGE").is_ok(); let run_on_all_crates = env::var(RUN_ON_ALL_CRATES).is_ok(); let normal_rustc = arg_value(&args, "--print", |_| true).is_some(); - let is_target_crate = match (env::var(SPECIFIC_CRATE), env::var(SPECIFIC_TARGET)) { - (Ok(krate), Ok(target)) => { - arg_value(&args, "--crate-name", |name| name == krate).is_some() - && arg_value(&args, "--crate-type", |name| name == target).is_some() - } - _ => true, - }; + let is_target_crate = is_target_crate(&args); let run_plugin = !normal_rustc && (run_on_all_crates || primary_package) && is_target_crate; @@ -165,3 +159,14 @@ is_target_crate={is_target_crate}" } })) } + +fn is_target_crate(args: &[String]) -> bool { + match (env::var(SPECIFIC_CRATE), env::var(SPECIFIC_TARGET)) { + (Ok(krate), Ok(target)) => { + arg_value(args, "--crate-name", |name| name == krate).is_some() + && (arg_value(args, "--crate-type", |_| true).is_none() // integration test crate + || arg_value(args, "--crate-type", |name| name == target).is_some()) + } + _ => true, + } +}