Skip to content

Convert some ABI tests to use extern "rust-invalid" #142992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions compiler/rustc_target/src/spec/abi_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@ impl AbiMap {
(ExternAbi::System { .. }, _) => CanonAbi::C,

// fallible lowerings
/* multi-platform */
// always and forever
(ExternAbi::RustInvalid, _) => return AbiMapping::Invalid,

(ExternAbi::EfiApi, Arch::Arm(..)) => CanonAbi::Arm(ArmCall::Aapcs),
(ExternAbi::EfiApi, Arch::X86_64) => CanonAbi::X86(X86Call::Win64),
(ExternAbi::EfiApi, Arch::Aarch64 | Arch::Riscv | Arch::X86) => CanonAbi::C,
(ExternAbi::EfiApi, _) => return AbiMapping::Invalid,

/* arm */
(ExternAbi::Aapcs { .. }, Arch::Arm(..)) => CanonAbi::Arm(ArmCall::Aapcs),
(ExternAbi::Aapcs { .. }, _) => return AbiMapping::Invalid,

Expand All @@ -103,6 +108,12 @@ impl AbiMap {
return AbiMapping::Invalid;
}

/* gpu */
(ExternAbi::PtxKernel, Arch::Nvptx) => CanonAbi::GpuKernel,
(ExternAbi::GpuKernel, Arch::Amdgpu | Arch::Nvptx) => CanonAbi::GpuKernel,
(ExternAbi::PtxKernel | ExternAbi::GpuKernel, _) => return AbiMapping::Invalid,

/* x86 */
(ExternAbi::Cdecl { .. }, Arch::X86) => CanonAbi::C,
(ExternAbi::Cdecl { .. }, _) => return AbiMapping::Deprecated(CanonAbi::C),

Expand Down Expand Up @@ -130,10 +141,7 @@ impl AbiMap {
(ExternAbi::Win64 { .. }, Arch::X86_64) => CanonAbi::X86(X86Call::Win64),
(ExternAbi::SysV64 { .. } | ExternAbi::Win64 { .. }, _) => return AbiMapping::Invalid,

(ExternAbi::PtxKernel, Arch::Nvptx) => CanonAbi::GpuKernel,
(ExternAbi::GpuKernel, Arch::Amdgpu | Arch::Nvptx) => CanonAbi::GpuKernel,
(ExternAbi::PtxKernel | ExternAbi::GpuKernel, _) => return AbiMapping::Invalid,

/* interrupts */
(ExternAbi::AvrInterrupt, Arch::Avr) => CanonAbi::Interrupt(InterruptKind::Avr),
(ExternAbi::AvrNonBlockingInterrupt, Arch::Avr) => {
CanonAbi::Interrupt(InterruptKind::AvrNonBlocking)
Expand All @@ -156,8 +164,7 @@ impl AbiMap {
| ExternAbi::Msp430Interrupt
| ExternAbi::RiscvInterruptM
| ExternAbi::RiscvInterruptS
| ExternAbi::X86Interrupt
| ExternAbi::RustInvalid,
| ExternAbi::X86Interrupt,
_,
) => return AbiMapping::Invalid,
};
Expand Down
5 changes: 5 additions & 0 deletions src/doc/rustc-dev-guide/src/tests/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ The output is normalized to ignore unwanted differences, see the
[Normalization](#normalization) section. If the file is missing, then
compiletest expects the corresponding output to be empty.

A common reason to use normalization, revisions, and most of the other following tools,
is to account for platform differences. Consider alternatives to these tools, like
e.g. using the `extern "rust-invalid"` ABI that is invalid on every platform
instead of fixing the test to use cross-compilation and testing every possibly-invalid ABI.

There can be multiple stdout/stderr files. The general form is:

```text
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ For now, only immediate subdirectories under `tests/ui/` are described, but thes

These tests deal with *Application Binary Interfaces* (ABI), mostly relating to function name mangling (and the `#[no_mangle]` attribute), calling conventions, or compiler flags which affect ABI.

Tests for unsupported ABIs can be made cross-platform by using the `extern "rust-invalid"` ABI, which is considered unsupported on every platform.

## `tests/ui/allocator`

These tests exercise `#![feature(allocator_api)]` and the `#[global_allocator]` attribute.
Expand Down
15 changes: 6 additions & 9 deletions tests/ui/abi/unsupported-abi-transmute.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
//@ add-core-stubs
//@ compile-flags: --crate-type=lib --target x86_64-unknown-none
//@ needs-llvm-components: x86
// Check we error before unsupported ABIs reach codegen stages.

//@ edition: 2018
#![no_core]
#![feature(no_core, lang_items)]
extern crate minicore;
use minicore::*;
//@ compile-flags: --crate-type=lib
#![feature(rustc_attrs)]

// Check we error before unsupported ABIs reach codegen stages.
use core::mem;

fn anything() {
let a = unsafe { mem::transmute::<usize, extern "thiscall" fn(i32)>(4) }(2);
let a = unsafe { mem::transmute::<usize, extern "rust-invalid" fn(i32)>(4) }(2);
//~^ ERROR: is not a supported ABI for the current target [E0570]
}
8 changes: 4 additions & 4 deletions tests/ui/abi/unsupported-abi-transmute.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported-abi-transmute.rs:13:53
error[E0570]: "rust-invalid" is not a supported ABI for the current target
--> $DIR/unsupported-abi-transmute.rs:10:53
|
LL | let a = unsafe { mem::transmute::<usize, extern "thiscall" fn(i32)>(4) }(2);
| ^^^^^^^^^^
LL | let a = unsafe { mem::transmute::<usize, extern "rust-invalid" fn(i32)>(4) }(2);
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Expand Down
36 changes: 36 additions & 0 deletions tests/ui/abi/unsupported-in-impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Test for https://github.com/rust-lang/rust/issues/86232
// Due to AST-to-HIR lowering nuances, we used to allow unsupported ABIs to "leak" into the HIR
// without being checked, as we would check after generating the ExternAbi.
// Checking afterwards only works if we examine every HIR construct that contains an ExternAbi,
// and those may be very different in HIR, even if they read the same in source.
// This made it very easy to make mistakes.
//
// Here we test that an unsupported ABI in various impl-related positions will be rejected,
// both in the original declarations and the actual implementations.

#![feature(rustc_attrs)]
//@ compile-flags: --crate-type lib

pub struct FnPtrBearer {
pub ptr: extern "rust-invalid" fn(),
//~^ ERROR: is not a supported ABI
}

impl FnPtrBearer {
pub extern "rust-invalid" fn inherent_fn(self) {
//~^ ERROR: is not a supported ABI
(self.ptr)()
}
}

pub trait Trait {
extern "rust-invalid" fn trait_fn(self);
//~^ ERROR: is not a supported ABI
}

impl Trait for FnPtrBearer {
extern "rust-invalid" fn trait_fn(self) {
//~^ ERROR: is not a supported ABI
self.inherent_fn()
}
}
27 changes: 27 additions & 0 deletions tests/ui/abi/unsupported-in-impls.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0570]: "rust-invalid" is not a supported ABI for the current target
--> $DIR/unsupported-in-impls.rs:15:21
|
LL | pub ptr: extern "rust-invalid" fn(),
| ^^^^^^^^^^^^^^

error[E0570]: "rust-invalid" is not a supported ABI for the current target
--> $DIR/unsupported-in-impls.rs:20:16
|
LL | pub extern "rust-invalid" fn inherent_fn(self) {
| ^^^^^^^^^^^^^^

error[E0570]: "rust-invalid" is not a supported ABI for the current target
--> $DIR/unsupported-in-impls.rs:27:12
|
LL | extern "rust-invalid" fn trait_fn(self);
| ^^^^^^^^^^^^^^

error[E0570]: "rust-invalid" is not a supported ABI for the current target
--> $DIR/unsupported-in-impls.rs:32:12
|
LL | extern "rust-invalid" fn trait_fn(self) {
| ^^^^^^^^^^^^^^

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0570`.
1 change: 1 addition & 0 deletions tests/ui/abi/unsupported-varargs-fnptr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// FIXME(workingjubilee): add revisions and generalize to other platform-specific varargs ABIs,
// preferably after the only-arch directive is enhanced with an "or pattern" syntax
// NOTE: This deliberately tests an ABI that supports varargs, so no `extern "rust-invalid"`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to, in fact, say "yes, extern "rust-invalid" can support varargs", but that seems silly.

//@ only-x86_64

// We have to use this flag to force ABI computation of an invalid ABI
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/abi/unsupported-varargs-fnptr.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0570]: "aapcs" is not a supported ABI for the current target
--> $DIR/unsupported-varargs-fnptr.rs:13:20
--> $DIR/unsupported-varargs-fnptr.rs:14:20
|
LL | fn aapcs(f: extern "aapcs" fn(usize, ...)) {
| ^^^^^^^
Expand Down
26 changes: 1 addition & 25 deletions tests/ui/abi/unsupported.aarch64.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -156,30 +156,6 @@ error[E0570]: "C-cmse-nonsecure-entry" is not a supported ABI for the current ta
LL | extern "C-cmse-nonsecure-entry" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:141:17
|
LL | ptr: extern "thiscall" fn(),
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:146:16
|
LL | pub extern "thiscall" fn inherent_fn(self) {
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:153:12
|
LL | extern "thiscall" fn trait_fn(self);
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:158:12
|
LL | extern "thiscall" fn trait_fn(self) {
| ^^^^^^^^^^

warning: "cdecl" is not a supported ABI for the current target
--> $DIR/unsupported.rs:99:17
|
Expand Down Expand Up @@ -221,6 +197,6 @@ LL | extern "cdecl" fn cdecl() {}
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
= help: use `extern "C"` instead

error: aborting due to 29 previous errors; 4 warnings emitted
error: aborting due to 25 previous errors; 4 warnings emitted

For more information about this error, try `rustc --explain E0570`.
26 changes: 1 addition & 25 deletions tests/ui/abi/unsupported.arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,6 @@ error[E0570]: "C-cmse-nonsecure-entry" is not a supported ABI for the current ta
LL | extern "C-cmse-nonsecure-entry" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:141:17
|
LL | ptr: extern "thiscall" fn(),
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:146:16
|
LL | pub extern "thiscall" fn inherent_fn(self) {
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:153:12
|
LL | extern "thiscall" fn trait_fn(self);
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:158:12
|
LL | extern "thiscall" fn trait_fn(self) {
| ^^^^^^^^^^

warning: "cdecl" is not a supported ABI for the current target
--> $DIR/unsupported.rs:99:17
|
Expand Down Expand Up @@ -203,6 +179,6 @@ LL | extern "cdecl" fn cdecl() {}
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
= help: use `extern "C"` instead

error: aborting due to 26 previous errors; 4 warnings emitted
error: aborting due to 22 previous errors; 4 warnings emitted

For more information about this error, try `rustc --explain E0570`.
26 changes: 1 addition & 25 deletions tests/ui/abi/unsupported.riscv32.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,6 @@ error[E0570]: "C-cmse-nonsecure-entry" is not a supported ABI for the current ta
LL | extern "C-cmse-nonsecure-entry" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:141:17
|
LL | ptr: extern "thiscall" fn(),
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:146:16
|
LL | pub extern "thiscall" fn inherent_fn(self) {
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:153:12
|
LL | extern "thiscall" fn trait_fn(self);
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:158:12
|
LL | extern "thiscall" fn trait_fn(self) {
| ^^^^^^^^^^

warning: "cdecl" is not a supported ABI for the current target
--> $DIR/unsupported.rs:99:17
|
Expand Down Expand Up @@ -215,6 +191,6 @@ LL | extern "cdecl" fn cdecl() {}
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
= help: use `extern "C"` instead

error: aborting due to 28 previous errors; 4 warnings emitted
error: aborting due to 24 previous errors; 4 warnings emitted

For more information about this error, try `rustc --explain E0570`.
26 changes: 1 addition & 25 deletions tests/ui/abi/unsupported.riscv64.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,6 @@ error[E0570]: "C-cmse-nonsecure-entry" is not a supported ABI for the current ta
LL | extern "C-cmse-nonsecure-entry" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:141:17
|
LL | ptr: extern "thiscall" fn(),
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:146:16
|
LL | pub extern "thiscall" fn inherent_fn(self) {
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:153:12
|
LL | extern "thiscall" fn trait_fn(self);
| ^^^^^^^^^^

error[E0570]: "thiscall" is not a supported ABI for the current target
--> $DIR/unsupported.rs:158:12
|
LL | extern "thiscall" fn trait_fn(self) {
| ^^^^^^^^^^

warning: "cdecl" is not a supported ABI for the current target
--> $DIR/unsupported.rs:99:17
|
Expand Down Expand Up @@ -215,6 +191,6 @@ LL | extern "cdecl" fn cdecl() {}
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
= help: use `extern "C"` instead

error: aborting due to 28 previous errors; 4 warnings emitted
error: aborting due to 24 previous errors; 4 warnings emitted

For more information about this error, try `rustc --explain E0570`.
24 changes: 0 additions & 24 deletions tests/ui/abi/unsupported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,3 @@ extern "C-cmse-nonsecure-entry" {}
extern "cdecl" {}
//[x64_win]~^ WARN unsupported_calling_conventions
//[x64_win]~^^ WARN this was previously accepted

struct FnPtrBearer {
ptr: extern "thiscall" fn(),
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
}

impl FnPtrBearer {
pub extern "thiscall" fn inherent_fn(self) {
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
(self.ptr)()
}
}

trait Trait {
extern "thiscall" fn trait_fn(self);
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
}

impl Trait for FnPtrBearer {
extern "thiscall" fn trait_fn(self) {
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
self.inherent_fn()
}
}
Loading
Loading