-
-
Notifications
You must be signed in to change notification settings - Fork 675
feat: support 'this' type on methods and properties #2906
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
Open
+4,518
−9
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -748,7 +748,7 @@ export class Resolver extends DiagnosticEmitter { | |
} | ||
|
||
// otherwise resolve the non-generic call as usual | ||
return this.resolveFunction(prototype, null, new Map(), reportMode); | ||
return this.resolveFunction(prototype, null, new Map(), reportMode, ctxFlow); | ||
} | ||
|
||
private inferGenericTypeArguments( | ||
|
@@ -1515,7 +1515,10 @@ export class Resolver extends DiagnosticEmitter { | |
let member = classLikeTarget.getMember(propertyName); | ||
if (member) { | ||
if (member.kind == ElementKind.PropertyPrototype) { | ||
let propertyInstance = this.resolveProperty(<PropertyPrototype>member, reportMode); | ||
if (!member.is(CommonFlags.Static)) { | ||
this.currentThisExpression = targetNode; | ||
} | ||
let propertyInstance = this.resolveProperty(<PropertyPrototype>member, reportMode, ctxFlow); | ||
if (!propertyInstance) return null; | ||
member = propertyInstance; | ||
if (propertyInstance.is(CommonFlags.Static)) { | ||
|
@@ -2851,14 +2854,35 @@ export class Resolver extends DiagnosticEmitter { | |
/** Contextual types, i.e. `T`. */ | ||
ctxTypes: Map<string,Type> = new Map(), | ||
/** How to proceed with eventual diagnostics. */ | ||
reportMode: ReportMode = ReportMode.Report | ||
reportMode: ReportMode = ReportMode.Report, | ||
/** Contextual flow. */ | ||
ctxFlow: Flow | null = null | ||
): Function | null { | ||
let classInstance: Class | null = null; // if an instance method | ||
let instanceKey = typeArguments ? typesToString(typeArguments) : ""; | ||
|
||
// Instance method prototypes are pre-bound to their concrete class as their parent | ||
if (prototype.is(CommonFlags.Instance)) { | ||
classInstance = assert(prototype.getBoundClassOrInterface()); | ||
|
||
// The actual class instance may be a subclass of the bound class | ||
if (this.currentThisExpression && ctxFlow) { | ||
// In the case of a function or property that uses the polymorphic `this` type, | ||
// this is important, so the return is typed as the actual class instance. | ||
// Note: It should work without this outer type check, and does when testing, but fails the "bootstrap" build. | ||
// TODO: Figure out why and remove the extra check. | ||
Comment on lines
+2871
to
+2872
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you analyze more detail about bootstrap? What is the meaning of "outer type check"? |
||
let type = prototype.functionTypeNode.returnType; | ||
if (type.kind == NodeKind.NamedType && (<NamedTypeNode>type).name.identifier.text == CommonNames.this_) { | ||
let element = this.lookupExpression(this.currentThisExpression!, ctxFlow); | ||
if (element && element.kind == ElementKind.Class) { | ||
classInstance = <Class>element; | ||
} | ||
} | ||
} | ||
|
||
// Otherwise, the bound class is the actual class instance | ||
if (!classInstance) { | ||
classInstance = assert(prototype.getBoundClassOrInterface()); | ||
} | ||
|
||
// check if this exact concrete class and function combination is known already | ||
let resolvedInstance = prototype.getResolvedInstance(instanceKey); | ||
|
@@ -2998,9 +3022,9 @@ export class Resolver extends DiagnosticEmitter { | |
prototype.setResolvedInstance(instanceKey, instance); | ||
|
||
// check against overridden base member | ||
if (classInstance) { | ||
if (prototype.is(CommonFlags.Instance)) { | ||
let methodOrPropertyName = instance.declaration.name.text; | ||
let baseClass = classInstance.base; | ||
let baseClass = (assert(prototype.getBoundClassOrInterface())).base; | ||
if (baseClass) { | ||
let baseMember = baseClass.getMember(methodOrPropertyName); | ||
if (baseMember) { | ||
|
@@ -3492,6 +3516,14 @@ export class Resolver extends DiagnosticEmitter { | |
); | ||
break; | ||
} | ||
if (assert(boundPrototype.typeNode).kind == NodeKind.NamedType && (<NamedTypeNode>boundPrototype.typeNode).name.identifier.text == CommonNames.this_) { | ||
this.error( | ||
DiagnosticCode.Not_implemented_0, | ||
assert(boundPrototype.typeNode).range, | ||
"Polymorphic 'this' typed fields" | ||
); | ||
break; | ||
} | ||
let needsLayout = true; | ||
if (base) { | ||
let existingMember = base.getMember(boundPrototype.name); | ||
|
@@ -3749,7 +3781,9 @@ export class Resolver extends DiagnosticEmitter { | |
/** The prototype of the property. */ | ||
prototype: PropertyPrototype, | ||
/** How to proceed with eventual diagnostics. */ | ||
reportMode: ReportMode = ReportMode.Report | ||
reportMode: ReportMode = ReportMode.Report, | ||
/** Contextual flow. */ | ||
ctxFlow: Flow | null = null | ||
): Property | null { | ||
let instance = prototype.instance; | ||
if (instance) return instance; | ||
|
@@ -3763,7 +3797,8 @@ export class Resolver extends DiagnosticEmitter { | |
getterPrototype, | ||
null, | ||
new Map(), | ||
reportMode | ||
reportMode, | ||
ctxFlow | ||
); | ||
if (getterInstance) { | ||
instance.getterInstance = getterInstance; | ||
|
@@ -3776,7 +3811,8 @@ export class Resolver extends DiagnosticEmitter { | |
setterPrototype, | ||
null, | ||
new Map(), | ||
reportMode | ||
reportMode, | ||
ctxFlow | ||
); | ||
if (setterInstance) { | ||
instance.setterInstance = setterInstance; | ||
|
Oops, something went wrong.
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.