Skip to content

Commit c592bf7

Browse files
committed
feat(profile): panic behavior can be specified for custom harness
It's libtest that requires tests to be always unwound. For custom harnesses they don't need such a requirement. Cargo relaxes it a bit in profile settings.
1 parent d013ac9 commit c592bf7

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ fn deps_of_roots(roots: &[Unit], state: &mut State<'_, '_>) -> CargoResult<()> {
213213
if unit.target.proc_macro() {
214214
// Special-case for proc-macros, which are forced to for-host
215215
// since they need to link with the proc_macro crate.
216-
UnitFor::new_host_test(state.gctx, root_compile_kind)
216+
UnitFor::new_host_test(state.gctx, &unit.target, root_compile_kind)
217217
} else {
218-
UnitFor::new_test(state.gctx, root_compile_kind)
218+
UnitFor::new_test(state.gctx, &unit.target, root_compile_kind)
219219
}
220220
} else if unit.target.is_custom_build() {
221221
// This normally doesn't happen, except `clean` aggressively

src/cargo/core/profiles.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,19 +1109,28 @@ impl UnitFor {
11091109
/// whether `panic=abort` is supported for tests. Historical versions of
11101110
/// rustc did not support this, but newer versions do with an unstable
11111111
/// compiler flag.
1112-
pub fn new_test(gctx: &GlobalContext, root_compile_kind: CompileKind) -> UnitFor {
1112+
///
1113+
/// Moreover, `target` is taken here for determining whether the test is
1114+
/// driven by libtest harness. Cargo relaxes the panic behaviour if it is
1115+
/// a custom harness, which is not required to be always unwound.
1116+
pub fn new_test(
1117+
gctx: &GlobalContext,
1118+
target: &Target,
1119+
root_compile_kind: CompileKind,
1120+
) -> UnitFor {
1121+
// We're testing out an unstable feature (`-Zpanic-abort-tests`)
1122+
// which inherits the panic setting from the dev/release profile
1123+
// (basically avoid recompiles) but historical defaults required
1124+
// that we always unwound.
1125+
let panic_setting = if gctx.cli_unstable().panic_abort_tests || !target.harness() {
1126+
PanicSetting::ReadProfile
1127+
} else {
1128+
PanicSetting::AlwaysUnwind
1129+
};
11131130
UnitFor {
11141131
host: false,
11151132
host_features: false,
1116-
// We're testing out an unstable feature (`-Zpanic-abort-tests`)
1117-
// which inherits the panic setting from the dev/release profile
1118-
// (basically avoid recompiles) but historical defaults required
1119-
// that we always unwound.
1120-
panic_setting: if gctx.cli_unstable().panic_abort_tests {
1121-
PanicSetting::ReadProfile
1122-
} else {
1123-
PanicSetting::AlwaysUnwind
1124-
},
1133+
panic_setting,
11251134
root_compile_kind,
11261135
artifact_target_for_features: None,
11271136
}
@@ -1130,8 +1139,14 @@ impl UnitFor {
11301139
/// This is a special case for unit tests of a proc-macro.
11311140
///
11321141
/// Proc-macro unit tests are forced to be run on the host.
1133-
pub fn new_host_test(gctx: &GlobalContext, root_compile_kind: CompileKind) -> UnitFor {
1134-
let mut unit_for = UnitFor::new_test(gctx, root_compile_kind);
1142+
///
1143+
/// See [`UnitFor::new_test`] for more.
1144+
pub fn new_host_test(
1145+
gctx: &GlobalContext,
1146+
target: &Target,
1147+
root_compile_kind: CompileKind,
1148+
) -> UnitFor {
1149+
let mut unit_for = UnitFor::new_test(gctx, target, root_compile_kind);
11351150
unit_for.host = true;
11361151
unit_for.host_features = true;
11371152
unit_for

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'a> UnitGenerator<'a, '_> {
148148
//
149149
// Forcing the lib to be compiled three times during `cargo
150150
// test` is probably also not desirable.
151-
UnitFor::new_test(self.ws.gctx(), kind)
151+
UnitFor::new_test(self.ws.gctx(), target, kind)
152152
} else if target.for_host() {
153153
// Proc macro / plugin should not have `panic` set.
154154
UnitFor::new_compiler(kind)

tests/testsuite/profiles.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,29 @@ fn profile_panic_test_with_custom_harness() {
428428
.file("benches/libtest.rs", "")
429429
.build();
430430

431+
#[cfg(not(windows))]
432+
let exit_code = 101;
433+
#[cfg(windows)]
434+
let exit_code = winapi::shared::ntstatus::STATUS_STACK_BUFFER_OVERRUN;
435+
431436
// panic abort on custom harness
432437
p.cargo("test --test custom --verbose")
433-
.with_stderr_does_not_contain("[..]panic=abort[..]")
438+
.with_stderr_contains("[RUNNING] `rustc --crate-name custom [..]-C panic=abort [..]")
434439
.with_stderr_contains("[..]thread '[..]' panicked at [..]")
435-
.with_stderr_does_not_contain(
440+
.with_stderr_contains("[..]abort![..]")
441+
.with_stderr_contains(
436442
"[..]process didn't exit successfully: `[..]/target/debug/deps/custom-[..]",
437443
)
438-
.with_status(101)
444+
.with_status(exit_code)
439445
.run();
440446
p.cargo("bench --bench custom --verbose")
441-
.with_stderr_does_not_contain("[..]panic=abort[..]")
447+
.with_stderr_contains("[RUNNING] `rustc --crate-name custom [..]-C panic=abort [..]")
442448
.with_stderr_contains("[..]thread '[..]' panicked at [..]")
443-
.with_stderr_does_not_contain(
449+
.with_stderr_contains("[..]abort![..]")
450+
.with_stderr_contains(
444451
"[..]process didn't exit successfully: `[..]/target/release/deps/custom-[..]",
445452
)
446-
.with_status(101)
453+
.with_status(exit_code)
447454
.run();
448455

449456
// panic behaviour of libtest cannot be set as `abort` as of now.

0 commit comments

Comments
 (0)