Skip to content

feat: add type narrow to support better type check #2352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
df38498
feat: add type narrow to support better type check
HerrCai0907 Jun 30, 2022
f55e01a
Revert "feat: add type narrow to support better type check"
HerrCai0907 Jul 1, 2022
aab3bfd
add narrow map in flow and use narrowedType if available [WIP]
HerrCai0907 Jul 1, 2022
5fd12b3
fix: style
HerrCai0907 Jul 1, 2022
c623709
update
HerrCai0907 Jul 3, 2022
999ce39
add testcase
HerrCai0907 Jul 3, 2022
9bfac7e
fix: or operator
HerrCai0907 Jul 3, 2022
2ccd59b
rename function
HerrCai0907 Jul 3, 2022
3c7d0dd
support logic and / or in condition
HerrCai0907 Jul 3, 2022
bbc3fe1
test: add test case for #2359
HerrCai0907 Jul 4, 2022
86f7bce
fix: Fix `||` and `&&` will not inherit branch status
HerrCai0907 Jul 4, 2022
4ea7f9e
update test
HerrCai0907 Jul 4, 2022
58d53a4
distinguish condi kind
HerrCai0907 Jul 4, 2022
e6b9291
update
HerrCai0907 Jul 4, 2022
a9d22ea
refactory: use class replace map
HerrCai0907 Jul 4, 2022
f344b10
Merge branch 'fix/nullable-flag-not-set-after-bool-operator' into fea…
HerrCai0907 Jul 5, 2022
53d5811
fix: assign in condition does not reset element type correctly
HerrCai0907 Jul 5, 2022
df62468
refactory: clean useless code
HerrCai0907 Jul 5, 2022
5cac092
test: add assign in condition testcase
HerrCai0907 Jul 5, 2022
67ce5bd
fix: reduce memory usage
HerrCai0907 Jul 5, 2022
450d490
refactory: add invertedCondition
HerrCai0907 Jul 5, 2022
b0f3aef
test: add unknown condition testcase
HerrCai0907 Jul 5, 2022
3ae97bd
test: add assign testcase
HerrCai0907 Jul 5, 2022
3277d93
update testcase
HerrCai0907 Jul 5, 2022
c5d94fa
Update src/narrow.ts
HerrCai0907 Jul 5, 2022
b1ea921
Update src/narrow.ts
HerrCai0907 Jul 5, 2022
8979ac8
lazy init narrowedType
HerrCai0907 Jul 6, 2022
cecd1a8
Merge branch 'main' into feat/add-type-narrow
HerrCai0907 Jul 22, 2022
2abc9a4
refactory: merge nonnull flag and narrowedTypeMap
HerrCai0907 Jul 28, 2022
60547ae
Merge branch 'main' into feat/add-type-narrow
HerrCai0907 Jul 28, 2022
81a63e3
fix: unused variant
HerrCai0907 Jul 28, 2022
6625289
fix bugs
HerrCai0907 Jul 28, 2022
5b244a3
fix: split logic for type and nullable
HerrCai0907 Jul 28, 2022
403cae8
docs: add comment for function
HerrCai0907 Jul 28, 2022
9115a5c
Update src/compiler.ts
HerrCai0907 Jul 28, 2022
a01d883
feat: add type infer for `||` operator
HerrCai0907 Aug 3, 2022
4824b44
Update src/flow.ts
HerrCai0907 Aug 3, 2022
9b23ad2
Update src/narrow.ts
MaxGraey Aug 4, 2022
4ea58aa
revert: testcase update in 2abc9a4a81be4e30c1fb3386e598626406c3f3e5
HerrCai0907 Aug 4, 2022
f0763d0
fix: type narrowing only effect for Local
HerrCai0907 Aug 4, 2022
71f26aa
update other testcase
HerrCai0907 Aug 4, 2022
db96d70
revert `return assignmentExpression;`
HerrCai0907 Aug 4, 2022
2506962
replace `if(a == null) a = new expression` with `||`
HerrCai0907 Aug 4, 2022
e245743
fix: lint error
HerrCai0907 Aug 4, 2022
5d401b8
add more testcases for nullable
HerrCai0907 Aug 4, 2022
dc2f28a
rename `conditionalNarrowedType` with `typeNarrowChecker`
HerrCai0907 Aug 4, 2022
41d4e4b
refactory testcase
HerrCai0907 Aug 5, 2022
265eff1
fix logic or narrow bugs
HerrCai0907 Aug 5, 2022
e2256c8
remove `||` assert
HerrCai0907 Aug 5, 2022
9af6064
fix: remove default initialized typeNarrowChecker
HerrCai0907 Aug 5, 2022
ac12ee8
fix
HerrCai0907 Aug 5, 2022
0a0135b
refactory `forkTrueBranch` and `forkFalseBranch`
HerrCai0907 Aug 5, 2022
bfc9ed8
refactory type narrow in `inheritBranch`
HerrCai0907 Aug 6, 2022
85387c8
test: add initType testcase
HerrCai0907 Aug 10, 2022
fb117fa
switch position for `inheritBranch` and `freeScopedLocals`
HerrCai0907 Aug 10, 2022
3554be4
copy `narrowedTypes` when `inhertiMutual` inherit 1 flow
HerrCai0907 Aug 10, 2022
3edce9d
refacrory `getVariantType`
HerrCai0907 Aug 10, 2022
51de90e
Merge branch 'main' into feat/add-type-narrow
HerrCai0907 Aug 21, 2022
c11571f
fix merge conflict
HerrCai0907 Aug 21, 2022
5ff9408
Merge branch 'main' into feat/add-type-narrow
HerrCai0907 Sep 3, 2022
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
24 changes: 8 additions & 16 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2555,8 +2555,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentFlow = flow;

// Compile the body assuming the condition turned out true
var bodyFlow = flow.fork();
bodyFlow.inheritNarrowedTypeIfTrue(condExpr);
var bodyFlow = flow.forkTrueBranch(condExpr);
this.currentFlow = bodyFlow;
var bodyStmts = new Array<ExpressionRef>();
var body = statement.statement;
Expand Down Expand Up @@ -2699,9 +2698,8 @@ export class Compiler extends DiagnosticEmitter {

// Compile ifTrue assuming the condition turned out true
var thenStmts = new Array<ExpressionRef>();
var thenFlow = flow.fork();
var thenFlow = flow.forkTrueBranch(condExpr);
this.currentFlow = thenFlow;
thenFlow.inheritNarrowedTypeIfTrue(condExpr);

if (ifTrue.kind == NodeKind.BLOCK) {
this.compileStatements((<BlockStatement>ifTrue).statements, false, thenStmts);
Expand All @@ -2718,9 +2716,8 @@ export class Compiler extends DiagnosticEmitter {
// Compile ifFalse assuming the condition turned out false, if present
if (ifFalse) {
let elseStmts = new Array<ExpressionRef>();
let elseFlow = flow.fork();
let elseFlow = flow.forkFalseBranch(condExpr);
this.currentFlow = elseFlow;
elseFlow.inheritNarrowedTypeIfFalse(condExpr);
if (ifFalse.kind == NodeKind.BLOCK) {
this.compileStatements((<BlockStatement>ifFalse).statements, false, elseStmts);
} else {
Expand Down Expand Up @@ -3240,8 +3237,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentFlow = flow;

// Compile the body assuming the condition turned out true
var bodyFlow = flow.fork();
bodyFlow.inheritNarrowedTypeIfTrue(condExpr);
var bodyFlow = flow.forkTrueBranch(condExpr);
this.currentFlow = bodyFlow;
var bodyStmts = new Array<ExpressionRef>();
var body = statement.statement;
Expand Down Expand Up @@ -4547,9 +4543,8 @@ export class Compiler extends DiagnosticEmitter {
leftExpr = this.compileExpression(left, contextualType.exceptVoid, inheritedConstraints);
leftType = this.currentType;

let rightFlow = flow.fork();
let rightFlow = flow.forkTrueBranch(leftExpr);
this.currentFlow = rightFlow;
rightFlow.inheritNarrowedTypeIfTrue(leftExpr);

// simplify if only interested in true or false
if (contextualType == Type.bool || contextualType == Type.void) {
Expand Down Expand Up @@ -4613,9 +4608,8 @@ export class Compiler extends DiagnosticEmitter {
leftExpr = this.compileExpression(left, contextualType.exceptVoid, inheritedConstraints);
leftType = this.currentType;

let rightFlow = flow.fork();
let rightFlow = flow.forkFalseBranch(leftExpr);
this.currentFlow = rightFlow;
rightFlow.inheritNarrowedTypeIfFalse(leftExpr);

// simplify if only interested in true or false
if (contextualType == Type.bool || contextualType == Type.void) {
Expand Down Expand Up @@ -9284,14 +9278,12 @@ export class Compiler extends DiagnosticEmitter {
}

var outerFlow = this.currentFlow;
var ifThenFlow = outerFlow.fork();
ifThenFlow.inheritNarrowedTypeIfTrue(condExpr);
var ifThenFlow = outerFlow.forkTrueBranch(condExpr);
this.currentFlow = ifThenFlow;
var ifThenExpr = this.compileExpression(ifThen, ctxType);
var ifThenType = this.currentType;

var ifElseFlow = outerFlow.fork();
ifElseFlow.inheritNarrowedTypeIfFalse(condExpr);
var ifElseFlow = outerFlow.forkFalseBranch(condExpr);
this.currentFlow = ifElseFlow;
var ifElseExpr = this.compileExpression(ifElse, ctxType == Type.auto ? ifThenType : ctxType);
var ifElseType = this.currentType;
Expand Down
12 changes: 12 additions & 0 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,18 @@ export class Flow {
return branch;
}

/** Fork this flow to a child flow in branch */
forkTrueBranch(condExpr: ExpressionRef, resetBreakContext: bool = false): Flow {
let branch = this.fork(resetBreakContext);
branch.inheritNarrowedTypeIfTrue(condExpr);
return branch;
}
forkFalseBranch(condExpr: ExpressionRef, resetBreakContext: bool = false): Flow {
let branch = this.fork(resetBreakContext);
branch.inheritNarrowedTypeIfFalse(condExpr);
return branch;
}

/** Gets a free temporary local of the specified type. */
getTempLocal(type: Type, except: BitSet | null = null): Local {
var parentFunction = this.parentFunction;
Expand Down