feat: improve checking terminate state in try#19130
feat: improve checking terminate state in try#19130alexander-akait merged 2 commits intowebpack:mainfrom
Conversation
|
For maintainers only:
|
|
It is ready and tests, just forgot to merge 😞 We need just rebase and we can merge, also it solve more variants |
|
Not the same #15497, this PR can bailout statements if an explicit dead control flow is detected in inner statement. eg. current behavior is as follows Currently we only bailout inner statements inside If statement, but I want to skip the outer as well as we know exactly the rest statements is unreachable |
|
|
||
| it("should not include unused assets", (done) => { | ||
| try { | ||
| if (true) { |
There was a problem hiding this comment.
Make sure you have a test case for try/finally:
try {
return;
} finally {
// This code *will* execute after the 'return'
}There was a problem hiding this comment.
@dmichon-msft we have other PR for such stuff(link above), we need to merge it firstly, I will take care about it
There was a problem hiding this comment.
But they can union in some places, that’s is why we need to make it step by step
8d803ea to
be78f47
Compare
|
I did rebase and also wrote TODO for tomorrow, will finish tomorrow too 😄 |
Oh I see, it's similar Do I need to do anything to help ? |
|
No, will finish soon 👍 |
|
@JSerFeng @dmichon-msft I added basic dead control flow check (#14357), but there are some limitations currently:
function test() {
try {
if (true) {
throw new Error("should throw");
}
require("fail");
} catch(e) {
require('./used');
}
}Now it only works then you have
function test() {
switch ("production") {
return require("fail");
}
case "production":
return require("./used");
default:
return require("fail");
}
}Rare case, if you want you can improve.
This limitation because of we need to handle Examaple:
So you can rebase and improve above cases |
240fb4f to
ad9aec0
Compare
|
Yes, I think the one, We can't change terminate state if inTry because we can't pass that terminate state outside of TryStatement, so I only check if current statement is exactly in TryStatement other than other statement |
adb82e2 to
19a508b
Compare
19a508b to
aff60b6
Compare
lib/javascript/JavascriptParser.js
Outdated
| // we meet other statement, we are not directly in try block | ||
| break loop; | ||
| } | ||
| } |
There was a problem hiding this comment.
I am afraid it can be slow, can we do it only when necessary?
There was a problem hiding this comment.
Especially in very big and nested code
There was a problem hiding this comment.
Or maybe we can refactor our logic and break only in statements?
There was a problem hiding this comment.
This should not be slow as it will break immediately when meets any statement that is not BlockStatement, it can only be slow if user code like ;{{{{ statement; }}}}
|
@JSerFeng Sorry for delay here, I made some improvements (without loop) and fix edge cases with |
What kind of change does this PR introduce?
Original PR:
Add check for dead control flow
We can skip walking some branch that is ignored by dead control flow. For example
We can skip resolve
foomodule. Dead control flow usually caused by DefinePlugin. Users can define a variable as bool literal so that it can be removed at compile time.After knowing that there is already a implementation for this
Improve terminate state checking in TryStatement.
Every terminate state in TryStatement will not be passed to old scope, because catch clause can handle the error, thus outer statements can continue executing.
We can improve this by checking if we are right under TryStatement instead of if statement or for statement
Did you add tests for your changes?
Does this PR introduce a breaking change?
What needs to be documented once your changes are merged?