Skip to content

Commit f817f1c

Browse files
committed
Get rid of env var race condition once and for all
1 parent dd5b010 commit f817f1c

File tree

4 files changed

+63
-38
lines changed

4 files changed

+63
-38
lines changed

Cargo.lock

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

miri/bin/miri.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ use rustc::ty::TyCtxt;
1919
use syntax::ast::{self, MetaItemKind, NestedMetaItemKind};
2020
use std::path::PathBuf;
2121

22-
struct MiriCompilerCalls(RustcDefaultCalls);
22+
struct MiriCompilerCalls {
23+
default: RustcDefaultCalls,
24+
/// whether we are building for the host
25+
host_target: bool,
26+
}
2327

2428
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
2529
fn early_callback(
@@ -30,7 +34,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
3034
descriptions: &rustc_errors::registry::Registry,
3135
output: ErrorOutputType,
3236
) -> Compilation {
33-
self.0.early_callback(
37+
self.default.early_callback(
3438
matches,
3539
sopts,
3640
cfg,
@@ -47,7 +51,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
4751
ofile: &Option<PathBuf>,
4852
descriptions: &rustc_errors::registry::Registry,
4953
) -> Option<(Input, Option<PathBuf>)> {
50-
self.0.no_input(
54+
self.default.no_input(
5155
matches,
5256
sopts,
5357
cfg,
@@ -64,17 +68,17 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
6468
odir: &Option<PathBuf>,
6569
ofile: &Option<PathBuf>,
6670
) -> Compilation {
67-
self.0.late_callback(matches, sess, input, odir, ofile)
71+
self.default.late_callback(matches, sess, input, odir, ofile)
6872
}
6973
fn build_controller(
7074
&mut self,
7175
sess: &Session,
7276
matches: &getopts::Matches,
7377
) -> CompileController<'a> {
74-
let mut control = self.0.build_controller(sess, matches);
78+
let mut control = self.default.build_controller(sess, matches);
7579
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
7680
control.after_analysis.callback = Box::new(after_analysis);
77-
if std::env::var("MIRI_HOST_TARGET") != Ok("yes".to_owned()) {
81+
if !self.host_target {
7882
// only fully compile targets on the host
7983
control.after_analysis.stop = Compilation::Stop;
8084
}
@@ -254,6 +258,16 @@ fn main() {
254258

255259
// for auxilary builds in unit tests
256260
args.push("-Zalways-encode-mir".to_owned());
261+
let mut host_target = false;
262+
args.retain(|arg| if arg == "--miri_host_target" {
263+
host_target = true;
264+
false // remove the flag, rustc doesn't know it
265+
} else {
266+
true
267+
});
257268

258-
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls(RustcDefaultCalls), None, None);
269+
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls {
270+
default: RustcDefaultCalls,
271+
host_target,
272+
}, None, None);
259273
}

rustc_tests/src/main.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ use rustc::hir::{self, itemlikevisit};
2020
use rustc::ty::TyCtxt;
2121
use syntax::ast;
2222

23-
struct MiriCompilerCalls(RustcDefaultCalls);
23+
struct MiriCompilerCalls {
24+
default: RustcDefaultCalls,
25+
/// whether we are building for the host
26+
host_target: bool,
27+
}
2428

2529
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
2630
fn early_callback(
@@ -31,7 +35,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
3135
descriptions: &rustc_errors::registry::Registry,
3236
output: ErrorOutputType
3337
) -> Compilation {
34-
self.0.early_callback(matches, sopts, cfg, descriptions, output)
38+
self.default.early_callback(matches, sopts, cfg, descriptions, output)
3539
}
3640
fn no_input(
3741
&mut self,
@@ -42,7 +46,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
4246
ofile: &Option<PathBuf>,
4347
descriptions: &rustc_errors::registry::Registry
4448
) -> Option<(Input, Option<PathBuf>)> {
45-
self.0.no_input(matches, sopts, cfg, odir, ofile, descriptions)
49+
self.default.no_input(matches, sopts, cfg, odir, ofile, descriptions)
4650
}
4751
fn late_callback(
4852
&mut self,
@@ -52,13 +56,13 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
5256
odir: &Option<PathBuf>,
5357
ofile: &Option<PathBuf>
5458
) -> Compilation {
55-
self.0.late_callback(matches, sess, input, odir, ofile)
59+
self.default.late_callback(matches, sess, input, odir, ofile)
5660
}
5761
fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> CompileController<'a> {
58-
let mut control = self.0.build_controller(sess, matches);
62+
let mut control = self.default.build_controller(sess, matches);
5963
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
6064
control.after_analysis.callback = Box::new(after_analysis);
61-
if std::env::var("MIRI_HOST_TARGET") != Ok("yes".to_owned()) {
65+
if !self.host_target {
6266
// only fully compile targets on the host
6367
control.after_analysis.stop = Compilation::Stop;
6468
}
@@ -139,7 +143,15 @@ fn main() {
139143
}
140144
let stderr = std::io::stderr();
141145
write!(stderr.lock(), "test [miri-pass] {} ... ", path.display()).unwrap();
142-
let mut args: Vec<String> = std::env::args().collect();
146+
let mut host_target = false;
147+
let mut args: Vec<String> = std::env::args().filter(|arg| {
148+
if arg == "--miri_host_target" {
149+
host_target = true;
150+
false // remove the flag, rustc doesn't know it
151+
} else {
152+
true
153+
}
154+
}).collect();
143155
// file to process
144156
args.push(path.display().to_string());
145157

tests/compiletest.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,14 @@ fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
126126
// For now, only validate without optimizations. Inlining breaks validation.
127127
flags.push("-Zmir-emit-validate=1".to_owned());
128128
}
129+
if target == host {
130+
flags.push("--miri_host_target".to_owned());
131+
}
129132
config.target_rustcflags = Some(flags.join(" "));
130133
// don't actually execute the final binary, it might be for other targets and we only care
131134
// about running miri, not the binary.
132135
config.runtool = Some("echo \"\" || ".to_owned());
133-
if target == host {
134-
std::env::set_var("MIRI_HOST_TARGET", "yes");
135-
}
136136
compiletest::run_tests(&config);
137-
std::env::set_var("MIRI_HOST_TARGET", "");
138137
}
139138

140139
fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {

0 commit comments

Comments
 (0)