]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE when handling case of unknown field in HIR::FieldAccess
authorPhilip Herron <herron.philip@googlemail.com>
Thu, 17 Apr 2025 12:50:55 +0000 (13:50 +0100)
committerPhilip Herron <philip.herron@embecosm.com>
Fri, 18 Apr 2025 14:01:15 +0000 (14:01 +0000)
We were wrongly adding the assertion that this must not be an enum but
this is a pointless assertion we only care that there are variant in the
ADT and if the field exists in the first variant.

Fixes Rust-GCC#3581

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): fix bad assertion

gcc/testsuite/ChangeLog:

* rust/compile/nonexistent-field.rs: fix bad error message
* rust/compile/issue-3581-1.rs: New test.
* rust/compile/issue-3581-2.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/testsuite/rust/compile/issue-3581-1.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/issue-3581-2.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/nonexistent-field.rs

index b960e73abd920c8061239a4b33a85ca6a2a5160e..115aba7047b6bbeae9375bf8dc33185dde104f94 100644 (file)
@@ -1142,27 +1142,25 @@ TypeCheckExpr::visit (HIR::FieldAccessExpr &expr)
   bool is_valid_type = struct_base->get_kind () == TyTy::TypeKind::ADT;
   if (!is_valid_type)
     {
-      rust_error_at (expr.get_locus (),
-                    "expected algebraic data type got: [%s]",
-                    struct_base->as_string ().c_str ());
+      rust_error_at (expr.get_locus (), "expected algebraic data type got %qs",
+                    struct_base->get_name ().c_str ());
       return;
     }
 
   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (struct_base);
-  rust_assert (!adt->is_enum ());
-  rust_assert (adt->number_of_variants () == 1);
-
+  rust_assert (adt->number_of_variants () > 0);
   TyTy::VariantDef *vaiant = adt->get_variants ().at (0);
 
   TyTy::StructFieldType *lookup = nullptr;
   bool found = vaiant->lookup_field (expr.get_field_name ().as_string (),
                                     &lookup, nullptr);
-  if (!found)
+  if (!found || adt->is_enum ())
     {
-      rust_error_at (expr.get_locus (), ErrorCode::E0609,
-                    "no field %qs on type %qs",
+      rich_location r (line_table, expr.get_locus ());
+      r.add_range (expr.get_field_name ().get_locus ());
+      rust_error_at (r, ErrorCode::E0609, "no field %qs on type %qs",
                     expr.get_field_name ().as_string ().c_str (),
-                    adt->as_string ().c_str ());
+                    adt->get_name ().c_str ());
       return;
     }
 
diff --git a/gcc/testsuite/rust/compile/issue-3581-1.rs b/gcc/testsuite/rust/compile/issue-3581-1.rs
new file mode 100644 (file)
index 0000000..eb2f5f0
--- /dev/null
@@ -0,0 +1,12 @@
+enum Foo {
+    Bar,
+}
+
+struct Baz;
+
+fn main() {
+    Foo::Bar.a;
+    // { dg-error "no field .a. on type .Foo. .E0609." "" { target *-*-* } .-1 }
+    Baz.a;
+    // { dg-error "no field .a. on type .Baz. .E0609." "" { target *-*-* } .-1 }
+}
diff --git a/gcc/testsuite/rust/compile/issue-3581-2.rs b/gcc/testsuite/rust/compile/issue-3581-2.rs
new file mode 100644 (file)
index 0000000..5059784
--- /dev/null
@@ -0,0 +1,9 @@
+enum A {
+    X { inner: i32 },
+    Y,
+}
+
+pub fn test() {
+    let _ = A::Y.inner;
+    // { dg-error "no field .inner. on type .A. .E0609." "" { target *-*-* } .-1 }
+}
index e20c49d3ebf4e8bf0fe8e3ce566c88d2001c7ba1..9bcfb2fe84fb65096553c7624fe4bfa79f62f116 100644 (file)
@@ -6,7 +6,7 @@ fn main() {
 
     let s = StructWithFields { x: 0 };
     s.foo;
-    // { dg-error "no field .foo. on type .StructWithFields.StructWithFields .x.u32... .E0609." "" { target *-*-* } .-1 }
+    // { dg-error "no field .foo. on type .StructWithFields. .E0609." "" { target *-*-* } .-1 }
 
     let numbers = (1, 2, 3);
     numbers.3;