From b6b63b314b0bade9f073663c24f8148bc49863c4 Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Fri, 3 Jan 2025 18:00:53 +0100 Subject: [PATCH] Fix inline reduction for casedef guards with asInstanceOf --- .../dotty/tools/dotc/inlines/InlineReducer.scala | 2 +- tests/pos/i22300.scala | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i22300.scala diff --git a/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala b/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala index 0c321570bad5..3edb323e6b3b 100644 --- a/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala +++ b/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala @@ -362,7 +362,7 @@ class InlineReducer(inliner: Inliner)(using Context): val (caseBindings, from, to) = substBindings(caseBindingMap.toList, mutable.ListBuffer(), Nil, Nil) val (guardOK, canReduceGuard) = if cdef.guard.isEmpty then (true, true) - else typer.typed(cdef.guard.subst(from, to), defn.BooleanType) match { + else stripInlined(typer.typed(cdef.guard.subst(from, to), defn.BooleanType)) match { case ConstantValue(v: Boolean) => (v, true) case _ => (false, false) } diff --git a/tests/pos/i22300.scala b/tests/pos/i22300.scala new file mode 100644 index 000000000000..156d913951b6 --- /dev/null +++ b/tests/pos/i22300.scala @@ -0,0 +1,16 @@ +class Foo: + + inline def inlineMatch[T]: String = + inline compiletime.erasedValue[T] match + case _: EmptyTuple => "" + case _: (h *: _) if checkType[h](0) => "" + + private inline def checkType[T](i: Int): Boolean = + inline compiletime.erasedValue[T] match + case _: Int => true + case _ => false + +val foo = Foo() + +@main def main () = + foo.inlineMatch[(Int, Boolean)]