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
12 changes: 7 additions & 5 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ should be a complete file that was run (`tslang file.ts`, JIT, Windows x64), wit

## Open

Items 1-23 (collected 2026-10-12 to pre-alpha113) are fixed or answered; their write-ups are in this
file's git history. The last two: 22 (`T | null` of a `@value` type whose only field is a class or
interface crashed the compiler, a regression from #760) was fixed by #763; 23 (`h && cond` with `h`
a `@cpp` class `| null` failed with "instanceof can't be used with a @cpp class") was fixed by #765,
regression test `tslang/test/tester/tests/00cpp_union_truth.ts`.
Items 1-26 (collected 2026-10-12 to pre-alpha115) are fixed or answered; their write-ups are in this
file's git history. The last three, fixed by #775 (regression tests
`tslang/test/tester/tests/00tslang_issues_24_26.ts` and `ref_element_layout_error.ts`): 24 (an array
literal holding `const X = 1 << n` widened to 64-bit elements: shifts and bitwise operators now give an
`s32`, as `int` is); 25 (`cond ? [1] : []` crashed when iterated: an empty array literal now takes the
other branch's array type); 26 (`Ref(x)` of a `number` was accepted for a `Ref<f32>` parameter: a
reference to a number laid out otherwise is now an error).
38 changes: 38 additions & 0 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,44 @@ namespace mlirgen
}
}

// a reference is the address of what it refers to: one to a number (a double) handed on as a
// `Ref<f32>` is a `double*` read as a `float*`. Numbers referred to must be laid out alike
if (auto destRefType = dyn_cast<mlir_ts::RefType>(type))
{
if (auto srcRefType = dyn_cast<mlir_ts::RefType>(valueType))
{
// {is float, width} of a scalar number, or none
auto layoutOf = [](mlir::Type elementType) -> std::optional<std::pair<bool, unsigned>> {
if (isa<mlir_ts::NumberType>(elementType))
{
return std::make_pair(true, 64u);
}

if (auto floatType = dyn_cast<mlir::FloatType>(elementType))
{
return std::make_pair(true, floatType.getWidth());
}

if (auto intType = dyn_cast<mlir::IntegerType>(elementType); intType && intType.getWidth() > 1)
{
return std::make_pair(false, intType.getWidth());
}

return std::nullopt;
};

auto srcLayout = layoutOf(srcRefType.getElementType());
auto destLayout = layoutOf(destRefType.getElementType());
if (srcLayout && destLayout && *srcLayout != *destLayout)
{
emitError(location) << "a reference to " << to_print(srcRefType.getElementType())
<< " can't be used as a reference to " << to_print(destRefType.getElementType())
<< ": what it refers to is laid out otherwise";
return mlir::failure();
}
}
}

if (auto result = castFromSourceSpecialCases(location, type, value, valueType, genContext))
{
return *result;
Expand Down
22 changes: 21 additions & 1 deletion tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,22 @@ namespace mlirgen
};
auto trueType = widenArrayLiteral(resultTrue.getType());
auto falseType = widenArrayLiteral(resultFalse.getType());

// an empty array literal is an array of the other branch's elements: TypeScript has
// `c ? [1] : []` a `number[]`. Left an `any[]`, the merge was a union `s32[] | any[]`
// whose `for...of` read the array through the union's tag and crashed
auto isEmptyArrayLiteral = [](mlir::Type type) {
auto constArrayType = dyn_cast<mlir_ts::ConstArrayType>(type);
return constArrayType && constArrayType.getSize() == 0;
};
if (isEmptyArrayLiteral(resultFalse.getType()) && isa<mlir_ts::ArrayType>(trueType))
{
falseType = trueType;
}
else if (isEmptyArrayLiteral(resultTrue.getType()) && isa<mlir_ts::ArrayType>(falseType))
{
trueType = falseType;
}
auto defaultUnionType = getUnionType(location, trueType, falseType);
auto merged = false;
auto resultType = mth.findBaseType(trueType, falseType, merged, defaultUnionType);
Expand All @@ -832,7 +848,11 @@ namespace mlirgen
auto array = value;
if (isa<mlir_ts::ConstArrayType>(value.getType()))
{
auto result = cast(location, mth.removeConstType(value.getType()), value, genContext);
// an empty literal is made an array of the merged type's elements directly
auto arrayType = isEmptyArrayLiteral(value.getType()) && isa<mlir_ts::ArrayType>(resultType)
? resultType
: mth.removeConstType(value.getType());
auto result = cast(location, arrayType, value, genContext);
if (result.failed_or_no_value())
{
return mlir::Value();
Expand Down
6 changes: 5 additions & 1 deletion tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8245,7 +8245,11 @@ class MLIRGenImpl
// cast to int: JavaScript's Int32 coercion, but a 64-bit integer left operand keeps its
// type (and signedness), the right one is cast to it. Cast to i32, `u64 << 40` shifted
// by 40 mod 32 and dropped the upper half.
mlir::Type intType = builder.getI32Type();
// ToInt32 is signed: `1 << 18` is an s32 as `int` is, so `[1 << 18, w]` with `w: int` is an
// s32[]. Signless, it merged with s32 as an index would, to s64. `>>>` is ToUint32's
mlir::Type intType = opCode == SyntaxKind::GreaterThanGreaterThanGreaterThanToken
? builder.getI32Type()
: mlir::IntegerType::get(builder.getContext(), 32, mlir::IntegerType::Signed);
if (auto leftIntType = dyn_cast<mlir::IntegerType>(leftExpressionValue.getType()))
{
if (leftIntType.getWidth() > 32)
Expand Down
11 changes: 11 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ tslang_add_test(NAME test-compile-00-closure-fewer-parameters COMMAND test-runne
tslang_add_test(NAME test-compile-00-in-interface-union COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00in_interface_union.ts")
tslang_add_test(NAME test-compile-00-class-values COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_values.ts")
tslang_add_test(NAME test-compile-00-mixins COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00mixins.ts")
tslang_add_test(NAME test-compile-00-tslang-issues-24-26 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issues_24_26.ts")
tslang_add_test(NAME test-compile-00-class-arrow-fields COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_arrow_fields.ts")
tslang_add_test(NAME test-compile-00-class-arrow-getter COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_arrow_getter.ts")
tslang_add_test(NAME test-compile-00-interface-generic-method COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_generic_method.ts")
Expand Down Expand Up @@ -1774,6 +1775,16 @@ set_tests_properties(test-compile-class-value-constructor-error PROPERTIES PASS_
foreach(mixin_mm gc rc none own)
tslang_add_test(NAME test-jit-00-mixins-${mixin_mm} COMMAND test-runner -jit -mm=${mixin_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00mixins.ts")
endforeach()
# TSLANG_ISSUES 24-26 (bitwise results are s32, an empty array literal in a conditional, references laid
# out alike) under every memory model
foreach(issues_mm gc rc none own)
tslang_add_test(NAME test-jit-00-tslang-issues-24-26-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issues_24_26.ts")
endforeach()
# ...a reference to a number is no reference to an f32
add_test(NAME test-compile-ref-element-layout-error
COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/ref_element_layout_error.ts")
set_tests_properties(test-compile-ref-element-layout-error PROPERTIES PASS_REGULAR_EXPRESSION "a reference to number can't be used as a reference to f32"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
# ...a class extending a class value known at run time only is an error
add_test(NAME test-compile-mixin-runtime-base-error
COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/mixin_runtime_base_error.ts")
Expand Down
72 changes: 72 additions & 0 deletions tslang/test/tester/tests/00tslang_issues_24_26.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// TSLANG_ISSUES 24 and 25, and the references 26 still allows.

// 24: a shift, `&`, `|` or `^` is ToInt32's signed 32-bit integer, as `int` is: an array literal holding
// `1 << 18` and an `int` is an int array, not one of 64-bit integers
const CAP = 1 << 18;
const MASK = 0xff & 0x0f;

class Grid {
w: int = 290;

header() {
return [CAP, this.w, MASK | 1];
}
}

// 25: an empty array literal against a non-empty one is an array of the other's elements
class S {
scene: int = 4;

check(): int {
const expected = this.scene == 4 ? [1] : [];
let sum = 0;
for (const k of expected) {
sum += k;
}

return sum;
}

nested(): int {
const expected = this.scene == 2 ? [0, 2] : this.scene == 4 ? [1] : this.scene == 5 ? [2, 3] : [];
let sum = 0;
for (const k of expected) {
sum += k;
}

return sum + expected.length * 10;
}

emptyFirst(): int {
const expected = this.scene != 4 ? [] : [5, 6];
return expected.length;
}
}

// 26: a reference to a number laid out as the parameter's is accepted
function fillF32(dst: Ref<f32>) {
return 1;
}

function fillInt(dst: Ref<int>) {
return 2;
}

function main() {
const header = new Grid().header();
assert(header.length == 3 && header[0] == 262144 && header[1] == 290 && header[2] == 15, "values");
assert(sizeof(header[0]) == 4, "an int array");

const s = new S();
assert(s.check() == 1, "the non-empty branch");
assert(s.nested() == 11, "nested conditionals");
assert(s.emptyFirst() == 2, "the empty literal first");
s.scene = 3;
assert(s.check() == 0 && s.nested() == 0 && s.emptyFirst() == 0, "the empty branch");

const floats: f32[] = [1, 2, 3];
const ints: int[] = [1, 2, 3];
assert(fillF32(Ref(floats[0])) == 1 && fillInt(Ref(ints[1])) == 2, "references laid out alike");

print("done.");
}
17 changes: 17 additions & 0 deletions tslang/test/tester/tests/ref_element_layout_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TSLANG_ISSUES 26: a reference to a number (a double) is no `Ref<f32>`: the function would read a
// `double*` as a `float*`.
declare function Fill(enable: int, dst: Ref<f32>): int;

class A {
sensed: number[] = [0.0, 0.0, 0.0];

run() {
Fill(1, Ref(this.sensed[0]));
print(this.sensed[0]);
}
}

function main() {
const a = new A();
a.run();
}
4 changes: 3 additions & 1 deletion tslang/test/tester/verify-ownership.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ set(not_compilable_alone
class_field_capture_error.ts
cpp_class_value_error.ts
# an error by design (test-compile-mixin-runtime-base-error)
mixin_runtime_base_error.ts)
mixin_runtime_base_error.ts
# an error by design (test-compile-ref-element-layout-error)
ref_element_layout_error.ts)

file(GLOB corpus "${TESTS_DIR}/*.ts")
list(SORT corpus)
Expand Down
Loading