Report an error for super() in an arrow in a typed class constructor - #2128
Open
tmikov wants to merge 1 commit into
Open
Report an error for super() in an arrow in a typed class constructor#2128tmikov wants to merge 1 commit into
tmikov wants to merge 1 commit into
Conversation
Typed classes only support `super()` as the first statement of the
derived constructor. `genClassDeclaration()` rejects a constructor that
doesn't start with one, and `genCallExpr()` builds the call out of the
constructor's own `this` parameter, new.target parameter and class type.
An arrow nested in the constructor gets its own `FunctionContext`, and
`genCapturingFunction()` doesn't set `superClassNode_` or
`typedClassContext` on it. A `super()` in that arrow reached
`genCallExpr()` with a null `superClassNode_` and hit an assertion:
class A { x: number; constructor() { this.x = 1; } }
class B extends A {
constructor() {
super();
(() => { super(); })();
}
}
Nothing is wrong with that program as far as the compiler is concerned.
The required first statement is there, and no diagnostic was emitted
before the assertion fired. Release builds drop the assertion and call
`genExpression()` on a null node.
Making it work would mean routing the constructor's captured `this` and
new.target into the arrow. That is a feature, and it cuts against the
restriction `genClassDeclaration()` deliberately imposes. Report the
restriction instead. The assertion goes away with it, since it no longer
guards anything reachable.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Depends on #2127 and must land after it. The new check fires whenever |
|
@tmikov has imported this pull request. If you are a Meta employee, you can view this in D115505157. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Typed classes only support
super()as the first statement of thederived constructor.
genClassDeclaration()rejects a constructor thatdoesn't start with one, and
genCallExpr()builds the call out of theconstructor's own
thisparameter, new.target parameter and class type.An arrow nested in the constructor gets its own
FunctionContext, andgenCapturingFunction()doesn't setsuperClassNode_ortypedClassContexton it. Asuper()in that arrow reachedgenCallExpr()with a nullsuperClassNode_and hit an assertion:Nothing is wrong with that program as far as the compiler is concerned.
The required first statement is there, and no diagnostic was emitted
before the assertion fired. Release builds drop the assertion and call
genExpression()on a null node.Making it work would mean routing the constructor's captured
thisandnew.target into the arrow. That is a feature, and it cuts against the
restriction
genClassDeclaration()deliberately imposes. Report therestriction instead. The assertion goes away with it, since it no longer
guards anything reachable.
Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com