Skip to content

Commit c72f1ea

Browse files
authored
Consistently use len(x) != 0 checks for Signature.typeParameters (#1449)
1 parent 6104a31 commit c72f1ea

File tree

8 files changed

+34
-55
lines changed

8 files changed

+34
-55
lines changed

internal/checker/checker.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6987,7 +6987,7 @@ func (c *Checker) getQuickTypeOfExpression(node *ast.Node) *Type {
69876987

69886988
func (c *Checker) getReturnTypeOfSingleNonGenericCallSignature(funcType *Type) *Type {
69896989
signature := c.getSingleCallSignature(funcType)
6990-
if signature != nil && signature.typeParameters == nil {
6990+
if signature != nil && len(signature.typeParameters) == 0 {
69916991
return c.getReturnTypeOfSignature(signature)
69926992
}
69936993
return nil
@@ -9121,7 +9121,7 @@ func (c *Checker) inferTypeArguments(node *ast.Node, signature *Signature, args
91219121
// Above, the type of the 'value' parameter is inferred to be 'A'.
91229122
contextualSignature := c.getSingleCallSignature(instantiatedType)
91239123
var inferenceSourceType *Type
9124-
if contextualSignature != nil && contextualSignature.typeParameters != nil {
9124+
if contextualSignature != nil && len(contextualSignature.typeParameters) != 0 {
91259125
inferenceSourceType = c.getOrCreateTypeFromSignature(c.getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters), nil)
91269126
} else {
91279127
inferenceSourceType = instantiatedType
@@ -9382,7 +9382,7 @@ func (c *Checker) addImplementationSuccessElaboration(s *CallState, failed *Sign
93829382
candidate := c.getSignatureFromDeclaration(implementation)
93839383
localState := *s
93849384
localState.candidates = []*Signature{candidate}
9385-
localState.isSingleNonGenericCandidate = candidate.typeParameters == nil
9385+
localState.isSingleNonGenericCandidate = len(candidate.typeParameters) == 0
93869386
if c.chooseOverload(&localState, c.assignableRelation) != nil {
93879387
diagnostic.AddRelatedInfo(NewDiagnosticForNode(implementation, diagnostics.The_call_would_have_succeeded_against_this_implementation_but_implementation_signatures_of_overloads_are_not_externally_visible))
93889388
}
@@ -9949,8 +9949,8 @@ func (c *Checker) isAritySmaller(signature *Signature, target *ast.Node) bool {
99499949
}
99509950

99519951
func (c *Checker) assignContextualParameterTypes(sig *Signature, context *Signature) {
9952-
if context.typeParameters != nil {
9953-
if sig.typeParameters != nil {
9952+
if len(context.typeParameters) != 0 {
9953+
if len(sig.typeParameters) != 0 {
99549954
// This signature has already has a contextual inference performed and cached on it
99559955
return
99569956
}
@@ -20201,8 +20201,8 @@ func (c *Checker) getUnionSignatures(signatureLists [][]*Signature) []*Signature
2020120201
if !core.Same(signatures, masterList) {
2020220202
signature := signatures[0]
2020320203
// Debug.assert(signature, "getUnionSignatures bails early on empty signature lists and should not have empty lists on second pass")
20204-
if signature.typeParameters != nil && core.Some(results, func(s *Signature) bool {
20205-
return s.typeParameters != nil && !c.compareTypeParametersIdentical(signature.typeParameters, s.typeParameters)
20204+
if len(signature.typeParameters) != 0 && core.Some(results, func(s *Signature) bool {
20205+
return len(s.typeParameters) != 0 && !c.compareTypeParametersIdentical(signature.typeParameters, s.typeParameters)
2020620206
}) {
2020720207
results = nil
2020820208
} else {

internal/checker/flow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,7 @@ func (c *Checker) getEffectsSignature(node *ast.Node) *Signature {
20402040
}
20412041
signatures := c.getSignaturesOfType(core.OrElse(apparentType, c.unknownType), SignatureKindCall)
20422042
switch {
2043-
case len(signatures) == 1 && signatures[0].typeParameters == nil:
2043+
case len(signatures) == 1 && len(signatures[0].typeParameters) == 0:
20442044
signature = signatures[0]
20452045
case core.Some(signatures, c.hasTypePredicateOrNeverReturnType):
20462046
signature = c.getResolvedSignature(node, nil, CheckModeNormal)

internal/checker/nodebuilderimpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,11 +1690,11 @@ func (b *nodeBuilderImpl) signatureToSignatureDeclarationHelper(signature *Signa
16901690
b.ctx.approximateLength += 3
16911691
// Usually a signature contributes a few more characters than this, but 3 is the minimum
16921692

1693-
if b.ctx.flags&nodebuilder.FlagsWriteTypeArgumentsOfSignature != 0 && signature.target != nil && signature.mapper != nil && signature.target.typeParameters != nil {
1693+
if b.ctx.flags&nodebuilder.FlagsWriteTypeArgumentsOfSignature != 0 && signature.target != nil && signature.mapper != nil && len(signature.target.typeParameters) != 0 {
16941694
for _, parameter := range signature.target.typeParameters {
16951695
typeParameters = append(typeParameters, b.typeToTypeNode(b.ch.instantiateType(parameter, signature.mapper)))
16961696
}
1697-
} else if signature.typeParameters != nil {
1697+
} else {
16981698
for _, parameter := range signature.typeParameters {
16991699
typeParameters = append(typeParameters, b.typeParameterToDeclaration(parameter))
17001700
}

internal/checker/relater.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ func (c *Checker) compareSignaturesRelated(source *Signature, target *Signature,
14621462
}
14631463
return TernaryFalse
14641464
}
1465-
if source.typeParameters != nil && !core.Same(source.typeParameters, target.typeParameters) {
1465+
if len(source.typeParameters) != 0 && !core.Same(source.typeParameters, target.typeParameters) {
14661466
target = c.getCanonicalSignature(target)
14671467
source = c.instantiateSignatureInContextOf(source, target /*inferenceContext*/, nil, compareTypes)
14681468
}
@@ -1653,7 +1653,7 @@ func (c *Checker) compareTypePredicateRelatedTo(source *TypePredicate, target *T
16531653

16541654
// Returns true if `s` is `(...args: A) => R` where `A` is `any`, `any[]`, `never`, or `never[]`, and `R` is `any` or `unknown`.
16551655
func (c *Checker) isTopSignature(s *Signature) bool {
1656-
if s.typeParameters == nil && (s.thisParameter == nil || IsTypeAny(c.getTypeOfParameter(s.thisParameter))) && len(s.parameters) == 1 && signatureHasRestParameter(s) {
1656+
if len(s.typeParameters) == 0 && (s.thisParameter == nil || IsTypeAny(c.getTypeOfParameter(s.thisParameter))) && len(s.parameters) == 1 && signatureHasRestParameter(s) {
16571657
paramType := c.getTypeOfParameter(s.parameters[0])
16581658
var restType *Type
16591659
if c.isArrayType(paramType) {
@@ -2090,7 +2090,7 @@ func (c *Checker) isResolvingReturnTypeOfSignature(signature *Signature) bool {
20902090
}
20912091

20922092
func (c *Checker) findMatchingSignatures(signatureLists [][]*Signature, signature *Signature, listIndex int) []*Signature {
2093-
if signature.typeParameters != nil {
2093+
if len(signature.typeParameters) != 0 {
20942094
// We require an exact match for generic signatures, so we only return signatures from the first
20952095
// signature list and only if they have exact matches in the other signature lists.
20962096
if listIndex > 0 {
@@ -2150,7 +2150,7 @@ func (c *Checker) compareSignaturesIdentical(source *Signature, target *Signatur
21502150
}
21512151
// Check that type parameter constraints and defaults match. If they do, instantiate the source
21522152
// signature with the type parameters of the target signature and continue the comparison.
2153-
if target.typeParameters != nil {
2153+
if len(target.typeParameters) != 0 {
21542154
mapper := newTypeMapper(source.typeParameters, target.typeParameters)
21552155
for i := range len(target.typeParameters) {
21562156
s := source.typeParameters[i]

testdata/baselines/reference/submodule/compiler/abstractClassUnionInstantiation.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ new cls2(); // should error
4646
>cls2 : Abstracts
4747

4848
new cls3(); // should work
49-
>new cls3() : ConcreteA
49+
>new cls3() : ConcreteA | ConcreteB
5050
>cls3 : Concretes
5151

5252
[ConcreteA, AbstractA, AbstractB].map(cls => new cls()); // should error

testdata/baselines/reference/submodule/compiler/abstractClassUnionInstantiation.types.diff

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
--- old.abstractClassUnionInstantiation.types
22
+++ new.abstractClassUnionInstantiation.types
3-
@@= skipped -45, +45 lines =@@
4-
>cls2 : Abstracts
5-
6-
new cls3(); // should work
7-
->new cls3() : ConcreteA | ConcreteB
8-
+>new cls3() : ConcreteA
9-
>cls3 : Concretes
10-
11-
[ConcreteA, AbstractA, AbstractB].map(cls => new cls()); // should error
12-
@@= skipped -31, +31 lines =@@
3+
@@= skipped -76, +76 lines =@@
134

145
[ConcreteA, ConcreteB].map(cls => new cls()); // should work
156
>[ConcreteA, ConcreteB].map(cls => new cls()) : ConcreteA[]

testdata/baselines/reference/submodule/compiler/jsxComponentTypeErrors.errors.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ jsxComponentTypeErrors.tsx(27,16): error TS2786: 'ClassComponent' cannot be used
1717
Its instance type 'ClassComponent' is not a valid JSX element.
1818
Types of property 'type' are incompatible.
1919
Type 'string' is not assignable to type '"element-class"'.
20-
jsxComponentTypeErrors.tsx(28,16): error TS2604: JSX element type 'MixedComponent' does not have any construct or call signatures.
20+
jsxComponentTypeErrors.tsx(28,16): error TS2786: 'MixedComponent' cannot be used as a JSX component.
21+
Its element type 'ClassComponent | { type: string | undefined; }' is not a valid JSX element.
22+
Type 'ClassComponent' is not assignable to type 'Element | ElementClass | null'.
23+
Type 'ClassComponent' is not assignable to type 'Element | ElementClass'.
24+
Type 'ClassComponent' is not assignable to type 'ElementClass'.
25+
Types of property 'type' are incompatible.
26+
Type 'string' is not assignable to type '"element-class"'.
2127
jsxComponentTypeErrors.tsx(37,16): error TS2786: 'obj.MemberFunctionComponent' cannot be used as a JSX component.
2228
Property 'type' is missing in type '{}' but required in type 'Element'.
2329
jsxComponentTypeErrors.tsx(38,16): error TS2786: 'obj. MemberClassComponent' cannot be used as a JSX component.
@@ -77,7 +83,13 @@ jsxComponentTypeErrors.tsx(38,16): error TS2786: 'obj. MemberClassComponent' can
7783
!!! error TS2786: Type 'string' is not assignable to type '"element-class"'.
7884
const elem4 = <MixedComponent />;
7985
~~~~~~~~~~~~~~
80-
!!! error TS2604: JSX element type 'MixedComponent' does not have any construct or call signatures.
86+
!!! error TS2786: 'MixedComponent' cannot be used as a JSX component.
87+
!!! error TS2786: Its element type 'ClassComponent | { type: string | undefined; }' is not a valid JSX element.
88+
!!! error TS2786: Type 'ClassComponent' is not assignable to type 'Element | ElementClass | null'.
89+
!!! error TS2786: Type 'ClassComponent' is not assignable to type 'Element | ElementClass'.
90+
!!! error TS2786: Type 'ClassComponent' is not assignable to type 'ElementClass'.
91+
!!! error TS2786: Types of property 'type' are incompatible.
92+
!!! error TS2786: Type 'string' is not assignable to type '"element-class"'.
8193

8294
const obj = {
8395
MemberFunctionComponent() {

testdata/baselines/reference/submodule/compiler/jsxComponentTypeErrors.errors.txt.diff

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
--- old.jsxComponentTypeErrors.errors.txt
22
+++ new.jsxComponentTypeErrors.errors.txt
3-
@@= skipped -16, +16 lines =@@
4-
Its instance type 'ClassComponent' is not a valid JSX element.
5-
Types of property 'type' are incompatible.
6-
Type 'string' is not assignable to type '"element-class"'.
7-
-jsxComponentTypeErrors.tsx(28,16): error TS2786: 'MixedComponent' cannot be used as a JSX component.
8-
- Its element type 'ClassComponent | { type: string | undefined; }' is not a valid JSX element.
9-
- Type 'ClassComponent' is not assignable to type 'Element | ElementClass | null'.
10-
- Type 'ClassComponent' is not assignable to type 'Element | ElementClass'.
11-
- Type 'ClassComponent' is not assignable to type 'ElementClass'.
12-
- Types of property 'type' are incompatible.
13-
- Type 'string' is not assignable to type '"element-class"'.
14-
+jsxComponentTypeErrors.tsx(28,16): error TS2604: JSX element type 'MixedComponent' does not have any construct or call signatures.
3+
@@= skipped -24, +24 lines =@@
4+
Types of property 'type' are incompatible.
5+
Type 'string' is not assignable to type '"element-class"'.
156
jsxComponentTypeErrors.tsx(37,16): error TS2786: 'obj.MemberFunctionComponent' cannot be used as a JSX component.
167
- Its return type '{}' is not a valid JSX element.
178
- Property 'type' is missing in type '{}' but required in type 'Element'.
@@ -23,22 +14,7 @@
2314

2415

2516
==== jsxComponentTypeErrors.tsx (7 errors) ====
26-
@@= skipped -68, +60 lines =@@
27-
!!! error TS2786: Type 'string' is not assignable to type '"element-class"'.
28-
const elem4 = <MixedComponent />;
29-
~~~~~~~~~~~~~~
30-
-!!! error TS2786: 'MixedComponent' cannot be used as a JSX component.
31-
-!!! error TS2786: Its element type 'ClassComponent | { type: string | undefined; }' is not a valid JSX element.
32-
-!!! error TS2786: Type 'ClassComponent' is not assignable to type 'Element | ElementClass | null'.
33-
-!!! error TS2786: Type 'ClassComponent' is not assignable to type 'Element | ElementClass'.
34-
-!!! error TS2786: Type 'ClassComponent' is not assignable to type 'ElementClass'.
35-
-!!! error TS2786: Types of property 'type' are incompatible.
36-
-!!! error TS2786: Type 'string' is not assignable to type '"element-class"'.
37-
+!!! error TS2604: JSX element type 'MixedComponent' does not have any construct or call signatures.
38-
39-
const obj = {
40-
MemberFunctionComponent() {
41-
@@= skipped -18, +12 lines =@@
17+
@@= skipped -78, +76 lines =@@
4218
const elem5 = <obj.MemberFunctionComponent />;
4319
~~~~~~~~~~~~~~~~~~~~~~~~~~~
4420
!!! error TS2786: 'obj.MemberFunctionComponent' cannot be used as a JSX component.

0 commit comments

Comments
 (0)