From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id 21D743858439 for ; Sat, 21 Aug 2021 23:48:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 21D743858439 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (unknown [172.31.128.44]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 0054A300071C; Sun, 22 Aug 2021 01:48:04 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 7DC992E80D59; Sun, 22 Aug 2021 01:48:04 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Cc: Mark Wielaard Subject: [PATCH] Reject duplicate field names in structs and unions. Date: Sun, 22 Aug 2021 01:47:48 +0200 Message-Id: <20210821234748.182213-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 Aug 2021 23:48:22 -0000 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 &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 > generic_params; @@ -206,6 +225,10 @@ public: std::unique_ptr (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 (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 &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 > generic_params; @@ -191,6 +210,10 @@ public: std::unique_ptr (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 (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