diff --git a/docs/how/wasm/test/wasm_skips.txt b/docs/how/wasm/test/wasm_skips.txt index 70a07e628..010e96f69 100644 --- a/docs/how/wasm/test/wasm_skips.txt +++ b/docs/how/wasm/test/wasm_skips.txt @@ -10,9 +10,6 @@ internals.ts # inline_asm with x86 instructions 00cppvtable_hidden_return.ts # a @cpp class returning a struct needs the MSVC or a 64-bit Itanium ABI (excluded on i686 too) fetch.ts # (default library) fetch(): the host has no network, so the request throws -# compiler gaps on wasm, each to be fixed and taken off this list -path.ts # a field of Readonly read through Maybe<...> is read wrong: the loop never ends - # fails natively under the same model too (not run natively under it by ctest) 01tuple.ts own # frees a block it does not own (native own: the program does not finish either) 00global_interface_initializer.ts own # asserts "a field read" (native own: the same assert) diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index b11b9bc33..f41a9bea6 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -2210,7 +2210,10 @@ namespace mlirgen // not the default of its union (a zeroed value, read as null or as T's fields) - below auto missingValueToString = isa(type) && MLIRTypeHelper::getMissingValueElement(optType); - if (isa(optType.getElementType()) && !missingValueToString) + // ...nor its truth: the default of a union is zeroed, its type "undefined", and that is + // not null, so `while (curr)` over a `null | { ... }` that has none was true (below, a + // missing value is false) + if (isa(optType.getElementType()) && !missingValueToString && !isa(type)) { auto val = V(builder.create(location, optType.getElementType(), value)); CAST_A(unwrappedValue, location, type, val, genContext); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index cdf5ce8bb..75ef177c1 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -299,6 +299,7 @@ tslang_add_test(NAME test-compile-00-declare-function-twice COMMAND test-runner tslang_add_test(NAME test-compile-00-function-overloads COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00function_overloads.ts") tslang_add_test(NAME test-compile-00-class-override-arity COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_override_arity.ts") tslang_add_test(NAME test-compile-00-bound-method-wider-callback COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00bound_method_wider_callback.ts") +tslang_add_test(NAME test-compile-00-optional-union-truthiness COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00optional_union_truthiness.ts") tslang_add_test(NAME test-compile-00-gc-allocations-distinct COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00gc_allocations_distinct.ts") tslang_add_test(NAME test-compile-00-assert-message COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00assert_message.ts") tslang_add_test(NAME test-compile-01-funcs-vararg COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01funcs_vararg.ts") @@ -980,6 +981,7 @@ tslang_add_test(NAME test-jit-00-declare-function-twice COMMAND test-runner -jit tslang_add_test(NAME test-jit-00-function-overloads COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00function_overloads.ts") tslang_add_test(NAME test-jit-00-class-override-arity COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_override_arity.ts") tslang_add_test(NAME test-jit-00-bound-method-wider-callback COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00bound_method_wider_callback.ts") +tslang_add_test(NAME test-jit-00-optional-union-truthiness COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00optional_union_truthiness.ts") tslang_add_test(NAME test-jit-00-gc-allocations-distinct COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00gc_allocations_distinct.ts") tslang_add_test(NAME test-jit-00-assert-message COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00assert_message.ts") tslang_add_test(NAME test-jit-01-funcs-vararg COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01funcs_vararg.ts") @@ -2599,6 +2601,7 @@ set(TSLANG_CORPUS 00function_overloads.ts 00class_override_arity.ts 00bound_method_wider_callback.ts + 00optional_union_truthiness.ts 00gc_allocations_distinct.ts 00assert_message.ts 00funcs.ts diff --git a/tslang/test/tester/tests/00optional_union_truthiness.ts b/tslang/test/tester/tests/00optional_union_truthiness.ts new file mode 100644 index 000000000..f02e242ea --- /dev/null +++ b/tslang/test/tester/tests/00optional_union_truthiness.ts @@ -0,0 +1,43 @@ +// `null | undefined | { ... }` with no value is false in a condition. The optional's default, a +// zeroed union whose type is "undefined", is not null, and it was read as true: the loop never ended. +// (path.ts got this type from the default library's Readonly.) + +interface Path { + readonly prev: Path | undefined; + readonly key: string | number; +} + +type PathLike = null | undefined | { key: string | number, prev: Path | undefined }; + +function pathToArray(path: PathLike): (string | number)[] { + let curr = path; + let flattened = <(string | number)[]>[]; + while (curr) { + flattened.push(curr.key); + curr = curr.prev; + } + return flattened; +} + +function count(p: PathLike) { + let n = 0; + if (p) n++; + if (!p) n += 10; + return p ? n + 100 : n; +} + +function main() { + const keys = pathToArray({ key: "path", prev: { key: 2, prev: undefined } }); + assert(keys.length == 2); + assert(keys[0] == "path"); + assert(keys[1] == 2); + + assert(pathToArray(undefined).length == 0); + assert(pathToArray(null).length == 0); + + assert(count(undefined) == 10); + assert(count(null) == 10); + assert(count({ key: 1, prev: undefined }) == 101); + + print("done."); +}