Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add improved error when a field is redefined in a struct constructor #2797

Merged
merged 1 commit into from
Jan 18, 2024

Conversation

robertgoss
Copy link
Contributor

@robertgoss robertgoss commented Jan 16, 2024

Fixes #2381

If a struct type is initialized with one of it's fields repeated it will currently issue an error at the use site. However we would like to give the rust error code and (like rustc) show both the specifications for the field to help the user diagnose the issue.

We move the check after the index for the field has been established so we can look up if the field has already been defined and get it's location.

We update the visit method to return if it has handled an error otherwise we then output a second fatal error as not all the field in the specification have been processed.

gcc/rust/ChangeLog:

* gcc/rust/typecheck/rust-hir-type-check-struct-field.h: Allow visit to return a bool
* gcc/rust/typecheck/rust-hir-type-check-struct-field.cc: Improve check of respecification of fields

gcc/testsuite/ChangeLog:

* rust/compile/repeated_constructor_fields.rs: Added case with fields in constructor repeated

I have read and accept the developers certificate of origin at https://gcc.gnu.org/dco.html

@robertgoss robertgoss force-pushed the gossr/error_code_e0062_i2381 branch 6 times, most recently from 5013ae3 to d089463 Compare January 16, 2024 23:55
Fixes Rust-GCC#2381

If a struct type is initialized with one of it's fields repeated it will currently issue an error at the use site. However we would like to give the rust error code and (like rustc) show both the specifications for the field to help the user diagnose the issue.

We move the check after the index for the field has been established so we can look up if the field has already been defined and get it's location.

We update the visit method to return if it has handled an error otherwise we then output a second fatal error as not all the field in the specification have been processed.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-struct-field.h: Allow visit to return a bool
	* typecheck/rust-hir-type-check-struct.cc: Improve check of repeat fields

gcc/testsuite/ChangeLog:

	* rust/compile/repeated_constructor_fields.rs: Added case with constructor field repeated

Signed-off-by: Robert Goss <[email protected]>
@robertgoss robertgoss force-pushed the gossr/error_code_e0062_i2381 branch from d089463 to 0bf6ce3 Compare January 17, 2024 00:21
@dkm
Copy link
Member

dkm commented Jan 17, 2024

👋 I believe this is your first contribution to GCC / GCCrs. Have you read https://gcc.gnu.org/dco.html? If so, can you state that in your (first) PR description, so that it's written somewhere? Thanks!

Also, small typo in the commit log where you are missing a space in return a ;)

@robertgoss
Copy link
Contributor Author

👋 I believe this is your first contribution to GCC / GCCrs. Have you read https://gcc.gnu.org/dco.html? If so, can you state that in your (first) PR description, so that it's written somewhere? Thanks!

Thank you I have added a comment saying this in the PR description now.

Also, small typo in the commit log where you are missing a space in return a ;)

Ah yes - should be fixed now thanks.

Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work this seems very reasonable. I was slightly concerned about the changing of the visit signiture to return bool but i think this works here because i dont think its a proper visitor here in that class iirc.

Thanks! More eyes on the type checker please!

@philberty philberty added the diagnostic diagnostic static analysis label Jan 18, 2024
@philberty philberty added this to the GCC 14.1 release milestone Jan 18, 2024
@philberty philberty added this pull request to the merge queue Jan 18, 2024
Merged via the queue into Rust-GCC:master with commit 972a4f0 Jan 18, 2024
9 checks passed
@robertgoss robertgoss deleted the gossr/error_code_e0062_i2381 branch January 23, 2024 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostic diagnostic static analysis
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Fatal Error: used more than once - [E0062]
3 participants