Skip to content

Commit 3069215

Browse files
Redo tryCompiletimeConstantFold in tryNormalize
But with caching
1 parent 11cb7af commit 3069215

File tree

8 files changed

+29
-35
lines changed

8 files changed

+29
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class CheckRealizable(using Context) {
116116
case _: SingletonType | NoPrefix =>
117117
Realizable
118118
case tp =>
119-
def isConcrete(tp: Type): Boolean = tp.dealias.compiletimeConstantFolded match {
119+
def isConcrete(tp: Type): Boolean = tp.dealias match {
120120
case tp: TypeRef => tp.symbol.isClass
121121
case tp: TypeParamRef => false
122122
case tp: TypeProxy => isConcrete(tp.underlying)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object TypeEval:
1212
def tryCompiletimeConstantFold(tp: AppliedType)(using Context): Type = tp.tycon match
1313
case tycon: TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) =>
1414
extension (tp: Type) def fixForEvaluation: Type =
15-
tp.normalized.dealias.compiletimeConstantFolded match
15+
tp.normalized.dealias match
1616
// enable operations for constant singleton terms. E.g.:
1717
// ```
1818
// final val one = 1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ object TypeOps:
143143
defn.MatchCase(simplify(pat, theMap), body)
144144
case tp: AppliedType =>
145145
if tp.isMatchAlias then isFullyDefined(tp, ForceDegree.all)
146-
val normed = tp.tryNormalize.orElse(tp.tryCompiletimeConstantFold)
146+
val normed = tp.tryNormalize
147147
if normed.exists then simplify(normed, theMap) else tp.map(simplify(_, theMap))
148148
case tp: TypeParamRef =>
149149
val tvar = ctx.typerState.constraint.typeVarOfParam(tp)

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ object Types extends TypeUtils {
466466
case _ => false
467467

468468
/** Does this application expand to a match type? */
469-
def isMatchAlias(using Context): Boolean = underlyingMatchType.exists
469+
def isMatchAlias(using Context): Boolean = underlyingNormalizable.isMatch
470470

471471
/** Is this a higher-kinded type lambda with given parameter variances?
472472
* These lambdas are used as the RHS of higher-kinded abstract types or
@@ -1519,27 +1519,23 @@ object Types extends TypeUtils {
15191519
}
15201520
deskolemizer(this)
15211521

1522-
/** Constant fold applied compiletime operations if possible */
1523-
final def compiletimeConstantFolded(using Context) = this match
1524-
case tp: AppliedType => tp.tryCompiletimeConstantFold.orElse(this)
1525-
case tp => tp
1526-
15271522
/** The result of normalization, or the type itself if none apply. */
15281523
final def normalized(using Context): Type = tryNormalize.orElse(this)
15291524

1530-
/** If this type has an underlying match type, the result after applying all
1531-
* toplevel normalizations, otherwise NoType.
1525+
/** If this type has an underlying match type or applied compiletime.ops,
1526+
* then the result after applying all toplevel normalizations, otherwise NoType.
15321527
*/
1533-
def tryNormalize(using Context): Type = underlyingMatchType match
1528+
def tryNormalize(using Context): Type = underlyingNormalizable match
15341529
case mt: MatchType => mt.reduced.normalized
1530+
case tp: AppliedType => tp.tryCompiletimeConstantFold
15351531
case _ => NoType
15361532

15371533
/** Perform successive dealiasings and beta-reductions of applied types until
15381534
* a match type is reached if any, otherwise NoType.
15391535
*/
1540-
def underlyingMatchType(using Context): Type = this.safeDealias.stripLazyRef match
1536+
def underlyingNormalizable(using Context): Type = this.safeDealias.stripLazyRef match
15411537
case tp: MatchType => tp
1542-
case tp: AppliedType => tp.underlyingMatchType
1538+
case tp: AppliedType => tp.underlyingNormalizable
15431539
case _ => NoType
15441540

15451541
private def widenDealias1(keep: AnnotatedType => Context ?=> Boolean)(using Context): Type = {
@@ -4586,8 +4582,8 @@ object Types extends TypeUtils {
45864582
private var myEvalRunId: RunId = NoRunId
45874583
private var myEvalued: Type = uninitialized
45884584

4589-
private var validUnderlyingMatch: Period = Nowhere
4590-
private var cachedUnderlyingMatch: Type = uninitialized
4585+
private var validUnderlyingNormalizable: Period = Nowhere
4586+
private var cachedUnderlyingNormalizable: Type = uninitialized
45914587

45924588
def isGround(acc: TypeAccumulator[Boolean])(using Context): Boolean =
45934589
if myGround == 0 then myGround = if acc.foldOver(true, this) then 1 else -1
@@ -4649,11 +4645,13 @@ object Types extends TypeUtils {
46494645
* Anything else should have already been reduced in `appliedTo` by the TypeAssigner.
46504646
* May reduce several HKTypeLambda applications before the underlying MatchType is reached.
46514647
*/
4652-
override def underlyingMatchType(using Context): Type =
4653-
if ctx.period != validUnderlyingMatch then
4654-
validUnderlyingMatch = if tycon.isProvisional then Nowhere else ctx.period
4655-
cachedUnderlyingMatch = superType.underlyingMatchType
4656-
cachedUnderlyingMatch
4648+
override def underlyingNormalizable(using Context): Type =
4649+
if ctx.period != validUnderlyingNormalizable then
4650+
validUnderlyingNormalizable = if tycon.isProvisional then Nowhere else ctx.period
4651+
cachedUnderlyingNormalizable = tycon match
4652+
case tycon: TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) => this
4653+
case _ => superType.underlyingNormalizable
4654+
cachedUnderlyingNormalizable
46574655

46584656
override def tryNormalize(using Context): Type =
46594657
if MatchTypeTrace.isRecording && isMatchAlias then
@@ -5153,8 +5151,7 @@ object Types extends TypeUtils {
51535151
myReduced = trace(i"reduce match type $this $hashCode", matchTypes, show = true):
51545152
withMode(Mode.Type):
51555153
TypeComparer.reduceMatchWith: cmp =>
5156-
val scrutNormed = scrutinee.normalized.compiletimeConstantFolded
5157-
cmp.matchCases(scrutNormed, cases.map(MatchTypeCaseSpec.analyze))
5154+
cmp.matchCases(scrutinee.normalized, cases.map(MatchTypeCaseSpec.analyze))
51585155
catch case ex: Throwable =>
51595156
myReduced = NoType
51605157
handleRecursive("reduce type ", i"$scrutinee match ...", ex)
@@ -5188,7 +5185,7 @@ object Types extends TypeUtils {
51885185
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
51895186
unique(new CachedMatchType(bound, scrutinee, cases))
51905187

5191-
def thatReducesUsingGadt(tp: Type)(using Context): Boolean = tp.underlyingMatchType match
5188+
def thatReducesUsingGadt(tp: Type)(using Context): Boolean = tp.underlyingNormalizable match
51925189
case mt: MatchType => mt.reducesUsingGadt
51935190
case _ => false
51945191

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,13 @@ object Inlines:
445445
evidence
446446
}
447447

448-
def unrollTupleTypes(tpe: Type): Option[List[Type]] = tpe.dealias match
448+
def unrollTupleTypes(tpe: Type): Option[List[Type]] = tpe.dealias.normalized match
449449
case AppliedType(tycon, args) if defn.isTupleClass(tycon.typeSymbol) =>
450450
Some(args)
451451
case AppliedType(tycon, head :: tail :: Nil) if tycon.isRef(defn.PairClass) =>
452452
unrollTupleTypes(tail).map(head :: _)
453453
case tpe: TermRef if tpe.symbol == defn.EmptyTupleModule =>
454454
Some(Nil)
455-
case tpe: AppliedType if tpe.isMatchAlias =>
456-
unrollTupleTypes(tpe.tryNormalize)
457455
case _ =>
458456
None
459457

compiler/src/dotty/tools/dotc/typer/Synthesizer.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
224224
New(defn.ValueOfClass.typeRef.appliedTo(t.tpe), t :: Nil).withSpan(span)
225225
formal.argInfos match
226226
case arg :: Nil =>
227-
val arg1 = fullyDefinedType(arg, "ValueOf argument", ctx.source.atSpan(span))
228-
arg1.normalized.dealias.compiletimeConstantFolded match
227+
fullyDefinedType(arg, "ValueOf argument", ctx.source.atSpan(span)).normalized.dealias match
229228
case ConstantType(c: Constant) =>
230229
withNoErrors(success(Literal(c)))
231230
case tp: TypeRef if tp.isRef(defn.UnitClass) =>

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18471847
case _ => false
18481848
}
18491849

1850-
val result = pt.underlyingMatchType match {
1850+
val result = pt.underlyingNormalizable match {
18511851
case mt: MatchType if isMatchTypeShaped(mt) =>
18521852
typedDependentMatchFinish(tree, sel1, selType, tree.cases, mt)
18531853
case _ =>

tests/neg/singleton-ops-any.check

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
6 | val t34: 10 == "5" = true // error
33
| ^^^^
44
| Found: (true : Boolean)
5-
| Required: (10 : Int) == ("5" : String)
5+
| Required: (false : Boolean)
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E007] Type Mismatch Error: tests/neg/singleton-ops-any.scala:7:22 --------------------------------------------------
99
7 | val t35: 10 == 10 = false // error
1010
| ^^^^^
1111
| Found: (false : Boolean)
12-
| Required: (10 : Int) == (10 : Int)
12+
| Required: (true : Boolean)
1313
|
1414
| longer explanation available when compiling with `-explain`
1515
-- [E007] Type Mismatch Error: tests/neg/singleton-ops-any.scala:12:24 -------------------------------------------------
1616
12 | val t38: false != 5 = false // error
1717
| ^^^^^
1818
| Found: (false : Boolean)
19-
| Required: (false : Boolean) != (5 : Int)
19+
| Required: (true : Boolean)
2020
|
2121
| longer explanation available when compiling with `-explain`
2222
-- [E007] Type Mismatch Error: tests/neg/singleton-ops-any.scala:13:22 -------------------------------------------------
2323
13 | val t39: 10 != 10 = true // error
2424
| ^^^^
2525
| Found: (true : Boolean)
26-
| Required: (10 : Int) != (10 : Int)
26+
| Required: (false : Boolean)
2727
|
2828
| longer explanation available when compiling with `-explain`
2929
-- [E007] Type Mismatch Error: tests/neg/singleton-ops-any.scala:18:27 -------------------------------------------------
@@ -37,6 +37,6 @@
3737
32 | val t48: IsConst[Any] = true // error
3838
| ^^^^
3939
| Found: (true : Boolean)
40-
| Required: scala.compiletime.ops.any.IsConst[Any]
40+
| Required: (false : Boolean)
4141
|
4242
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)