Skip to content

Add error message for E0532 #3118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2024
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
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);
Comment on lines +54 to +55
Copy link
Contributor

@tamaroning tamaroning Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it is okay that patterns reference non-ADT const items.


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");
Comment on lines +69 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message seems incorrect. found tuple variant should be changed to found %s variant. I will fix this in #3125

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this. I only put it in because its what rustc uses

}

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" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be found struct variant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should say "found unit variant"
Since State::Failed is a unit variant, but we needed the struct variant State::Failed { x:y}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are also right too. I feel a bit weird for this message.
We (and rustc) should rethink about this message :)

_ => ()
}
}

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

print_on_failure(&b);

}
Loading