Skip to content

Commit a093372

Browse files
committed
disallow c-variadic associated functions (for now)
there is no reason this should not work, really, we're just cutting some scope for now
1 parent 1656f6c commit a093372

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ ast_passes_body_in_extern = incorrect `{$kind}` inside `extern` block
6666
6767
ast_passes_bound_in_context = bounds on `type`s in {$ctx} have no effect
6868
69+
ast_passes_c_variadic_associated_function = associated functions cannot have a C variable argument list
70+
6971
ast_passes_const_and_c_variadic = functions cannot be both `const` and C-variadic
7072
.const = `const` because of this
7173
.variadic = C-variadic because of this

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,16 @@ impl<'a> AstValidator<'a> {
704704
{
705705
return;
706706
}
707-
_ => {}
707+
_ => {
708+
self.dcx().emit_err(errors::BadCVariadic { span: variadic_param.span });
709+
}
708710
},
709-
FnCtxt::Assoc(_) => {}
710-
};
711-
712-
self.dcx().emit_err(errors::BadCVariadic { span: variadic_param.span });
711+
FnCtxt::Assoc(_) => {
712+
// For now, C variable argument lists are unsupported in associated functions.
713+
let err = errors::CVariadicAssociatedFunction { span: variadic_param.span };
714+
self.dcx().emit_err(err);
715+
}
716+
}
713717
}
714718

715719
fn check_item_named(&self, ident: Ident, kind: &str) {

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ pub(crate) struct ExternItemAscii {
318318
pub block: Span,
319319
}
320320

321+
#[derive(Diagnostic)]
322+
#[diag(ast_passes_c_variadic_associated_function)]
323+
pub(crate) struct CVariadicAssociatedFunction {
324+
#[primary_span]
325+
pub span: Span,
326+
}
327+
321328
#[derive(Diagnostic)]
322329
#[diag(ast_passes_bad_c_variadic)]
323330
pub(crate) struct BadCVariadic {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#![crate_type="lib"]
1+
#![crate_type = "lib"]
22

3-
pub unsafe extern "C" fn test(_: i32, ap: ...) { }
3+
pub unsafe extern "C" fn test(_: i32, ap: ...) {}
44
//~^ ERROR C-variadic functions are unstable
55

66
trait Trait {
7-
unsafe extern "C" fn trait_test(_: i32, ap: ...) { }
7+
unsafe extern "C" fn trait_test(_: i32, ap: ...) {}
88
//~^ ERROR C-variadic functions are unstable
9-
//~| ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
9+
//~| ERROR associated functions cannot have a C variable argument list
1010
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
1+
error: associated functions cannot have a C variable argument list
22
--> $DIR/feature-gate-c_variadic.rs:7:45
33
|
4-
LL | unsafe extern "C" fn trait_test(_: i32, ap: ...) { }
4+
LL | unsafe extern "C" fn trait_test(_: i32, ap: ...) {}
55
| ^^^^^^^
66

77
error[E0658]: C-variadic functions are unstable
88
--> $DIR/feature-gate-c_variadic.rs:3:1
99
|
10-
LL | pub unsafe extern "C" fn test(_: i32, ap: ...) { }
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | pub unsafe extern "C" fn test(_: i32, ap: ...) {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
|
1313
= note: see issue #44930 <https://github.com/rust-lang/rust/issues/44930> for more information
1414
= help: add `#![feature(c_variadic)]` to the crate attributes to enable
@@ -17,8 +17,8 @@ LL | pub unsafe extern "C" fn test(_: i32, ap: ...) { }
1717
error[E0658]: C-variadic functions are unstable
1818
--> $DIR/feature-gate-c_variadic.rs:7:5
1919
|
20-
LL | unsafe extern "C" fn trait_test(_: i32, ap: ...) { }
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
LL | unsafe extern "C" fn trait_test(_: i32, ap: ...) {}
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2222
|
2323
= note: see issue #44930 <https://github.com/rust-lang/rust/issues/44930> for more information
2424
= help: add `#![feature(c_variadic)]` to the crate attributes to enable

tests/ui/parser/variadic-ffi-semantic-restrictions.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,30 @@ struct X;
5050

5151
impl X {
5252
fn i_f1(x: isize, ...) {}
53-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
53+
//~^ ERROR associated functions cannot have a C variable argument list
5454
fn i_f2(...) {}
55-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
55+
//~^ ERROR associated functions cannot have a C variable argument list
5656
fn i_f3(..., x: isize, ...) {}
57-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
57+
//~^ ERROR associated functions cannot have a C variable argument list
5858
//~| ERROR `...` must be the last argument of a C-variadic function
5959
fn i_f4(..., x: isize, ...) {}
60-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
60+
//~^ ERROR associated functions cannot have a C variable argument list
6161
//~| ERROR `...` must be the last argument of a C-variadic function
6262
const fn i_f5(x: isize, ...) {}
63-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
63+
//~^ ERROR associated functions cannot have a C variable argument list
6464
//~| ERROR functions cannot be both `const` and C-variadic
6565
//~| ERROR destructor of `VaListImpl<'_>` cannot be evaluated at compile-time
6666
}
6767

6868
trait T {
6969
fn t_f1(x: isize, ...) {}
70-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
70+
//~^ ERROR associated functions cannot have a C variable argument list
7171
fn t_f2(x: isize, ...);
72-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
72+
//~^ ERROR associated functions cannot have a C variable argument list
7373
fn t_f3(...) {}
74-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
74+
//~^ ERROR associated functions cannot have a C variable argument list
7575
fn t_f4(...);
76-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
76+
//~^ ERROR associated functions cannot have a C variable argument list
7777
fn t_f5(..., x: isize) {}
7878
//~^ ERROR `...` must be the last argument of a C-variadic function
7979
fn t_f6(..., x: isize);

tests/ui/parser/variadic-ffi-semantic-restrictions.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ error: `...` must be the last argument of a C-variadic function
8888
LL | fn e_f2(..., x: isize);
8989
| ^^^
9090

91-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
91+
error: associated functions cannot have a C variable argument list
9292
--> $DIR/variadic-ffi-semantic-restrictions.rs:52:23
9393
|
9494
LL | fn i_f1(x: isize, ...) {}
9595
| ^^^
9696

97-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
97+
error: associated functions cannot have a C variable argument list
9898
--> $DIR/variadic-ffi-semantic-restrictions.rs:54:13
9999
|
100100
LL | fn i_f2(...) {}
@@ -106,7 +106,7 @@ error: `...` must be the last argument of a C-variadic function
106106
LL | fn i_f3(..., x: isize, ...) {}
107107
| ^^^
108108

109-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
109+
error: associated functions cannot have a C variable argument list
110110
--> $DIR/variadic-ffi-semantic-restrictions.rs:56:28
111111
|
112112
LL | fn i_f3(..., x: isize, ...) {}
@@ -118,7 +118,7 @@ error: `...` must be the last argument of a C-variadic function
118118
LL | fn i_f4(..., x: isize, ...) {}
119119
| ^^^
120120

121-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
121+
error: associated functions cannot have a C variable argument list
122122
--> $DIR/variadic-ffi-semantic-restrictions.rs:59:28
123123
|
124124
LL | fn i_f4(..., x: isize, ...) {}
@@ -132,31 +132,31 @@ LL | const fn i_f5(x: isize, ...) {}
132132
| |
133133
| `const` because of this
134134

135-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
135+
error: associated functions cannot have a C variable argument list
136136
--> $DIR/variadic-ffi-semantic-restrictions.rs:62:29
137137
|
138138
LL | const fn i_f5(x: isize, ...) {}
139139
| ^^^
140140

141-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
141+
error: associated functions cannot have a C variable argument list
142142
--> $DIR/variadic-ffi-semantic-restrictions.rs:69:23
143143
|
144144
LL | fn t_f1(x: isize, ...) {}
145145
| ^^^
146146

147-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
147+
error: associated functions cannot have a C variable argument list
148148
--> $DIR/variadic-ffi-semantic-restrictions.rs:71:23
149149
|
150150
LL | fn t_f2(x: isize, ...);
151151
| ^^^
152152

153-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
153+
error: associated functions cannot have a C variable argument list
154154
--> $DIR/variadic-ffi-semantic-restrictions.rs:73:13
155155
|
156156
LL | fn t_f3(...) {}
157157
| ^^^
158158

159-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
159+
error: associated functions cannot have a C variable argument list
160160
--> $DIR/variadic-ffi-semantic-restrictions.rs:75:13
161161
|
162162
LL | fn t_f4(...);

0 commit comments

Comments
 (0)