Skip to content

Commit b00696b

Browse files
committed
fix compile_fail doctests with post-mono errors
1 parent 5162995 commit b00696b

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

cargo-miri/src/phases.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
364364
let out_filename = out_filename.expect("rustdoc must pass `-o`");
365365

366366
cmd.args(&args);
367-
cmd.env("MIRI_BE_RUSTC", "target");
367+
cmd.env("MIRI_BE_RUSTC", "rustdoc");
368368

369369
if verbose > 0 {
370370
eprintln!(

src/bin/miri.rs

+41-10
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,31 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
123123
}
124124
}
125125

126+
#[derive(Copy, Clone, Debug, PartialEq)]
127+
enum MiriBeRustcMode {
128+
Target,
129+
Host,
130+
Rustdoc,
131+
}
132+
133+
impl MiriBeRustcMode {
134+
fn target_crate(self) -> bool {
135+
use MiriBeRustcMode::*;
136+
match self {
137+
Target | Rustdoc => true,
138+
Host => false,
139+
}
140+
}
141+
}
142+
126143
struct MiriBeRustCompilerCalls {
127-
target_crate: bool,
144+
mode: MiriBeRustcMode,
128145
}
129146

130147
impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
131148
#[allow(rustc::potential_query_instability)] // rustc_codegen_ssa (where this code is copied from) also allows this lint
132149
fn config(&mut self, config: &mut Config) {
133-
if config.opts.prints.is_empty() && self.target_crate {
150+
if config.opts.prints.is_empty() && self.mode == MiriBeRustcMode::Target {
134151
// Queries overridden here affect the data stored in `rmeta` files of dependencies,
135152
// which will be used later in non-`MIRI_BE_RUSTC` mode.
136153
config.override_queries = Some(|_, local_providers| {
@@ -179,6 +196,21 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
179196
});
180197
}
181198
}
199+
200+
fn after_analysis<'tcx>(
201+
&mut self,
202+
_: &rustc_interface::interface::Compiler,
203+
queries: &'tcx rustc_interface::Queries<'tcx>,
204+
) -> Compilation {
205+
queries.global_ctxt().unwrap().enter(|tcx| {
206+
// Make sure compile_fail tests with post-monomorphization const-eval errors work as
207+
// intended in rustdoc mode.
208+
if self.mode == MiriBeRustcMode::Rustdoc {
209+
let _ = tcx.collect_and_partition_mono_items(());
210+
}
211+
});
212+
Compilation::Continue
213+
}
182214
}
183215

184216
fn show_error(msg: &impl std::fmt::Display) -> ! {
@@ -351,19 +383,18 @@ fn main() {
351383
rustc_driver::install_ice_hook(rustc_driver::DEFAULT_BUG_REPORT_URL, |_| ());
352384
rustc_driver::init_rustc_env_logger(&early_dcx);
353385

354-
let target_crate = if crate_kind == "target" {
355-
true
356-
} else if crate_kind == "host" {
357-
false
358-
} else {
359-
panic!("invalid `MIRI_BE_RUSTC` value: {crate_kind:?}")
386+
let mode = match crate_kind.to_str().unwrap_or_default() {
387+
"target" => MiriBeRustcMode::Target,
388+
"rustdoc" => MiriBeRustcMode::Rustdoc,
389+
"host" => MiriBeRustcMode::Host,
390+
_ => panic!("invalid `MIRI_BE_RUSTC` value: {crate_kind:?}"),
360391
};
361392

362393
// We cannot use `rustc_driver::main` as we need to adjust the CLI arguments.
363394
run_compiler(
364395
args,
365-
target_crate,
366-
&mut MiriBeRustCompilerCalls { target_crate },
396+
mode.target_crate(),
397+
&mut MiriBeRustCompilerCalls { mode },
367398
using_internal_features,
368399
)
369400
}

test-cargo-miri/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
/// Doc-test test
2+
///
23
/// ```rust
34
/// assert!(cargo_miri_test::make_true());
45
/// ```
6+
///
7+
/// `no_run` test:
8+
///
59
/// ```rust,no_run
610
/// assert!(!cargo_miri_test::make_true());
711
/// ```
12+
///
13+
/// `compile_fail` test:
14+
///
815
/// ```rust,compile_fail
916
/// assert!(cargo_miri_test::make_true() == 5);
1017
/// ```
18+
///
19+
/// Post-monomorphization error in `compile_fail` test:
20+
///
21+
/// ```rust,compile_fail
22+
/// struct Fail<T>(T);
23+
/// impl<T> Fail<T> {
24+
/// const C: () = panic!();
25+
/// }
26+
///
27+
/// let _val = Fail::<i32>::C;
28+
/// ```
1129
#[no_mangle]
1230
pub fn make_true() -> bool {
1331
issue_1567::use_the_dependency();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ running 6 tests
1010
test result: ok. 5 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out
1111

1212

13-
running 4 tests
14-
....
15-
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
13+
running 5 tests
14+
.....
15+
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
1616

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 5 filtered out
1313

1414
running 0 tests
1515

16-
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 4 filtered out; finished in $TIME
16+
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 5 filtered out; finished in $TIME
1717

0 commit comments

Comments
 (0)