diff options
author | Mark Wielaard <mark@klomp.org> | 2021-08-22 01:09:10 +0200 |
---|---|---|
committer | Mark Wielaard <mark@klomp.org> | 2021-08-22 16:53:25 +0200 |
commit | 5a731790054474a6361b88a7a123abc8013e4d4e (patch) | |
tree | f5854ab5f3c824e9f93f4b2e73dd533f50aaa682 /gcc | |
parent | ac3be517de2c0ec596eeee754b863243cb071098 (diff) | |
download | gcc-5a731790054474a6361b88a7a123abc8013e4d4e.zip gcc-5a731790054474a6361b88a7a123abc8013e4d4e.tar.gz gcc-5a731790054474a6361b88a7a123abc8013e4d4e.tar.bz2 |
Reject duplicate field names in structs and unions.
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 27 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-stmt.h | 27 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/dup_fields.rs | 23 |
3 files changed, 77 insertions, 0 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 7b7ff6f..c7f874d 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -175,6 +175,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; @@ -205,6 +224,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; }); @@ -257,6 +280,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 c4c00ac..1e72c8a 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 0000000..ab39955 --- /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 } + } +} |