Skip to content

Commit

Permalink
Move currentPeriod back to Context
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jul 20, 2020
1 parent 9b99a46 commit b81cc98
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 112 deletions.
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
ref(NamedType(sym.owner.thisType, sym.name, sym.denot))

private def followOuterLinks(t: Tree)(using Context) = t match {
case t: This if currentlyAfterErasure && !(t.symbol == ctx.owner.enclosingClass || t.symbol.isStaticOwner) =>
case t: This if ctx.erasedTypes && !(t.symbol == ctx.owner.enclosingClass || t.symbol.isStaticOwner) =>
// after erasure outer paths should be respected
ExplicitOuter.OuterOps(ctx).path(toCls = t.tpe.widen.classSymbol)
case t =>
Expand Down Expand Up @@ -458,7 +458,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
def newArr =
ref(defn.DottyArraysModule).select(defn.newArrayMethod).withSpan(span)

if (!currentlyAfterErasure) {
if (!ctx.erasedTypes) {
assert(!TypeErasure.isGeneric(elemTpe), elemTpe) //needs to be done during typer. See Applications.convertNewGenericArray
newArr.appliedToTypeTrees(TypeTree(returnTpe) :: Nil).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withSpan(span)
}
Expand Down Expand Up @@ -962,7 +962,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
/** cast tree to `tp`, assuming no exception is raised, i.e the operation is pure */
def cast(tp: Type)(using Context): Tree = {
assert(tp.isValueType, i"bad cast: $tree.asInstanceOf[$tp]")
tree.select(if (currentlyAfterErasure) defn.Any_asInstanceOf else defn.Any_typeCast)
tree.select(if (ctx.erasedTypes) defn.Any_asInstanceOf else defn.Any_typeCast)
.appliedToType(tp)
}

Expand All @@ -972,7 +972,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
*/
def ensureConforms(tp: Type)(using Context): Tree =
if (tree.tpe <:< tp) tree
else if (!currentlyAfterErasure) cast(tp)
else if (!ctx.erasedTypes) cast(tp)
else Erasure.Boxing.adaptToType(tree, tp)

/** `tree ne null` (might need a cast to be type correct) */
Expand Down Expand Up @@ -1156,7 +1156,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {

/** A tree that corresponds to `Predef.classOf[$tp]` in source */
def clsOf(tp: Type)(using Context): Tree =
if currentlyAfterErasure then
if ctx.erasedTypes then
def TYPE(module: TermSymbol) = ref(module).select(nme.TYPE_)
defn.scalaClassName(tp) match
case tpnme.Boolean => TYPE(defn.BoxedBooleanModule)
Expand Down
11 changes: 4 additions & 7 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ object Contexts {
inline def atPhaseNoEarlier[T](limit: Phase)(inline op: Context ?=> T)(using Context): T =
op(using if !limit.exists || limit <= ctx.phase then ctx else ctx.withPhase(limit))

inline def currentPeriod(using ctx: Context): Period = ctx.period

def currentlyAfterTyper(using Context): Boolean = ctx.base.isAfterTyper(ctx.phase)

/** Does current phase use an erased types interpretation? */
def currentlyAfterErasure(using Context): Boolean = ctx.phase.erasedTypes

inline def inMode[T](mode: Mode)(inline op: Context ?=> T)(using ctx: Context): T =
op(using if mode != ctx.mode then ctx.fresh.setMode(mode) else ctx)

Expand Down Expand Up @@ -366,7 +359,11 @@ object Contexts {
final def phase: Phase = base.phases(period.firstPhaseId)
final def runId = period.runId
final def phaseId = period.phaseId

/** Does current phase use an erased types interpretation? */
final def erasedTypes = phase.erasedTypes

/** Is current phase after FrontEnd? */
final def isAfterTyper = base.isAfterTyper(phase)

/** Is this a context for the members of a class definition? */
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ class Definitions {

object ArrayOf {
def apply(elem: Type)(using Context): Type =
if (currentlyAfterErasure) JavaArrayType(elem)
if (ctx.erasedTypes) JavaArrayType(elem)
else ArrayType.appliedTo(elem :: Nil)
def unapply(tp: Type)(using Context): Option[Type] = tp.dealias match {
case AppliedType(at, arg :: Nil) if at.isRef(ArrayType.symbol) => Some(arg)
Expand Down Expand Up @@ -1020,7 +1020,7 @@ class Definitions {
@tu lazy val Function0_apply: Symbol = ImplementedFunctionType(0).symbol.requiredMethod(nme.apply)

def FunctionType(n: Int, isContextual: Boolean = false, isErased: Boolean = false)(using Context): TypeRef =
if (n <= MaxImplementedFunctionArity && (!isContextual || currentlyAfterErasure) && !isErased) ImplementedFunctionType(n)
if (n <= MaxImplementedFunctionArity && (!isContextual || ctx.erasedTypes) && !isErased) ImplementedFunctionType(n)
else FunctionClass(n, isContextual, isErased).typeRef

lazy val PolyFunctionClass = requiredClass("scala.PolyFunction")
Expand Down Expand Up @@ -1285,7 +1285,7 @@ class Definitions {
*/
object ContextFunctionType:
def unapply(tp: Type)(using Context): Option[(List[Type], Type, Boolean)] =
if currentlyAfterErasure then
if ctx.erasedTypes then
atPhase(erasurePhase)(unapply(tp))
else
val tp1 = tp.dealias
Expand Down
16 changes: 8 additions & 8 deletions compiler/src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ object Denotations {
/** The denotation with info(s) as seen from prefix type */
final def asSeenFrom(pre: Type)(using Context): AsSeenFromResult =
if (Config.cacheAsSeenFrom) {
if ((cachedPrefix ne pre) || currentPeriod != validAsSeenFrom) {
if ((cachedPrefix ne pre) || ctx.period != validAsSeenFrom) {
cachedAsSeenFrom = computeAsSeenFrom(pre)
cachedPrefix = pre
validAsSeenFrom = if (pre.isProvisional) Nowhere else currentPeriod
validAsSeenFrom = if (pre.isProvisional) Nowhere else ctx.period
}
cachedAsSeenFrom
}
Expand Down Expand Up @@ -693,7 +693,7 @@ object Denotations {
s"denotation $this invalid in run ${ctx.runId}. ValidFor: $validFor")
var d: SingleDenotation = this
while ({
d.validFor = Period(currentPeriod.runId, d.validFor.firstPhaseId, d.validFor.lastPhaseId)
d.validFor = Period(ctx.runId, d.validFor.firstPhaseId, d.validFor.lastPhaseId)
d.invalidateInheritedInfo()
d = d.nextInRun
d ne this
Expand Down Expand Up @@ -746,7 +746,7 @@ object Denotations {
if (myValidFor.code <= 0) nextDefined else this

/** Produce a denotation that is valid for the given context.
* Usually called when !(validFor contains currentPeriod)
* Usually called when !(validFor contains ctx.period)
* (even though this is not a precondition).
* If the runId of the context is the same as runId of this denotation,
* the right flock member is located, or, if it does not exist yet,
Expand All @@ -758,7 +758,7 @@ object Denotations {
* the symbol is stale, which constitutes an internal error.
*/
def current(using Context): SingleDenotation = {
val currentPeriod = Contexts.currentPeriod
val currentPeriod = ctx.period
val valid = myValidFor
if (valid.code <= 0) {
// can happen if we sit on a stale denotation which has been replaced
Expand Down Expand Up @@ -919,7 +919,7 @@ object Denotations {
case denot: SymDenotation => s"in ${denot.owner}"
case _ => ""
}
s"stale symbol; $this#${symbol.id} $ownerMsg, defined in ${myValidFor}, is referred to in run ${currentPeriod}"
s"stale symbol; $this#${symbol.id} $ownerMsg, defined in ${myValidFor}, is referred to in run ${ctx.period}"
}

/** The period (interval of phases) for which there exists
Expand Down Expand Up @@ -977,10 +977,10 @@ object Denotations {
true
case MethodNotAMethodMatch =>
// Java allows defining both a field and a zero-parameter method with the same name
!currentlyAfterErasure && !(symbol.is(JavaDefined) && other.symbol.is(JavaDefined))
!ctx.erasedTypes && !(symbol.is(JavaDefined) && other.symbol.is(JavaDefined))
case ParamMatch =>
// The signatures do not tell us enough to be sure about matching
!currentlyAfterErasure && info.matches(other.info)
!ctx.erasedTypes && info.matches(other.info)
case noMatch =>
false
end matches
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Periods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object Periods {

/** Are all base types in the current period guaranteed to be the same as in period `p`? */
def currentHasSameBaseTypesAs(p: Period)(using Context): Boolean =
val period = currentPeriod
val period = ctx.period
period == p ||
period.runId == p.runId &&
unfusedPhases(period.phaseId).sameBaseTypesStartId ==
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ object Phases {
/** Is this phase the standard typerphase? True for FrontEnd, but
* not for other first phases (such as FromTasty). The predicate
* is tested in some places that perform checks and corrections. It's
* different from currentlyAfterTyper (and cheaper to test).
* different from ctx.isAfterTyper (and cheaper to test).
*/
def isTyper: Boolean = false

Expand Down
28 changes: 14 additions & 14 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ object SymDenotations {
def membersNeedAsSeenFrom(pre: Type)(using Context): Boolean =
!( this.isTerm
|| this.isStaticOwner && !this.seesOpaques
|| currentlyAfterErasure
|| ctx.erasedTypes
|| (pre eq NoPrefix)
|| (pre eq thisType)
)
Expand All @@ -875,7 +875,7 @@ object SymDenotations {
* Default parameters are recognized until erasure.
*/
def hasDefaultParams(using Context): Boolean =
if currentlyAfterErasure then false
if ctx.erasedTypes then false
else if is(HasDefaultParams) then true
else if is(NoDefaultParams) then false
else
Expand Down Expand Up @@ -1450,7 +1450,7 @@ object SymDenotations {
// simulate default parameters, while also passing implicit context ctx to the default values
val initFlags1 = (if (initFlags != UndefinedFlags) initFlags else this.flags)
val info1 = if (info != null) info else this.info
if (currentlyAfterTyper && changedClassParents(info, info1, completersMatter = false))
if (ctx.isAfterTyper && changedClassParents(info, info1, completersMatter = false))
assert(ctx.phase.changesParents, i"undeclared parent change at ${ctx.phase} for $this, was: $info, now: $info1")
val privateWithin1 = if (privateWithin != null) privateWithin else this.privateWithin
val annotations1 = if (annotations != null) annotations else this.annotations
Expand Down Expand Up @@ -1567,17 +1567,17 @@ object SymDenotations {
private var memberNamesCache: MemberNames = MemberNames.None

private def memberCache(using Context): LRUCache[Name, PreDenotation] = {
if (myMemberCachePeriod != currentPeriod) {
if (myMemberCachePeriod != ctx.period) {
myMemberCache = new LRUCache
myMemberCachePeriod = currentPeriod
myMemberCachePeriod = ctx.period
}
myMemberCache
}

private def baseTypeCache(using Context): BaseTypeMap = {
if !currentHasSameBaseTypesAs(myBaseTypeCachePeriod) then
myBaseTypeCache = new BaseTypeMap
myBaseTypeCachePeriod = currentPeriod
myBaseTypeCachePeriod = ctx.period
myBaseTypeCache
}

Expand Down Expand Up @@ -1641,7 +1641,7 @@ object SymDenotations {
override final def typeParams(using Context): List[TypeSymbol] = {
if (myTypeParams == null)
myTypeParams =
if (currentlyAfterErasure || is(Module)) Nil // fast return for modules to avoid scanning package decls
if (ctx.erasedTypes || is(Module)) Nil // fast return for modules to avoid scanning package decls
else {
val di = initial
if (this ne di) di.typeParams
Expand Down Expand Up @@ -1729,7 +1729,7 @@ object SymDenotations {

def computeBaseData(implicit onBehalf: BaseData, ctx: Context): (List[ClassSymbol], BaseClassSet) = {
def emptyParentsExpected =
is(Package) || (symbol == defn.AnyClass) || currentlyAfterErasure && (symbol == defn.ObjectClass)
is(Package) || (symbol == defn.AnyClass) || ctx.erasedTypes && (symbol == defn.ObjectClass)
if (classParents.isEmpty && !emptyParentsExpected)
onBehalf.signalProvisional()
val builder = new BaseDataBuilder
Expand Down Expand Up @@ -1825,7 +1825,7 @@ object SymDenotations {
*/
def ensureTypeParamsInCorrectOrder()(using Context): Unit = {
val tparams = typeParams
if (!currentlyAfterErasure && !typeParamsFromDecls.corresponds(tparams)(_.name == _.name)) {
if (!ctx.erasedTypes && !typeParamsFromDecls.corresponds(tparams)(_.name == _.name)) {
val decls = info.decls
val decls1 = newScope
for (tparam <- typeParams) decls1.enter(decls.lookup(tparam.name))
Expand Down Expand Up @@ -2372,7 +2372,7 @@ object SymDenotations {
def traceInvalid(denot: Denotation)(using Context): Boolean = {
def show(d: Denotation) = s"$d#${d.symbol.id}"
def explain(msg: String) = {
println(s"${show(denot)} is invalid at ${currentPeriod} because $msg")
println(s"${show(denot)} is invalid at ${ctx.period} because $msg")
false
}
denot match {
Expand Down Expand Up @@ -2535,7 +2535,7 @@ object SymDenotations {
implicit val None: MemberNames = new InvalidCache with MemberNames {
def apply(keepOnly: NameFilter, clsd: ClassDenotation)(implicit onBehalf: MemberNames, ctx: Context) = ???
}
def newCache()(using Context): MemberNames = new MemberNamesImpl(currentPeriod)
def newCache()(using Context): MemberNames = new MemberNamesImpl(ctx.period)
}

/** A cache for baseclasses, as a sequence in linearization order and as a set that
Expand All @@ -2552,7 +2552,7 @@ object SymDenotations {
def apply(clsd: ClassDenotation)(implicit onBehalf: BaseData, ctx: Context) = ???
def signalProvisional() = ()
}
def newCache()(using Context): BaseData = new BaseDataImpl(currentPeriod)
def newCache()(using Context): BaseData = new BaseDataImpl(ctx.period)
}

private abstract class InheritedCacheImpl(val createdAt: Period) extends InheritedCache {
Expand All @@ -2575,11 +2575,11 @@ object SymDenotations {
}

def isValidAt(phase: Phase)(using Context) =
checkedPeriod == currentPeriod ||
checkedPeriod == ctx.period ||
createdAt.runId == ctx.runId &&
createdAt.phaseId < unfusedPhases.length &&
sameGroup(unfusedPhases(createdAt.phaseId), phase) &&
{ checkedPeriod = currentPeriod; true }
{ checkedPeriod = ctx.period; true }
}

private class InvalidCache extends InheritedCache {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ object Symbols {
final def denot(using Context): SymDenotation = {
util.Stats.record("Symbol.denot")
val lastd = lastDenot
if (checkedPeriod == currentPeriod) lastd
if (checkedPeriod == ctx.period) lastd
else computeDenot(lastd)
}

private def computeDenot(lastd: SymDenotation)(using Context): SymDenotation = {
util.Stats.record("Symbol.computeDenot")
val now = currentPeriod
val now = ctx.period
checkedPeriod = now
if (lastd.validFor contains now) lastd else recomputeDenot(lastd)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class TypeApplications(val self: Type) extends AnyVal {
val typParams = self.typeParams
val stripped = self.stripTypeVar
val dealiased = stripped.safeDealias
if (args.isEmpty || currentlyAfterErasure) self
if (args.isEmpty || ctx.erasedTypes) self
else dealiased match {
case dealiased: HKTypeLambda =>
def tryReduce =
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
// This is safe because X$ self-type is X.type
sym1 = sym1.companionModule
if ((sym1 ne NoSymbol) && (sym1 eq sym2))
currentlyAfterErasure ||
ctx.erasedTypes ||
sym1.isStaticOwner ||
isSubPrefix(tp1.prefix, tp2.prefix) ||
thirdTryNamed(tp2)
Expand Down Expand Up @@ -1312,7 +1312,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
*/
def compareCaptured(arg1: TypeBounds, arg2: Type) = tparam match {
case tparam: Symbol =>
if (leftRoot.isStable || (currentlyAfterTyper || ctx.mode.is(Mode.TypevarsMissContext))
if (leftRoot.isStable || (ctx.isAfterTyper || ctx.mode.is(Mode.TypevarsMissContext))
&& leftRoot.member(tparam.name).exists) {
val captured = TypeRef(leftRoot, tparam)
try isSubArg(captured, arg2)
Expand Down Expand Up @@ -1862,7 +1862,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
final def glb(tps: List[Type]): Type = tps.foldLeft(AnyType: Type)(glb)

def widenInUnions(using Context): Boolean =
migrateTo3 || currentlyAfterErasure
migrateTo3 || ctx.erasedTypes

/** The least upper bound of two types
* @param canConstrain If true, new constraints might be added to simplify the lub.
Expand Down Expand Up @@ -2021,7 +2021,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
}

private def andTypeGen(tp1: Type, tp2: Type, op: (Type, Type) => Type,
original: (Type, Type) => Type = _ & _, isErased: Boolean = currentlyAfterErasure): Type = trace(s"glb(${tp1.show}, ${tp2.show})", subtyping, show = true) {
original: (Type, Type) => Type = _ & _, isErased: Boolean = ctx.erasedTypes): Type = trace(s"glb(${tp1.show}, ${tp2.show})", subtyping, show = true) {
val t1 = distributeAnd(tp1, tp2)
if (t1.exists) t1
else {
Expand Down Expand Up @@ -2049,7 +2049,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
* Finally, refined types with the same refined name are
* opportunistically merged.
*/
final def andType(tp1: Type, tp2: Type, isErased: Boolean = currentlyAfterErasure): Type =
final def andType(tp1: Type, tp2: Type, isErased: Boolean = ctx.erasedTypes): Type =
andTypeGen(tp1, tp2, AndType(_, _), isErased = isErased)

final def simplifyAndTypeWithFallback(tp1: Type, tp2: Type, fallback: Type): Type =
Expand All @@ -2064,7 +2064,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
* @param isErased Apply erasure semantics. If erased is true, instead of creating
* an OrType, the lub will be computed using TypeCreator#erasedLub.
*/
final def orType(tp1: Type, tp2: Type, isErased: Boolean = currentlyAfterErasure): Type = {
final def orType(tp1: Type, tp2: Type, isErased: Boolean = ctx.erasedTypes): Type = {
val t1 = distributeOr(tp1, tp2)
if (t1.exists) t1
else {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ object TypeErasure {

/** The current context with a phase no later than erasure */
def preErasureCtx(using Context) =
if (currentlyAfterErasure) ctx.withPhase(erasurePhase) else ctx
if (ctx.erasedTypes) ctx.withPhase(erasurePhase) else ctx

/** The standard erasure of a Scala type. Value classes are erased as normal classes.
*
Expand Down Expand Up @@ -608,7 +608,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
else
val cls = normalizeClass(sym.asClass)
val fullName =
if !currentlyAfterErasure then
if !ctx.erasedTypes then
// It's important to use the initial symbol to compute the full name
// because the current symbol might have a different name or owner
// and signatures are required to be stable before erasure.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ object TypeOps:
val accu1 = if (accu exists (_ derivesFrom c)) accu else c :: accu
if (cs == c.baseClasses) accu1 else dominators(rest, accu1)
case Nil => // this case can happen because after erasure we do not have a top class anymore
assert(currentlyAfterErasure || ctx.reporter.errorsReported)
assert(ctx.erasedTypes || ctx.reporter.errorsReported)
defn.ObjectClass :: Nil
}

Expand Down
Loading

0 comments on commit b81cc98

Please sign in to comment.