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 no fields in initializer #2793

Merged

Conversation

robertgoss
Copy link
Contributor

@robertgoss robertgoss commented Jan 14, 2024

Fixes #2389

If a struct type with a variant that has fields is initialized with some fields the expression HIR StructExprStructFields is checked that all the fields are assigned. However, if no fields are initialized the HIR StructExprStruct is generated. This doesn't check if the struct is a unit during typechekc and only fails at the compile stage with a ICE.

Add a check at the typecheck stage that makes sure the struct does not have a variant with fields and give an error message based on the rustc one.

We have also updated the message given in the case where one field was present to list the missing fields and match more closely the new message.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit) Add additional check
* typecheck/rust-hir-type-check-struct-field.h: A helper method to make error added
* typecheck/rust-hir-type-check-expr-struct.cc (TypeCheckStructExpr::resolve) Update message

gcc/testsuite/ChangeLog:

* rust/compile/missing_constructor_fields.rs: Added case with no initializers

Signed-off-by: Robert Goss [email protected]

@P-E-P P-E-P requested review from P-E-P and CohenArthur January 15, 2024 09:12
@P-E-P P-E-P added this to the GCC 14.1 release milestone Jan 15, 2024
Copy link
Member

@P-E-P P-E-P left a comment

Choose a reason for hiding this comment

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

LGTM. Great work!

This looks like your first PR. Could you please add a message stating that you've read and acknowledge the developer certificate of origins?

@robertgoss
Copy link
Contributor Author

Hi I have read and acknowledge the developer certificate of origins - do i need to modify the commit to add a signed off tag at the end?

@P-E-P
Copy link
Member

P-E-P commented Jan 15, 2024

do i need to modify the commit to add a signed off tag at the end?

Mmh actually yes please! But what bothers me is that the CI should have caught this 🤔

@robertgoss robertgoss force-pushed the gossr/2389_no_fields_in_initializer branch from f7de7e0 to 7b30d56 Compare January 15, 2024 17:13
@robertgoss
Copy link
Contributor Author

do i need to modify the commit to add a signed off tag at the end?

Mmh actually yes please! But what bothers me is that the CI should have caught this 🤔

Should be correct now

Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

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

thanks for working on this :) I checked it out locally and realized that this only errors out for one of the missing fields. If we do have the first field, like in the following example:

struct Foo {
    x: i32,
    y: i32,
    z: i32,
}

fn main() {
    let x = Foo { x: 15 };
}

then we don't hit your error, but the one in rust-hir-type-check-struct.cc at line 153. I think it might be worth modifying this error instead so that it reports which fields are missing?

if (!variant->get_fields ().empty ())
{
rust_error_at (struct_expr.get_locus (), ErrorCode::E0063,
"missing field %s in initializer of %<%s%>",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"missing field %s in initializer of %<%s%>",
"missing field %s in initializer of %qs",

this is a shorter version of the same thing

@robertgoss
Copy link
Contributor Author

then we don't hit your error, but the one in rust-hir-type-check-struct.cc at line 153. I think it might be worth modifying this error instead so that it reports which fields are missing?

Yes as this was my first PR I was a bit nervy about expanding my change too much happy to do the same change to those ones as well

@robertgoss robertgoss force-pushed the gossr/2389_no_fields_in_initializer branch 3 times, most recently from 9d7dee8 to 731af2c Compare January 17, 2024 18:39
@philberty philberty added the diagnostic diagnostic static analysis label Jan 18, 2024
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.

I like this change but i would prefer if you didn't use std::stringstream, it would be ideal if you gather like std::vector then you check if that is non empty as the error condition.

Minor nit if no one else cares then i would go for it but it tends to be more useful to keep track of the error nodes instead of building the error message in one go as we might do something else with that information down the line.

@robertgoss
Copy link
Contributor Author

I like this change but i would prefer if you didn't use std::stringstream, it would be ideal if you gather like std::vector then you check if that is non empty as the error condition.

Sure that sounds sensible and I can make the std::vector of the names. The manual processing to format them into a string I didn't like but wasn't aware of an available method to do that.

Minor nit if no one else cares then i would go for it but it tends to be more useful to keep track of the error nodes instead of building the error message in one go as we might do something else with that information down the line.

I can have a look at this. I was basing if off some of the other error handling I had seen in the same area.

@robertgoss robertgoss force-pushed the gossr/2389_no_fields_in_initializer branch from 731af2c to 2fb1f42 Compare January 19, 2024 22:43
@robertgoss robertgoss requested a review from philberty January 19, 2024 22:44
@robertgoss robertgoss force-pushed the gossr/2389_no_fields_in_initializer branch from 2fb1f42 to a95c92d Compare January 19, 2024 23:20
If a struct type with a variant that has fields is initialized with some fields the expression  HIR StructExprStructFields is checked that all the fields are assigned. However, if no fields are initialized the HIR StructExprStruct is generated. This doesn't check if the struct is a unit during typechekc and only fails at the compile stage with a ICE.

Add a check at the typecheck stage that makes sure the struct does not have a variant with fields and give an error message based on the rustc one.

We have also updated the message given in the case where one field was present to list the missing fields and match more closely the new message.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit) Add additional check
	* typecheck/rust-hir-type-check-struct-field.h: A helper method to make error added
	* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve) Update message

gcc/testsuite/ChangeLog:

	* rust/compile/missing_constructor_fields.rs: Added case with no initializers

Signed-off-by: Robert Goss <[email protected]>
@robertgoss robertgoss force-pushed the gossr/2389_no_fields_in_initializer branch from a95c92d to 529ba6a Compare January 22, 2024 17:33
Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

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

LGTM :)

@CohenArthur CohenArthur added this pull request to the merge queue Jan 23, 2024
Merged via the queue into Rust-GCC:master with commit 7b203f6 Jan 23, 2024
9 checks passed
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.

No fields in initializer - Internal compiler error
4 participants