]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Attempted to access a nonexistent field [E0609]
authorMuhammad Mahad <mahadtxt@gmail.com>
Thu, 15 Aug 2024 16:44:55 +0000 (16:44 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 19 Mar 2025 14:32:03 +0000 (15:32 +0100)
gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Add error code and update error message

gcc/testsuite/ChangeLog:

* rust/compile/nonexistent-field.rs: New test.

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/testsuite/rust/compile/nonexistent-field.rs [new file with mode: 0644]

index 0e897813d8f05b14e58caec6609a2871ec87fbb8..81d8295255042a207a5a3b60d73fafc1f473ec60 100644 (file)
@@ -85,7 +85,9 @@ TypeCheckExpr::visit (HIR::TupleIndexExpr &expr)
       TupleIndex index = expr.get_tuple_index ();
       if ((size_t) index >= tuple->num_fields ())
        {
-         rust_error_at (expr.get_locus (), "unknown field at index %i", index);
+         rust_error_at (expr.get_locus (), ErrorCode::E0609,
+                        "no field %qi on type %qs", index,
+                        resolved->get_name ().c_str ());
          return;
        }
 
@@ -1078,7 +1080,8 @@ TypeCheckExpr::visit (HIR::FieldAccessExpr &expr)
                                     &lookup, nullptr);
   if (!found)
     {
-      rust_error_at (expr.get_locus (), "unknown field [%s] for type [%s]",
+      rust_error_at (expr.get_locus (), ErrorCode::E0609,
+                    "no field %qs on type %qs",
                     expr.get_field_name ().as_string ().c_str (),
                     adt->as_string ().c_str ());
       return;
diff --git a/gcc/testsuite/rust/compile/nonexistent-field.rs b/gcc/testsuite/rust/compile/nonexistent-field.rs
new file mode 100644 (file)
index 0000000..e20c49d
--- /dev/null
@@ -0,0 +1,14 @@
+#![allow(unused)]
+fn main() {
+    struct StructWithFields {
+        x: u32,
+    }
+
+    let s = StructWithFields { x: 0 };
+    s.foo;
+    // { dg-error "no field .foo. on type .StructWithFields.StructWithFields .x.u32... .E0609." "" { target *-*-* } .-1 }
+
+    let numbers = (1, 2, 3);
+    numbers.3;
+    // { dg-error "no field .3. on type ..<integer>, <integer>, <integer>.. .E0609." "" { target *-*-* } .-1 }
+}