diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-08-22 20:09:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-22 20:09:47 +0000 |
commit | 40042ce11fc5d7f62e31be99e82bf6a0db83234a (patch) | |
tree | 65c19173e49e2e8f79a2a87bfb82e4c57b34de4f /gcc | |
parent | b3bb2e194c814434e80b9b427df37fe24fc70e6c (diff) | |
parent | 5a731790054474a6361b88a7a123abc8013e4d4e (diff) | |
download | gcc-40042ce11fc5d7f62e31be99e82bf6a0db83234a.zip gcc-40042ce11fc5d7f62e31be99e82bf6a0db83234a.tar.gz gcc-40042ce11fc5d7f62e31be99e82bf6a0db83234a.tar.bz2 |
Merge #642
642: Reject duplicate field names in structs and unions. r=philberty a=dkm
From Mark Wielaard : https://gcc.gnu.org/pipermail/gcc-rust/2021-August/000150.html
> 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.
Co-authored-by: Mark Wielaard <mark@klomp.org>
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 } + } +} |