Skip to content

Commit

Permalink
Enable betterMatchTypeExtractors in >= 3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
mbovel committed Aug 20, 2024
1 parent fd45847 commit 8c890ef
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/config/Feature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ object Feature:

def scala2ExperimentalMacroEnabled(using Context) = enabled(scala2macros)

def betterMatchTypeExtractorsEnabled(using Context) = enabled(betterMatchTypeExtractors)

def quotedPatternsWithPolymorphicFunctionsEnabled(using Context) =
enabled(quotedPatternsWithPolymorphicFunctions)

Expand Down
12 changes: 5 additions & 7 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import TypeOps.refineUsingParent
import collection.mutable
import util.{Stats, NoSourcePosition, EqHashMap}
import config.Config
import config.Feature.{betterMatchTypeExtractorsEnabled, migrateTo3, sourceVersion}
import config.Feature.{migrateTo3, sourceVersion}
import config.Printers.{subtyping, gadts, matchTypes, capt, noPrinter}
import config.SourceVersion
import TypeErasure.{erasedLub, erasedGlb}
Expand Down Expand Up @@ -3621,10 +3621,8 @@ class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
case MatchTypeCasePattern.TypeMemberExtractor(typeMemberName, capture) =>
/** Try to remove references to `skolem` from a type in accordance with the spec.
*
* If `betterMatchTypeExtractorsEnabled` is enabled then references
* to `skolem` occuring are avoided by following aliases and
* singletons, otherwise no attempt made to avoid references to
* `skolem`.
* References to `skolem` occuring are avoided by following aliases and
* singletons.
*
* If any reference to `skolem` remains in the result type,
* `refersToSkolem` is set to true.
Expand All @@ -3638,7 +3636,7 @@ class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
case `skolem` =>
refersToSkolem = true
tp
case tp: NamedType if betterMatchTypeExtractorsEnabled =>
case tp: NamedType =>
val pre1 = apply(tp.prefix)
if refersToSkolem then
tp match
Expand All @@ -3656,7 +3654,7 @@ class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
tp.derivedSelect(pre1)
else
tp.derivedSelect(pre1)
case tp: LazyRef if betterMatchTypeExtractorsEnabled =>
case tp: LazyRef =>
// By default, TypeMap maps LazyRefs lazily. We need to
// force it for `refersToSkolem` to be correctly set.
apply(tp.ref)
Expand Down
1 change: 1 addition & 0 deletions library/src/scala/runtime/stdLibPatches/language.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ object language:
* @see [[https://github.com/scala/improvement-proposals/pull/84]]
*/
@compileTimeOnly("`betterMatchTypeExtractors` can only be used at compile time in import statements")
@deprecated("The experimental.betterMatchTypeExtractors language import is no longer needed since the feature is now standard. This setting will have no effect, including when setting an older source version.", since = "3.6")
object betterMatchTypeExtractors

/** Experimental support for quote pattern matching with polymorphic functions
Expand Down
2 changes: 0 additions & 2 deletions tests/neg/mt-deskolemize-2.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//> using options -language:experimental.betterMatchTypeExtractors

trait Expr:
type Value
object Expr:
Expand Down
32 changes: 32 additions & 0 deletions tests/pos/20538.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
trait Column:
type T
type F[X]
type Q = F[T]

class varchar extends Column:
type T = String

trait notnull extends Column:
type F[X] = X

object Error:

val firstName = new varchar with notnull
val lastName = new varchar with notnull

val relation = (firstName, lastName)

type RelationTypes = Tuple.InverseMap[relation.type, [X] =>> Column { type Q = X }]

summon[RelationTypes =:= (String, String)]

object Works:

object firstName extends varchar with notnull
object lastName extends varchar with notnull

val relation = (firstName, lastName)

type RelationTypes = Tuple.InverseMap[relation.type, [X] =>> Column { type Q = X }]

summon[RelationTypes =:= (String, String)]
19 changes: 19 additions & 0 deletions tests/pos/20538b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
trait A:
type T
type U = T

trait B extends A:
type T = String

object C extends B


type F[X] = A { type U = X } // works when `U` is replaced with `T`

type InvF[Y] = Y match
case F[x] => x


object Test:
summon[InvF[C.type] =:= String] // ok
summon[InvF[B] =:= String] // error: selector B does not uniquely determine parameter x
2 changes: 0 additions & 2 deletions tests/pos/mt-deskolemize.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//> using options -language:experimental.betterMatchTypeExtractors

trait Expr:
type Value

Expand Down

0 comments on commit 8c890ef

Please sign in to comment.