]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Emit error when tuple-indexing on non-tuples
authorRyo Yoshida <low.ryoshida@gmail.com>
Sun, 12 Oct 2025 10:25:41 +0000 (19:25 +0900)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 20:30:56 +0000 (21:30 +0100)
Fixes Rust-GCC#3927

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): check
and properly emit an error when the tuple index is on a non-tuple-struct.

gcc/testsuite/ChangeLog:

* rust/compile/tuple_index_on_non_tuple.rs: New test.

Signed-off-by: Ryo Yoshida <low.ryoshida@gmail.com>
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/testsuite/rust/compile/tuple_index_on_non_tuple.rs [new file with mode: 0644]

index 71dcaa316ab35b4dee09ce0451c6b9c36762e627..7885dfcf7463c721c84db42355db16d62e9e68b5 100644 (file)
@@ -126,7 +126,13 @@ TypeCheckExpr::visit (HIR::TupleIndexExpr &expr)
     }
 
   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (resolved);
-  rust_assert (!adt->is_enum ());
+  if (!adt->is_tuple_struct ())
+    {
+      rust_error_at (expr.get_locus (),
+                    "expected tuple or tuple struct, found %qs",
+                    adt->get_name ().c_str ());
+      return;
+    }
   rust_assert (adt->number_of_variants () == 1);
 
   TyTy::VariantDef *variant = adt->get_variants ().at (0);
diff --git a/gcc/testsuite/rust/compile/tuple_index_on_non_tuple.rs b/gcc/testsuite/rust/compile/tuple_index_on_non_tuple.rs
new file mode 100644 (file)
index 0000000..f94b8c3
--- /dev/null
@@ -0,0 +1,15 @@
+enum E {
+    V(usize),
+}
+
+struct S {
+    field: i32,
+}
+
+fn main() {
+    let e = E::V(0);
+    let _ = e.0; // { dg-error "expected tuple or tuple struct, found 'E'" }
+
+    let s = S { field: 0 };
+    let _ = s.0; // { dg-error "expected tuple or tuple struct, found 'S'" }
+}