Skip to content

Commit b7cb10a

Browse files
committed
Improve testing
1 parent 166eb8b commit b7cb10a

24 files changed

+212
-24
lines changed

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -404,21 +404,26 @@ fn activate_injected_dep(
404404
/// there's only going to be one panic runtime in the output.
405405
fn verify_ok(tcx: TyCtxt<'_>, list: &DependencyList) {
406406
let sess = &tcx.sess;
407+
let list: Vec<_> = list
408+
.iter_enumerated()
409+
.filter_map(
410+
|(cnum, linkage)| if *linkage == Linkage::NotLinked { None } else { Some(cnum) },
411+
)
412+
.collect();
407413
if list.is_empty() {
408414
return;
409415
}
410416
let desired_strategy = sess.panic_strategy();
411417

412418
// If we are panic=immediate-abort, make sure everything in the dependency tree has also been
413419
// compiled with immediate-abort.
414-
if desired_strategy == PanicStrategy::ImmediateAbort {
420+
if list
421+
.iter()
422+
.any(|cnum| tcx.required_panic_strategy(*cnum) == Some(PanicStrategy::ImmediateAbort))
423+
{
415424
let mut invalid_crates = Vec::new();
416-
for (cnum, linkage) in list.iter_enumerated() {
417-
if let Linkage::NotLinked = *linkage {
418-
continue;
419-
}
420-
let found_strategy = tcx.required_panic_strategy(cnum);
421-
if found_strategy != Some(PanicStrategy::ImmediateAbort) {
425+
for cnum in list.iter().copied() {
426+
if tcx.required_panic_strategy(cnum) != Some(PanicStrategy::ImmediateAbort) {
422427
invalid_crates.push(cnum);
423428
// If core is incompatible, it's very likely that we'd emit an error for every
424429
// sysroot crate, so instead of doing that emit a single fatal error that suggests
@@ -435,11 +440,7 @@ fn verify_ok(tcx: TyCtxt<'_>, list: &DependencyList) {
435440
}
436441

437442
let mut panic_runtime = None;
438-
for (cnum, linkage) in list.iter_enumerated() {
439-
if let Linkage::NotLinked = *linkage {
440-
continue;
441-
}
442-
443+
for cnum in list.iter().copied() {
443444
if tcx.is_panic_runtime(cnum) {
444445
if let Some((prev, _)) = panic_runtime {
445446
let prev_name = tcx.crate_name(prev);
@@ -472,10 +473,7 @@ fn verify_ok(tcx: TyCtxt<'_>, list: &DependencyList) {
472473
// strategy. If the dep isn't linked, we ignore it, and if our strategy
473474
// is abort then it's compatible with everything. Otherwise all crates'
474475
// panic strategy must match our own.
475-
for (cnum, linkage) in list.iter_enumerated() {
476-
if let Linkage::NotLinked = *linkage {
477-
continue;
478-
}
476+
for cnum in list.iter().copied() {
479477
if cnum == runtime_cnum || tcx.is_compiler_builtins(cnum) {
480478
continue;
481479
}

compiler/rustc_session/src/config/cfg.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
206206
}
207207

208208
ins_sym!(sym::panic, sess.panic_strategy().desc_symbol());
209+
if sess.panic_strategy() == PanicStrategy::ImmediateAbort {
210+
ins_sym!(sym::panic, PanicStrategy::Abort.desc_symbol());
211+
}
209212

210213
// JUSTIFICATION: before wrapper fn is available
211214
#[allow(rustc::bad_opt_access)]

src/doc/rustc/src/codegen-options/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,13 @@ If not specified, overflow checks are enabled if
471471
This option lets you control what happens when the code panics.
472472

473473
* `abort`: terminate the process upon panic
474+
* `immediate-abort`: terminate the process upon panic, and do not call any panic hooks
474475
* `unwind`: unwind the stack upon panic
475476

476477
If not specified, the default depends on the target.
477478

478479
If any crate in the crate graph uses `abort`, the final binary (`bin`, `dylib`, `cdylib`, `staticlib`) must also use `abort`.
480+
If any crate in the crate graph uses `immediate-abort`, every crate in the graph must use `immediate-abort`.
479481
If `std` is used as a `dylib` with `unwind`, the final binary must also use `unwind`.
480482

481483
## passes

src/tools/compiletest/src/directives.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ pub struct TestProps {
201201
/// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
202202
/// that don't otherwise want/need `-Z build-std`.
203203
pub add_core_stubs: bool,
204+
/// Add these flags to the build of `minicore`.
205+
pub core_stubs_compile_flags: Vec<String>,
204206
/// Whether line annotatins are required for the given error kind.
205207
pub dont_require_annotations: HashSet<ErrorKind>,
206208
/// Whether pretty printers should be disabled in gdb.
@@ -253,6 +255,7 @@ mod directives {
253255
pub const FILECHECK_FLAGS: &'static str = "filecheck-flags";
254256
pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
255257
pub const ADD_CORE_STUBS: &'static str = "add-core-stubs";
258+
pub const CORE_STUBS_COMPILE_FLAGS: &'static str = "core-stubs-compile-flags";
256259
// This isn't a real directive, just one that is probably mistyped often
257260
pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
258261
pub const DISABLE_GDB_PRETTY_PRINTERS: &'static str = "disable-gdb-pretty-printers";
@@ -311,6 +314,7 @@ impl TestProps {
311314
no_auto_check_cfg: false,
312315
has_enzyme: false,
313316
add_core_stubs: false,
317+
core_stubs_compile_flags: vec![],
314318
dont_require_annotations: Default::default(),
315319
disable_gdb_pretty_printers: false,
316320
compare_output_by_lines: false,
@@ -653,6 +657,21 @@ impl TestProps {
653657

654658
self.update_add_core_stubs(ln, config);
655659

660+
if let Some(flags) = config.parse_name_value_directive(
661+
ln,
662+
directives::CORE_STUBS_COMPILE_FLAGS,
663+
testfile,
664+
line_number,
665+
) {
666+
let flags = split_flags(&flags);
667+
for flag in &flags {
668+
if flag == "--edition" || flag.starts_with("--edition=") {
669+
panic!("you must use `//@ edition` to configure the edition");
670+
}
671+
}
672+
self.core_stubs_compile_flags.extend(flags);
673+
}
674+
656675
if let Some(err_kind) = config.parse_name_value_directive(
657676
ln,
658677
DONT_REQUIRE_ANNOTATIONS,

src/tools/compiletest/src/directives/directive_names.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
1919
"check-test-line-numbers-match",
2020
"compare-output-by-lines",
2121
"compile-flags",
22+
"core-stubs-compile-flags",
2223
"disable-gdb-pretty-printers",
2324
"doc-flags",
2425
"dont-check-compiler-stderr",

src/tools/compiletest/src/runtest.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ impl<'test> TestCx<'test> {
13221322

13231323
rustc.args(&["--crate-type", "rlib"]);
13241324
rustc.arg("-Cpanic=abort");
1325+
rustc.args(self.props.core_stubs_compile_flags.clone());
13251326

13261327
let res = self.compose_and_run(rustc, self.config.compile_lib_path.as_path(), None, None);
13271328
if !res.status.success() {
@@ -1432,6 +1433,12 @@ impl<'test> TestCx<'test> {
14321433

14331434
aux_rustc.arg("-L").arg(&aux_dir);
14341435

1436+
if aux_props.add_core_stubs {
1437+
let minicore_path = self.build_minicore();
1438+
aux_rustc.arg("--extern");
1439+
aux_rustc.arg(&format!("minicore={}", minicore_path));
1440+
}
1441+
14351442
let auxres = aux_cx.compose_and_run(
14361443
aux_rustc,
14371444
aux_cx.config.compile_lib_path.as_path(),
@@ -1858,21 +1865,22 @@ impl<'test> TestCx<'test> {
18581865
}
18591866
}
18601867

1861-
rustc.args(&self.props.compile_flags);
1862-
18631868
// FIXME(jieyouxu): we should report a fatal error or warning if user wrote `-Cpanic=` with
1864-
// something that's not `abort` and `-Cforce-unwind-tables` with a value that is not `yes`,
1865-
// however, by moving this last we should override previous `-Cpanic`s and
1866-
// `-Cforce-unwind-tables`s. Note that checking here is very fragile, because we'd have to
1867-
// account for all possible compile flag splittings (they have some... intricacies and are
1868-
// not yet normalized).
1869+
// something that's not `abort` and `-Cforce-unwind-tables` with a value that is not `yes`.
1870+
//
1871+
// We could apply these last and override any provided flags. That would ensure that the
1872+
// build works, but some tests want to exercise that mixing panic modes in specific ways is
1873+
// rejected. So we enable aborting panics and unwind tables before adding flags, just to
1874+
// change the default.
18691875
//
18701876
// `minicore` requires `#![no_std]` and `#![no_core]`, which means no unwinding panics.
18711877
if self.props.add_core_stubs {
18721878
rustc.arg("-Cpanic=abort");
18731879
rustc.arg("-Cforce-unwind-tables=yes");
18741880
}
18751881

1882+
rustc.args(&self.props.compile_flags);
1883+
18761884
rustc
18771885
}
18781886

File renamed without changes.
File renamed without changes.

tests/run-make-cargo/panic-immediate-abort/rmake.rs renamed to tests/run-make-cargo/panic-immediate-abort-codegen/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn main() {
99
.args(&[
1010
"build",
1111
"--release",
12+
"--lib",
1213
"--manifest-path",
1314
"Cargo.toml",
1415
"-Zbuild-std=core",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "hello"
3+
version = "0.1.0"
4+
edition = "2024"

0 commit comments

Comments
 (0)