Skip to content

Commit cd4652a

Browse files
authored
Rollup merge of rust-lang#68357 - ollie27:rustdoc_test_errors, r=GuillaumeGomez
rustdoc: Fix handling of compile errors when running `rustdoc --test` * Call `abort_if_errors` so all errors actually stop rustdoc. * Don't panic with "compiler aborted in rustdoc!", instead just exit to avoid the ugly panic message. * Use rlib as the crate type when searching for doctests matching what is used for doc generation so `#[no_std]` crates don't create "no global memory allocator" errors. Fixes rust-lang#52243 Fixes rust-lang#54010 r? @GuillaumeGomez
2 parents fb86b82 + 17f4cbc commit cd4652a

9 files changed

+69
-3
lines changed

src/librustdoc/test.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn run(options: Options) -> i32 {
4343
let crate_types = if options.proc_macro_crate {
4444
vec![config::CrateType::ProcMacro]
4545
} else {
46-
vec![config::CrateType::Dylib]
46+
vec![config::CrateType::Rlib]
4747
};
4848

4949
let sessopts = config::Options {
@@ -117,12 +117,16 @@ pub fn run(options: Options) -> i32 {
117117
intravisit::walk_crate(this, krate);
118118
});
119119
});
120+
compiler.session().abort_if_errors();
120121

121122
let ret: Result<_, ErrorReported> = Ok(collector.tests);
122123
ret
123124
})
124-
})
125-
.expect("compiler aborted in rustdoc!");
125+
});
126+
let tests = match tests {
127+
Ok(tests) => tests,
128+
Err(ErrorReported) => return 1,
129+
};
126130

127131
test_args.insert(0, "rustdoctest".to_string());
128132

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// compile-flags:--test
2+
3+
/// ```
4+
/// assert!(true)
5+
/// ```
6+
pub fn f() {}
7+
8+
pub fn f() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0428]: the name `f` is defined multiple times
2+
--> $DIR/test-compile-fail1.rs:8:1
3+
|
4+
6 | pub fn f() {}
5+
| ---------- previous definition of the value `f` here
6+
7 |
7+
8 | pub fn f() {}
8+
| ^^^^^^^^^^ `f` redefined here
9+
|
10+
= note: `f` must be defined only once in the value namespace of this module
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0428`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags:--test
2+
3+
fail
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected one of `!` or `::`, found `<eof>`
2+
--> $DIR/test-compile-fail2.rs:3:1
3+
|
4+
3 | fail
5+
| ^^^^ expected one of `!` or `::`
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags:--test
2+
3+
"fail
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unterminated double quote string
2+
--> $DIR/test-compile-fail3.rs:3:1
3+
|
4+
3 | "fail
5+
| ^^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/rustdoc-ui/test-no_std.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-flags:--test
2+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
3+
// build-pass
4+
5+
#![no_std]
6+
7+
extern crate alloc;
8+
9+
/// ```
10+
/// assert!(true)
11+
/// ```
12+
pub fn f() {}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/test-no_std.rs - f (line 9) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
6+

0 commit comments

Comments
 (0)