diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index a92feb7e3..2f60d69d0 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -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` parameter: a +reference to a number laid out otherwise is now an error). diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index 23b5dc72b..696a26256 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -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` is a `double*` read as a `float*`. Numbers referred to must be laid out alike + if (auto destRefType = dyn_cast(type)) + { + if (auto srcRefType = dyn_cast(valueType)) + { + // {is float, width} of a scalar number, or none + auto layoutOf = [](mlir::Type elementType) -> std::optional> { + if (isa(elementType)) + { + return std::make_pair(true, 64u); + } + + if (auto floatType = dyn_cast(elementType)) + { + return std::make_pair(true, floatType.getWidth()); + } + + if (auto intType = dyn_cast(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; diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index f8ba7d746..fc130f9e1 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -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(type); + return constArrayType && constArrayType.getSize() == 0; + }; + if (isEmptyArrayLiteral(resultFalse.getType()) && isa(trueType)) + { + falseType = trueType; + } + else if (isEmptyArrayLiteral(resultTrue.getType()) && isa(falseType)) + { + trueType = falseType; + } auto defaultUnionType = getUnionType(location, trueType, falseType); auto merged = false; auto resultType = mth.findBaseType(trueType, falseType, merged, defaultUnionType); @@ -832,7 +848,11 @@ namespace mlirgen auto array = value; if (isa(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(resultType) + ? resultType + : mth.removeConstType(value.getType()); + auto result = cast(location, arrayType, value, genContext); if (result.failed_or_no_value()) { return mlir::Value(); diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 24cb78a01..266e157bc 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -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(leftExpressionValue.getType())) { if (leftIntType.getWidth() > 32) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 351379912..889d0a24e 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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") @@ -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") diff --git a/tslang/test/tester/tests/00tslang_issues_24_26.ts b/tslang/test/tester/tests/00tslang_issues_24_26.ts new file mode 100644 index 000000000..7fe09ea7e --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issues_24_26.ts @@ -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) { + return 1; +} + +function fillInt(dst: Ref) { + 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."); +} diff --git a/tslang/test/tester/tests/ref_element_layout_error.ts b/tslang/test/tester/tests/ref_element_layout_error.ts new file mode 100644 index 000000000..4b5d7b15c --- /dev/null +++ b/tslang/test/tester/tests/ref_element_layout_error.ts @@ -0,0 +1,17 @@ +// TSLANG_ISSUES 26: a reference to a number (a double) is no `Ref`: the function would read a +// `double*` as a `float*`. +declare function Fill(enable: int, dst: Ref): 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(); +} diff --git a/tslang/test/tester/verify-ownership.cmake b/tslang/test/tester/verify-ownership.cmake index 884a08bdb..2c1f9c8ec 100644 --- a/tslang/test/tester/verify-ownership.cmake +++ b/tslang/test/tester/verify-ownership.cmake @@ -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)