Skip to content

Commit fe54c3a

Browse files
authored
Rollup merge of #142464 - RalfJung:variadic-fn-abi-error, r=workingjubilee
variadic functions: remove list of supported ABIs from error I think this list is problematic for multiple reasons: - It is bound to go out-of-date as it is in a very different place from where we actually define which functions support varagrs (`fn supports_varargs`). - Many of the ABIs we list only work on some targets; it makes no sense to mention "aapcs" as a possible ABI when building for x86_64. (This led to a lot of confusion in #110505 where the author thought they should use "cdecl" and then were promptly told that "cdecl" is not a legal ABI on their target.) - Typically, when the programmer wrote `extern "foobar"`, it is because they need the "foobar" ABI. It is of little use to tell them that there are other ABIs with which varargs would work. Cc ``@workingjubilee``
2 parents 4cf4473 + 963fdbc commit fe54c3a

14 files changed

+34
-44
lines changed

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ hir_analysis_value_of_associated_struct_already_specified =
599599
.label = re-bound here
600600
.previous_bound_label = `{$item_name}` bound here first
601601
602-
hir_analysis_variadic_function_compatible_convention = C-variadic function must have a compatible calling convention, like {$conventions}
602+
hir_analysis_variadic_function_compatible_convention = C-variadic functions with the {$convention} calling convention are not supported
603603
.label = C-variadic function must have a compatible calling convention
604604
605605
hir_analysis_variances_of = {$variances}

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ pub(crate) struct VariadicFunctionCompatibleConvention<'a> {
633633
#[primary_span]
634634
#[label]
635635
pub span: Span,
636-
pub conventions: &'a str,
636+
pub convention: &'a str,
637637
}
638638

639639
#[derive(Diagnostic)]

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@ fn require_c_abi_if_c_variadic(
115115
abi: ExternAbi,
116116
span: Span,
117117
) {
118-
const CONVENTIONS_UNSTABLE: &str =
119-
"`C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`";
120-
const CONVENTIONS_STABLE: &str = "`C` or `cdecl`";
121-
const UNSTABLE_EXPLAIN: &str =
122-
"using calling conventions other than `C` or `cdecl` for varargs functions is unstable";
123-
124118
// ABIs which can stably use varargs
125119
if !decl.c_variadic || matches!(abi, ExternAbi::C { .. } | ExternAbi::Cdecl { .. }) {
126120
return;
@@ -140,20 +134,18 @@ fn require_c_abi_if_c_variadic(
140134

141135
// Looks like we need to pick an error to emit.
142136
// Is there any feature which we could have enabled to make this work?
137+
let unstable_explain =
138+
format!("C-variadic functions with the {abi} calling convention are unstable");
143139
match abi {
144140
ExternAbi::System { .. } => {
145-
feature_err(&tcx.sess, sym::extern_system_varargs, span, UNSTABLE_EXPLAIN)
141+
feature_err(&tcx.sess, sym::extern_system_varargs, span, unstable_explain)
146142
}
147143
abi if abi.supports_varargs() => {
148-
feature_err(&tcx.sess, sym::extended_varargs_abi_support, span, UNSTABLE_EXPLAIN)
144+
feature_err(&tcx.sess, sym::extended_varargs_abi_support, span, unstable_explain)
149145
}
150146
_ => tcx.dcx().create_err(errors::VariadicFunctionCompatibleConvention {
151147
span,
152-
conventions: if tcx.sess.opts.unstable_features.is_nightly_build() {
153-
CONVENTIONS_UNSTABLE
154-
} else {
155-
CONVENTIONS_STABLE
156-
},
148+
convention: &format!("{abi}"),
157149
}),
158150
}
159151
.emit();

tests/ui/c-variadic/feature-gate-extended_varargs_abi_support.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//@ only-x86_64
22

33
fn efiapi(f: extern "efiapi" fn(usize, ...)) {
4-
//~^ ERROR: using calling conventions other than `C` or `cdecl` for varargs functions is unstable
4+
//~^ ERROR: unstable
55
f(22, 44);
66
}
77
fn sysv(f: extern "sysv64" fn(usize, ...)) {
8-
//~^ ERROR: using calling conventions other than `C` or `cdecl` for varargs functions is unstable
8+
//~^ ERROR: unstable
99
f(22, 44);
1010
}
1111
fn win(f: extern "win64" fn(usize, ...)) {
12-
//~^ ERROR: using calling conventions other than `C` or `cdecl` for varargs functions is unstable
12+
//~^ ERROR: unstable
1313
f(22, 44);
1414
}
1515

tests/ui/c-variadic/feature-gate-extended_varargs_abi_support.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: using calling conventions other than `C` or `cdecl` for varargs functions is unstable
1+
error[E0658]: C-variadic functions with the "efiapi" calling convention are unstable
22
--> $DIR/feature-gate-extended_varargs_abi_support.rs:3:14
33
|
44
LL | fn efiapi(f: extern "efiapi" fn(usize, ...)) {
@@ -8,7 +8,7 @@ LL | fn efiapi(f: extern "efiapi" fn(usize, ...)) {
88
= help: add `#![feature(extended_varargs_abi_support)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

11-
error[E0658]: using calling conventions other than `C` or `cdecl` for varargs functions is unstable
11+
error[E0658]: C-variadic functions with the "sysv64" calling convention are unstable
1212
--> $DIR/feature-gate-extended_varargs_abi_support.rs:7:12
1313
|
1414
LL | fn sysv(f: extern "sysv64" fn(usize, ...)) {
@@ -18,7 +18,7 @@ LL | fn sysv(f: extern "sysv64" fn(usize, ...)) {
1818
= help: add `#![feature(extended_varargs_abi_support)]` to the crate attributes to enable
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

21-
error[E0658]: using calling conventions other than `C` or `cdecl` for varargs functions is unstable
21+
error[E0658]: C-variadic functions with the "win64" calling convention are unstable
2222
--> $DIR/feature-gate-extended_varargs_abi_support.rs:11:11
2323
|
2424
LL | fn win(f: extern "win64" fn(usize, ...)) {

tests/ui/c-variadic/variadic-ffi-1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use minicore::*;
99

1010
extern "stdcall" {
1111
fn printf(_: *const u8, ...);
12-
//~^ ERROR: C-variadic function must have a compatible calling convention,
13-
// like C, cdecl, win64, sysv64 or efiapi
12+
//~^ ERROR: C-variadic functions with the "stdcall" calling convention are not supported
1413
}
1514

1615
extern "C" {

tests/ui/c-variadic/variadic-ffi-1.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
error[E0045]: C-variadic function must have a compatible calling convention, like `C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`
1+
error[E0045]: C-variadic functions with the "stdcall" calling convention are not supported
22
--> $DIR/variadic-ffi-1.rs:11:5
33
|
44
LL | fn printf(_: *const u8, ...);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C-variadic function must have a compatible calling convention
66

77
error[E0060]: this function takes at least 2 arguments but 0 arguments were supplied
8-
--> $DIR/variadic-ffi-1.rs:24:9
8+
--> $DIR/variadic-ffi-1.rs:23:9
99
|
1010
LL | foo();
1111
| ^^^-- two arguments of type `isize` and `u8` are missing
1212
|
1313
note: function defined here
14-
--> $DIR/variadic-ffi-1.rs:17:8
14+
--> $DIR/variadic-ffi-1.rs:16:8
1515
|
1616
LL | fn foo(f: isize, x: u8, ...);
1717
| ^^^ - -
@@ -21,13 +21,13 @@ LL | foo(/* isize */, /* u8 */);
2121
| +++++++++++++++++++++
2222

2323
error[E0060]: this function takes at least 2 arguments but 1 argument was supplied
24-
--> $DIR/variadic-ffi-1.rs:25:9
24+
--> $DIR/variadic-ffi-1.rs:24:9
2525
|
2626
LL | foo(1);
2727
| ^^^--- argument #2 of type `u8` is missing
2828
|
2929
note: function defined here
30-
--> $DIR/variadic-ffi-1.rs:17:8
30+
--> $DIR/variadic-ffi-1.rs:16:8
3131
|
3232
LL | fn foo(f: isize, x: u8, ...);
3333
| ^^^ -
@@ -37,7 +37,7 @@ LL | foo(1, /* u8 */);
3737
| ++++++++++
3838

3939
error[E0308]: mismatched types
40-
--> $DIR/variadic-ffi-1.rs:27:56
40+
--> $DIR/variadic-ffi-1.rs:26:56
4141
|
4242
LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
4343
| ------------------------------------- ^^^ expected non-variadic fn, found variadic function
@@ -48,7 +48,7 @@ LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
4848
found fn item `unsafe extern "C" fn(_, _, ...) {foo}`
4949

5050
error[E0308]: mismatched types
51-
--> $DIR/variadic-ffi-1.rs:28:54
51+
--> $DIR/variadic-ffi-1.rs:27:54
5252
|
5353
LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
5454
| ----------------------------------- ^^^ expected variadic fn, found non-variadic function
@@ -59,7 +59,7 @@ LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
5959
found fn item `extern "C" fn(_, _) {bar}`
6060

6161
error[E0617]: can't pass `f32` to variadic function
62-
--> $DIR/variadic-ffi-1.rs:30:19
62+
--> $DIR/variadic-ffi-1.rs:29:19
6363
|
6464
LL | foo(1, 2, 3f32);
6565
| ^^^^
@@ -70,7 +70,7 @@ LL | foo(1, 2, 3f32 as c_double);
7070
| +++++++++++
7171

7272
error[E0617]: can't pass `bool` to variadic function
73-
--> $DIR/variadic-ffi-1.rs:31:19
73+
--> $DIR/variadic-ffi-1.rs:30:19
7474
|
7575
LL | foo(1, 2, true);
7676
| ^^^^
@@ -81,7 +81,7 @@ LL | foo(1, 2, true as c_int);
8181
| ++++++++
8282

8383
error[E0617]: can't pass `i8` to variadic function
84-
--> $DIR/variadic-ffi-1.rs:32:19
84+
--> $DIR/variadic-ffi-1.rs:31:19
8585
|
8686
LL | foo(1, 2, 1i8);
8787
| ^^^
@@ -92,7 +92,7 @@ LL | foo(1, 2, 1i8 as c_int);
9292
| ++++++++
9393

9494
error[E0617]: can't pass `u8` to variadic function
95-
--> $DIR/variadic-ffi-1.rs:33:19
95+
--> $DIR/variadic-ffi-1.rs:32:19
9696
|
9797
LL | foo(1, 2, 1u8);
9898
| ^^^
@@ -103,7 +103,7 @@ LL | foo(1, 2, 1u8 as c_uint);
103103
| +++++++++
104104

105105
error[E0617]: can't pass `i16` to variadic function
106-
--> $DIR/variadic-ffi-1.rs:34:19
106+
--> $DIR/variadic-ffi-1.rs:33:19
107107
|
108108
LL | foo(1, 2, 1i16);
109109
| ^^^^
@@ -114,7 +114,7 @@ LL | foo(1, 2, 1i16 as c_int);
114114
| ++++++++
115115

116116
error[E0617]: can't pass `u16` to variadic function
117-
--> $DIR/variadic-ffi-1.rs:35:19
117+
--> $DIR/variadic-ffi-1.rs:34:19
118118
|
119119
LL | foo(1, 2, 1u16);
120120
| ^^^^

tests/ui/c-variadic/variadic-ffi-2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![feature(extended_varargs_abi_support)]
22

33
fn baz(f: extern "Rust" fn(usize, ...)) {
4-
//~^ ERROR: C-variadic function must have a compatible calling convention,
5-
// like C, cdecl, system, aapcs, win64, sysv64 or efiapi
4+
//~^ ERROR: C-variadic functions with the "Rust" calling convention are not supported
65
f(22, 44);
76
}
87

tests/ui/c-variadic/variadic-ffi-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0045]: C-variadic function must have a compatible calling convention, like `C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`
1+
error[E0045]: C-variadic functions with the "Rust" calling convention are not supported
22
--> $DIR/variadic-ffi-2.rs:3:11
33
|
44
LL | fn baz(f: extern "Rust" fn(usize, ...)) {

tests/ui/cmse-nonsecure/cmse-nonsecure-call/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ type WithTransparentTraitObject =
3838
//~^ ERROR return value of `"C-cmse-nonsecure-call"` function too large to pass via registers [E0798]
3939

4040
type WithVarArgs = extern "C-cmse-nonsecure-call" fn(u32, ...);
41-
//~^ ERROR C-variadic function must have a compatible calling convention, like `C`
41+
//~^ ERROR C-variadic functions with the "C-cmse-nonsecure-call" calling convention are not supported

tests/ui/cmse-nonsecure/cmse-nonsecure-call/generics.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ LL | extern "C-cmse-nonsecure-call" fn(WrapperTransparent) -> WrapperTranspa
6969
= note: functions with the `"C-cmse-nonsecure-call"` ABI must pass their result via the available return registers
7070
= note: the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
7171

72-
error[E0045]: C-variadic function must have a compatible calling convention, like `C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`
72+
error[E0045]: C-variadic functions with the "C-cmse-nonsecure-call" calling convention are not supported
7373
--> $DIR/generics.rs:40:20
7474
|
7575
LL | type WithVarArgs = extern "C-cmse-nonsecure-call" fn(u32, ...);

tests/ui/error-codes/E0045.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0045]: C-variadic function must have a compatible calling convention, like `C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`
1+
error[E0045]: C-variadic functions with the "Rust" calling convention are not supported
22
--> $DIR/E0045.rs:1:17
33
|
44
LL | extern "Rust" { fn foo(x: u8, ...); }

tests/ui/feature-gates/feature-gate-extern_system_varargs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn system(f: extern "system" fn(usize, ...)) {
2-
//~^ ERROR using calling conventions other than `C` or `cdecl` for varargs functions is unstable
2+
//~^ ERROR unstable
33

44
f(22, 44);
55
}

tests/ui/feature-gates/feature-gate-extern_system_varargs.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: using calling conventions other than `C` or `cdecl` for varargs functions is unstable
1+
error[E0658]: C-variadic functions with the "system" calling convention are unstable
22
--> $DIR/feature-gate-extern_system_varargs.rs:1:14
33
|
44
LL | fn system(f: extern "system" fn(usize, ...)) {

0 commit comments

Comments
 (0)