]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: [E0124] field x is already declared in struct
authorMuhammad Mahad <mahadtxt@gmail.com>
Mon, 10 Jul 2023 12:26:45 +0000 (17:26 +0500)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:49:35 +0000 (18:49 +0100)
Refactored error message for more
than one duplicate fields.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-base.cc (struct_field_name_exists):
called error function.

gcc/testsuite/ChangeLog:

* rust/compile/bad_pub_enumitems.rs: changed comment to pass test cases.
* rust/compile/dup_fields.rs: likewise.
* rust/execute/same_field_name.rs: New test.

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
gcc/rust/hir/rust-ast-lower-base.cc
gcc/testsuite/rust/compile/bad_pub_enumitems.rs
gcc/testsuite/rust/compile/dup_fields.rs
gcc/testsuite/rust/execute/same_field_name.rs [new file with mode: 0644]

index 82d1d2cb00136075a1423eb865079f526f39d67d..5a1ce74588a284da3c435b620ddf98af6b135edc 100644 (file)
@@ -681,7 +681,8 @@ struct_field_name_exists (std::vector<HIR::StructField> &fields,
        {
          RichLocation r (new_field.get_locus ());
          r.add_range (field.get_locus ());
-         rust_error_at (r, "duplicate field name %qs",
+         rust_error_at (r, ErrorCode ("E0124"),
+                        "field %qs is already declared",
                         field.get_field_name ().as_string ().c_str ());
          return true;
        }
index e7fd5edb9811c2acca873b4e7d3eb9f9083b4d6e..e7eb1caa779fe8d724c692dcb9fc0f982e1c31da 100644 (file)
@@ -18,7 +18,7 @@ enum E1
 enum E2
 {
   pub A (u8, i32, u64), // { dg-error "visibility qualifier" }
-  B { a: u8, a: u8 }  // { dg-error "duplicate field" }}
+  B { a: u8, a: u8 }  // { dg-error "field .a. is already declared" }}
 }
 
 fn main ()
@@ -41,7 +41,7 @@ fn main ()
 
   enum E2
     {
-      Alpha { a: u8, a: u8 },  // { dg-error "duplicate field" }}
+      Alpha { a: u8, a: u8 },  // { dg-error "field .a. is already declared" }}
       pub Beta (u8, i32, u64) // { dg-error "visibility qualifier" }
     }
 }
index ab39955eca05fd5b400f5505ead52f619b09f84e..67003dbed687a0c1c8713e4032dc86bd7a63038a 100644 (file)
@@ -1,23 +1,23 @@
 struct S { a: i32, b: i32, c: u8, a: i128 }
-// { dg-error "duplicate field" "" { target *-*-* } .-1 }
+// { dg-error "field .a. is already declared" "" { target *-*-* } .-1 }
 
 union U
   {
     a: i32,
     b: i32,
     c: u8,
-    b: char // { dg-error "duplicate field" "" { target *-*-* } }
+    b: char // { dg-error "field .b. is already declared" "" { target *-*-* } }
   }
 
 fn main ()
 {
   struct SS { alpha: i32, beta: i32, gamma: u8, gamma: i128 }
-  // { dg-error "duplicate field" "" { target *-*-* } .-1 }
+  // { dg-error "field .gamma. is already declared" "" { target *-*-* } .-1 }
 
   union UU
     {
       alpha: i32, beta: i32,
       gamma: u8, beta: char
-      // { dg-error "duplicate field" "" { target *-*-* } .-1 }
+      // { dg-error "field .beta. is already declared" "" { target *-*-* } .-1 }
     }
 }
diff --git a/gcc/testsuite/rust/execute/same_field_name.rs b/gcc/testsuite/rust/execute/same_field_name.rs
new file mode 100644 (file)
index 0000000..d57562b
--- /dev/null
@@ -0,0 +1,8 @@
+// https://doc.rust-lang.org/error_codes/E0124.html
+fn main() {
+    struct Foo {
+        field1: i32, // { dg-error "field .field1. is already declared" }
+        field1: i32, // { dg-error "field .field1. is already declared" }
+        field1: i32, // { dg-error "field .field1. is already declared" }
+    }
+}