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

Disable expandos for type-annotated function expressions in TS #61072

Closed
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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12188,6 +12188,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let links = getSymbolLinks(symbol);
const originalLinks = links;
if (!links.type) {
// Disable all this block to turn off expando assignments for all function expressions
const expando = symbol.valueDeclaration && getSymbolOfExpando(symbol.valueDeclaration, /*allowDeclaration*/ false);
if (expando) {
const merged = mergeJSSymbols(symbol, expando);
Expand Down Expand Up @@ -37142,7 +37143,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let name: Expression | BindingName | undefined;
let decl: Node | undefined;
if (isVariableDeclaration(node.parent) && node.parent.initializer === node) {
if (!isInJSFile(node) && !(isVarConstLike(node.parent) && isFunctionLikeDeclaration(node))) {
if (!isInJSFile(node)) {
return undefined;
}
name = node.parent.name;
Expand Down
36 changes: 36 additions & 0 deletions tests/baselines/reference/annotatedExpandoFunc.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
annotatedExpandoFunc.ts(6,7): error TS2741: Property 'p' is missing in type '() => 1' but required in type 'F'.
annotatedExpandoFunc.ts(8,3): error TS2339: Property 'extra' does not exist on type 'F'.
annotatedExpandoFunc.ts(12,3): error TS2339: Property 'q' does not exist on type '() => number'.
annotatedExpandoFunc.ts(13,20): error TS2339: Property 'q' does not exist on type '() => number'.


==== annotatedExpandoFunc.ts (4 errors) ====
interface F {
(): 1;
p: 2;
}
// disallowed
const f: F = () => 1;
~
!!! error TS2741: Property 'p' is missing in type '() => 1' but required in type 'F'.
!!! related TS2728 annotatedExpandoFunc.ts:3:5: 'p' is declared here.
f.p = 2;
f.extra = 3
~~~~~
!!! error TS2339: Property 'extra' does not exist on type 'F'.
const r1 = f() + f.p
// function expressions are still allowed, by analogy with function declarations
const e = () => 4
e.q = 5
~
!!! error TS2339: Property 'q' does not exist on type '() => number'.
const r2 = e() + e.q
~
!!! error TS2339: Property 'q' does not exist on type '() => number'.
// function declarations are still allowed
function g() {
return 6
}
g.r = 7
const r3 = g() + g.r

60 changes: 60 additions & 0 deletions tests/baselines/reference/annotatedExpandoFunc.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//// [tests/cases/conformance/salsa/annotatedExpandoFunc.ts] ////

=== annotatedExpandoFunc.ts ===
interface F {
>F : Symbol(F, Decl(annotatedExpandoFunc.ts, 0, 0))

(): 1;
p: 2;
>p : Symbol(F.p, Decl(annotatedExpandoFunc.ts, 1, 10))
}
// disallowed
const f: F = () => 1;
>f : Symbol(f, Decl(annotatedExpandoFunc.ts, 5, 5), Decl(annotatedExpandoFunc.ts, 5, 21), Decl(annotatedExpandoFunc.ts, 6, 8))
>F : Symbol(F, Decl(annotatedExpandoFunc.ts, 0, 0))

f.p = 2;
>f.p : Symbol(F.p, Decl(annotatedExpandoFunc.ts, 1, 10))
>f : Symbol(f, Decl(annotatedExpandoFunc.ts, 5, 5), Decl(annotatedExpandoFunc.ts, 5, 21), Decl(annotatedExpandoFunc.ts, 6, 8))
>p : Symbol(F.p, Decl(annotatedExpandoFunc.ts, 1, 10))

f.extra = 3
>f : Symbol(f, Decl(annotatedExpandoFunc.ts, 5, 5), Decl(annotatedExpandoFunc.ts, 5, 21), Decl(annotatedExpandoFunc.ts, 6, 8))

const r1 = f() + f.p
>r1 : Symbol(r1, Decl(annotatedExpandoFunc.ts, 8, 5))
>f : Symbol(f, Decl(annotatedExpandoFunc.ts, 5, 5), Decl(annotatedExpandoFunc.ts, 5, 21), Decl(annotatedExpandoFunc.ts, 6, 8))
>f.p : Symbol(F.p, Decl(annotatedExpandoFunc.ts, 1, 10))
>f : Symbol(f, Decl(annotatedExpandoFunc.ts, 5, 5), Decl(annotatedExpandoFunc.ts, 5, 21), Decl(annotatedExpandoFunc.ts, 6, 8))
>p : Symbol(F.p, Decl(annotatedExpandoFunc.ts, 1, 10))

// function expressions are still allowed, by analogy with function declarations
const e = () => 4
>e : Symbol(e, Decl(annotatedExpandoFunc.ts, 10, 5), Decl(annotatedExpandoFunc.ts, 10, 17))

e.q = 5
>e : Symbol(e, Decl(annotatedExpandoFunc.ts, 10, 5), Decl(annotatedExpandoFunc.ts, 10, 17))

const r2 = e() + e.q
>r2 : Symbol(r2, Decl(annotatedExpandoFunc.ts, 12, 5))
>e : Symbol(e, Decl(annotatedExpandoFunc.ts, 10, 5), Decl(annotatedExpandoFunc.ts, 10, 17))
>e : Symbol(e, Decl(annotatedExpandoFunc.ts, 10, 5), Decl(annotatedExpandoFunc.ts, 10, 17))

// function declarations are still allowed
function g() {
>g : Symbol(g, Decl(annotatedExpandoFunc.ts, 12, 20), Decl(annotatedExpandoFunc.ts, 16, 1))

return 6
}
g.r = 7
>g.r : Symbol(g.r, Decl(annotatedExpandoFunc.ts, 16, 1))
>g : Symbol(g, Decl(annotatedExpandoFunc.ts, 12, 20), Decl(annotatedExpandoFunc.ts, 16, 1))
>r : Symbol(g.r, Decl(annotatedExpandoFunc.ts, 16, 1))

const r3 = g() + g.r
>r3 : Symbol(r3, Decl(annotatedExpandoFunc.ts, 18, 5))
>g : Symbol(g, Decl(annotatedExpandoFunc.ts, 12, 20), Decl(annotatedExpandoFunc.ts, 16, 1))
>g.r : Symbol(g.r, Decl(annotatedExpandoFunc.ts, 16, 1))
>g : Symbol(g, Decl(annotatedExpandoFunc.ts, 12, 20), Decl(annotatedExpandoFunc.ts, 16, 1))
>r : Symbol(g.r, Decl(annotatedExpandoFunc.ts, 16, 1))

132 changes: 132 additions & 0 deletions tests/baselines/reference/annotatedExpandoFunc.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//// [tests/cases/conformance/salsa/annotatedExpandoFunc.ts] ////

=== annotatedExpandoFunc.ts ===
interface F {
(): 1;
p: 2;
>p : 2
> : ^
}
// disallowed
const f: F = () => 1;
>f : F
> : ^
>() => 1 : () => 1
> : ^^^^^^^
>1 : 1
> : ^

f.p = 2;
>f.p = 2 : 2
> : ^
>f.p : 2
> : ^
>f : F
> : ^
>p : 2
> : ^
>2 : 2
> : ^

f.extra = 3
>f.extra = 3 : 3
> : ^
>f.extra : any
> : ^^^
>f : F
> : ^
>extra : any
> : ^^^
>3 : 3
> : ^

const r1 = f() + f.p
>r1 : number
> : ^^^^^^
>f() + f.p : number
> : ^^^^^^
>f() : 1
> : ^
>f : F
> : ^
>f.p : 2
> : ^
>f : F
> : ^
>p : 2
> : ^

// function expressions are still allowed, by analogy with function declarations
const e = () => 4
>e : () => number
> : ^^^^^^^^^^^^
>() => 4 : () => number
> : ^^^^^^^^^^^^
>4 : 4
> : ^

e.q = 5
>e.q = 5 : 5
> : ^
>e.q : any
> : ^^^
>e : () => number
> : ^^^^^^^^^^^^
>q : any
> : ^^^
>5 : 5
> : ^

const r2 = e() + e.q
>r2 : any
> : ^^^
>e() + e.q : any
> : ^^^
>e() : number
> : ^^^^^^
>e : () => number
> : ^^^^^^^^^^^^
>e.q : any
> : ^^^
>e : () => number
> : ^^^^^^^^^^^^
>q : any
> : ^^^

// function declarations are still allowed
function g() {
>g : typeof g
> : ^^^^^^^^

return 6
>6 : 6
> : ^
}
g.r = 7
>g.r = 7 : 7
> : ^
>g.r : number
> : ^^^^^^
>g : typeof g
> : ^^^^^^^^
>r : number
> : ^^^^^^
>7 : 7
> : ^

const r3 = g() + g.r
>r3 : number
> : ^^^^^^
>g() + g.r : number
> : ^^^^^^
>g() : number
> : ^^^^^^
>g : typeof g
> : ^^^^^^^^
>g.r : number
> : ^^^^^^
>g : typeof g
> : ^^^^^^^^
>r : number
> : ^^^^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declarationEmitExpandoWithGenericConstraint.ts(14,7): error TS2339: Property 'zero' does not exist on type '(x: number, y: number) => Point'.


==== declarationEmitExpandoWithGenericConstraint.ts (1 errors) ====
export interface Point {
readonly x: number;
readonly y: number;
}

export interface Rect<p extends Point> {
readonly a: p;
readonly b: p;
}

export const Point = (x: number, y: number): Point => ({ x, y });
export const Rect = <p extends Point>(a: p, b: p): Rect<p> => ({ a, b });

Point.zero = (): Point => Point(0, 0);
~~~~
!!! error TS2339: Property 'zero' does not exist on type '(x: number, y: number) => Point'.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ export interface Rect<p extends Point> {
readonly a: p;
readonly b: p;
}
export declare const Point: {
(x: number, y: number): Point;
zero(): Point;
};
export declare const Point: (x: number, y: number) => Point;
export declare const Rect: <p extends Point>(a: p, b: p) => Rect<p>;
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export const Rect = <p extends Point>(a: p, b: p): Rect<p> => ({ a, b });
>b : Symbol(b, Decl(declarationEmitExpandoWithGenericConstraint.ts, 11, 67))

Point.zero = (): Point => Point(0, 0);
>Point.zero : Symbol(Point.zero, Decl(declarationEmitExpandoWithGenericConstraint.ts, 11, 73))
>Point : Symbol(Point, Decl(declarationEmitExpandoWithGenericConstraint.ts, 0, 0), Decl(declarationEmitExpandoWithGenericConstraint.ts, 10, 12), Decl(declarationEmitExpandoWithGenericConstraint.ts, 11, 73))
>zero : Symbol(Point.zero, Decl(declarationEmitExpandoWithGenericConstraint.ts, 11, 73))
>Point : Symbol(Point, Decl(declarationEmitExpandoWithGenericConstraint.ts, 0, 0), Decl(declarationEmitExpandoWithGenericConstraint.ts, 10, 12), Decl(declarationEmitExpandoWithGenericConstraint.ts, 11, 73))
>Point : Symbol(Point, Decl(declarationEmitExpandoWithGenericConstraint.ts, 0, 0), Decl(declarationEmitExpandoWithGenericConstraint.ts, 10, 12), Decl(declarationEmitExpandoWithGenericConstraint.ts, 11, 73))

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export interface Rect<p extends Point> {
}

export const Point = (x: number, y: number): Point => ({ x, y });
>Point : { (x: number, y: number): Point; zero(): Point; }
> : ^^^ ^^ ^^ ^^ ^^^ ^^^^^^^^^^ ^^^
>(x: number, y: number): Point => ({ x, y }) : { (x: number, y: number): Point; zero(): Point; }
> : ^^^ ^^ ^^ ^^ ^^^ ^^^^^^^^^^ ^^^
>Point : (x: number, y: number) => Point
> : ^ ^^ ^^ ^^ ^^^^^
>(x: number, y: number): Point => ({ x, y }) : (x: number, y: number) => Point
> : ^ ^^ ^^ ^^ ^^^^^
>x : number
> : ^^^^^^
>y : number
Expand Down Expand Up @@ -60,18 +60,18 @@ export const Rect = <p extends Point>(a: p, b: p): Rect<p> => ({ a, b });
Point.zero = (): Point => Point(0, 0);
>Point.zero = (): Point => Point(0, 0) : () => Point
> : ^^^^^^
>Point.zero : () => Point
> : ^^^^^^
>Point : { (x: number, y: number): Point; zero(): Point; }
> : ^^^ ^^ ^^ ^^ ^^^ ^^^^^^^^^^ ^^^
>zero : () => Point
> : ^^^^^^
>Point.zero : any
> : ^^^
>Point : (x: number, y: number) => Point
> : ^ ^^ ^^ ^^ ^^^^^
>zero : any
> : ^^^
>(): Point => Point(0, 0) : () => Point
> : ^^^^^^
>Point(0, 0) : Point
> : ^^^^^
>Point : { (x: number, y: number): Point; zero(): Point; }
> : ^^^ ^^ ^^ ^^ ^^^ ^^^^^^^^^^ ^^^
>Point : (x: number, y: number) => Point
> : ^ ^^ ^^ ^^ ^^^^^
>0 : 0
> : ^
>0 : 0
Expand Down
Loading
Loading