Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4418,13 +4418,31 @@ struct GlobalOpLowering : public TsLlvmPattern<mlir_ts::GlobalOp>
{
createAsGlobalConstructor = true;
}
else if (auto binaryOp = dyn_cast<mlir_ts::ArithmeticBinaryOp>(op);
binaryOp && isa<mlir_ts::StringType>(binaryOp.getResult().getType()))
{
// `"text" + 4` is a string concatenation, which allocates, even when every operand
// is a constant (#469)
createAsGlobalConstructor = true;
}
else if (auto castOp = dyn_cast<mlir_ts::CastOp>(op))
{
auto castType = castOp.getRes().getType();
auto inType = castOp.getIn().getType();
if (auto literalType = dyn_cast<mlir_ts::LiteralType>(inType))
{
inType = literalType.getElementType();
}

if (isa<mlir_ts::ArrayType>(castType) || isa<mlir_ts::TupleType>(castType))
{
createAsGlobalConstructor = true;
}
else if (isa<mlir_ts::StringType>(castType) && !isa<mlir_ts::StringType>(inType))
{
// a number or a boolean made into a string allocates it (#469)
createAsGlobalConstructor = true;
}
else
{
// see CreateUnionInstanceOp above: a cast to or from a tagged union lowers to
Expand Down
3 changes: 3 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ tslang_add_test(NAME test-compile-00-void COMMAND test-runner "${PROJECT_SOURCE_
tslang_add_test(NAME test-compile-00-in COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00in.ts")
tslang_add_test(NAME test-compile-00-in-method-names COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00in_method_names.ts")
tslang_add_test(NAME test-compile-00-class-structural-extends COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_structural_extends.ts")
tslang_add_test(NAME test-compile-00-global-constant-concat COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00global_constant_concat.ts")
tslang_add_test(NAME test-compile-00-global-object-literal COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00global_object_literal.ts")
tslang_add_test(NAME test-compile-00-instanceof COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00instanceof.ts")
tslang_add_test(NAME test-compile-00-const-of-let COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00const_of_let.ts")
Expand Down Expand Up @@ -955,6 +956,7 @@ tslang_add_test(NAME test-jit-00-void COMMAND test-runner -jit "${PROJECT_SOURCE
tslang_add_test(NAME test-jit-00-in COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00in.ts")
tslang_add_test(NAME test-jit-00-in-method-names COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00in_method_names.ts")
tslang_add_test(NAME test-jit-00-class-structural-extends COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_structural_extends.ts")
tslang_add_test(NAME test-jit-00-global-constant-concat COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00global_constant_concat.ts")
tslang_add_test(NAME test-jit-00-global-object-literal COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00global_object_literal.ts")
tslang_add_test(NAME test-jit-00-instanceof COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00instanceof.ts")
tslang_add_test(NAME test-jit-00-const-of-let COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00const_of_let.ts")
Expand Down Expand Up @@ -1777,6 +1779,7 @@ set(TSLANG_CORPUS
00infer.ts
00global_union_from_call.ts
00instanceof.ts
00global_constant_concat.ts
00const_of_let.ts
00instanceof_namespace_narrowing.ts
00instanceof_union.ts
Expand Down
22 changes: 22 additions & 0 deletions tslang/test/tester/tests/00global_constant_concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// A module-level variable initialised with a string concatenation of constants, read by a function
// (#469). The initializer looked constant, so it was placed where nothing may run, and the
// concatenation (and the number's conversion to a string) failed to compile there: "ops with side
// effects not allowed in global initializers".
let S = "text" + 4;
const C = "text" + "four";
let U: string | null = "nullable" + 5;
let N = "" + 6.5;
let P = "plain";

function main() {
assert(S == "text4", "a let from a constant concatenation");
assert(C == "textfour", "a const from two string constants");
assert(U == "nullable5", "a nullable string from a constant concatenation");
assert(N == "6.5", "a number converted to a string");
assert(P == "plain", "a plain string constant stays as it is");

S = S + "!";
assert(S == "text4!");

print("done.");
}
Loading