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
37 changes: 37 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,14 @@ class MLIRGenImpl

if (typeToken)
{
if (text == S("function") || text == S("class") || text == S("interface") || text == S("object") || text == S("array"))
{
if (auto membersType = getUnionMembersOfTypeOf(expr, wstos(text), genContext))
{
return addSafeCastStatement(expr, membersType, inverse, elseSafeCase, genContext);
}
}

return addSafeCastStatement(expr, typeToken, inverse, elseSafeCase, genContext);
}

Expand All @@ -3405,6 +3413,35 @@ class MLIRGenImpl
return mlir::failure();
}

// "function", "class", "interface", "object" and "array" name a kind, not a type, so they narrow to
// Opaque (or Opaque[]). A union already lists its members of that kind - the members whose runtime
// tag, typeOfAsString, is that name - and when there is exactly one, the union is narrowed to it:
// through Opaque a function could not be called and a class field not resolved.
// With several members of the kind it stays Opaque. The union cast helper castFromUnion generates
// (`if (typeof t == 'function') return t;`) depends on that: narrowed to a smaller union, `return t`
// would be a union cast again and instantiate the helper without end.
// Returns no type when the value is not a union or does not have exactly one member of the kind.
mlir::Type getUnionMembersOfTypeOf(Expression expr, llvm::StringRef typeOfName, const GenContext &genContext)
{
auto unionType = dyn_cast_or_null<mlir_ts::UnionType>(evaluate(expr, genContext));
if (!unionType)
{
return mlir::Type();
}

TypeOfOpHelper toh(builder);
SmallVector<mlir::Type> members;
for (auto member : unionType.getTypes())
{
if (toh.typeOfAsString(member) == typeOfName)
{
members.push_back(member);
}
}

return members.size() == 1 ? members.front() : mlir::Type();
}

mlir::LogicalResult checkSafeCastUndefined(Expression optVal, Expression undefVal, bool inverse, ElseSafeCase *elseSafeCase, const GenContext &genContext)
{
auto expr = stripParentheses(undefVal);
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 @@ -173,6 +173,7 @@ add_test(NAME test-compile-00-funcs-hybrid-null-this COMMAND test-runner "${PROJ
add_test(NAME test-compile-00-typeof-static-fold COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_static_fold.ts")
add_test(NAME test-compile-00-typeof-function-narrowing COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_function_narrowing.ts")
add_test(NAME test-compile-00-typeof-static-fold-conditions COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_static_fold_conditions.ts")
add_test(NAME test-compile-00-typeof-union-narrowing COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_union_narrowing.ts")
add_test(NAME test-compile-00-array-move-wide-elements COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_move_wide_elements.ts")
add_test(NAME test-compile-00-funcs-expression-generic COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00funcs_expression_generic.ts")
add_test(NAME test-compile-00-funcs-expression-iterator COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00funcs_expression_iterator.ts")
Expand Down Expand Up @@ -592,6 +593,7 @@ add_test(NAME test-jit-00-funcs-hybrid-null-this COMMAND test-runner -jit "${PRO
add_test(NAME test-jit-00-typeof-static-fold COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_static_fold.ts")
add_test(NAME test-jit-00-typeof-function-narrowing COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_function_narrowing.ts")
add_test(NAME test-jit-00-typeof-static-fold-conditions COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_static_fold_conditions.ts")
add_test(NAME test-jit-00-typeof-union-narrowing COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00typeof_union_narrowing.ts")
add_test(NAME test-jit-00-array-move-wide-elements COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_move_wide_elements.ts")
add_test(NAME test-jit-00-funcs-expression-generic COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00funcs_expression_generic.ts")
add_test(NAME test-jit-00-funcs-expression-iterator COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00funcs_expression_iterator.ts")
Expand Down Expand Up @@ -1581,6 +1583,7 @@ set(TSLANG_CORPUS
00typeof_function_narrowing.ts
00typeof_static_fold.ts
00typeof_static_fold_conditions.ts
00typeof_union_narrowing.ts
00types_indexedaccesstype.ts
00types_keyof_enum.ts
00types_mappedtype.ts
Expand Down
44 changes: 44 additions & 0 deletions tslang/test/tester/tests/00typeof_union_narrowing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// `typeof u === "function"` (also "class", "array") names a kind, not a type. For a union the
// members of that kind are known, so `u` is narrowed to them. It was narrowed to Opaque instead:
// a function could not be called and a class field could not be resolved.
class Box {
value = 5;
}

function one(): number {
return 1;
}

function main() {
let f: (() => number) | string = one;
if (typeof f === "function") {
assert(f() == 1, "function member is callable");
} else {
assert(false, "function: wrong branch");
}

f = "abc";
if (typeof f !== "function") {
assert(f.length == 3, "string member after !==");
} else {
assert(false, "string: wrong branch");
}

let c: Box | number = new Box();
if (typeof c === "class") {
assert(c.value == 5, "class member field");
} else {
assert(false, "class: wrong branch");
}

const numbers: number[] = [1, 2, 3];
let a: number[] | string = numbers;
if (typeof a === "array") {
assert(a.length == 3, "array member length");
assert(a[2] == 3, "array member element");
} else {
assert(false, "array: wrong branch");
}

print("done.");
}
Loading