Skip to content

Commit 3fef754

Browse files
authored
Enter signature scope in SerializeReturnTypeForSignature (#3572)
1 parent fdfe96e commit 3fef754

11 files changed

Lines changed: 17 additions & 84 deletions

File tree

internal/checker/nodebuilder.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ func (b *NodeBuilder) IndexInfoToIndexSignatureDeclaration(info *IndexInfo, encl
117117
func (b *NodeBuilder) SerializeReturnTypeForSignature(signatureDeclaration *ast.Node, enclosingDeclaration *ast.Node, flags nodebuilder.Flags, internalFlags nodebuilder.InternalFlags, tracker nodebuilder.SymbolTracker) *ast.Node {
118118
b.enterContext(enclosingDeclaration, flags, internalFlags, tracker)
119119
signature := b.impl.ch.getSignatureFromDeclaration(signatureDeclaration)
120-
return b.exitContext(b.impl.serializeReturnTypeForSignature(signature, true))
120+
_, cleanup := b.impl.enterSignatureScope(signature)
121+
result := b.impl.serializeReturnTypeForSignature(signature, true)
122+
cleanup()
123+
return b.exitContext(result)
121124
}
122125

123126
func (b *NodeBuilder) SerializeTypeParametersForSignature(signatureDeclaration *ast.Node, enclosingDeclaration *ast.Node, flags nodebuilder.Flags, internalFlags nodebuilder.InternalFlags, tracker nodebuilder.SymbolTracker) []*ast.Node {

internal/checker/nodebuilderimpl.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,8 +1823,7 @@ type SignatureToSignatureDeclarationOptions struct {
18231823
func (b *NodeBuilderImpl) signatureToSignatureDeclarationHelper(signature *Signature, kind ast.Kind, options *SignatureToSignatureDeclarationOptions) *ast.Node {
18241824
var typeParameters []*ast.Node
18251825

1826-
expandedParams := b.ch.getExpandedParameters(signature, true /*skipUnionExpanding*/)[0]
1827-
cleanup := b.enterNewScope(signature.declaration, expandedParams, signature.typeParameters, signature.parameters, signature.mapper)
1826+
expandedParams, cleanup := b.enterSignatureScope(signature)
18281827
b.ctx.approximateLength += 3
18291828
// Usually a signature contributes a few more characters than this, but 3 is the minimum
18301829

internal/checker/nodebuilderscopes.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func (b *NodeBuilderImpl) addSymbolTypeToContext(symbol *ast.Symbol, t *Type) fu
6262
}
6363
}
6464

65+
func (b *NodeBuilderImpl) enterSignatureScope(signature *Signature) (expandedParams []*ast.Symbol, cleanup func()) {
66+
expandedParams = b.ch.getExpandedParameters(signature, true /*skipUnionExpanding*/)[0]
67+
cleanup = b.enterNewScope(signature.declaration, expandedParams, signature.typeParameters, signature.parameters, signature.mapper)
68+
return expandedParams, cleanup
69+
}
70+
6571
func (b *NodeBuilderImpl) enterNewScope(declaration *ast.Node, expandedParams []*ast.Symbol, typeParameters []*Type, originalParameters []*ast.Symbol, mapper *TypeMapper) func() {
6672
cleanupContext := cloneNodeBuilderContext(b.ctx)
6773
// For regular function/method declarations, the enclosing declaration will already be signature.declaration,

testdata/baselines/reference/submodule/compiler/declarationEmitNestedGenerics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ function g(x) {
2424

2525

2626
//// [declarationEmitNestedGenerics.d.ts]
27-
declare function f<T>(p: T): <T>(x: T) => typeof p;
27+
declare function f<T>(p: T): <T_1>(x: T_1) => typeof p;
2828
declare function g<T>(x: T): T extends (infer T_1)[] ? T_1 : T;

testdata/baselines/reference/submodule/compiler/declarationEmitShadowing.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,4 @@ function needsRenameForShadowing() {
3737
export declare class A<T = any> {
3838
readonly ShadowedButDoesNotRequireRenaming: <T_1>() => T_1;
3939
}
40-
export declare function needsRenameForShadowing<T>(): <T>(t: T_1, t2: T) => void;
41-
42-
43-
//// [DtsFileErrors]
44-
45-
46-
declarationEmitShadowing.d.ts(4,62): error TS2304: Cannot find name 'T_1'.
47-
48-
49-
==== declarationEmitShadowing.d.ts (1 errors) ====
50-
export declare class A<T = any> {
51-
readonly ShadowedButDoesNotRequireRenaming: <T_1>() => T_1;
52-
}
53-
export declare function needsRenameForShadowing<T>(): <T>(t: T_1, t2: T) => void;
54-
~~~
55-
!!! error TS2304: Cannot find name 'T_1'.
56-
40+
export declare function needsRenameForShadowing<T>(): <T_1>(t: T, t2: T_1) => void;

testdata/baselines/reference/submodule/conformance/typeParametersAvailableInNestedScope3.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ function foo(v) {
3131

3232
//// [typeParametersAvailableInNestedScope3.d.ts]
3333
declare function foo<T>(v: T): {
34-
a: <T>(a: T) => T;
34+
a: <T_1>(a: T_1) => T_1;
3535
b: () => T;
36-
c: <T>(v: T) => {
37-
a: <T_1>(a: T_1) => T_1;
38-
b: () => T;
36+
c: <T_1>(v: T_1) => {
37+
a: <T_2>(a: T_2) => T_2;
38+
b: () => T_1;
3939
};
4040
};

testdata/baselines/reference/submoduleAccepted/compiler/declarationEmitNestedGenerics.js.diff

Lines changed: 0 additions & 9 deletions
This file was deleted.

testdata/baselines/reference/submoduleAccepted/conformance/typeParametersAvailableInNestedScope3.js.diff

Lines changed: 0 additions & 17 deletions
This file was deleted.

testdata/baselines/reference/submoduleTriaged/compiler/declarationEmitShadowing.js.diff

Lines changed: 0 additions & 24 deletions
This file was deleted.

testdata/submoduleAccepted.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,6 @@ compiler/declarationEmitClassMemberWithComputedPropertyName.js.diff
10561056
compiler/isolatedDeclarationsLiterals.js.diff
10571057
conformance/constAssertions.js.diff
10581058

1059-
## Type parameter disambiguation suffixes (`_1`, `_2`) removed in nested/overlapping scopes (shadowing matches runtime semantics; `_1` suffixes were unnecessary)
1060-
## Inner generic type parameters shadow outer ones with the same name instead of being renamed
1061-
compiler/declarationEmitNestedGenerics.js.diff
1062-
conformance/typeParametersAvailableInNestedScope3.js.diff
1063-
10641059
## Comma expressions in emitted JS are now parenthesized for correctness (parenthesizing comma expressions prevents precedence bugs)
10651060
## `a, b` in computed properties and JSX context now emitted as `(a, b)`
10661061
conformance/jsxCheckJsxNoTypeArgumentsAllowed.js.diff

0 commit comments

Comments
 (0)