Skip to content

Commit

Permalink
[#2324] Add error message for E0532
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
liamnaddell authored and P-E-P committed Aug 6, 2024
1 parent 6a05b0a commit be1e78b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ void
TypeCheckPattern::visit (HIR::PathInExpression &pattern)
{
infered = TypeCheckExpr::Resolve (&pattern);

/*
* We are compiling a PathInExpression, which can't be a Struct or Tuple
* pattern. We should check that the declaration we are referencing IS NOT a
* struct pattern or tuple with values.
*/

rust_assert (infered->get_kind () == TyTy::TypeKind::ADT);
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered);

HirId variant_id = UNKNOWN_HIRID;
bool ok
= context->lookup_variant_definition (pattern.get_mappings ().get_hirid (),
&variant_id);
rust_assert (ok);

TyTy::VariantDef *variant = nullptr;
ok = adt->lookup_variant_by_id (variant_id, &variant);

TyTy::VariantDef::VariantType vty = variant->get_variant_type ();

if (vty != TyTy::VariantDef::VariantType::NUM)
rust_error_at (
pattern.get_final_segment ().get_locus (), ErrorCode::E0532,
"expected unit struct, unit variant or constant, found tuple variant");
}

void
Expand Down
19 changes: 19 additions & 0 deletions gcc/testsuite/rust/compile/issue-2324-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum State {
Succeeded,
Failed(u32),
}

fn print_on_failure(state: &State) {
match *state {
State::Succeeded => (),
State::Failed => (), // { dg-error "expected unit struct, unit variant or constant, found tuple variant" }
_ => ()
}
}

fn main() {
let b = State::Failed(1);

print_on_failure(&b);

}
19 changes: 19 additions & 0 deletions gcc/testsuite/rust/compile/issue-2324-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum State {
Succeeded,
Failed { x: u32 },
}

fn print_on_failure(state: &State) {
match *state {
State::Succeeded => (),
State::Failed => (), // { dg-error "expected unit struct, unit variant or constant, found tuple variant" }
_ => ()
}
}

fn main() {
let b = State::Failed{x: 1};

print_on_failure(&b);

}

0 comments on commit be1e78b

Please sign in to comment.