public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-8547] gccrs: Add ast validation check on union variant number
@ 2024-01-30 11:58 Arthur Cohen
0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-30 11:58 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:72792563e60fb56b42c2e96b3ae27d28e09c2e7d
commit r14-8547-g72792563e60fb56b42c2e96b3ae27d28e09c2e7d
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date: Mon Nov 20 15:41:39 2023 +0100
gccrs: 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 <pierre-emmanuel.patry@embecosm.com>
Diff:
---
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(-)
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 @@ public:
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<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
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" }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-01-30 11:58 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-30 11:58 [gcc r14-8547] gccrs: Add ast validation check on union variant number Arthur Cohen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).