Skip to content
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

Incorrect dead code elimination with class instanceof #3618

Closed
AshleyScirra opened this issue Jun 13, 2020 · 2 comments
Closed

Incorrect dead code elimination with class instanceof #3618

AshleyScirra opened this issue Jun 13, 2020 · 2 comments
Assignees

Comments

@AshleyScirra
Copy link

AshleyScirra commented Jun 13, 2020

Using Closure Compiler v20200517

in.js:

window.Class = class MyClass {
	constructor()
	{
		if (this instanceof MyClass)
			console.log("test");
	}
}

new window.Class();

Note if you run this in a browser console it logs "test".

Compile with the following options:

java -jar ./closure-compiler.jar --js in.js --js_output_file out.js --compilation_level ADVANCED --language_in ECMASCRIPT_2015 --language_out ECMASCRIPT_2015

out.js looks like this:

window.a=class{
	constructor(){}
};
new window.a;

Note it has incorrectly removed the console log. Note the class name has been removed, which may have then caused this instanceof MyClass to be identified as false and therefore dead code.

@concavelenz
Copy link
Contributor

Thank you for the report and minimal repro case. This look similiar to another issue @brad4d was looking and maybe has a fix pending for?

@brad4d
Copy link
Contributor

brad4d commented Jun 16, 2020

This is similar to another issue that I fixed, but not quite the same.

I've reproduced the problem with the latest version of the compiler code by following the example here.

  1. RemoveUnusedCode sees only one reference to MyClass, and it's an instanceof check, so it believes no objects of MyClass get created and replaces the instanceof check with false.
  2. Then RemoveUnusedCode removes the MyClass name from the class definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants