Skip to content

Commit

Permalink
gccrs: [E0124] field x is already declared in struct
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
MahadMuhammad authored and philberty committed Jul 11, 2023
1 parent 5d124d1 commit 1827610
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions gcc/testsuite/rust/compile/bad_pub_enumitems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand All @@ -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" }
}
}
8 changes: 4 additions & 4 deletions gcc/testsuite/rust/compile/dup_fields.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
}
8 changes: 8 additions & 0 deletions gcc/testsuite/rust/execute/same_field_name.rs
Original file line number Diff line number Diff line change
@@ -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" }
}
}

0 comments on commit 1827610

Please sign in to comment.