]> git.ipfire.org Git - thirdparty/gcc.git/commit
gccrs: Add improved error when no fields in initializer
authorRobert Goss <goss.robert@gmail.com>
Sun, 14 Jan 2024 17:34:22 +0000 (17:34 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 30 Jan 2024 11:36:52 +0000 (12:36 +0100)
commitc3ce1d6d8728969997a556e3f8bc59b726aff49a
treebf5182f884c3dce66af37c4006d4645b49a3a07d
parentb49afa9ba4ad94fc71a246bceca2fcc277cb0ada
gccrs: Add improved error when no fields in initializer

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 <goss.robert@gmail.com>
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-hir-type-check-struct-field.h
gcc/rust/typecheck/rust-hir-type-check-struct.cc
gcc/testsuite/rust/compile/missing_constructor_fields.rs