google / google/closure-compiler

Missed redundant assignment optimization

Open
#3,187 7 comments 0 reactions 0 assignees View on GitHub
internal-issue-created triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

```
$ npx google-closure-compiler --version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20181210
Built on: 2018-12-12 22:34
```

```
$ cat src.js
var foo = {};
foo = 'asdf';
console.log(foo);
$ npx google-closure-compiler --js=src.js --js_output_file=out.js --compilation_level ADVANCED
$ cat out.js
var a={};a="asdf";console.log(a);
```

I was expecting the above to instead generate

```
var a="asdf";console.log(a);
```

since the first assignment should be redundant.

Curiously, the following optimizes well:

```
$ cat src.js
function f() {
var foo = {};
foo = 'asdf';
console.log(foo);
}
f();
$ npx google-closure-compiler --js=src.js --js_output_file=out.js --compilation_level ADVANCED
$ cat out.js
console.log("asdf");
```

I wonder if I am missing some subtlety here that I was not aware of?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.