From 546e25139bf223a388a06f6dcd458c9a73715e5b Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Mon, 20 Nov 2023 15:41:39 +0100 Subject: [PATCH] Add ast validation check on union variant number 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 --- gcc/rust/checks/errors/rust-ast-validation.cc | 10 ++++++++++ gcc/rust/checks/errors/rust-ast-validation.h | 2 ++ gcc/testsuite/rust/compile/const_generics_8.rs | 7 ++++++- gcc/testsuite/rust/compile/empty_union.rs | 2 ++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/compile/empty_union.rs diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index 4142cc6317ee..dad7f5edded6 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -18,6 +18,7 @@ #include "rust-ast-validation.h" #include "rust-diagnostics.h" +#include "rust-item.h" #include "rust-keyword-values.h" namespace Rust { @@ -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) { diff --git a/gcc/rust/checks/errors/rust-ast-validation.h b/gcc/rust/checks/errors/rust-ast-validation.h index 49133ee0c3d8..1052168ea728 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.h +++ b/gcc/rust/checks/errors/rust-ast-validation.h @@ -21,6 +21,7 @@ #include "rust-ast-visitor.h" #include "rust-ast-full.h" +#include "rust-item.h" namespace Rust { @@ -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); }; diff --git a/gcc/testsuite/rust/compile/const_generics_8.rs b/gcc/testsuite/rust/compile/const_generics_8.rs index c7810830f466..bb34652b9a63 100644 --- a/gcc/testsuite/rust/compile/const_generics_8.rs +++ b/gcc/testsuite/rust/compile/const_generics_8.rs @@ -8,7 +8,12 @@ enum Bidoule {} type Bipboupe = Bidule; trait Fooable {} -union Bidoulepe {} // { dg-error "default values for const generic parameters are not allowed in .union. items" } +union Bidoulepe { + // { dg-error "default values for const generic parameters are not allowed in .union. items" "" {target *-*-* } .-1 } + int: i32, + float: f32, +} + fn const_default() {} // { dg-error "default values for const generic parameters are not allowed in .function. items" } // Note - missing generic parameter - needs name resolution on const generics diff --git a/gcc/testsuite/rust/compile/empty_union.rs b/gcc/testsuite/rust/compile/empty_union.rs new file mode 100644 index 000000000000..6114df5d9c04 --- /dev/null +++ b/gcc/testsuite/rust/compile/empty_union.rs @@ -0,0 +1,2 @@ +#[repr(C)] +union MyUnion {} // { dg-error "unions cannot have zero fields" }