Skip to content

Commit 1e065f2

Browse files
committed
Panic beaviour in profile can be specified if using 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 b1b25a0 commit 1e065f2

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/cargo/core/compiler/unit_dependencies.rs

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

src/cargo/core/profiles.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -921,19 +921,24 @@ impl UnitFor {
921921
/// whether `panic=abort` is supported for tests. Historical versions of
922922
/// rustc did not support this, but newer versions do with an unstable
923923
/// compiler flag.
924-
pub fn new_test(config: &Config, root_compile_kind: CompileKind) -> UnitFor {
924+
///
925+
/// Moreover, `target` is taken here for determining whether the test is
926+
/// driven by libtest harness. Cargo relaxes the panic behaviour if it is
927+
/// a custom harness, which is not required to be always unwound.
928+
pub fn new_test(config: &Config, target: &Target, root_compile_kind: CompileKind) -> UnitFor {
929+
// We're testing out an unstable feature (`-Zpanic-abort-tests`)
930+
// which inherits the panic setting from the dev/release profile
931+
// (basically avoid recompiles) but historical defaults required
932+
// that we always unwound.
933+
let panic_setting = if config.cli_unstable().panic_abort_tests || !target.harness() {
934+
PanicSetting::ReadProfile
935+
} else {
936+
PanicSetting::AlwaysUnwind
937+
};
925938
UnitFor {
926939
host: false,
927940
host_features: false,
928-
// We're testing out an unstable feature (`-Zpanic-abort-tests`)
929-
// which inherits the panic setting from the dev/release profile
930-
// (basically avoid recompiles) but historical defaults required
931-
// that we always unwound.
932-
panic_setting: if config.cli_unstable().panic_abort_tests {
933-
PanicSetting::ReadProfile
934-
} else {
935-
PanicSetting::AlwaysUnwind
936-
},
941+
panic_setting,
937942
root_compile_kind,
938943
artifact_target_for_features: None,
939944
}
@@ -942,8 +947,14 @@ impl UnitFor {
942947
/// This is a special case for unit tests of a proc-macro.
943948
///
944949
/// Proc-macro unit tests are forced to be run on the host.
945-
pub fn new_host_test(config: &Config, root_compile_kind: CompileKind) -> UnitFor {
946-
let mut unit_for = UnitFor::new_test(config, root_compile_kind);
950+
///
951+
/// See [`UnitFor::new_test`] for more.
952+
pub fn new_host_test(
953+
config: &Config,
954+
target: &Target,
955+
root_compile_kind: CompileKind,
956+
) -> UnitFor {
957+
let mut unit_for = UnitFor::new_test(config, target, root_compile_kind);
947958
unit_for.host = true;
948959
unit_for.host_features = true;
949960
unit_for

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ fn generate_targets(
678678
//
679679
// Forcing the lib to be compiled three times during `cargo
680680
// test` is probably also not desirable.
681-
UnitFor::new_test(config, *kind)
681+
UnitFor::new_test(config, &target, *kind)
682682
} else if target.for_host() {
683683
// Proc macro / plugin should not have `panic` set.
684684
UnitFor::new_compiler(*kind)

0 commit comments

Comments
 (0)