gccrs : TypeCheck : Enhance missing fields error message

improved the error message from "a, b, c" to "'a', 'b' and 'c'"

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-struct.cc: enhance missing fields error message

gcc/testsuite/ChangeLog:

	* rust/compile/missing_constructor_fields.rs: update to match the new error message

Signed-off-by: Islam-Imad <islamimad404@gmail.com>
This commit is contained in:
Islam-Imad
2026-02-27 15:57:11 +01:00
committed by Arthur Cohen
parent dd0c8e761b
commit ea3073609d
2 changed files with 18 additions and 12 deletions
@@ -16,6 +16,7 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "rust-diagnostics.h"
#include "rust-hir-type-check.h"
#include "rust-hir-type-check-expr.h"
#include "rust-hir-type-check-struct-field.h"
@@ -416,21 +417,26 @@ TypeCheckStructExpr::make_missing_field_error (
if (missing_field_names.size () == 1)
{
return Error (locus, ErrorCode::E0063,
"missing field %s in initializer of %qs",
"missing field %qs in initializer of %qs",
missing_field_names[0].c_str (), struct_name.c_str ());
}
// Make comma separated string for display
std::stringstream display_field_names;
bool first = true;
for (auto &name : missing_field_names)
size_t field_count = missing_field_names.size ();
for (size_t i = 0; i + 2 < field_count; ++i)
{
if (!first)
{
display_field_names << ", ";
}
first = false;
display_field_names << name;
const auto &field_name = missing_field_names[i];
display_field_names << rust_open_quote () << field_name
<< rust_close_quote () << ", ";
}
display_field_names << rust_open_quote ()
<< missing_field_names[field_count - 2]
<< rust_close_quote ();
display_field_names << " and ";
display_field_names << rust_open_quote ()
<< missing_field_names[field_count - 1]
<< rust_close_quote ();
return Error (locus, ErrorCode::E0063,
"missing fields %s in initializer of %qs",
display_field_names.str ().c_str (), struct_name.c_str ());
@@ -9,7 +9,7 @@ struct Foo {
}
fn main() {
let z = Foo { x: 0 , y:1 }; // { dg-error "missing field z in initializer of 'Foo'" }
let xz = Foo { y:1 }; // { dg-error "missing fields x, z in initializer of 'Foo'" }
let xyz = Foo { }; // { dg-error "missing fields x, y, z in initializer of 'Foo'" }
let z = Foo { x: 0 , y:1 }; // { dg-error "missing field 'z' in initializer of 'Foo'" }
let xz = Foo { y:1 }; // { dg-error "missing fields 'x' and 'z' in initializer of 'Foo'" }
let xyz = Foo { }; // { dg-error "missing fields 'x', 'y' and 'z' in initializer of 'Foo'" }
}