Skip to content

Commit

Permalink
Getting rid of substThis0
Browse files Browse the repository at this point in the history
Instead, substRefinedThis handles refinement types in its `to`
argument specially: They are mapped to RefinedThis types with the same
level as the RefinedThis they replace, but with the `to` refinement
as a binder.
  • Loading branch information
odersky committed Jan 9, 2015
1 parent 710e40e commit 1773ce7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 58 deletions.
38 changes: 6 additions & 32 deletions src/dotty/tools/dotc/core/Substituters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,35 +179,13 @@ trait Substituters { this: Context =>
.mapOver(tp)
}

// !!! TODO remove once we are sure new RefinedThis scheme works and we drop the ref to
// the target type from RefinedThis.
final def substThis0(tp: Type, from: RefinedType, to: Type, theMap: SubstRefinedThisMap0): Type =
final def substRefinedThis(tp: Type, level: Int, to: Type, theMap: SubstRefinedThisMap): Type =
tp match {
case tp @ RefinedThis(rt, level) =>
if (rt eq from)
to match { // !!! TODO drop
case RefinedThis(rt1, -1) => RefinedThis(rt1, level)
case _ => to
}
else tp
case tp: NamedType =>
if (tp.currentSymbol.isStatic) tp
else tp.derivedSelect(substThis0(tp.prefix, from, to, theMap))
case _: ThisType | _: BoundType | NoPrefix =>
tp
case tp: RefinedType =>
tp.derivedRefinedType(substThis0(tp.parent, from, to, theMap), tp.refinedName, substThis0(tp.refinedInfo, from, to, theMap))
case tp: TypeAlias =>
tp.derivedTypeAlias(substThis0(tp.alias, from, to, theMap))
case _ =>
(if (theMap != null) theMap else new SubstRefinedThisMap0(from, to))
.mapOver(tp)
}

final def substRefinedThis(tp: Type, level: Int, to: Type, theMap: SubstRefinedThisMap): Type =
tp match {
case tp @ RefinedThis(rt, l) if l == level =>
to
case tp @ RefinedThis(rt, `level`) =>
to match {
case to: RefinedType => RefinedThis(to, level)
case _ => to
}
case tp: NamedType =>
if (tp.currentSymbol.isStatic) tp
else tp.derivedSelect(substRefinedThis(tp.prefix, level, to, theMap))
Expand Down Expand Up @@ -296,10 +274,6 @@ trait Substituters { this: Context =>
def apply(tp: Type): Type = substThis(tp, from, to, this)
}

final class SubstRefinedThisMap0(from: RefinedType, to: Type) extends DeepTypeMap {
def apply(tp: Type): Type = substThis0(tp, from, to, this)
}

final class SubstRefinedThisMap(to: Type) extends DeepTypeMap {
var level: Int = 0
def apply(tp: Type): Type = substRefinedThis(tp, level, to, this)
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ class TypeApplications(val self: Type) extends AnyVal {
* LambdaXYZ { type Apply = B[$hkArg$0, ..., $hkArg$n] }
* { type $hkArg$0 = T1; ...; type $hkArg$n = Tn }
*
* satisfies predicate `p`. Try base types in the order of ther occurrence in `baseClasses`.
* satisfies predicate `p`. Try base types in the order of their occurrence in `baseClasses`.
* A type parameter matches a varianve V if it has V as its variance or if V == 0.
*/
def testLifted(tparams: List[Symbol], p: Type => Boolean)(implicit ctx: Context): Boolean = {
Expand Down
34 changes: 9 additions & 25 deletions src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,9 @@ object Types {
}
def goRefined(tp: RefinedType) = {
val pdenot = go(tp.parent)
val rinfo = pre match {
case pre: RefinedType => tp.refinedInfo.substThis0(tp, RefinedThis(pre, -1))
case _ =>
if (tp.refinementRefersToThis) tp.refinedInfo.substRefinedThis(0, pre)
else tp.refinedInfo
}
if (Types.goRefinedCheck) {
val rinfo0 = tp.refinedInfo.substThis0(tp, pre)
if ((rinfo0 ne rinfo) && (rinfo0.show != rinfo.show))
println(s"findMember discrepancy for $tp , $name, pre = $pre, old = $rinfo0, new = $rinfo")
}
val rinfo =
if (tp.refinementRefersToThis) tp.refinedInfo.substRefinedThis(0, pre)
else tp.refinedInfo
if (name.isTypeName) { // simplified case that runs more efficiently
val jointInfo = if (rinfo.isAlias) rinfo else pdenot.info & rinfo
pdenot.asSingleDenotation.derivedSingleDenotation(pdenot.symbol, jointInfo)
Expand Down Expand Up @@ -948,11 +940,10 @@ object Types {
final def substThisUnlessStatic(cls: ClassSymbol, tp: Type)(implicit ctx: Context): Type =
if (cls.isStaticOwner) this else ctx.substThis(this, cls, tp, null)

/** Substitute all occurrences of `RefinedThis(rt)` by `tp` !!! TODO remove */
final def substThis0(rt: RefinedType, tp: Type)(implicit ctx: Context): Type =
ctx.substThis0(this, rt, tp, null)

/** Substitute all occurrences of `RefinedThis(level)` by `tp` */
/** Substitute all occurrences of `RefinedThis(level)` by replacement `to`.
* Refinement types as replacements are handled specially: Instead of the
* refinement type itself, a RefinedThis type is substituted.
*/
final def substRefinedThis(level: Int, to: Type)(implicit ctx: Context): Type =
ctx.substRefinedThis(this, level, to, null)

Expand Down Expand Up @@ -1774,9 +1765,6 @@ object Types {
}
}
if (rt eq RefinedType.this) assert(l == level, RefinedType.this)
if (Types.reverseLevelCheck && l == level)
assert(dominates(rt, RefinedType.this) || dominates(RefinedType.this, rt),
RefinedType.this)
case tp: RefinedType =>
level += 1
apply(x, tp.refinedInfo)
Expand Down Expand Up @@ -1821,14 +1809,13 @@ object Types {

if ((parent eq this.parent) && (refinedName eq this.refinedName) && (refinedInfo eq this.refinedInfo))
this
else if ( refinedName.isLambdaArgName
else if (refinedName.isLambdaArgName
//&& { println(s"deriving $refinedName $parent $underlyingTypeParams"); true }
&& refinedName.lambdaArgIndex < underlyingTypeParams.length
&& !parent.isLambda)
derivedRefinedType(parent.EtaExpand, refinedName, refinedInfo)
else
if (false) RefinedType(parent, refinedName, refinedInfo)
else RefinedType(parent, refinedName, rt => refinedInfo.substThis0(this, RefinedThis(rt, -1))) // !!! TODO: replace with simply `refinedInfo`
RefinedType(parent, refinedName, refinedInfo.substRefinedThis(0, _))
}

override def equals(that: Any) = that match {
Expand Down Expand Up @@ -3050,9 +3037,6 @@ object Types {

var debugTrace = false

var reverseLevelCheck = false
var goRefinedCheck = false

val watchList = List[String](
) map (_.toTypeName)

Expand Down

0 comments on commit 1773ce7

Please sign in to comment.