Compiler doesn't remove unreachable branches
See original GitHub issueThis can be a limitation of the compiler. Input:
const flags = {
"enabled": false
};
if (!flags["enabled"]) {
console.log("not enabled");
} else {
console.log("enabled");
}
if (flags["otherOption"]) {
console.log("other option");
}
Command:
java -jar node_modules/google-closure-compiler/compiler.jar --entry_point input.js \
--compilation_level ADVANCED_OPTIMIZATIONS \
--isolation_mode IIFE input.js
Output:
(function(){var a={enabled:!1};a.enabled?console.log("enabled"):console.log("not enabled");a.otherOption&&console.log("other option");}).call(this);
I would expect the output to only contain one console.log as other branches are unreachable. Is this not possible, e.g. is it unable to determine these conditions in compilation time?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Does Fortran optimize away unreachable branches?
Both Fortran and C compilers may (but do not have to) optimize by removing dead code. Notice that optimization is a compiler thing....
Read more >Dead-code elimination - Wikipedia
In compiler theory, dead-code elimination is a compiler optimization to remove code which does not affect the program results. Removing such code has ......
Read more >How to Find and Remove Dead Code - LinearB
Compiler optimizations are typically conservative approaches to removing dead or unreachable code. If there is any ambiguity regarding program ...
Read more >Control-Flow and Low-Level Optimizations Outline
Branches to branches are remarkably common! – An unconditional branch to an unconditional branch can be replaced by a branch to the latter's...
Read more >unreachable_unchecked in core::hint - Rust
As the compiler assumes that all forms of Undefined Behavior can never happen, it will eliminate all branches in the surrounding code that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ChadKillingsworth this makes sense. I think my example was oversimplified. I use javascript modules and I was hoping that closure compiler can help removing unused functions in these modules if they all share the same config object.
The structure is going to be very similar, but the function is imported from a module.
Not sure why the function is causing problems, but you are going to get much better elimination if you aren’t depending on CollapseProperties. Variables work much more dependably than object properties for this.