public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Arthur Cohen <cohenarthur@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-8547] gccrs: Add ast validation check on union variant number
Date: Tue, 30 Jan 2024 11:58:44 +0000 (GMT)	[thread overview]
Message-ID: <20240130115844.876653858000@sourceware.org> (raw)

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" }

                 reply	other threads:[~2024-01-30 11:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240130115844.876653858000@sourceware.org \
    --to=cohenarthur@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).