Skip to content

Keep opaques in match type scrutinee #20033

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3488,7 +3488,7 @@ class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
false

case MatchTypeCasePattern.AbstractTypeConstructor(tycon, argPatterns) =>
scrut.dealias match
scrut.dealiasKeepOpaques match
Copy link
Member

Choose a reason for hiding this comment

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

This change is direct contradiction with the spec, and will break as many things as it will fix (since another use case might be to precisely take private knowledge into account to be able to perform a reduction).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I understood correctly, the following

object OpaqueScope:
  opaque type Opaque[N <: Any] = Double
  val foo: StripOpaque[Opaque[Int]] = 1

  // Note that match types within OpaqueScope are not limited by the absence of dealiasing
  type NeedsRhs[D] = D match
    case Double => String
  val bar: NeedsRhs[Opaque[Float]] = "hello"

is an example of what you are mentioning right ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, exactly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then it still works, and is part of the test.

Copy link
Member

Choose a reason for hiding this comment

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

Oh yes because you only changed the dealiasing rule for the AbstractTypeConstructor case. You have to complicate the test a bit more so that it would also fall into that case to observe the issue.

case scrutDealias @ AppliedType(scrutTycon, args) if scrutTycon =:= tycon =>
matchArgs(argPatterns, args, tycon.typeParams, scrutIsWidenedAbstract)
case _ =>
Expand Down
37 changes: 37 additions & 0 deletions tests/pos/i19326.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import OpaqueScope.Opaque

type MapSum[D1, D2] = D1 match
case Opaque[l1] => D2 match
case Opaque[l2] => Opaque[Sum[l1, l2]]

object OpaqueScope:
opaque type Opaque[N <: NatT] = Double

val foo: MapSum[Opaque[Succ[Zero]], Opaque[Zero]] = 1.0
end OpaqueScope

sealed trait NatT derives CanEqual
case class Zero() extends NatT
case class Succ[N <: NatT](n: N) extends NatT

type Sum[M <: NatT, N <: NatT] <: NatT = (M, N) match
case (_, Zero) => M
case (Zero, _) => N
case (Succ[predM], Succ[predN]) => Succ[Succ[Sum[predM, predN]]]


object Minimized:

object OpaqueScope:
opaque type Opaque[N <: Any] = Double
val foo: StripOpaque[Opaque[Int]] = 1

// Note that match types within OpaqueScope are not limited by the absence of dealiasing
type NeedsRhs[D] = D match
case Double => String
val bar: NeedsRhs[Opaque[Float]] = "hello"

type StripOpaque[D] = D match
case OpaqueScope.Opaque[x] => x