Skip to content
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

Always consider parameters in scope visible to node builder #58075

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6329,7 +6329,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (
entityName.parent.kind === SyntaxKind.TypeQuery ||
entityName.parent.kind === SyntaxKind.ExpressionWithTypeArguments && !isPartOfTypeNode(entityName.parent) ||
entityName.parent.kind === SyntaxKind.ComputedPropertyName
entityName.parent.kind === SyntaxKind.ComputedPropertyName ||
entityName.parent.kind === SyntaxKind.TypePredicate && (entityName.parent as TypePredicateNode).parameterName === entityName
) {
// Typeof value
meaning = SymbolFlags.Value | SymbolFlags.ExportValue;
Expand Down Expand Up @@ -8672,6 +8673,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const meaning = getMeaningOfEntityNameReference(node);
const sym = resolveEntityName(leftmost, meaning, /*ignoreErrors*/ true, /*dontResolveAlias*/ true);
if (sym) {
// If a parameter is resolvable in the current context it is also visible, so no need to go to symbol accesibility
if (
sym.flags & SymbolFlags.FunctionScopedVariable
&& sym.valueDeclaration
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem is that this will not work for binding elements. The original check was mean to walk up the binding elements. I this i lost something when I copied it over.

Copy link
Member Author

Choose a reason for hiding this comment

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

The confusingly-named isParameterDeclaration does not do what you think it does - it does walk binding elements.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we should really rename that to isPartOfParameterDeclaration. See also #52283

if (isParameterDeclaration(sym.valueDeclaration)) {
return { introducesError, node: attachSymbolToLeftmostIdentifier(node) as T };
}
}
if (isSymbolAccessible(sym, context.enclosingDeclaration, meaning, /*shouldComputeAliasesToMakeVisible*/ false).accessibility !== SymbolAccessibility.Accessible) {
if (!isDeclarationName(node)) {
introducesError = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ var out = foo((x, y) => {
> : ^^^^^^^
>foo : { (func: (x: string, y: string) => any): boolean; (func: (x: string, y: number) => any): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>(x, y) => { function bar(a: typeof x): void; function bar(b: typeof y): void; function bar() { } return bar;} : (x: string, y: string) => { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
>(x, y) => { function bar(a: typeof x): void; function bar(b: typeof y): void; function bar() { } return bar;} : (x: string, y: string) => { (a: typeof x): void; (b: typeof y): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^
>x : string
> : ^^^^^^
>y : string
> : ^^^^^^

function bar(a: typeof x): void;
>bar : { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
>bar : { (a: typeof x): void; (b: string): void; }
> : ^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^
>a : string
> : ^^^^^^
>x : string
> : ^^^^^^

function bar(b: typeof y): void;
>bar : { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
>bar : { (a: string): void; (b: typeof y): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^
>b : string
> : ^^^^^^
>y : string
Expand Down Expand Up @@ -88,16 +88,16 @@ var out2 = foo2((x, y) => {
> : ^^^^^^^
>foo2 : { (func: (x: string, y: string) => any): boolean; (func: (x: string, y: number) => any): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>(x, y) => { var bar: { (a: typeof x): void; (b: typeof y): void; }; return bar;} : (x: string, y: string) => { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
>(x, y) => { var bar: { (a: typeof x): void; (b: typeof y): void; }; return bar;} : (x: string, y: string) => { (a: typeof x): void; (b: typeof y): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^
>x : string
> : ^^^^^^
>y : string
> : ^^^^^^

var bar: {
>bar : { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
>bar : { (a: typeof x): void; (b: typeof y): void; }
> : ^^^^^^ ^^^ ^^^^^^ ^^^ ^^^

(a: typeof x): void;
>a : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ type Mapped = {

const propSelector = <propName extends string>(propName: propName): propName => propName;
>propSelector : <propName extends string>(propName: propName) => propName
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
><propName extends string>(propName: propName): propName => propName : <propName extends string>(propName: propName) => propName
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
>propName : propName
> : ^^^^^^^^
>propName : propName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ interface I {
declare function f1({ a: string }: O): void;
declare const f2: ({ a: string }: O) => void;
declare const f3: ({ a: string, b, c }: O) => void;
declare const f4: ({ a: string }: O) => string;
declare const f5: ({ a: string, b, c }: O) => string;
declare const f4: ({ a: string }: O) => typeof string;
declare const f5: ({ a: string, b, c }: O) => typeof string;
declare const obj1: {
method({ a: string }: O): void;
};
declare const obj2: {
method({ a: string }: O): string;
method({ a: string }: O): typeof string;
};
declare function f6({ a: string }: O): void;
declare const f7: ({ a: string, b, c }: O) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ const f3 = ({ a: string, b, c }: O) => { };
> : ^^^^^^

const f4 = function({ a: string }: O): typeof string { return string; };
>f4 : ({ a: string }: O) => string
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>function({ a: string }: O): typeof string { return string; } : ({ a: string }: O) => string
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>f4 : ({ a: string }: O) => typeof string
> : ^^^^^^^^^^^^^^^^ ^^^^^
>function({ a: string }: O): typeof string { return string; } : ({ a: string }: O) => typeof string
> : ^^^^^^^^^^^^^^^^ ^^^^^
>a : any
> : ^^^
>string : string
Expand All @@ -339,10 +339,10 @@ const f4 = function({ a: string }: O): typeof string { return string; };
> : ^^^^^^

const f5 = ({ a: string, b, c }: O): typeof string => '';
>f5 : ({ a: string, b, c }: O) => string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>({ a: string, b, c }: O): typeof string => '' : ({ a: string, b, c }: O) => string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>f5 : ({ a: string, b, c }: O) => typeof string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
>({ a: string, b, c }: O): typeof string => '' : ({ a: string, b, c }: O) => typeof string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
>a : any
> : ^^^
>string : string
Expand Down Expand Up @@ -372,14 +372,14 @@ const obj1 = {

};
const obj2 = {
>obj2 : { method({ a: string }: O): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
>{ method({ a: string }: O): typeof string { return string; }} : { method({ a: string }: O): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
>obj2 : { method({ a: string }: O): typeof string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^
>{ method({ a: string }: O): typeof string { return string; }} : { method({ a: string }: O): typeof string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^

method({ a: string }: O): typeof string { return string; }
>method : ({ a: string }: O) => string
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>method : ({ a: string }: O) => typeof string
> : ^^^^^^^^^^^^^^^^ ^^^^^
>a : any
> : ^^^
>string : string
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/subtypingWithCallSignatures.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module CallSignature {
> : ^^^^^^^^^^^^^^^^^^^^

declare function foo1(cb: (x: number) => void): typeof cb;
>foo1 : { (cb: (x: number) => void): (x: number) => void; (cb: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo1 : { (cb: (x: number) => void): typeof cb; (cb: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>cb : (x: number) => void
> : ^^^^ ^^^^^
>x : number
Expand Down Expand Up @@ -49,8 +49,8 @@ module CallSignature {
> : ^^

declare function foo2(cb: (x: number, y: number) => void): typeof cb;
>foo2 : { (cb: (x: number, y: number) => void): (x: number, y: number) => void; (cb: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo2 : { (cb: (x: number, y: number) => void): typeof cb; (cb: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>cb : (x: number, y: number) => void
> : ^^^^ ^^^^^ ^^^^^
>x : number
Expand Down
44 changes: 22 additions & 22 deletions tests/baselines/reference/subtypingWithCallSignatures3.types
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ module Errors {
> : ^^^^^^

declare function foo2(a2: (x: number) => string[]): typeof a2;
>foo2 : { (a2: (x: number) => string[]): (x: number) => string[]; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo2 : { (a2: (x: number) => string[]): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: number) => string[]
> : ^^^^ ^^^^^
>x : number
Expand All @@ -54,8 +54,8 @@ module Errors {
>a2 : any

declare function foo7(a2: (x: (arg: Base) => Derived) => (r: Base) => Derived2): typeof a2;
>foo7 : { (a2: (x: (arg: Base) => Derived) => (r: Base) => Derived2): (x: (arg: Base) => Derived) => (r: Base) => Derived2; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo7 : { (a2: (x: (arg: Base) => Derived) => (r: Base) => Derived2): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: (arg: Base) => Derived) => (r: Base) => Derived2
> : ^^^^ ^^^^^
>x : (arg: Base) => Derived
Expand All @@ -73,8 +73,8 @@ module Errors {
>a2 : any

declare function foo8(a2: (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived): typeof a2;
>foo8 : { (a2: (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived): (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo8 : { (a2: (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived
> : ^^^^ ^^^^^ ^^^^^
>x : (arg: Base) => Derived
Expand All @@ -96,8 +96,8 @@ module Errors {
>a2 : any

declare function foo10(a2: (...x: Base[]) => Base): typeof a2;
>foo10 : { (a2: (...x: Base[]) => Base): (...x: Base[]) => Base; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo10 : { (a2: (...x: Base[]) => Base): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (...x: Base[]) => Base
> : ^^^^^^^ ^^^^^
>x : Base[]
Expand All @@ -111,8 +111,8 @@ module Errors {
>a2 : any

declare function foo11(a2: (x: { foo: string }, y: { foo: string; bar: string }) => Base): typeof a2;
>foo11 : { (a2: (x: { foo: string; }, y: { foo: string; bar: string; }) => Base): (x: { foo: string; }, y: { foo: string; bar: string; }) => Base; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo11 : { (a2: (x: { foo: string; }, y: { foo: string; bar: string; }) => Base): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: { foo: string; }, y: { foo: string; bar: string; }) => Base
> : ^^^^ ^^^^^ ^^^^^
>x : { foo: string; }
Expand All @@ -134,8 +134,8 @@ module Errors {
>a2 : any

declare function foo12(a2: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>): typeof a2;
>foo12 : { (a2: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>): (x: Array<Base>, y: Array<Derived2>) => Array<Derived>; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo12 : { (a2: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: Array<Base>, y: Array<Derived2>) => Array<Derived>
> : ^^^^ ^^^^^ ^^^^^
>x : Base[]
Expand All @@ -151,8 +151,8 @@ module Errors {
>a2 : any

declare function foo15(a2: (x: { a: string; b: number }) => number): typeof a2;
>foo15 : { (a2: (x: { a: string; b: number; }) => number): (x: { a: string; b: number; }) => number; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo15 : { (a2: (x: { a: string; b: number; }) => number): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: { a: string; b: number; }) => number
> : ^^^^ ^^^^^
>x : { a: string; b: number; }
Expand All @@ -170,8 +170,8 @@ module Errors {
>a2 : any

declare function foo16(a2: {
>foo16 : { (a2: { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }): { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^
>foo16 : { (a2: { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }
> : ^^^^^^ ^^^ ^^^^^^ ^^^ ^^^

Expand Down Expand Up @@ -212,8 +212,8 @@ module Errors {
>a2 : any

declare function foo17(a2: {
>foo17 : { (a2: { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }): { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^
>foo17 : { (a2: { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }
> : ^^^^^^ ^^^ ^^^^^^ ^^^ ^^^

Expand Down Expand Up @@ -748,8 +748,8 @@ module WithGenericSignaturesInBaseType {
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

declare function foo2(a2: <T>(x: T) => T[]): typeof a2;
>foo2 : { (a2: <T>(x: T) => T[]): <T>(x: T) => T[]; (a2: any): any; }
> : ^^^^^^^ ^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo2 : { (a2: <T>(x: T) => T[]): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : <T>(x: T) => T[]
> : ^ ^^^^^ ^^^^^
>x : T
Expand Down Expand Up @@ -783,8 +783,8 @@ module WithGenericSignaturesInBaseType {
> : ^^^^^^^^^^^^^^^^^^^^^

declare function foo3(a2: <T>(x: T) => string[]): typeof a2;
>foo3 : { (a2: <T>(x: T) => string[]): <T>(x: T) => string[]; (a2: any): any; }
> : ^^^^^^^ ^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo3 : { (a2: <T>(x: T) => string[]): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : <T>(x: T) => string[]
> : ^ ^^^^^ ^^^^^
>x : T
Expand Down
Loading