Skip to content

Commit 10ee102

Browse files
lowrphilberty
authored andcommitted
gccrs: Fix ICE on enum in tuple struct pattern
When we typecheck a tuple struct pattern and the type of its path is an enum, it may refer to the enum itself and not a variant. Emit an E0532 error on such cases. Fixes #3917 Fixes #3918 Fixes #3926 gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Emit an error when the path refers to an enum itself rather than its variant. gcc/testsuite/ChangeLog: * rust/compile/match-tuplestructpattern-non-variant.rs: New test. Signed-off-by: Ryo Yoshida <[email protected]>
1 parent 8133584 commit 10ee102

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

gcc/rust/typecheck/rust-hir-type-check-pattern.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,32 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern)
162162

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

167-
TyTy::VariantDef *variant = adt->get_variants ().at (0);
166+
TyTy::VariantDef *variant = nullptr;
168167
if (adt->is_enum ())
169168
{
170169
HirId variant_id = UNKNOWN_HIRID;
171170
bool ok = context->lookup_variant_definition (
172171
pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
173-
rust_assert (ok);
172+
if (!ok)
173+
{
174+
rust_error_at (
175+
pattern.get_locus (), ErrorCode::E0532,
176+
"expected tuple struct or tuple variant, found enum %qs",
177+
pattern_ty->get_name ().c_str ());
178+
return;
179+
}
174180

175181
ok = adt->lookup_variant_by_id (variant_id, &variant);
176182
rust_assert (ok);
177183
}
184+
else
185+
{
186+
rust_assert (adt->number_of_variants () > 0);
187+
variant = adt->get_variants ().at (0);
188+
}
189+
190+
rust_assert (variant != nullptr);
178191

179192
// error[E0532]: expected tuple struct or tuple variant, found struct
180193
// variant `Foo::D`, E0532 by rustc 1.49.0 , E0164 by rustc 1.71.0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
enum Empty {}
2+
enum NonEmpty {
3+
Foo(i32),
4+
}
5+
6+
fn f(e: Empty) {
7+
match e {
8+
Empty(0) => {} // { dg-error "expected tuple struct or tuple variant, found enum 'Empty'" }
9+
}
10+
11+
match e {
12+
Empty(Empty(..)) => {} // { dg-error "expected tuple struct or tuple variant, found enum 'Empty'" }
13+
}
14+
}
15+
16+
fn g(e: NonEmpty) {
17+
match e {
18+
NonEmpty(0) => {} // { dg-error "expected tuple struct or tuple variant, found enum 'NonEmpty'" }
19+
}
20+
}

0 commit comments

Comments
 (0)