Skip to content

Commit

Permalink
deskolemize the type of ValDef
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Sep 23, 2021
1 parent a781883 commit d4cbbe5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,6 @@ object Types {
if tp.isOverloaded then tp else tp.underlying.widen
case tp: SingletonType => tp.underlying.widen
case tp: ExprType => tp.resultType.widen
case tp: AndType => tp.derivedAndType(tp.tp1.widen, tp.tp2.widen)
case tp =>
val tp1 = tp.stripped
if tp1 eq tp then tp
Expand Down Expand Up @@ -1390,6 +1389,17 @@ object Types {
/** Like `dealiasKeepAnnots`, but keeps only refining annotations */
final def dealiasKeepRefiningAnnots(using Context): Type = dealias1(keepIfRefining)

/** Approximate this type with a type that does not contain skolem types. */
final def deskolemized(using Context): Type =
val deskolemizer = new ApproximatingTypeMap {
def apply(tp: Type) = /*trace(i"deskolemize($tp) at $variance", show = true)*/
tp match {
case tp: SkolemType => range(defn.NothingType, atVariance(1)(apply(tp.info)))
case _ => mapOver(tp)
}
}
deskolemizer(this)

/** The result of normalization using `tryNormalize`, or the type itself if
* tryNormlize yields NoType
*/
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/EtaExpansion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class Lifter {
else {
val name = UniqueName.fresh(prefix)
// don't instantiate here, as the type params could be further constrained, see tests/pos/pickleinf.scala
var liftedType = expr.tpe.widen
var liftedType = expr.tpe.widen.deskolemized
if (liftedFlags.is(Method)) liftedType = ExprType(liftedType)
val lifted = newSymbol(ctx.owner, name, liftedFlags | Synthetic, liftedType, coord = spanCoord(expr.span))
defs += liftedDef(lifted, expr)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ class Namer { typer: Typer =>
}
}

def cookedRhsType = deskolemize(dealiasIfUnit(rhsType))
def cookedRhsType = dealiasIfUnit(rhsType).deskolemized
def lhsType = fullyDefinedType(cookedRhsType, "right-hand side", mdef.span)
//if (sym.name.toString == "y") println(i"rhs = $rhsType, cooked = $cookedRhsType")
if (inherited.exists)
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i13197.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension [T](x: T | String) inline def forceString: x.type & String =
trait Bar:
def b: String | Int

class Foo(a: String = "", b: Any)
class Foo(a: String = "", b: String)

object Foo:
def foo(bar: Bar) = Foo(b = bar.b.forceString)

0 comments on commit d4cbbe5

Please sign in to comment.