public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Reject duplicate field names in structs and unions.
@ 2021-08-21 23:47 Mark Wielaard
  2021-08-22 21:04 ` Philip Herron
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Wielaard @ 2021-08-21 23:47 UTC (permalink / raw)
  To: gcc-rust; +Cc: Mark Wielaard

Odd things happen if structs or unions have duplicate field names.
Emit an error when lowering an struct/union item or declaration
statement and a duplicate field name is detected.  A new testcase
'dup_fields.rs' checks an error is actually produced.
---

https://code.wildebeest.org/git/user/mjw/gccrs/commit/?h=dup_fields

 gcc/rust/hir/rust-ast-lower-item.h       | 27 ++++++++++++++++++++++++
 gcc/rust/hir/rust-ast-lower-stmt.h       | 27 ++++++++++++++++++++++++
 gcc/testsuite/rust/compile/dup_fields.rs | 23 ++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/dup_fields.rs

diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h
index bcf83ee63b6..b324d0b5b8b 100644
--- a/gcc/rust/hir/rust-ast-lower-item.h
+++ b/gcc/rust/hir/rust-ast-lower-item.h
@@ -176,6 +176,25 @@ public:
 			       struct_decl.get_locus ());
   }
 
+  /* Checks whether the name of a field already exists.  Returns true
+     and produces an error if so.  */
+  static bool struct_field_name_exists (std::vector<HIR::StructField> &fields,
+					HIR::StructField &new_field)
+  {
+    for (auto &field : fields)
+      {
+	if (field.get_field_name ().compare (new_field.get_field_name ()) == 0)
+	  {
+	    RichLocation r (new_field.get_locus ());
+	    r.add_range (field.get_locus ());
+	    rust_error_at (r, "duplicate field name %qs",
+			   field.get_field_name ().c_str ());
+	    return true;
+	  }
+      }
+    return false;
+  }
+
   void visit (AST::StructStruct &struct_decl) override
   {
     std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
@@ -206,6 +225,10 @@ public:
 					 std::unique_ptr<HIR::Type> (type), vis,
 					 field.get_locus (),
 					 field.get_outer_attrs ());
+
+      if (struct_field_name_exists (fields, translated_field))
+	return false;
+
       fields.push_back (std::move (translated_field));
       return true;
     });
@@ -258,6 +281,10 @@ public:
 					   std::unique_ptr<HIR::Type> (type),
 					   vis, variant.get_locus (),
 					   variant.get_outer_attrs ());
+
+      if (struct_field_name_exists (variants, translated_variant))
+	return false;
+
       variants.push_back (std::move (translated_variant));
       return true;
     });
diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h b/gcc/rust/hir/rust-ast-lower-stmt.h
index c4c00ac0bee..1e72c8a2023 100644
--- a/gcc/rust/hir/rust-ast-lower-stmt.h
+++ b/gcc/rust/hir/rust-ast-lower-stmt.h
@@ -161,6 +161,25 @@ public:
 			       struct_decl.get_locus ());
   }
 
+  /* Checks whether the name of a field already exists.  Returns true
+     and produces an error if so.  */
+  static bool struct_field_name_exists (std::vector<HIR::StructField> &fields,
+					HIR::StructField &new_field)
+  {
+    for (auto &field : fields)
+      {
+	if (field.get_field_name ().compare (new_field.get_field_name ()) == 0)
+	  {
+	    RichLocation r (new_field.get_locus ());
+	    r.add_range (field.get_locus ());
+	    rust_error_at (r, "duplicate field name %qs",
+			   field.get_field_name ().c_str ());
+	    return true;
+	  }
+      }
+    return false;
+  }
+
   void visit (AST::StructStruct &struct_decl) override
   {
     std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
@@ -191,6 +210,10 @@ public:
 					 std::unique_ptr<HIR::Type> (type), vis,
 					 field.get_locus (),
 					 field.get_outer_attrs ());
+
+      if (struct_field_name_exists (fields, translated_field))
+	return false;
+
       fields.push_back (std::move (translated_field));
       return true;
     });
@@ -242,6 +265,10 @@ public:
 					   std::unique_ptr<HIR::Type> (type),
 					   vis, variant.get_locus (),
 					   variant.get_outer_attrs ());
+
+      if (struct_field_name_exists (variants, translated_variant))
+	return false;
+
       variants.push_back (std::move (translated_variant));
       return true;
     });
diff --git a/gcc/testsuite/rust/compile/dup_fields.rs b/gcc/testsuite/rust/compile/dup_fields.rs
new file mode 100644
index 00000000000..ab39955eca0
--- /dev/null
+++ b/gcc/testsuite/rust/compile/dup_fields.rs
@@ -0,0 +1,23 @@
+struct S { a: i32, b: i32, c: u8, a: i128 }
+// { dg-error "duplicate field" "" { target *-*-* } .-1 }
+
+union U
+  {
+    a: i32,
+    b: i32,
+    c: u8,
+    b: char // { dg-error "duplicate field" "" { target *-*-* } }
+  }
+
+fn main ()
+{
+  struct SS { alpha: i32, beta: i32, gamma: u8, gamma: i128 }
+  // { dg-error "duplicate field" "" { target *-*-* } .-1 }
+
+  union UU
+    {
+      alpha: i32, beta: i32,
+      gamma: u8, beta: char
+      // { dg-error "duplicate field" "" { target *-*-* } .-1 }
+    }
+}
-- 
2.32.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-22 21:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-21 23:47 [PATCH] Reject duplicate field names in structs and unions Mark Wielaard
2021-08-22 21:04 ` Philip Herron

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).