From: Islam-Imad <[email protected]>

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 <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/1c6d3bf074b947fcd3aa5521651cec86eb5c3ac9

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4429

 .../typecheck/rust-hir-type-check-struct.cc   | 24 ++++++++++++-------
 .../compile/missing_constructor_fields.rs     |  6 ++---
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-struct.cc 
b/gcc/rust/typecheck/rust-hir-type-check-struct.cc
index 5707b678a..b6ea1028f 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-struct.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-struct.cc
@@ -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 ());
diff --git a/gcc/testsuite/rust/compile/missing_constructor_fields.rs 
b/gcc/testsuite/rust/compile/missing_constructor_fields.rs
index fcfa17d06..2d373d224 100644
--- a/gcc/testsuite/rust/compile/missing_constructor_fields.rs
+++ b/gcc/testsuite/rust/compile/missing_constructor_fields.rs
@@ -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'" }
 }

base-commit: f24240c2b2aad3e28586033e89ff3627162d3aa7
-- 
2.52.0

Reply via email to