8000 Optimizer: Add isStat flag to constrainedLub · scala-js/scala-js@b46a094 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit b46a094

Browse files
committed
Optimizer: Add isStat flag to constrainedLub
Split out from #5066. This is to ensure that we correctly raise the overall tree type in case we are in statement position. Before this commit, the inner trees were typed as `VoidType` (due to the `isStat` flag on `transform`), but the outer (aggregate) tree kept its original (expression) type. This lead to ill-typed IR.
1 parent f0aa3df commit b46a094

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

linker/shared/src/main/scala/org/scalajs/linker/frontend/optimizer/OptimizerCore.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ private[optimizer] abstract class OptimizerCore(
407407
val newThenp = transform(thenp, isStat)
408408
val newElsep = transform(elsep, isStat)
409409
val refinedType =
410-
constrainedLub(newThenp.tpe, newElsep.tpe, tree.tpe)
410+
constrainedLub(newThenp.tpe, newElsep.tpe, tree.tpe, isStat)
411411
foldIf(newCond, newThenp, newElsep)(refinedType)
412412
}
413413

@@ -454,7 +454,7 @@ private[optimizer] abstract class OptimizerCore(
454454
transform(handler, isStat)(handlerScope)
455455
}
456456

457-
val refinedType = constrainedLub(newBlock.tpe, newHandler.tpe, tree.tpe)
457+
val refinedType = constrainedLub(newBlock.tpe, newHandler.tpe, tree.tpe, isStat)
458458
TryCatch(newBlock, LocalIdent(newName)(errVar.pos), newOriginalName,
459459
newHandler)(refinedType)
460460

@@ -479,7 +479,7 @@ private[optimizer] abstract class OptimizerCore(
479479
val newDefault = transform(default, isStat)
480480

481481
val refinedType = (newDefault.tpe :: newCases.map(_._2.tpe))
482-
.reduce(constrainedLub(_, _, tree.tpe))
482+
.reduce(constrainedLub(_, _, tree.tpe, isStat))
483483

484484
Match(newSelector, newCases, newDefault)(refinedType)
485485
}
@@ -1190,7 +1190,7 @@ private[optimizer] abstract class OptimizerCore(
11901190
val newThenp = finishTransformExpr(tthenpNoLocalDef)
11911191
val newElsep = finishTransformExpr(telsepNoLocalDef)
11921192
val refinedType =
1193-
constrainedLub(newThenp.tpe, newElsep.tpe, tree.tpe)
1193+
constrainedLub(newThenp.tpe, newElsep.tpe, tree.tpe, isStat = false)
11941194
cont(foldIf(newCond, newThenp, newElsep)(
11951195
refinedType).toPreTransform)
11961196
}
@@ -1200,7 +1200,7 @@ private[optimizer] abstract class OptimizerCore(
12001200
val newThenp = transformExpr(thenp)
12011201
val newElsep = transformExpr(elsep)
12021202
val refinedType =
1203-
constrainedLub(newThenp.tpe, newElsep.tpe, tree.tpe)
1203+
constrainedLub(newThenp.tpe, newElsep.tpe, tree.tpe, isStat = false)
12041204
cont(foldIf(newCond, newThenp, newElsep)(
12051205
refinedType).toPreTransform)
12061206
}
@@ -5161,7 +5161,7 @@ private[optimizer] abstract class OptimizerCore(
51615161

51625162
def doMakeTree(newBody: Tree, returnedTypes: List[Type]): Tree = {
51635163
val refinedType =
5164-
returnedTypes.reduce(constrainedLub(_, _, resultType))
5164+
returnedTypes.reduce(constrainedLub(_, _, resultType, isStat))
51655165
val returnCount = returnedTypes.size - 1
51665166

51675167
tryOptimizePatternMatch(oldLabelName, newLabel, refinedType,
@@ -5538,23 +5538,26 @@ private[optimizer] abstract class OptimizerCore(
55385538
/** Finds a type as precise as possible which is a supertype of lhs and rhs
55395539
* but still a subtype of upperBound.
55405540
* Requires that lhs and rhs be subtypes of upperBound, obviously.
5541+
*
5542+
* The RefinedType version does not have an `isStat` flag, since RefinedTypes
5543+
* only exist in a PreTransform context, which is always an expression context.
55415544
*/
55425545
private def constrainedLub(lhs: RefinedType, rhs: RefinedType,
55435546
upperBound: Type): RefinedType = {
5544-
if (upperBound == VoidType) RefinedType(upperBound)
5547+
if (upperBound == VoidType) RefinedType(VoidType)
55455548
else if (lhs == rhs) lhs
55465549
else if (lhs.isNothingType) rhs
55475550
else if (rhs.isNothingType) lhs
5548-
else RefinedType(constrainedLub(lhs.base, rhs.base, upperBound))
5551+
else RefinedType(constrainedLub(lhs.base, rhs.base, upperBound, isStat = false))
55495552
}
55505553

55515554
/** Finds a type as precise as possible which is a supertype of lhs and rhs
55525555
* but still a subtype of upperBound.
55535556
* Requires that lhs and rhs be subtypes of upperBound, obviously.
55545557
*/
5555-
private def constrainedLub(lhs: Type, rhs: Type, upperBound: Type): Type = {
5558+
private def constrainedLub(lhs: Type, rhs: Type, upperBound: Type, isStat: Boolean): Type = {
55565559
// TODO Improve this
5557-
if (upperBound == VoidType) upperBound
5560+
if (isStat || upperBound == VoidType) VoidType
55585561
else if (lhs == rhs) lhs
55595562
else if (lhs == NothingType) rhs
55605563
else if (rhs == NothingType) lhs

0 commit comments

Comments
 (0)
0