Skip to content

Commit e66a89c

Browse files
committed
avoid some dead code and test no_run tests
1 parent 29bc8a5 commit e66a89c

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

cargo-miri/bin.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum MiriCommand {
3838
}
3939

4040
/// The information to run a crate with the given environment.
41-
#[derive(Serialize, Deserialize)]
41+
#[derive(Serialize, Deserialize, Clone)]
4242
struct CrateRunEnv {
4343
/// The command-line arguments.
4444
args: Vec<String>,
@@ -50,16 +50,7 @@ struct CrateRunEnv {
5050
stdin: Vec<u8>,
5151
}
5252

53-
/// The information Miri needs to run a crate. Stored as JSON when the crate is "compiled".
54-
#[derive(Serialize, Deserialize)]
55-
enum CrateRunInfo {
56-
/// Run it with the given environment.
57-
RunWith(CrateRunEnv),
58-
/// Skip it as Miri does not support interpreting such kind of crates.
59-
SkipProcMacroTest,
60-
}
61-
62-
impl CrateRunInfo {
53+
impl CrateRunEnv {
6354
/// Gather all the information we need.
6455
fn collect(args: env::Args) -> Self {
6556
let args = args.collect();
@@ -71,9 +62,20 @@ impl CrateRunInfo {
7162
std::io::stdin().lock().read_to_end(&mut stdin).expect("cannot read stdin");
7263
}
7364

74-
Self::RunWith(CrateRunEnv { args, env, current_dir, stdin })
65+
CrateRunEnv { args, env, current_dir, stdin }
7566
}
67+
}
7668

69+
/// The information Miri needs to run a crate. Stored as JSON when the crate is "compiled".
70+
#[derive(Serialize, Deserialize)]
71+
enum CrateRunInfo {
72+
/// Run it with the given environment.
73+
RunWith(CrateRunEnv),
74+
/// Skip it as Miri does not support interpreting such kind of crates.
75+
SkipProcMacroTest,
76+
}
77+
78+
impl CrateRunInfo {
7779
fn store(&self, filename: &Path) {
7880
let file = File::create(filename)
7981
.unwrap_or_else(|_| show_error(format!("cannot create `{}`", filename.display())));
@@ -644,7 +646,7 @@ fn phase_cargo_rustc(mut args: env::Args) {
644646
let target_crate = is_target_crate();
645647
let print = get_arg_flag_value("--print").is_some(); // whether this is cargo passing `--print` to get some infos
646648

647-
let store_json = |info: &CrateRunInfo| {
649+
let store_json = |info: CrateRunInfo| {
648650
// Create a stub .d file to stop Cargo from "rebuilding" the crate:
649651
// https://github.com/rust-lang/miri/issues/1724#issuecomment-787115693
650652
// As we store a JSON file instead of building the crate here, an empty file is fine.
@@ -672,30 +674,24 @@ fn phase_cargo_rustc(mut args: env::Args) {
672674
// like we want them.
673675
// Instead of compiling, we write JSON into the output file with all the relevant command-line flags
674676
// and environment variables; this is used when cargo calls us again in the CARGO_TARGET_RUNNER phase.
675-
let info = CrateRunInfo::collect(args);
676-
store_json(&info);
677+
let env = CrateRunEnv::collect(args);
677678

678679
// Rustdoc expects us to exit with an error code if the test is marked as `compile_fail`,
679680
// just creating the JSON file is not enough: we need to detect syntax errors,
680681
// so we need to run Miri with `MIRI_BE_RUSTC` for a check-only build.
681682
if std::env::var_os("MIRI_CALLED_FROM_RUSTDOC").is_some() {
682683
let mut cmd = miri();
683-
let env = if let CrateRunInfo::RunWith(env) = info {
684-
env
685-
} else {
686-
return;
687-
};
688684

689-
// ensure --emit argument for a check-only build is present
685+
// Ensure --emit argument for a check-only build is present.
690686
if let Some(i) = env.args.iter().position(|arg| arg.starts_with("--emit=")) {
691-
// We need to make sure we're not producing a binary that overwrites the JSON file.
692-
// rustdoc should only ever pass an --emit=metadata argument for tests marked as `no_run`:
687+
// For `no_run` tests, rustdoc passes a `--emit` flag; make sure it has the right shape.
693688
assert_eq!(env.args[i], "--emit=metadata");
694689
} else {
695-
cmd.arg("--emit=dep-info,metadata");
690+
// For all other kinds of tests, we can just add our flag.
691+
cmd.arg("--emit=metadata");
696692
}
697693

698-
cmd.args(env.args);
694+
cmd.args(&env.args);
699695
cmd.env("MIRI_BE_RUSTC", "1");
700696

701697
if verbose {
@@ -706,14 +702,16 @@ fn phase_cargo_rustc(mut args: env::Args) {
706702
exec_with_pipe(cmd, &env.stdin);
707703
}
708704

705+
store_json(CrateRunInfo::RunWith(env));
706+
709707
return;
710708
}
711709

712710
if runnable_crate && ArgFlagValueIter::new("--extern").any(|krate| krate == "proc_macro") {
713711
// This is a "runnable" `proc-macro` crate (unit tests). We do not support
714712
// interpreting that under Miri now, so we write a JSON file to (display a
715713
// helpful message and) skip it in the runner phase.
716-
store_json(&CrateRunInfo::SkipProcMacroTest);
714+
store_json(CrateRunInfo::SkipProcMacroTest);
717715
return;
718716
}
719717

test-cargo-miri/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
/// ```rust
33
/// assert!(cargo_miri_test::make_true());
44
/// ```
5+
/// ```rust,no_run
6+
/// assert!(cargo_miri_test::make_true());
7+
/// ```
58
/// ```rust,compile_fail
69
/// assert!(cargo_miri_test::make_true() == 5);
710
/// ```

test-cargo-miri/test.default.stdout.ref

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ running 7 tests
99
test result: ok. 6 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out
1010

1111

12-
running 2 tests
12+
running 3 tests
1313
test src/lib.rs - make_true (line 5) ... ok
14+
test src/lib.rs - make_true (line 8) ... ok
1415
test src/lib.rs - make_true (line 2) ... ok
1516

16-
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
17+
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
1718

test-cargo-miri/test.filter.stdout.ref

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 6 filtered out
1212

1313
running 0 tests
1414

15-
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in $TIME
15+
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out; finished in $TIME
1616

0 commit comments

Comments
 (0)