Skip to content

Commit

Permalink
Add ast validation check on union variant number
Browse files Browse the repository at this point in the history
Unions with zero fields are forbidden. Add regression test for empty
unions.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
	zero field check during ast validation pass.
	* checks/errors/rust-ast-validation.h: Add union visit function
	prototype.

gcc/testsuite/ChangeLog:

	* rust/compile/const_generics_8.rs: Fill the union with dummy values.
	* rust/compile/empty_union.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed Nov 21, 2023
1 parent 0f9752e commit 546e251
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "rust-ast-validation.h"
#include "rust-diagnostics.h"
#include "rust-item.h"
#include "rust-keyword-values.h"

namespace Rust {
Expand Down Expand Up @@ -81,6 +82,15 @@ ASTValidation::visit (AST::ExternalFunctionItem &item)
AST::ContextualASTVisitor::visit (item);
}

void
ASTValidation::visit (AST::Union &item)
{
if (item.get_variants ().empty ())
rust_error_at (item.get_locus (), "unions cannot have zero fields");

AST::ContextualASTVisitor::visit (item);
}

void
ASTValidation::visit (AST::Function &function)
{
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "rust-ast-visitor.h"
#include "rust-ast-full.h"
#include "rust-item.h"

namespace Rust {

Expand All @@ -37,6 +38,7 @@ class ASTValidation : public AST::ContextualASTVisitor
virtual void visit (AST::Lifetime &lifetime);
virtual void visit (AST::LoopLabel &label);
virtual void visit (AST::ExternalFunctionItem &item);
virtual void visit (AST::Union &item);
virtual void visit (AST::Function &function);
virtual void visit (AST::Trait &trait);
};
Expand Down
7 changes: 6 additions & 1 deletion gcc/testsuite/rust/compile/const_generics_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ enum Bidoule<const N: i32 = 15> {}
type Bipboupe<const N: i32 = 15> = Bidule;
trait Fooable<const N: i32 = 15> {}

union Bidoulepe<const N: i32 = 15> {} // { dg-error "default values for const generic parameters are not allowed in .union. items" }
union Bidoulepe<const N: i32 = 15> {
// { dg-error "default values for const generic parameters are not allowed in .union. items" "" {target *-*-* } .-1 }
int: i32,
float: f32,
}

fn const_default<const N: i32 = 15>() {} // { dg-error "default values for const generic parameters are not allowed in .function. items" }

// Note - missing generic parameter - needs name resolution on const generics
Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/empty_union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[repr(C)]
union MyUnion {} // { dg-error "unions cannot have zero fields" }

0 comments on commit 546e251

Please sign in to comment.