Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions gcc/rust/typecheck/rust-hir-type-check-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,32 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern)

infered = pattern_ty;
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered);
rust_assert (adt->number_of_variants () > 0);

TyTy::VariantDef *variant = adt->get_variants ().at (0);
TyTy::VariantDef *variant = nullptr;
if (adt->is_enum ())
{
HirId variant_id = UNKNOWN_HIRID;
bool ok = context->lookup_variant_definition (
pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
rust_assert (ok);
if (!ok)
{
rust_error_at (
pattern.get_locus (), ErrorCode::E0532,
"expected tuple struct or tuple variant, found enum %qs",
pattern_ty->get_name ().c_str ());
return;
}

ok = adt->lookup_variant_by_id (variant_id, &variant);
rust_assert (ok);
}
else
{
rust_assert (adt->number_of_variants () > 0);
variant = adt->get_variants ().at (0);
}

rust_assert (variant != nullptr);

// error[E0532]: expected tuple struct or tuple variant, found struct
// variant `Foo::D`, E0532 by rustc 1.49.0 , E0164 by rustc 1.71.0
Expand Down
20 changes: 20 additions & 0 deletions gcc/testsuite/rust/compile/match-tuplestructpattern-non-variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
enum Empty {}
enum NonEmpty {
Foo(i32),
}

fn f(e: Empty) {
match e {
Empty(0) => {} // { dg-error "expected tuple struct or tuple variant, found enum 'Empty'" }
}

match e {
Empty(Empty(..)) => {} // { dg-error "expected tuple struct or tuple variant, found enum 'Empty'" }
}
}

fn g(e: NonEmpty) {
match e {
NonEmpty(0) => {} // { dg-error "expected tuple struct or tuple variant, found enum 'NonEmpty'" }
}
}
Loading