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

Weird behavior redefining constructors #2928

Open
lauraharker opened this issue May 15, 2018 · 2 comments
Open

Weird behavior redefining constructors #2928

lauraharker opened this issue May 15, 2018 · 2 comments
Labels
internal-issue-created An internal Google issue has been created to track this GitHub issue

Comments

@lauraharker
Copy link
Contributor

This doesn't cause any type warnings. The type system merges the properties of the first and second declaration of C into a single type.

var C = class {
  f() {}
}; 
C = class {
  g() {}
};
var n = new C();

n.f();
n.g();

There are certain situations where we do get errors for redefining classes. FunctionTypeBuilder throws an error when the constructor signatures are different.

If the second class has a supertype, the supertype doesn't get put into the actual FunctionType, but it does result in a $jscomp.inherits() call that causes an error if it's incompatible with the first definition's superclass.

It's difficult to do this correctly for ES5-style classes because we don't have a single point that declares the class/properties, but prototype properties potentially scattered throughout the code. This could be easier for ES6-style classes because there is a single CLASS node representing the class declaration. We could check for structural compatibility there.

@The-Peppester
Copy link

Yes, this does appear to be a problem (tested here). I would guess the problem might be something like this: somewhere in the closure compiler source code, classes are additive accumulated. Then, the solution is to instead only use the last class assignment to a variable for its properties instead of accumulating the properties.

lauraharker added a commit to lauraharker/polymer that referenced this issue Sep 9, 2019
Fixing a compiler type-equality bug uncovers a new error caused by
google/closure-compiler#2928.
@brad4d brad4d added the internal-issue-created An internal Google issue has been created to track this GitHub issue label Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal-issue-created An internal Google issue has been created to track this GitHub issue
Projects
None yet
Development

No branches or pull requests

3 participants