From: Mark Wielaard <mark@klomp.org>
To: gcc-rust@gcc.gnu.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] Reject duplicate field names in structs and unions.
Date: Sun, 22 Aug 2021 01:47:48 +0200 [thread overview]
Message-ID: <20210821234748.182213-1-mark@klomp.org> (raw)
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
next reply other threads:[~2021-08-21 23:48 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-21 23:47 Mark Wielaard [this message]
2021-08-22 21:04 ` Philip Herron
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=20210821234748.182213-1-mark@klomp.org \
--to=mark@klomp.org \
--cc=gcc-rust@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).