gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Rebinding match expressions on structs are unhandled

Open CohenArthur opened this issue 1 year ago • 4 comments

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 avatar Mar 06 '24 14:03 CohenArthur

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);
    }

CohenArthur avatar Mar 06 '24 14:03 CohenArthur

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.

nobel-sh avatar Mar 09 '24 10:03 nobel-sh

@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?

CohenArthur avatar Mar 19 '24 15:03 CohenArthur

Yeah sure.

nobel-sh avatar Mar 20 '24 10:03 nobel-sh