Skip to content
/ rust Public
forked from rust-lang/rust

Commit

Permalink
Rollup merge of rust-lang#124048 - veera-sivarajan:bugfix-123773-c23-…
Browse files Browse the repository at this point in the history
…variadics, r=compiler-errors

Support C23's Variadics Without a Named Parameter

Fixes rust-lang#123773

This PR removes the static check that disallowed extern functions
with ellipsis (varargs) as the only parameter since this is now
valid in C23.

This will not break any existing code as mentioned in the proposal
document: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2975.pdf.

Also, adds a doc comment for `check_decl_cvariadic_pos()` and
fixes the name of the function (`varadic` -> `variadic`).
  • Loading branch information
jhpratt authored May 26, 2024
2 parents bdbbb6c + f005b45 commit ec0d36b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 111 deletions.
3 changes: 0 additions & 3 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ ast_passes_fn_body_extern = incorrect function inside `extern` block
ast_passes_fn_param_c_var_args_not_last =
`...` must be the last argument of a C-variadic function
ast_passes_fn_param_c_var_args_only =
C-variadic function must be declared with at least one named argument
ast_passes_fn_param_doc_comment =
documentation comments cannot be applied to function parameters
.label = doc comments are not allowed here
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl<'a> AstValidator<'a> {

fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
self.check_decl_num_args(fn_decl);
self.check_decl_cvaradic_pos(fn_decl);
self.check_decl_cvariadic_pos(fn_decl);
self.check_decl_attrs(fn_decl);
self.check_decl_self_param(fn_decl, self_semantic);
}
Expand All @@ -379,13 +379,11 @@ impl<'a> AstValidator<'a> {
}
}

fn check_decl_cvaradic_pos(&self, fn_decl: &FnDecl) {
/// Emits an error if a function declaration has a variadic parameter in the
/// beginning or middle of parameter list.
/// Example: `fn foo(..., x: i32)` will emit an error.
fn check_decl_cvariadic_pos(&self, fn_decl: &FnDecl) {
match &*fn_decl.inputs {
[Param { ty, span, .. }] => {
if let TyKind::CVarArgs = ty.kind {
self.dcx().emit_err(errors::FnParamCVarArgsOnly { span: *span });
}
}
[ps @ .., _] => {
for Param { ty, span, .. } in ps {
if let TyKind::CVarArgs = ty.kind {
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ pub struct FnParamTooMany {
pub max_num_args: usize,
}

#[derive(Diagnostic)]
#[diag(ast_passes_fn_param_c_var_args_only)]
pub struct FnParamCVarArgsOnly {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_fn_param_c_var_args_not_last)]
pub struct FnParamCVarArgsNotLast {
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/c-variadic/variadic-ffi-no-fixed-args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//@ build-pass

// Supported since C23
// https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2975.pdf
extern "C" {
fn foo(...);
//~^ ERROR C-variadic function must be declared with at least one named argument
}

fn main() {}
8 changes: 0 additions & 8 deletions tests/ui/c-variadic/variadic-ffi-no-fixed-args.stderr

This file was deleted.

8 changes: 0 additions & 8 deletions tests/ui/parser/variadic-ffi-semantic-restrictions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ fn f1_1(x: isize, ...) {}

fn f1_2(...) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR C-variadic function must be declared with at least one named argument

extern "C" fn f2_1(x: isize, ...) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic

extern "C" fn f2_2(...) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR C-variadic function must be declared with at least one named argument

extern "C" fn f2_3(..., x: isize) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
Expand All @@ -26,7 +24,6 @@ extern "C" fn f3_1(x: isize, ...) {}

extern "C" fn f3_2(...) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR C-variadic function must be declared with at least one named argument

extern "C" fn f3_3(..., x: isize) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
Expand All @@ -47,8 +44,6 @@ const extern "C" fn f4_3(..., x: isize, ...) {}
//~| ERROR `...` must be the last argument of a C-variadic function

extern "C" {
fn e_f1(...);
//~^ ERROR C-variadic function must be declared with at least one named argument
fn e_f2(..., x: isize);
//~^ ERROR `...` must be the last argument of a C-variadic function
}
Expand All @@ -60,7 +55,6 @@ impl X {
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
fn i_f2(...) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR C-variadic function must be declared with at least one named argument
fn i_f3(..., x: isize, ...) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR `...` must be the last argument of a C-variadic function
Expand All @@ -80,10 +74,8 @@ trait T {
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
fn t_f3(...) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR C-variadic function must be declared with at least one named argument
fn t_f4(...);
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR C-variadic function must be declared with at least one named argument
fn t_f5(..., x: isize) {}
//~^ ERROR only foreign or `unsafe extern "C"` functions may be C-variadic
//~| ERROR `...` must be the last argument of a C-variadic function
Expand Down
Loading

0 comments on commit ec0d36b

Please sign in to comment.