Skip to content

Fix infinite loop in Mirror synthesis of unreducible match type #20133

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

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
// avoid type aliases for tuples
Right(MirrorSource.GenericTuple(types))
case _ => reduce(tp.underlying)
case tp: MatchType => reduce(tp.normalized)
case tp: MatchType => reduce(tp.tryNormalize.orElse(tp.superType))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually not quite correct, if I change the definition of DoesNotReduce to:

type DoesNotReduce[T] = T match
  case String => T

Then it infers an upper-bound of T and tp.superType ends up being Option[Int] and so a mirror is found when none should be found. This seems like a more general problem with superType though, so maybe something we should address after fixing this regression.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps we should never summon the mirror if the match type cannot be reduced? Maybe other situations where we use superType are actually fine. /cc @sjrd

Copy link
Contributor

@EugeneFlesselle EugeneFlesselle Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree using superType looks a bit suspicious given that Mirror.Of[_] isn't covariant.
But then again, it's also used in the catch all right below.

case _ => reduce(tp.superType)
case tp @ AndType(l, r) =>
for
Expand Down
9 changes: 9 additions & 0 deletions tests/neg/i19198.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import deriving.Mirror
import compiletime.summonInline

type DoesNotReduce[T] = T match
case String => Any

class Foo
@main def Test: Unit =
summonInline[Mirror.Of[DoesNotReduce[Option[Int]]]] // error