Skip to content

Commit 2b9fce8

Browse files
committed
c-variadic: reject non-extern functions
1 parent 7075000 commit 2b9fce8

9 files changed

+26
-23
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ast_passes_bound_in_context = bounds on `type`s in {$ctx} have no effect
6868
6969
ast_passes_c_variadic_associated_function = associated functions cannot have a C variable argument list
7070
71+
ast_passes_c_variadic_no_extern = `...` is not supported for non-extern functions
72+
7173
ast_passes_const_and_c_variadic = functions cannot be both `const` and C-variadic
7274
.const = `const` because of this
7375
.variadic = C-variadic because of this

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ impl<'a> AstValidator<'a> {
714714
self.dcx().emit_err(errors::BadCVariadic { span: variadic_param.span });
715715
}
716716
Extern::None => {
717-
self.dcx().emit_err(errors::BadCVariadic { span: variadic_param.span });
717+
let err = errors::CVariadicNoExtern { span: variadic_param.span };
718+
self.dcx().emit_err(err);
718719
}
719720
},
720721
FnCtxt::Assoc(_) => {

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ pub(crate) struct CVariadicAssociatedFunction {
325325
pub span: Span,
326326
}
327327

328+
#[derive(Diagnostic)]
329+
#[diag(ast_passes_c_variadic_no_extern)]
330+
pub(crate) struct CVariadicNoExtern {
331+
#[primary_span]
332+
pub span: Span,
333+
}
334+
328335
#[derive(Diagnostic)]
329336
#[diag(ast_passes_bad_c_variadic)]
330337
pub(crate) struct BadCVariadic {

tests/ui/c-variadic/issue-86053-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ fn ordering4 < 'a , 'b > ( a : , self , self , self ,
1313
//~| ERROR unexpected `self` parameter in function
1414
//~| ERROR unexpected `self` parameter in function
1515
//~| ERROR `...` must be the last argument of a C-variadic function
16-
//~| ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
16+
//~| ERROR `...` is not supported for non-extern functions
1717
//~| ERROR cannot find type `F` in this scope
1818
}

tests/ui/c-variadic/issue-86053-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ error: `...` must be the last argument of a C-variadic function
4646
LL | self , ... , self , self , ... ) where F : FnOnce ( & 'a & 'b usize ) {
4747
| ^^^
4848

49-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
49+
error: `...` is not supported for non-extern functions
5050
--> $DIR/issue-86053-1.rs:11:36
5151
|
5252
LL | self , ... , self , self , ... ) where F : FnOnce ( & 'a & 'b usize ) {

tests/ui/mir/issue-83499-input-output-iteration-ice.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
fn main() {}
66

7-
fn foo(_: Bar, ...) -> impl {}
8-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
9-
//~| ERROR cannot find type `Bar` in this scope
7+
unsafe extern "C" fn foo(_: Bar, ...) -> impl {}
8+
//~^ ERROR cannot find type `Bar` in this scope
109
//~| ERROR at least one trait must be specified
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
2-
--> $DIR/issue-83499-input-output-iteration-ice.rs:7:16
3-
|
4-
LL | fn foo(_: Bar, ...) -> impl {}
5-
| ^^^
6-
71
error: at least one trait must be specified
8-
--> $DIR/issue-83499-input-output-iteration-ice.rs:7:24
2+
--> $DIR/issue-83499-input-output-iteration-ice.rs:7:42
93
|
10-
LL | fn foo(_: Bar, ...) -> impl {}
11-
| ^^^^
4+
LL | unsafe extern "C" fn foo(_: Bar, ...) -> impl {}
5+
| ^^^^
126

137
error[E0412]: cannot find type `Bar` in this scope
14-
--> $DIR/issue-83499-input-output-iteration-ice.rs:7:11
8+
--> $DIR/issue-83499-input-output-iteration-ice.rs:7:29
159
|
16-
LL | fn foo(_: Bar, ...) -> impl {}
17-
| ^^^ not found in this scope
10+
LL | unsafe extern "C" fn foo(_: Bar, ...) -> impl {}
11+
| ^^^ not found in this scope
1812

19-
error: aborting due to 3 previous errors
13+
error: aborting due to 2 previous errors
2014

2115
For more information about this error, try `rustc --explain E0412`.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
fn main() {}
55

66
fn f1_1(x: isize, ...) {}
7-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
7+
//~^ ERROR `...` is not supported for non-extern functions
88

99
fn f1_2(...) {}
10-
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
10+
//~^ ERROR `...` is not supported for non-extern functions
1111

1212
extern "C" fn f2_1(x: isize, ...) {}
1313
//~^ ERROR defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
1+
error: `...` is not supported for non-extern functions
22
--> $DIR/variadic-ffi-semantic-restrictions.rs:6:19
33
|
44
LL | fn f1_1(x: isize, ...) {}
55
| ^^^
66

7-
error: defining functions with C-variadic arguments is only allowed for free functions with the "C" or "C-unwind" calling convention
7+
error: `...` is not supported for non-extern functions
88
--> $DIR/variadic-ffi-semantic-restrictions.rs:9:9
99
|
1010
LL | fn f1_2(...) {}

0 commit comments

Comments
 (0)