From 0ae15d369059156c171da21c6c3b98326e45d316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Doeraene?= Date: Mon, 31 Jan 2022 10:53:34 +0100 Subject: [PATCH] Fix #4632: Fix strict-floats + optimized semantics. This was broken in e4b60eb6f16a1b733fab4482dd528382b9d12d62, which left a stray `genCallHelper("fround")` in the generation of `AsInstanceOf` for strict-floats coupled with unchecked asInstanceOfs. --- .../backend/emitter/FunctionEmitter.scala | 2 +- .../linker/backend/emitter/SJSGen.scala | 29 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/FunctionEmitter.scala b/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/FunctionEmitter.scala index e577b5dcc5..0dfdc4ec64 100644 --- a/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/FunctionEmitter.scala +++ b/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/FunctionEmitter.scala @@ -2597,7 +2597,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) { genIsInstanceOf(transformExprNoChar(expr), testType) case AsInstanceOf(expr, tpe) => - genAsInstanceOf(transformExprNoChar(expr), tpe) + extractWithGlobals(genAsInstanceOf(transformExprNoChar(expr), tpe)) case GetClass(expr) => genCallHelper("objectGetClass", transformExprNoChar(expr)) diff --git a/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/SJSGen.scala b/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/SJSGen.scala index 0bf560efce..124e1d2c08 100644 --- a/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/SJSGen.scala +++ b/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/SJSGen.scala @@ -268,31 +268,34 @@ private[emitter] final class SJSGen( def genAsInstanceOf(expr: Tree, tpe: Type)( implicit moduleContext: ModuleContext, globalKnowledge: GlobalKnowledge, - pos: Position): Tree = { + pos: Position): WithGlobals[Tree] = { import TreeDSL._ + // Local short-hand of WithGlobals(...) + def wg(tree: Tree): WithGlobals[Tree] = WithGlobals(tree) + if (semantics.asInstanceOfs == CheckedBehavior.Unchecked) { tpe match { case _:ClassType | _:ArrayType | AnyType => - expr + wg(expr) - case UndefType => Block(expr, Undefined()) - case BooleanType => !(!expr) - case CharType => genCallHelper("uC", expr) - case ByteType | ShortType| IntType => expr | 0 - case LongType => genCallHelper("uJ", expr) - case DoubleType => UnaryOp(irt.JSUnaryOp.+, expr) - case StringType => expr || StringLiteral("") + case UndefType => wg(Block(expr, Undefined())) + case BooleanType => wg(!(!expr)) + case CharType => wg(genCallHelper("uC", expr)) + case ByteType | ShortType| IntType => wg(expr | 0) + case LongType => wg(genCallHelper("uJ", expr)) + case DoubleType => wg(UnaryOp(irt.JSUnaryOp.+, expr)) + case StringType => wg(expr || StringLiteral("")) case FloatType => - if (semantics.strictFloats) genCallHelper("fround", expr) - else UnaryOp(irt.JSUnaryOp.+, expr) + if (semantics.strictFloats) genCallPolyfillableBuiltin(FroundBuiltin, expr) + else wg(UnaryOp(irt.JSUnaryOp.+, expr)) case NoType | NullType | NothingType | _:RecordType => throw new AssertionError(s"Unexpected type $tpe in genAsInstanceOf") } } else { - tpe match { + val resultTree = tpe match { case ClassType(ObjectClass) => expr case ClassType(className) => @@ -316,6 +319,8 @@ private[emitter] final class SJSGen( case NoType | NullType | NothingType | _:RecordType => throw new AssertionError(s"Unexpected type $tpe in genAsInstanceOf") } + + wg(resultTree) } }