Repository navigation
Avoid resolving property types that cannot be unit types when looking for key properties #65056
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1156,7 +1156,7 @@ func (c *Checker) getKeyPropertyCandidateName(types []*Type) string { | |||||||
| for _, t := range types { | ||||||||
| if t.flags&(TypeFlagsObject|TypeFlagsInstantiableNonPrimitive) != 0 { | ||||||||
| for _, p := range c.getPropertiesOfType(t) { | ||||||||
| if isUnitType(c.getTypeOfSymbol(p)) { | ||||||||
| if c.isPropertyOfUnitType(p) { | ||||||||
| return p.Name() | ||||||||
| } | ||||||||
| } | ||||||||
|
|
@@ -1165,6 +1165,39 @@ func (c *Checker) getKeyPropertyCandidateName(types []*Type) string { | |||||||
| return "" | ||||||||
| } | ||||||||
|
|
||||||||
| // Reports whether a property has a unit type, without resolving the types of methods, which are function types, or | ||||||||
| // instantiating the type of an instantiated or mapped property when the type it is instantiated from already rules | ||||||||
| // out a unit type. | ||||||||
| func (c *Checker) isPropertyOfUnitType(prop *ast.Symbol) bool { | ||||||||
| // A method is typed as a function type only when none of the flags getTypeOfSymbol checks before it are set | ||||||||
| if prop.Flags()&(ast.SymbolFlagsMethod|ast.SymbolFlagsAccessor|ast.SymbolFlagsVariable|ast.SymbolFlagsProperty) == ast.SymbolFlagsMethod { | ||||||||
| return false | ||||||||
| } | ||||||||
|
Comment on lines
+1172
to
+1175
Member
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. Why is this not just an early bailout on |
||||||||
| checkFlags := prop.CheckFlags() | ||||||||
|
Member
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.
Suggested change
|
||||||||
| if checkFlags&(ast.CheckFlagsInstantiated|ast.CheckFlagsMapped) != 0 { | ||||||||
| if links := c.valueSymbolLinks.Get(prop); links.resolvedType == nil { | ||||||||
| var uninstantiated *Type | ||||||||
| if checkFlags&ast.CheckFlagsInstantiated != 0 { | ||||||||
| uninstantiated = c.getTypeOfSymbol(links.target) | ||||||||
| } else { | ||||||||
| mappedType := links.getContainingType() | ||||||||
| uninstantiated = c.getTemplateTypeFromMappedType(core.OrElse(mappedType.AsMappedType().target, mappedType)) | ||||||||
| } | ||||||||
| // An object type other than a mapped type or a generic tuple type never instantiates to a unit type or to never, | ||||||||
| // so neither does a union containing one. A homomorphic mapped type instantiated for a primitive is that | ||||||||
| // primitive. A generic tuple type has a variadic element, like [string, ...T, number], and it normalizes to never | ||||||||
| // when that element is instantiated to never. The tuple then drops out of a union, so with T = never the union | ||||||||
| // [string, ...T, number] | K is just K, which can be a unit type. | ||||||||
| if core.Some(uninstantiated.Distributed(), func(t *Type) bool { | ||||||||
|
Member
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. I don't know if this would be inlined/optimized away but maybe
Suggested change
|
||||||||
| return t.flags&TypeFlagsObject != 0 && t.objectFlags&ObjectFlagsMapped == 0 && !isGenericTupleType(t) | ||||||||
| }) { | ||||||||
|
Comment on lines
+1191
to
+1193
Contributor
Author
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. this is perf-only and we can't really easily test that |
||||||||
| return false | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| return isUnitType(c.getTypeOfSymbol(prop)) | ||||||||
|
Member
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.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| // Given a set of constituent types and a property name, create and return a map keyed by the literal | ||||||||
| // types of the property by that name in each constituent type. No map is returned if some key property | ||||||||
| // has a non-literal type or if less than 10 or less than 50% of the constituents have a unique key. | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is slightly misleading, right? You may still resolve the type.