Skip to content
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

Rebinding match expressions on structs are unhandled #2906

Closed
CohenArthur opened this issue Mar 6, 2024 · 4 comments · Fixed by #2958
Closed

Rebinding match expressions on structs are unhandled #2906

CohenArthur opened this issue Mar 6, 2024 · 4 comments · Fixed by #2958

Comments

@CohenArthur
Copy link
Member

I tried this code:

struct Foo { a: i32 }

fn main() {
    let a = Foo { a: 15 };
    
    match a {
        b => { }
    }
}

this is very ugly Rust code, but it's used by rustc in a number of places for desugaring purposes, for example for loops - which get desugared as something of the form:

for <pat> in <head> <body>

becomes

{
    let result = match <head>.into_iter() {
        mut iter => loop { match iter.next() { ... } }
    }
}

I expected to see this happen: no error: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=eedc6ff5cd556e9e703c52cf601d2f4c

The issue lies in here:

  // rust-compile-expr.cc:952
  if (scrutinee_kind == TyTy::TypeKind::ADT)
    {
      // this will need to change but for now the first pass implementation,
      // lets assert this is the case
      TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_expr_tyty);
      rust_assert (adt->is_enum ());
      rust_assert (adt->number_of_variants () > 0);
    }

we are asserting that the scrutinee we're matching on is an enum, which is not the case here

Instead, this happened: explanation

Meta

  • What version of Rust GCC were you using, git sha if possible.
@CohenArthur CohenArthur added the bug label Mar 6, 2024
@CohenArthur
Copy link
Member Author

a very simple fix which is probably not correct is the following:

  if (scrutinee_kind == TyTy::TypeKind::ADT)
    {
      // this will need to change but for now the first pass implementation,
      // lets assert this is the case
      TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_expr_tyty);
      if (adt->is_enum ())
	rust_assert (adt->number_of_variants () > 0);
    }

@nobel-sh
Copy link
Contributor

nobel-sh commented Mar 9, 2024

Hey, I wanted to work on this. Thinking trivially I would have thought of the same solution as the last reply but could you please explain why this might not be right? Testing locally the above solution does seem to remove the ICE.

@CohenArthur
Copy link
Member Author

@nobel-sh it removes the ICE, but I whipped it up in 5 minutes without really thinking or testing it out so that's why I'm saying it might not be correct :) should I assign you to the issue?

@nobel-sh
Copy link
Contributor

Yeah sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants