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
14 changes: 4 additions & 10 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,14 +704,14 @@ namespace mlirgen
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
auto whenTrueExpression = conditionalExpressionAST->whenTrue;

ElseSafeCase elseSafeCase;
ElseSafeCases elseSafeCases;
mlir::Value resultTrue;
if (staticCondition.value_or(true))
{
// check if we do safe-cast here
SymbolTableScopeT varScope(symbolTable);
SafeTypesMapScopeT safeTypesMapScope(safeTypesMap);
checkSafeCast(conditionalExpressionAST->condition, V(result), &elseSafeCase, genContext);
checkSafeCast(conditionalExpressionAST->condition, V(result), &elseSafeCases, genContext);
auto result = mlirGen(whenTrueExpression, genContext);
if (!genContext.allowPartialResolve)
{
Expand All @@ -735,10 +735,7 @@ namespace mlirgen
{
SymbolTableScopeT varScope(symbolTable);
SafeTypesMapScopeT safeTypesMapScope(safeTypesMap);
if (elseSafeCase.safeType)
{
addSafeCastStatement(elseSafeCase.expr, elseSafeCase.safeType, false, nullptr, genContext);
}
addElseSafeCastStatements(elseSafeCases, genContext);

auto result2 = mlirGen(whenFalseExpression, genContext);
if (!genContext.allowPartialResolve)
Expand All @@ -751,10 +748,7 @@ namespace mlirgen
else
{
resultFalse = mlirGenSkippedBranch(location, whenFalseExpression, [&](const GenContext &evalGenContext) {
if (elseSafeCase.safeType)
{
addSafeCastStatement(elseSafeCase.expr, elseSafeCase.safeType, false, nullptr, evalGenContext);
}
addElseSafeCastStatements(elseSafeCases, evalGenContext);
}, genContext);
}

Expand Down
77 changes: 59 additions & 18 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4144,6 +4144,18 @@ class MLIRGenImpl
mlir::Type safeType;
};

// what a false condition narrows: one test gives one case, `a || b` (false when both are) gives
// the cases of both
using ElseSafeCases = SmallVector<ElseSafeCase, 2>;

void addElseSafeCastStatements(const ElseSafeCases &elseSafeCases, const GenContext &genContext)
{
for (auto &elseSafeCase : elseSafeCases)
{
addSafeCastStatement(elseSafeCase.expr, elseSafeCase.safeType, false, nullptr, genContext);
}
}

mlir::LogicalResult addSafeCastStatement(Expression expr, Node typeToken, bool inverse, ElseSafeCase* elseSafeCase, const GenContext &genContext)
{
auto safeType = getType(typeToken, genContext);
Expand Down Expand Up @@ -4750,7 +4762,9 @@ class MLIRGenImpl
return addSafeCastStatement(expr, typePredicateType.getElementType(), inverse, elseSafeCase, genContext);
}

mlir::LogicalResult checkSafeCast(Expression exprIn, mlir::Value conditionValue, ElseSafeCase *elseSafeCase, const GenContext &genContext)
// Narrows the then branch of a condition here, and gathers into elseSafeCases what narrows its
// else branch (the caller adds those there).
mlir::LogicalResult checkSafeCast(Expression exprIn, mlir::Value conditionValue, ElseSafeCases *elseSafeCases, const GenContext &genContext)
{
auto expr = stripParentheses(exprIn);

Expand All @@ -4760,25 +4774,51 @@ class MLIRGenImpl
auto op = (SyntaxKind)binExpr->operatorToken;
if (op == SyntaxKind::AmpersandAmpersandToken)
{
auto left = binExpr->left;
auto leftResult = checkSafeCast(left, conditionValue, elseSafeCase, genContext);
if (mlir::failed(leftResult))
{
return leftResult;
}
// `a && b` is true when both are: each side narrows the then branch, also when the other
// is no test (`k && x !== undefined` stopped at `k`). It is false when either is, so
// neither narrows the else branch: in the else of `x === undefined && k`, x was read as
// defined
auto leftResult = checkSafeCast(binExpr->left, conditionValue, nullptr, genContext);
auto rightResult = checkSafeCast(binExpr->right, conditionValue, nullptr, genContext);
return mlir::success(mlir::succeeded(leftResult) || mlir::succeeded(rightResult));
}

auto right = binExpr->right;
auto rightResult = checkSafeCast(right, conditionValue, elseSafeCase, genContext);
if (mlir::failed(rightResult))
if (op == SyntaxKind::BarBarToken)
{
// `a || b` is true when either is: neither narrows the then branch. It is false when both
// are, so the else branch has what each side's being false gives
if (elseSafeCases)
{
return rightResult;
collectElseSafeCases(binExpr->left, conditionValue, *elseSafeCases, genContext);
collectElseSafeCases(binExpr->right, conditionValue, *elseSafeCases, genContext);
}

return mlir::success();
}
}

return checkSafeCastOne(exprIn, conditionValue, elseSafeCase, genContext);
ElseSafeCase elseSafeCase{};
auto result = checkSafeCastOne(exprIn, conditionValue, elseSafeCases ? &elseSafeCase : nullptr, genContext);
if (elseSafeCases && elseSafeCase.safeType)
{
elseSafeCases->push_back(elseSafeCase);
}

return result;
}

// What a condition's being false narrows, without narrowing anything where it is true: the casts
// checkSafeCast emits for that go to a temporary module, thrown away
void collectElseSafeCases(Expression expr, mlir::Value conditionValue, ElseSafeCases &elseSafeCases, const GenContext &genContext)
{
TempModuleScope tempModuleScope(*this);
SymbolTableScopeT varScope(symbolTable);
SafeTypesMapScopeT safeTypesMapScope(safeTypesMap);

GenContext evalGenContext(genContext);
evalGenContext.allowPartialResolve = true;
evalGenContext.funcOp = tempFuncOp;
checkSafeCast(expr, conditionValue, &elseSafeCases, evalGenContext);
}

mlir::LogicalResult checkSafeCastOne(Expression exprIn, mlir::Value conditionValue, ElseSafeCase *elseSafeCase, const GenContext &genContext)
Expand Down Expand Up @@ -5604,7 +5644,6 @@ class MLIRGenImpl

builder.setInsertionPointToStart(&ifOp.getThenRegion().front());

ElseSafeCase elseSafeCase;
mlir::Value resultTrue;
{
if (rightSkipped)
Expand All @@ -5617,7 +5656,7 @@ class MLIRGenImpl
// check if we do safe-cast here
SymbolTableScopeT varScope(symbolTable);
SafeTypesMapScopeT safeTypesMapScope(safeTypesMap);
checkSafeCast(leftExpression, V(result), &elseSafeCase, genContext);
checkSafeCast(leftExpression, V(result), nullptr, genContext);

auto result = mlirGen(rightExpression, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(result)
Expand Down Expand Up @@ -5658,10 +5697,12 @@ class MLIRGenImpl
{
SymbolTableScopeT varScope(symbolTable);
SafeTypesMapScopeT safeTypesMapScope(safeTypesMap);
if (elseSafeCase.safeType)
{
addSafeCastStatement(elseSafeCase.expr, elseSafeCase.safeType, false, nullptr, genContext);
}

// the right side of `||` runs when the left is false: `x === undefined || x > 1`
// compares a defined x. Never gathered before, as only `&&` checked its left side
ElseSafeCases elseSafeCases;
collectElseSafeCases(leftExpression, leftExpressionValue, elseSafeCases, genContext);
addElseSafeCastStatements(elseSafeCases, genContext);

auto result = mlirGen(rightExpression, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(result)
Expand Down
14 changes: 5 additions & 9 deletions tslang/lib/TypeScript/MLIRGenStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ namespace mlirgen
// value, and for a statically decided test that cast can be one the value cannot take - an array
// narrowed to `string` by `typeof x === "string"`. Under --di the narrowed variable's debug record
// keeps such a cast alive into LLVM lowering even though the branch body was skipped.
ElseSafeCase elseSafeCase{};
ElseSafeCases elseSafeCases;
// `if (x === null) return;` narrows x for the rest of the block, as an else branch would
auto thenExits = !hasElse && !literalValue.has_value() && genContext.funcOp
&& statementAlwaysExits(ifStatementAST->thenStatement);
Expand All @@ -667,7 +667,7 @@ namespace mlirgen
if (processIf)
{
// check if we do safe-cast here
checkSafeCast(ifStatementAST->expression, V(result), hasElse || thenExits ? &elseSafeCase : nullptr, genContext);
checkSafeCast(ifStatementAST->expression, V(result), hasElse || thenExits ? &elseSafeCases : nullptr, genContext);

auto result = mlirGenBranchStatement(ifStatementAST->thenStatement, genContext);
EXIT_IF_FAILED(result)
Expand All @@ -684,11 +684,7 @@ namespace mlirgen
auto processIf = !literalValue.has_value() || !literalValue.value();
if (processIf)
{
if (elseSafeCase.safeType)
{
// add case statement
addSafeCastStatement(elseSafeCase.expr, elseSafeCase.safeType, false, nullptr, genContext);
}
addElseSafeCastStatements(elseSafeCases, genContext);

auto result = mlirGenBranchStatement(ifStatementAST->elseStatement, genContext);
EXIT_IF_FAILED(result)
Expand All @@ -697,9 +693,9 @@ namespace mlirgen

builder.setInsertionPointAfter(ifOp);

if (thenExits && elseSafeCase.safeType)
if (thenExits)
{
addSafeCastStatement(elseSafeCase.expr, elseSafeCase.safeType, false, nullptr, genContext);
addElseSafeCastStatements(elseSafeCases, genContext);
}

return mlir::success();
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 @@ -285,6 +285,7 @@ tslang_add_test(NAME test-compile-00-int64-mul-literal COMMAND test-runner "${PR
tslang_add_test(NAME test-compile-00-int-literal-above-s32 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00int_literal_above_s32.ts")
tslang_add_test(NAME test-compile-00-literal-union-arith COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00literal_union_arith.ts")
tslang_add_test(NAME test-compile-00-literal-union-cast COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00literal_union_cast.ts")
tslang_add_test(NAME test-compile-00-narrowing-and-or COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00narrowing_and_or.ts")
tslang_add_test(NAME test-compile-00-enum-compare COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00enum_compare.ts")
tslang_add_test(NAME test-compile-00-bool-const-array COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00bool_const_array.ts")
tslang_add_test(NAME test-compile-00-mixed-type-ops COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00mixed_type_ops.ts")
Expand Down Expand Up @@ -888,6 +889,7 @@ tslang_add_test(NAME test-jit-00-int64-mul-literal COMMAND test-runner -jit "${P
tslang_add_test(NAME test-jit-00-int-literal-above-s32 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00int_literal_above_s32.ts")
tslang_add_test(NAME test-jit-00-literal-union-arith COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00literal_union_arith.ts")
tslang_add_test(NAME test-jit-00-literal-union-cast COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00literal_union_cast.ts")
tslang_add_test(NAME test-jit-00-narrowing-and-or COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00narrowing_and_or.ts")
tslang_add_test(NAME test-jit-00-enum-compare COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00enum_compare.ts")
tslang_add_test(NAME test-jit-00-bool-const-array COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00bool_const_array.ts")
tslang_add_test(NAME test-jit-00-mixed-type-ops COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00mixed_type_ops.ts")
Expand Down Expand Up @@ -2119,6 +2121,7 @@ set(TSLANG_CORPUS
00int_literal_above_s32.ts
00literal_union_arith.ts
00literal_union_cast.ts
00narrowing_and_or.ts
00enum_compare.ts
00bool_const_array.ts
00break_continue_scope_exit.ts
Expand Down
138 changes: 138 additions & 0 deletions tslang/test/tester/tests/00narrowing_and_or.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// regression test: narrowing by a condition joined with && or ||.
//
// bugs found and fixed:
// - the else branch of `x === undefined && k` read x as defined: what `x === undefined` being false
// says was taken for the whole `&&`, which is false also when only `k` is. An undefined x was read
// as a number there, and `x === undefined` was false;
// - `k && x !== undefined` didn't narrow x: the test stopped at `k`;
// - `||` narrowed nothing: not the else branch (false when both sides are), not what follows
// `if (x === undefined || k) return;`, not the right side of `x === undefined || x > 1`.

class C {
v = 7;
get(): number { return this.v; }
}

// the else branch of `&&` is not narrowed
function andElse(a: number | undefined, k: boolean) {
if (a === undefined && k) {
return "then";
} else {
const e = a;
return e === undefined ? "else undefined" : "else " + e;
}
}

function andElseExits(a: C | undefined, k: boolean) {
if (a === undefined && k) return "then";
const e = a;
return e === undefined ? "after undefined" : "after " + e.get();
}

// both sides of `&&` narrow its then branch
function andRight(a: number | undefined, k: boolean) {
if (k && a !== undefined) {
const n: number = a;
return n * 2;
}

return -1;
}

function andBoth(a: number | undefined, b: number | undefined) {
if (a !== undefined && b !== undefined) {
return a + b;
}

return -1;
}

// `||`: its else branch, what follows when its then branch exits, its right side
function orElse(a: number | undefined, k: boolean) {
if (a === undefined || k) {
return -1;
} else {
const n: number = a;
return n * 2;
}
}

function orExits(a: C | undefined, b: C | undefined) {
if (a === undefined || b === undefined) return -1;
return a.get() + b.get();
}

function orRight(a: number | undefined) {
return a === undefined || a > 1;
}

function orThen(a: number | undefined, k: boolean) {
if (a !== undefined || k) {
const t = a;
return t === undefined ? "then undefined" : "then " + t;
}

return "else";
}

function orNull(a: C | null, k: boolean) {
if (a === null || k) return -1;
return a.get();
}

function orTypeOf(a: string | number, k: boolean) {
if (typeof a === "string" || k) return -1;
const n: number = a;
return n * 2;
}

function ternary(a: number | undefined, k: boolean) {
return a === undefined || k ? -1 : a * 2;
}

function main() {
assert(andElse(undefined, false) == "else undefined");
assert(andElse(3, false) == "else 3");
assert(andElse(3, true) == "else 3");
assert(andElse(undefined, true) == "then");

assert(andElseExits(undefined, false) == "after undefined");
assert(andElseExits(new C(), false) == "after 7");
assert(andElseExits(undefined, true) == "then");

assert(andRight(21, true) == 42);
assert(andRight(21, false) == -1);
assert(andRight(undefined, true) == -1);

assert(andBoth(1, 2) == 3);
assert(andBoth(1, undefined) == -1);

assert(orElse(21, false) == 42);
assert(orElse(21, true) == -1);
assert(orElse(undefined, false) == -1);

assert(orExits(new C(), new C()) == 14);
assert(orExits(new C(), undefined) == -1);
assert(orExits(undefined, new C()) == -1);

assert(orRight(undefined));
assert(orRight(5));
assert(!orRight(0));

assert(orThen(4, false) == "then 4");
assert(orThen(undefined, true) == "then undefined");
assert(orThen(undefined, false) == "else");

assert(orNull(new C(), false) == 7);
assert(orNull(null, false) == -1);
assert(orNull(new C(), true) == -1);

assert(orTypeOf(21, false) == 42);
assert(orTypeOf("s", false) == -1);
assert(orTypeOf(21, true) == -1);

assert(ternary(21, false) == 42);
assert(ternary(undefined, false) == -1);

print("done.");
}
Loading