Skip to content

Commit

Permalink
Add variadic check on function params
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
	Add variadic check on all parameters.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2850.rs: New test.

Signed-off-by: 0xn4utilus <[email protected]>
  • Loading branch information
0xn4utilus authored and CohenArthur committed Feb 15, 2024
1 parent a471675 commit d0233c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 8 additions & 4 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ ASTValidation::visit (AST::Function &function)
rust_error_at (function.get_locus (), "free function without a body");
}

if (function.is_variadic ())
rust_error_at (
function.get_function_params ().back ()->get_locus (),
"only foreign or %<unsafe extern \"C\"%> functions may be C-variadic");
auto &function_params = function.get_function_params ();
for (auto it = function_params.begin (); it != function_params.end (); it++)
{
if (it->get ()->is_variadic ())
rust_error_at (it->get ()->get_locus (),
"only foreign or %<unsafe extern \"C\"%> functions may "
"be C-variadic");
}

AST::ContextualASTVisitor::visit (function);
}
Expand Down
17 changes: 17 additions & 0 deletions gcc/testsuite/rust/compile/issue-2850.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn myfun0(...,_:i32) {}
// { dg-error "only foreign or .unsafe extern \"C\". functions may be C-variadic" "" { target *-*-* } .-1 }

fn myfun1(a:i32,...,_:i32) {}
// { dg-error "only foreign or .unsafe extern \"C\". functions may be C-variadic" "" { target *-*-* } .-1 }

struct z {
x: f64,
y: f64,
}

impl z {
fn new(x: f64, ..., y: f64) -> z {
// { dg-error "only foreign or .unsafe extern \"C\". functions may be C-variadic" "" { target *-*-* } .-1 }
z { x: x, y: y }
}
}

0 comments on commit d0233c6

Please sign in to comment.