Skip to content

Preserve original type parameter names more often when shadowed #55820

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

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 58 additions & 27 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6623,8 +6623,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (
context.flags & NodeBuilderFlags.GenerateNamesForShadowedTypeParams &&
type.flags & TypeFlags.TypeParameter &&
!isTypeSymbolAccessible(type.symbol, context.enclosingDeclaration)
type.flags & TypeFlags.TypeParameter
) {
const name = typeParameterToName(type, context);
context.approximateLength += idText(name).length;
Expand Down Expand Up @@ -7527,7 +7526,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
&& signature.declaration
&& signature.declaration !== context.enclosingDeclaration
&& !isInJSFile(signature.declaration)
&& some(expandedParams)
&& (some(expandedParams) || some(signature.typeParameters))
) {
// As a performance optimization, reuse the same fake scope within this chain.
// This is especially needed when we are working on an excessively deep type;
Expand All @@ -7545,32 +7544,64 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Note that we only check the most immediate enclosingDeclaration; the only place we
// could potentially add another fake scope into the chain is right here, so we don't
// traverse all ancestors.
const existingFakeScope = getNodeLinks(context.enclosingDeclaration).fakeScopeForSignatureDeclaration ? context.enclosingDeclaration : undefined;
Debug.assertOptionalNode(existingFakeScope, isBlock);
pushFakeScope(
"params",
add => {
for (const param of expandedParams) {
add(param.escapedName, param);
}
},
);

const locals = existingFakeScope?.locals ?? createSymbolTable();
if (context.flags & NodeBuilderFlags.GenerateNamesForShadowedTypeParams) {
// TODO(jakebailey): should this instead be done before walking type parameters?
pushFakeScope(
"typeParams",
add => {
for (const typeParam of signature.typeParameters ?? emptyArray) {
const typeParamName = typeParameterToName(typeParam, context).escapedText;
add(typeParamName, typeParam.symbol);
}
},
);
}

let newLocals: __String[] | undefined;
for (const param of expandedParams) {
if (!locals.has(param.escapedName)) {
newLocals = append(newLocals, param.escapedName);
locals.set(param.escapedName, param);
function pushFakeScope(kind: "params" | "typeParams", addAll: (addSymbol: (name: __String, symbol: Symbol) => void) => void) {
// We only ever need to look two declarations upward.
Debug.assert(context.enclosingDeclaration);
let existingFakeScope: Node | undefined;
if (getNodeLinks(context.enclosingDeclaration).fakeScopeForSignatureDeclaration === kind) {
existingFakeScope = context.enclosingDeclaration;
}
}
else if (context.enclosingDeclaration.parent && getNodeLinks(context.enclosingDeclaration.parent).fakeScopeForSignatureDeclaration === kind) {
existingFakeScope = context.enclosingDeclaration.parent;
}
Debug.assertOptionalNode(existingFakeScope, isBlock);

if (newLocals) {
function removeNewLocals() {
const locals = existingFakeScope?.locals ?? createSymbolTable();
let newLocals: __String[] | undefined;
addAll((name, symbol) => {
if (!locals.has(name)) {
newLocals = append(newLocals, name);
locals.set(name, symbol);
}
});
if (!newLocals) return;

const oldCleanup = cleanup;
function undo() {
forEach(newLocals, s => locals.delete(s));
oldCleanup?.();
}

if (existingFakeScope) {
cleanup = removeNewLocals;
cleanup = undo;
}
else {
// Use a Block for this; the type of the node doesn't matter so long as it
// has locals, and this is cheaper/easier than using a function-ish Node.
const fakeScope = parseNodeFactory.createBlock(emptyArray);
getNodeLinks(fakeScope).fakeScopeForSignatureDeclaration = true;
getNodeLinks(fakeScope).fakeScopeForSignatureDeclaration = kind;
fakeScope.locals = locals;

const saveEnclosingDeclaration = context.enclosingDeclaration;
Expand All @@ -7579,7 +7610,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

cleanup = () => {
context.enclosingDeclaration = saveEnclosingDeclaration;
removeNewLocals();
undo();
};
}
}
Expand Down Expand Up @@ -8127,13 +8158,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function typeParameterShadowsNameInScope(escapedName: __String, context: NodeBuilderContext, type: TypeParameter) {
function typeParameterShadowsOtherTypeParameterInScope(escapedName: __String, context: NodeBuilderContext, type: TypeParameter) {
const result = resolveName(context.enclosingDeclaration, escapedName, SymbolFlags.Type, /*nameNotFoundMessage*/ undefined, escapedName, /*isUse*/ false);
if (result) {
if (result.flags & SymbolFlags.TypeParameter && result === type.symbol) {
return false;
}
return true;
if (result && result.flags & SymbolFlags.TypeParameter) {
return result !== type.symbol;
}
return false;
}
Expand All @@ -8153,7 +8181,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const rawtext = result.escapedText as string;
let i = context.typeParameterNamesByTextNextNameCount?.get(rawtext) || 0;
let text = rawtext;
while (context.typeParameterNamesByText?.has(text) || typeParameterShadowsNameInScope(text as __String, context, type)) {
while (context.typeParameterNamesByText?.has(text) || typeParameterShadowsOtherTypeParameterInScope(text as __String, context, type)) {
i++;
text = `${rawtext}_${i}`;
}
Expand All @@ -8166,7 +8194,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// `i` we've used thus far, to save work later
(context.typeParameterNamesByTextNextNameCount ||= new Map()).set(rawtext, i);
(context.typeParameterNames ||= new Map()).set(getTypeId(type), result);
(context.typeParameterNamesByText ||= new Set()).add(rawtext);
(context.typeParameterNamesByText ||= new Set()).add(text);
}
return result;
}
Expand Down Expand Up @@ -8345,7 +8373,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getEnclosingDeclarationIgnoringFakeScope(enclosingDeclaration: Node) {
return getNodeLinks(enclosingDeclaration).fakeScopeForSignatureDeclaration ? enclosingDeclaration.parent : enclosingDeclaration;
while (getNodeLinks(enclosingDeclaration).fakeScopeForSignatureDeclaration) {
enclosingDeclaration = enclosingDeclaration.parent;
}
return enclosingDeclaration;
}

/**
Expand Down Expand Up @@ -8425,7 +8456,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (isIdentifier(node)) {
const type = getDeclaredTypeOfSymbol(sym);
const name = sym.flags & SymbolFlags.TypeParameter && !isTypeSymbolAccessible(type.symbol, context.enclosingDeclaration) ? typeParameterToName(type, context) : factory.cloneNode(node);
const name = sym.flags & SymbolFlags.TypeParameter ? typeParameterToName(type, context) : factory.cloneNode(node);
name.symbol = sym; // for quickinfo, which uses identifier symbol information
return { introducesError, node: setEmitFlags(setOriginalNode(name, node), EmitFlags.NoAsciiEscaping) };
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6047,7 +6047,7 @@ export interface NodeLinks {
decoratorSignature?: Signature; // Signature for decorator as if invoked by the runtime.
spreadIndices?: { first: number | undefined, last: number | undefined }; // Indices of first and last spread elements in array literal
parameterInitializerContainsUndefined?: boolean; // True if this is a parameter declaration whose type annotation contains "undefined".
fakeScopeForSignatureDeclaration?: boolean; // True if this is a fake scope injected into an enclosing declaration chain.
fakeScopeForSignatureDeclaration?: "params" | "typeParams"; // If present, this is a fake scope injected into an enclosing declaration chain.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this should be an enum, I just used a string here as the least annoying way to get two truthy values.

assertionExpressionType?: Type; // Cached type of the expression of a type assertion
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ a = a2;
>a2 : (x: number) => string

t = <T>(x: T) => 1;
>t = <T>(x: T) => 1 : <T_1>(x: T_1) => number
>t = <T>(x: T) => 1 : <T>(x: T) => number
>t : T
><T>(x: T) => 1 : <T_1>(x: T_1) => number
><T>(x: T) => 1 : <T>(x: T) => number
>x : T
>1 : 1

Expand All @@ -76,9 +76,9 @@ t = function (x: number) { return ''; }
>'' : ""

a = <T>(x: T) => 1;
>a = <T>(x: T) => 1 : <T_1>(x: T_1) => number
>a = <T>(x: T) => 1 : <T>(x: T) => number
>a : (x: number) => void
><T>(x: T) => 1 : <T_1>(x: T_1) => number
><T>(x: T) => 1 : <T>(x: T) => number
>x : T
>1 : 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ t = { f: () => 1 };
>1 : 1

t = { f: <T>(x:T) => 1 };
>t = { f: <T>(x:T) => 1 } : { f: <T_1>(x: T_1) => number; }
>t = { f: <T>(x:T) => 1 } : { f: <T>(x: T) => number; }
>t : T
>{ f: <T>(x:T) => 1 } : { f: <T_1>(x: T_1) => number; }
>f : <T_1>(x: T_1) => number
><T>(x:T) => 1 : <T_1>(x: T_1) => number
>{ f: <T>(x:T) => 1 } : { f: <T>(x: T) => number; }
>f : <T>(x: T) => number
><T>(x:T) => 1 : <T>(x: T) => number
>x : T
>1 : 1

Expand Down Expand Up @@ -102,11 +102,11 @@ a = { f: () => 1 }
>1 : 1

a = { f: <T>(x: T) => 1 };
>a = { f: <T>(x: T) => 1 } : { f: <T_1>(x: T_1) => number; }
>a = { f: <T>(x: T) => 1 } : { f: <T>(x: T) => number; }
>a : { f(x: number): void; }
>{ f: <T>(x: T) => 1 } : { f: <T_1>(x: T_1) => number; }
>f : <T_1>(x: T_1) => number
><T>(x: T) => 1 : <T_1>(x: T_1) => number
>{ f: <T>(x: T) => 1 } : { f: <T>(x: T) => number; }
>f : <T>(x: T) => number
><T>(x: T) => 1 : <T>(x: T) => number
>x : T
>1 : 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ b13 = a13; // ok
>a13 : (x: Base[], y: Derived[]) => Derived[]

var b14: <T>(x: { a: T; b: T }) => T;
>b14 : <T>(x: { a: T; b: T; }) => T
>b14 : <T>(x: { a: T; b: T;}) => T
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ module Errors {
>a12 : (x: Base[], y: Derived2[]) => Derived[]

var b15: <T>(x: { a: T; b: T }) => T;
>b15 : <T>(x: { a: T; b: T; }) => T
>b15 : <T>(x: { a: T; b: T;}) => T
>x : { a: T; b: T; }
>a : T
>b : T
Expand All @@ -240,7 +240,7 @@ module Errors {
>a15 : (x: { a: string; b: number; }) => number

var b15a: <T extends Base>(x: { a: T; b: T }) => number;
>b15a : <T extends Base>(x: { a: T; b: T; }) => number
>b15a : <T extends Base>(x: { a: T; b: T;}) => number
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ var a6: <T extends Base>(x: (arg: T) => Derived) => T;
>arg : T

var a11: <T>(x: { foo: T }, y: { foo: T; bar: T }) => Base;
>a11 : <T>(x: { foo: T; }, y: { foo: T; bar: T; }) => Base
>a11 : <T>(x: { foo: T;}, y: { foo: T; bar: T;}) => Base
>x : { foo: T; }
>foo : T
>y : { foo: T; bar: T; }
>foo : T
>bar : T

var a15: <T>(x: { a: T; b: T }) => T[];
>a15 : <T>(x: { a: T; b: T; }) => T[]
>a15 : <T>(x: { a: T; b: T;}) => T[]
>x : { a: T; b: T; }
>a : T
>b : T

var a16: <T extends Base>(x: { a: T; b: T }) => T[];
>a16 : <T extends Base>(x: { a: T; b: T; }) => T[]
>a16 : <T extends Base>(x: { a: T; b: T;}) => T[]
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down Expand Up @@ -194,7 +194,7 @@ b6 = a6; // ok
>a6 : <T extends Base>(x: (arg: T) => Derived) => T

var b11: <T, U>(x: { foo: T }, y: { foo: U; bar: U }) => Base;
>b11 : <T, U>(x: { foo: T; }, y: { foo: U; bar: U; }) => Base
>b11 : <T, U>(x: { foo: T;}, y: { foo: U; bar: U;}) => Base
>x : { foo: T; }
>foo : T
>y : { foo: U; bar: U; }
Expand All @@ -212,7 +212,7 @@ b11 = a11; // ok
>a11 : <T>(x: { foo: T; }, y: { foo: T; bar: T; }) => Base

var b15: <U, V>(x: { a: U; b: V; }) => U[];
>b15 : <U, V>(x: { a: U; b: V; }) => U[]
>b15 : <U, V>(x: { a: U; b: V;}) => U[]
>x : { a: U; b: V; }
>a : U
>b : V
Expand All @@ -228,7 +228,7 @@ b15 = a15; // ok
>a15 : <T>(x: { a: T; b: T; }) => T[]

var b16: <T>(x: { a: T; b: T }) => T[];
>b16 : <T>(x: { a: T; b: T; }) => T[]
>b16 : <T>(x: { a: T; b: T;}) => T[]
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ interface A {
>arg : T

a11: <T>(x: { foo: T }, y: { foo: T; bar: T }) => Base;
>a11 : <T>(x: { foo: T; }, y: { foo: T; bar: T; }) => Base
>a11 : <T>(x: { foo: T;}, y: { foo: T; bar: T;}) => Base
>x : { foo: T; }
>foo : T
>y : { foo: T; bar: T; }
>foo : T
>bar : T

a15: <T>(x: { a: T; b: T }) => T[];
>a15 : <T>(x: { a: T; b: T; }) => T[]
>a15 : <T>(x: { a: T; b: T;}) => T[]
>x : { a: T; b: T; }
>a : T
>b : T

a16: <T extends Base>(x: { a: T; b: T }) => T[];
>a16 : <T extends Base>(x: { a: T; b: T; }) => T[]
>a16 : <T extends Base>(x: { a: T; b: T;}) => T[]
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down Expand Up @@ -167,7 +167,7 @@ b5 = x.a5;
>a5 : <T, U>(x: (arg: T) => U) => T

var b11: <T, U>(x: { foo: T }, y: { foo: U; bar: U }) => Base;
>b11 : <T, U>(x: { foo: T; }, y: { foo: U; bar: U; }) => Base
>b11 : <T, U>(x: { foo: T;}, y: { foo: U; bar: U;}) => Base
>x : { foo: T; }
>foo : T
>y : { foo: U; bar: U; }
Expand All @@ -189,7 +189,7 @@ b11 = x.a11;
>a11 : <T>(x: { foo: T; }, y: { foo: T; bar: T; }) => Base

var b16: <T>(x: { a: T; b: T }) => T[];
>b16 : <T>(x: { a: T; b: T; }) => T[]
>b16 : <T>(x: { a: T; b: T;}) => T[]
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ b13 = a13; // ok
>a13 : new (x: Base[], y: Derived[]) => Derived[]

var b14: new <T>(x: { a: T; b: T }) => T;
>b14 : new <T>(x: { a: T; b: T; }) => T
>b14 : new <T>(x: { a: T; b: T;}) => T
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ module Errors {
>a12 : new (x: Base[], y: Derived2[]) => Derived[]

var b15: new <T>(x: { a: T; b: T }) => T;
>b15 : new <T>(x: { a: T; b: T; }) => T
>b15 : new <T>(x: { a: T; b: T;}) => T
>x : { a: T; b: T; }
>a : T
>b : T
Expand All @@ -240,7 +240,7 @@ module Errors {
>a15 : new (x: { a: string; b: number; }) => number

var b15a: new <T extends Base>(x: { a: T; b: T }) => number;
>b15a : new <T extends Base>(x: { a: T; b: T; }) => number
>b15a : new <T extends Base>(x: { a: T; b: T;}) => number
>x : { a: T; b: T; }
>a : T
>b : T
Expand Down
Loading