Skip to content

Commit

Permalink
Fix inline reduction for CaseDef guards with asInstanceOf (#22305)
Browse files Browse the repository at this point in the history
In Inliner.scala
we add asInstanceOf to references to private inline methods to make sure
we later are able to know which method is referenced (if e.g. we inline
out of the scope where that method would be visible). This added
asInstanceOf caused issue when inlining CaseDef guards, as instead of a
simple constant literal we get an Inlined node with an added binding,
like this:
```scala
{
  val A_this: A = A_this.asInstanceOf[A]
  true:Boolean
}
```
We fix that by just unpacking that Inlined node (and we do not need that
binding for constant literals, so we can just ignore it).
  • Loading branch information
jchyb authored Jan 3, 2025
1 parent 1f0c576 commit 1448123
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/pos/i22300.scala
Original file line number Diff line number Diff line change
@@ -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)]

0 comments on commit 1448123

Please sign in to comment.