Skip to content

Commit bdda3e8

Browse files
Remove tryCompiletimeConstantFold from tryNormalize
This is another step needed to make `tryNormalize.exists ==> underlyingMatchType.exists` In fact, `tryNormalize.exists` could be true without having a single match type involved.
1 parent 10e53a8 commit bdda3e8

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
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 match {
119+
def isConcrete(tp: Type): Boolean = tp.dealias.compiletimeConstantFolded 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 match
15+
tp.normalized.dealias.compiletimeConstantFolded match
1616
// enable operations for constant singleton terms. E.g.:
1717
// ```
1818
// final val one = 1

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,11 @@ object Types extends TypeUtils {
15241524
}
15251525
deskolemizer(this)
15261526

1527+
/** Contant fold applied compiletime ops if possible */
1528+
final def compiletimeConstantFolded(using Context) = this match
1529+
case tp: AppliedType => tp.tryCompiletimeConstantFold.orElse(this)
1530+
case tp => tp
1531+
15271532
/** The result of normalization using `tryNormalize`, or the type itself if
15281533
* tryNormlize yields NoType
15291534
*/
@@ -4651,14 +4656,9 @@ object Types extends TypeUtils {
46514656
cachedUnderlyingMatch
46524657

46534658
override def tryNormalize(using Context): Type =
4654-
def tryMatchAlias =
4655-
if isMatchAlias then trace(i"normalize $this", typr, show = true):
4656-
if MatchTypeTrace.isRecording then
4657-
MatchTypeTrace.recurseWith(this)(superType.tryNormalize)
4658-
else
4659-
underlyingMatchType.tryNormalize
4660-
else NoType
4661-
tryCompiletimeConstantFold.orElse(tryMatchAlias)
4659+
if MatchTypeTrace.isRecording && isMatchAlias then
4660+
MatchTypeTrace.recurseWith(this)(superType.tryNormalize)
4661+
else super.tryNormalize
46624662

46634663
/** Is this an unreducible application to wildcard arguments?
46644664
* This is the case if tycon is higher-kinded. This means
@@ -5160,7 +5160,8 @@ object Types extends TypeUtils {
51605160
myReduced = trace(i"reduce match type $this $hashCode", matchTypes, show = true):
51615161
withMode(Mode.Type):
51625162
TypeComparer.reduceMatchWith: cmp =>
5163-
cmp.matchCases(scrutinee.normalized, cases.map(MatchTypeCaseSpec.analyze))
5163+
val scrutNormed = scrutinee.normalized.compiletimeConstantFolded
5164+
cmp.matchCases(scrutNormed, cases.map(MatchTypeCaseSpec.analyze))
51645165
catch case ex: Throwable =>
51655166
myReduced = NoType
51665167
handleRecursive("reduce type ", i"$scrutinee match ...", ex)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ 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-
fullyDefinedType(arg, "ValueOf argument", ctx.source.atSpan(span)).normalized.dealias match
227+
val arg1 = fullyDefinedType(arg, "ValueOf argument", ctx.source.atSpan(span))
228+
arg1.normalized.dealias.compiletimeConstantFolded match
228229
case ConstantType(c: Constant) =>
229230
withNoErrors(success(Literal(c)))
230231
case tp: TypeRef if tp.isRef(defn.UnitClass) =>

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: (false : Boolean)
5+
| Required: (10 : Int) == ("5" : String)
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: (true : Boolean)
12+
| Required: (10 : Int) == (10 : Int)
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: (true : Boolean)
19+
| Required: (false : Boolean) != (5 : Int)
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: (false : Boolean)
26+
| Required: (10 : Int) != (10 : Int)
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: (false : Boolean)
40+
| Required: scala.compiletime.ops.any.IsConst[Any]
4141
|
4242
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)