Skip to content

Commit be1e78b

Browse files
liamnaddellP-E-P
authored andcommitted
[#2324] Add error message for E0532
gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-pattern.cc: Emit E0532 when trying to reference a Tuple or Struct variant using a non Tuple or Struct pattern. gcc/testsuite/ChangeLog: * rust/compile/issue-2324-1.rs: add test for E0532 with tuple enum variant * rust/compile/issue-2324-2.rs: add test for E0532 with struct enum variant Signed-off-by: Liam Naddell <[email protected]>
1 parent 6a05b0a commit be1e78b

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ void
4444
TypeCheckPattern::visit (HIR::PathInExpression &pattern)
4545
{
4646
infered = TypeCheckExpr::Resolve (&pattern);
47+
48+
/*
49+
* We are compiling a PathInExpression, which can't be a Struct or Tuple
50+
* pattern. We should check that the declaration we are referencing IS NOT a
51+
* struct pattern or tuple with values.
52+
*/
53+
54+
rust_assert (infered->get_kind () == TyTy::TypeKind::ADT);
55+
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered);
56+
57+
HirId variant_id = UNKNOWN_HIRID;
58+
bool ok
59+
= context->lookup_variant_definition (pattern.get_mappings ().get_hirid (),
60+
&variant_id);
61+
rust_assert (ok);
62+
63+
TyTy::VariantDef *variant = nullptr;
64+
ok = adt->lookup_variant_by_id (variant_id, &variant);
65+
66+
TyTy::VariantDef::VariantType vty = variant->get_variant_type ();
67+
68+
if (vty != TyTy::VariantDef::VariantType::NUM)
69+
rust_error_at (
70+
pattern.get_final_segment ().get_locus (), ErrorCode::E0532,
71+
"expected unit struct, unit variant or constant, found tuple variant");
4772
}
4873

4974
void
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
enum State {
2+
Succeeded,
3+
Failed(u32),
4+
}
5+
6+
fn print_on_failure(state: &State) {
7+
match *state {
8+
State::Succeeded => (),
9+
State::Failed => (), // { dg-error "expected unit struct, unit variant or constant, found tuple variant" }
10+
_ => ()
11+
}
12+
}
13+
14+
fn main() {
15+
let b = State::Failed(1);
16+
17+
print_on_failure(&b);
18+
19+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
enum State {
2+
Succeeded,
3+
Failed { x: u32 },
4+
}
5+
6+
fn print_on_failure(state: &State) {
7+
match *state {
8+
State::Succeeded => (),
9+
State::Failed => (), // { dg-error "expected unit struct, unit variant or constant, found tuple variant" }
10+
_ => ()
11+
}
12+
}
13+
14+
fn main() {
15+
let b = State::Failed{x: 1};
16+
17+
print_on_failure(&b);
18+
19+
}

0 commit comments

Comments
 (0)