Skip to content

Commit 20e47aa

Browse files
authored
Rollup merge of #142992 - workingjubilee:dont-validate-naughty-abis, r=jieyouxu
Convert some ABI tests to use `extern "rust-invalid"`
2 parents da42289 + c24914e commit 20e47aa

16 files changed

+101
-194
lines changed

compiler/rustc_target/src/spec/abi_map.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,16 @@ impl AbiMap {
8585
(ExternAbi::System { .. }, _) => CanonAbi::C,
8686

8787
// fallible lowerings
88+
/* multi-platform */
89+
// always and forever
90+
(ExternAbi::RustInvalid, _) => return AbiMapping::Invalid,
91+
8892
(ExternAbi::EfiApi, Arch::Arm(..)) => CanonAbi::Arm(ArmCall::Aapcs),
8993
(ExternAbi::EfiApi, Arch::X86_64) => CanonAbi::X86(X86Call::Win64),
9094
(ExternAbi::EfiApi, Arch::Aarch64 | Arch::Riscv | Arch::X86) => CanonAbi::C,
9195
(ExternAbi::EfiApi, _) => return AbiMapping::Invalid,
9296

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

@@ -103,6 +108,12 @@ impl AbiMap {
103108
return AbiMapping::Invalid;
104109
}
105110

111+
/* gpu */
112+
(ExternAbi::PtxKernel, Arch::Nvptx) => CanonAbi::GpuKernel,
113+
(ExternAbi::GpuKernel, Arch::Amdgpu | Arch::Nvptx) => CanonAbi::GpuKernel,
114+
(ExternAbi::PtxKernel | ExternAbi::GpuKernel, _) => return AbiMapping::Invalid,
115+
116+
/* x86 */
106117
(ExternAbi::Cdecl { .. }, Arch::X86) => CanonAbi::C,
107118
(ExternAbi::Cdecl { .. }, _) => return AbiMapping::Deprecated(CanonAbi::C),
108119

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

133-
(ExternAbi::PtxKernel, Arch::Nvptx) => CanonAbi::GpuKernel,
134-
(ExternAbi::GpuKernel, Arch::Amdgpu | Arch::Nvptx) => CanonAbi::GpuKernel,
135-
(ExternAbi::PtxKernel | ExternAbi::GpuKernel, _) => return AbiMapping::Invalid,
136-
144+
/* interrupts */
137145
(ExternAbi::AvrInterrupt, Arch::Avr) => CanonAbi::Interrupt(InterruptKind::Avr),
138146
(ExternAbi::AvrNonBlockingInterrupt, Arch::Avr) => {
139147
CanonAbi::Interrupt(InterruptKind::AvrNonBlocking)
@@ -156,8 +164,7 @@ impl AbiMap {
156164
| ExternAbi::Msp430Interrupt
157165
| ExternAbi::RiscvInterruptM
158166
| ExternAbi::RiscvInterruptS
159-
| ExternAbi::X86Interrupt
160-
| ExternAbi::RustInvalid,
167+
| ExternAbi::X86Interrupt,
161168
_,
162169
) => return AbiMapping::Invalid,
163170
};

src/doc/rustc-dev-guide/src/tests/ui.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ The output is normalized to ignore unwanted differences, see the
5959
[Normalization](#normalization) section. If the file is missing, then
6060
compiletest expects the corresponding output to be empty.
6161

62+
A common reason to use normalization, revisions, and most of the other following tools,
63+
is to account for platform differences. Consider alternatives to these tools, like
64+
e.g. using the `extern "rust-invalid"` ABI that is invalid on every platform
65+
instead of fixing the test to use cross-compilation and testing every possibly-invalid ABI.
66+
6267
There can be multiple stdout/stderr files. The general form is:
6368

6469
```text

tests/ui/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ For now, only immediate subdirectories under `tests/ui/` are described, but thes
88

99
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.
1010

11+
Tests for unsupported ABIs can be made cross-platform by using the `extern "rust-invalid"` ABI, which is considered unsupported on every platform.
12+
1113
## `tests/ui/allocator`
1214

1315
These tests exercise `#![feature(allocator_api)]` and the `#[global_allocator]` attribute.
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
//@ add-core-stubs
2-
//@ compile-flags: --crate-type=lib --target x86_64-unknown-none
3-
//@ needs-llvm-components: x86
1+
// Check we error before unsupported ABIs reach codegen stages.
2+
43
//@ edition: 2018
5-
#![no_core]
6-
#![feature(no_core, lang_items)]
7-
extern crate minicore;
8-
use minicore::*;
4+
//@ compile-flags: --crate-type=lib
5+
#![feature(rustc_attrs)]
96

10-
// Check we error before unsupported ABIs reach codegen stages.
7+
use core::mem;
118

129
fn anything() {
13-
let a = unsafe { mem::transmute::<usize, extern "thiscall" fn(i32)>(4) }(2);
10+
let a = unsafe { mem::transmute::<usize, extern "rust-invalid" fn(i32)>(4) }(2);
1411
//~^ ERROR: is not a supported ABI for the current target [E0570]
1512
}

tests/ui/abi/unsupported-abi-transmute.stderr

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

77
error: aborting due to 1 previous error
88

tests/ui/abi/unsupported-in-impls.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Test for https://github.com/rust-lang/rust/issues/86232
2+
// Due to AST-to-HIR lowering nuances, we used to allow unsupported ABIs to "leak" into the HIR
3+
// without being checked, as we would check after generating the ExternAbi.
4+
// Checking afterwards only works if we examine every HIR construct that contains an ExternAbi,
5+
// and those may be very different in HIR, even if they read the same in source.
6+
// This made it very easy to make mistakes.
7+
//
8+
// Here we test that an unsupported ABI in various impl-related positions will be rejected,
9+
// both in the original declarations and the actual implementations.
10+
11+
#![feature(rustc_attrs)]
12+
//@ compile-flags: --crate-type lib
13+
14+
pub struct FnPtrBearer {
15+
pub ptr: extern "rust-invalid" fn(),
16+
//~^ ERROR: is not a supported ABI
17+
}
18+
19+
impl FnPtrBearer {
20+
pub extern "rust-invalid" fn inherent_fn(self) {
21+
//~^ ERROR: is not a supported ABI
22+
(self.ptr)()
23+
}
24+
}
25+
26+
pub trait Trait {
27+
extern "rust-invalid" fn trait_fn(self);
28+
//~^ ERROR: is not a supported ABI
29+
}
30+
31+
impl Trait for FnPtrBearer {
32+
extern "rust-invalid" fn trait_fn(self) {
33+
//~^ ERROR: is not a supported ABI
34+
self.inherent_fn()
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0570]: "rust-invalid" is not a supported ABI for the current target
2+
--> $DIR/unsupported-in-impls.rs:15:21
3+
|
4+
LL | pub ptr: extern "rust-invalid" fn(),
5+
| ^^^^^^^^^^^^^^
6+
7+
error[E0570]: "rust-invalid" is not a supported ABI for the current target
8+
--> $DIR/unsupported-in-impls.rs:20:16
9+
|
10+
LL | pub extern "rust-invalid" fn inherent_fn(self) {
11+
| ^^^^^^^^^^^^^^
12+
13+
error[E0570]: "rust-invalid" is not a supported ABI for the current target
14+
--> $DIR/unsupported-in-impls.rs:27:12
15+
|
16+
LL | extern "rust-invalid" fn trait_fn(self);
17+
| ^^^^^^^^^^^^^^
18+
19+
error[E0570]: "rust-invalid" is not a supported ABI for the current target
20+
--> $DIR/unsupported-in-impls.rs:32:12
21+
|
22+
LL | extern "rust-invalid" fn trait_fn(self) {
23+
| ^^^^^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0570`.

tests/ui/abi/unsupported-varargs-fnptr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// FIXME(workingjubilee): add revisions and generalize to other platform-specific varargs ABIs,
22
// preferably after the only-arch directive is enhanced with an "or pattern" syntax
3+
// NOTE: This deliberately tests an ABI that supports varargs, so no `extern "rust-invalid"`
34
//@ only-x86_64
45

56
// We have to use this flag to force ABI computation of an invalid ABI

tests/ui/abi/unsupported-varargs-fnptr.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0570]: "aapcs" is not a supported ABI for the current target
2-
--> $DIR/unsupported-varargs-fnptr.rs:13:20
2+
--> $DIR/unsupported-varargs-fnptr.rs:14:20
33
|
44
LL | fn aapcs(f: extern "aapcs" fn(usize, ...)) {
55
| ^^^^^^^

tests/ui/abi/unsupported.aarch64.stderr

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,30 +156,6 @@ error[E0570]: "cmse-nonsecure-entry" is not a supported ABI for the current targ
156156
LL | extern "cmse-nonsecure-entry" {}
157157
| ^^^^^^^^^^^^^^^^^^^^^^
158158

159-
error[E0570]: "thiscall" is not a supported ABI for the current target
160-
--> $DIR/unsupported.rs:141:17
161-
|
162-
LL | ptr: extern "thiscall" fn(),
163-
| ^^^^^^^^^^
164-
165-
error[E0570]: "thiscall" is not a supported ABI for the current target
166-
--> $DIR/unsupported.rs:146:16
167-
|
168-
LL | pub extern "thiscall" fn inherent_fn(self) {
169-
| ^^^^^^^^^^
170-
171-
error[E0570]: "thiscall" is not a supported ABI for the current target
172-
--> $DIR/unsupported.rs:153:12
173-
|
174-
LL | extern "thiscall" fn trait_fn(self);
175-
| ^^^^^^^^^^
176-
177-
error[E0570]: "thiscall" is not a supported ABI for the current target
178-
--> $DIR/unsupported.rs:158:12
179-
|
180-
LL | extern "thiscall" fn trait_fn(self) {
181-
| ^^^^^^^^^^
182-
183159
warning: "cdecl" is not a supported ABI for the current target
184160
--> $DIR/unsupported.rs:99:17
185161
|
@@ -221,6 +197,6 @@ LL | extern "cdecl" fn cdecl() {}
221197
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
222198
= help: use `extern "C"` instead
223199

224-
error: aborting due to 29 previous errors; 4 warnings emitted
200+
error: aborting due to 25 previous errors; 4 warnings emitted
225201

226202
For more information about this error, try `rustc --explain E0570`.

tests/ui/abi/unsupported.arm.stderr

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,6 @@ error[E0570]: "cmse-nonsecure-entry" is not a supported ABI for the current targ
138138
LL | extern "cmse-nonsecure-entry" {}
139139
| ^^^^^^^^^^^^^^^^^^^^^^
140140

141-
error[E0570]: "thiscall" is not a supported ABI for the current target
142-
--> $DIR/unsupported.rs:141:17
143-
|
144-
LL | ptr: extern "thiscall" fn(),
145-
| ^^^^^^^^^^
146-
147-
error[E0570]: "thiscall" is not a supported ABI for the current target
148-
--> $DIR/unsupported.rs:146:16
149-
|
150-
LL | pub extern "thiscall" fn inherent_fn(self) {
151-
| ^^^^^^^^^^
152-
153-
error[E0570]: "thiscall" is not a supported ABI for the current target
154-
--> $DIR/unsupported.rs:153:12
155-
|
156-
LL | extern "thiscall" fn trait_fn(self);
157-
| ^^^^^^^^^^
158-
159-
error[E0570]: "thiscall" is not a supported ABI for the current target
160-
--> $DIR/unsupported.rs:158:12
161-
|
162-
LL | extern "thiscall" fn trait_fn(self) {
163-
| ^^^^^^^^^^
164-
165141
warning: "cdecl" is not a supported ABI for the current target
166142
--> $DIR/unsupported.rs:99:17
167143
|
@@ -203,6 +179,6 @@ LL | extern "cdecl" fn cdecl() {}
203179
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
204180
= help: use `extern "C"` instead
205181

206-
error: aborting due to 26 previous errors; 4 warnings emitted
182+
error: aborting due to 22 previous errors; 4 warnings emitted
207183

208184
For more information about this error, try `rustc --explain E0570`.

tests/ui/abi/unsupported.riscv32.stderr

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -150,30 +150,6 @@ error[E0570]: "cmse-nonsecure-entry" is not a supported ABI for the current targ
150150
LL | extern "cmse-nonsecure-entry" {}
151151
| ^^^^^^^^^^^^^^^^^^^^^^
152152

153-
error[E0570]: "thiscall" is not a supported ABI for the current target
154-
--> $DIR/unsupported.rs:141:17
155-
|
156-
LL | ptr: extern "thiscall" fn(),
157-
| ^^^^^^^^^^
158-
159-
error[E0570]: "thiscall" is not a supported ABI for the current target
160-
--> $DIR/unsupported.rs:146:16
161-
|
162-
LL | pub extern "thiscall" fn inherent_fn(self) {
163-
| ^^^^^^^^^^
164-
165-
error[E0570]: "thiscall" is not a supported ABI for the current target
166-
--> $DIR/unsupported.rs:153:12
167-
|
168-
LL | extern "thiscall" fn trait_fn(self);
169-
| ^^^^^^^^^^
170-
171-
error[E0570]: "thiscall" is not a supported ABI for the current target
172-
--> $DIR/unsupported.rs:158:12
173-
|
174-
LL | extern "thiscall" fn trait_fn(self) {
175-
| ^^^^^^^^^^
176-
177153
warning: "cdecl" is not a supported ABI for the current target
178154
--> $DIR/unsupported.rs:99:17
179155
|
@@ -215,6 +191,6 @@ LL | extern "cdecl" fn cdecl() {}
215191
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
216192
= help: use `extern "C"` instead
217193

218-
error: aborting due to 28 previous errors; 4 warnings emitted
194+
error: aborting due to 24 previous errors; 4 warnings emitted
219195

220196
For more information about this error, try `rustc --explain E0570`.

tests/ui/abi/unsupported.riscv64.stderr

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -150,30 +150,6 @@ error[E0570]: "cmse-nonsecure-entry" is not a supported ABI for the current targ
150150
LL | extern "cmse-nonsecure-entry" {}
151151
| ^^^^^^^^^^^^^^^^^^^^^^
152152

153-
error[E0570]: "thiscall" is not a supported ABI for the current target
154-
--> $DIR/unsupported.rs:141:17
155-
|
156-
LL | ptr: extern "thiscall" fn(),
157-
| ^^^^^^^^^^
158-
159-
error[E0570]: "thiscall" is not a supported ABI for the current target
160-
--> $DIR/unsupported.rs:146:16
161-
|
162-
LL | pub extern "thiscall" fn inherent_fn(self) {
163-
| ^^^^^^^^^^
164-
165-
error[E0570]: "thiscall" is not a supported ABI for the current target
166-
--> $DIR/unsupported.rs:153:12
167-
|
168-
LL | extern "thiscall" fn trait_fn(self);
169-
| ^^^^^^^^^^
170-
171-
error[E0570]: "thiscall" is not a supported ABI for the current target
172-
--> $DIR/unsupported.rs:158:12
173-
|
174-
LL | extern "thiscall" fn trait_fn(self) {
175-
| ^^^^^^^^^^
176-
177153
warning: "cdecl" is not a supported ABI for the current target
178154
--> $DIR/unsupported.rs:99:17
179155
|
@@ -215,6 +191,6 @@ LL | extern "cdecl" fn cdecl() {}
215191
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
216192
= help: use `extern "C"` instead
217193

218-
error: aborting due to 28 previous errors; 4 warnings emitted
194+
error: aborting due to 24 previous errors; 4 warnings emitted
219195

220196
For more information about this error, try `rustc --explain E0570`.

tests/ui/abi/unsupported.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,3 @@ extern "cmse-nonsecure-entry" {}
136136
extern "cdecl" {}
137137
//[x64_win]~^ WARN unsupported_calling_conventions
138138
//[x64_win]~^^ WARN this was previously accepted
139-
140-
struct FnPtrBearer {
141-
ptr: extern "thiscall" fn(),
142-
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
143-
}
144-
145-
impl FnPtrBearer {
146-
pub extern "thiscall" fn inherent_fn(self) {
147-
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
148-
(self.ptr)()
149-
}
150-
}
151-
152-
trait Trait {
153-
extern "thiscall" fn trait_fn(self);
154-
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
155-
}
156-
157-
impl Trait for FnPtrBearer {
158-
extern "thiscall" fn trait_fn(self) {
159-
//[x64,x64_win,arm,aarch64,riscv32,riscv64]~^ ERROR: is not a supported ABI
160-
self.inherent_fn()
161-
}
162-
}

0 commit comments

Comments
 (0)