diff --git a/compiler/src/mir/pattern_matching.rs b/compiler/src/mir/pattern_matching.rs index 010685e31..50c300798 100644 --- a/compiler/src/mir/pattern_matching.rs +++ b/compiler/src/mir/pattern_matching.rs @@ -798,7 +798,14 @@ impl<'a> Compiler<'a> { types: Vec, ) -> Vec { if !instance.instance_of().is_generic(self.db()) { - return types.into_iter().map(|t| self.new_variable(t)).collect(); + return types + .into_iter() + .map(|t| { + self.new_variable( + t.cast_according_to(source_variable_type, self.db()), + ) + }) + .collect(); } let args = TypeArguments::for_class(self.db_mut(), instance); diff --git a/std/test/compiler/test_pattern_matching.inko b/std/test/compiler/test_pattern_matching.inko index 589174f0c..ba1e0961c 100644 --- a/std/test/compiler/test_pattern_matching.inko +++ b/std/test/compiler/test_pattern_matching.inko @@ -12,6 +12,13 @@ class enum State { case Button(State) } +class Dummy {} + +class enum Transition { + case Empty(Dummy) + case Split(Transition) +} + fn pub tests(t: mut Tests) { # https://github.com/inko-lang/inko/issues/363 t.test('match with OR patterns and a guard') fn (t) { @@ -50,4 +57,13 @@ fn pub tests(t: mut Tests) { case _ -> false } } + + t.no_panic("match doesn't drop deeply nested bindings prematurely") fn { + let trans = Transition.Split(Transition.Empty(Dummy {})) + + match ref trans { + case Split(Empty(state1)) -> {} + case _ -> {} + } + } }