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 6, 2024
1 parent 97a711c commit c1033c1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/config/Feature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Feature:
val into = experimental("into")
val namedTuples = experimental("namedTuples")
val modularity = experimental("modularity")
val betterMatchTypeExtractors = experimental("betterMatchTypeExtractors")
val betterMatchTypeExtractors = deprecated("betterMatchTypeExtractors")

def experimentalAutoEnableFeatures(using Context): List[TermName] =
defn.languageExperimentalFeatures
Expand Down Expand Up @@ -128,8 +128,6 @@ object Feature:

def scala2ExperimentalMacroEnabled(using Context) = enabled(scala2macros)

def betterMatchTypeExtractorsEnabled(using Context) = enabled(betterMatchTypeExtractors)

/** Is pureFunctions enabled for this compilation unit? */
def pureFunsEnabled(using Context) =
enabledBySetting(pureFunctions)
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, noPrinter}
import config.SourceVersion
import TypeErasure.{erasedLub, erasedGlb}
Expand Down Expand Up @@ -3481,10 +3481,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 @@ -3498,7 +3496,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 @@ -3516,7 +3514,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", since = "3.6")
object betterMatchTypeExtractors
end experimental

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 c1033c1

Please sign in to comment.