From 36fd20078c3ac9d5852577ceb96c1aee934d87e0 Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Sun, 12 Oct 2025 19:25:41 +0900 Subject: [PATCH] gccrs: Emit error when tuple-indexing on non-tuples 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 --- gcc/rust/typecheck/rust-hir-type-check-expr.cc | 8 +++++++- .../rust/compile/tuple_index_on_non_tuple.rs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/compile/tuple_index_on_non_tuple.rs diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 71dcaa316ab..7885dfcf746 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -126,7 +126,13 @@ TypeCheckExpr::visit (HIR::TupleIndexExpr &expr) } TyTy::ADTType *adt = static_cast (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 index 00000000000..f94b8c3d7ee --- /dev/null +++ b/gcc/testsuite/rust/compile/tuple_index_on_non_tuple.rs @@ -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'" } +} -- 2.47.3