question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compiler doesn't remove unreachable branches

See original GitHub issue

This 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:open
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kosik86commented, Oct 28, 2018

@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.

0reactions
ChadKillingsworthcommented, Oct 27, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found