Skip to content

Commit a6bb4c2

Browse files
committed
Reduce context creations for value class related ops
1 parent 8957947 commit a6bb4c2

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
857857
}
858858

859859
/** After phase `trans`, set the owner of every definition in this tree that was formerly
860-
* owner by `from` to `to`.
860+
* owned by `from` to `to`.
861861
*/
862862
def changeOwnerAfter(from: Symbol, to: Symbol, trans: DenotTransformer)(using Context): ThisTree =
863863
if (ctx.phase == trans.next) {

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ object Contexts {
796796
changeOwnersInUse += 1
797797
nestedCtx.setOwner(owner).setTyperState(ctx.typerState)
798798

799-
/** Run `op` in current context, with a mode is temporarily set as specified.
799+
/** Run `op` in current context, in a reusable context that has the specified owner.
800800
*/
801801
inline def runWithOwner[T](owner: Symbol)(inline op: Context ?=> T)(using Context): T =
802802
if Config.reuseOwnerContexts then

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -848,13 +848,8 @@ object SymDenotations {
848848
def isSerializable(using Context): Boolean =
849849
isClass && derivesFrom(defn.JavaSerializableClass)
850850

851-
/** Is this symbol a class that extends `AnyVal`? */
852-
final def isValueClass(using Context): Boolean =
853-
val di = initial
854-
di.isClass
855-
&& atPhase(di.validFor.firstPhaseId)(di.derivesFrom(defn.AnyValClass))
856-
// We call derivesFrom at the initial phase both because AnyVal does not exist
857-
// after Erasure and to avoid cyclic references caused by forcing denotations
851+
/** Is this symbol a class that extends `AnyVal`? Overridden in ClassDenotation */
852+
def isValueClass(using Context): Boolean = false
858853

859854
/** Is this symbol a class of which `null` is a value? */
860855
final def isNullableClass(using Context): Boolean =
@@ -2006,6 +2001,17 @@ object SymDenotations {
20062001
/** Hook to do a pre-enter test. Overridden in PackageDenotation */
20072002
protected def proceedWithEnter(sym: Symbol, mscope: MutableScope)(using Context): Boolean = true
20082003

2004+
final override def isValueClass(using Context): Boolean =
2005+
val di = initial.asClass
2006+
val anyVal = defn.AnyValClass
2007+
if di.baseDataCache.isValid && !ctx.erasedTypes then
2008+
// fast path that does not demand time travel
2009+
(symbol eq anyVal) || di.baseClassSet.contains(anyVal)
2010+
else
2011+
// We call derivesFrom at the initial phase both because AnyVal does not exist
2012+
// after Erasure and to avoid cyclic references caused by forcing denotations
2013+
atPhase(di.validFor.firstPhaseId)(di.derivesFrom(anyVal))
2014+
20092015
/** Enter a symbol in current scope, and future scopes of same denotation.
20102016
* Note: We require that this does not happen after the first time
20112017
* someone does a findMember on a subclass.

compiler/src/dotty/tools/dotc/transform/ValueClasses.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ object ValueClasses {
2222
}
2323

2424
def isMethodWithExtension(sym: Symbol)(using Context): Boolean =
25-
atPhaseNoLater(extensionMethodsPhase) {
26-
val d = sym.denot
27-
d.validFor.containsPhaseId(ctx.phaseId) &&
28-
d.isRealMethod &&
29-
isDerivedValueClass(d.owner) &&
30-
!d.isConstructor &&
31-
!d.symbol.isSuperAccessor &&
32-
!d.is(Macro)
33-
}
25+
val d = sym.denot.initial
26+
d.validFor.firstPhaseId <= extensionMethodsPhase.id
27+
&& d.isRealMethod
28+
&& isDerivedValueClass(d.owner)
29+
&& !d.isConstructor
30+
&& !d.symbol.isSuperAccessor
31+
&& !d.is(Macro)
3432

3533
/** The member of a derived value class that unboxes it. */
3634
def valueClassUnbox(cls: ClassSymbol)(using Context): Symbol =

tests/pos/visitors.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object test {
2+
3+
def f() = 5;
4+
5+
}

0 commit comments

Comments
 (0)