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

Use strict variance checks for strict subtype checks unconditionally #48123

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 7 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -17818,7 +17818,7 @@ namespace ts {
target: Signature,
ignoreReturnTypes: boolean): boolean {
return compareSignaturesRelated(source, target, ignoreReturnTypes ? SignatureCheckMode.IgnoreReturnTypes : 0, /*reportErrors*/ false,
/*errorReporter*/ undefined, /*errorReporter*/ undefined, compareTypesAssignable, /*reportUnreliableMarkers*/ undefined) !== Ternary.False;
/*errorReporter*/ undefined, /*errorReporter*/ undefined, compareTypesAssignable, /*reportUnreliableMarkers*/ undefined, assignableRelation) !== Ternary.False;
}

type ErrorReporter = (message: DiagnosticMessage, arg0?: string, arg1?: string) => void;
@@ -17842,7 +17842,8 @@ namespace ts {
errorReporter: ErrorReporter | undefined,
incompatibleErrorReporter: ((source: Type, target: Type) => void) | undefined,
compareTypes: TypeComparer,
reportUnreliableMarkers: TypeMapper | undefined): Ternary {
reportUnreliableMarkers: TypeMapper | undefined,
relation: typeof strictSubtypeRelation): Ternary {
// TODO (drosen): De-duplicate code between related functions.
if (source === target) {
return Ternary.True;
@@ -17872,8 +17873,8 @@ namespace ts {
}

const kind = target.declaration ? target.declaration.kind : SyntaxKind.Unknown;
const strictVariance = !(checkMode & SignatureCheckMode.Callback) && strictFunctionTypes && kind !== SyntaxKind.MethodDeclaration &&
kind !== SyntaxKind.MethodSignature && kind !== SyntaxKind.Constructor;
const strictVariance = relation === strictSubtypeRelation || (!(checkMode & SignatureCheckMode.Callback) && strictFunctionTypes && kind !== SyntaxKind.MethodDeclaration &&
kind !== SyntaxKind.MethodSignature && kind !== SyntaxKind.Constructor);
let result = Ternary.True;

const sourceThisType = getThisTypeOfSignature(source);
@@ -17913,7 +17914,7 @@ namespace ts {
const callbacks = sourceSig && targetSig && !getTypePredicateOfSignature(sourceSig) && !getTypePredicateOfSignature(targetSig) &&
(getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable);
let related = callbacks ?
compareSignaturesRelated(targetSig, sourceSig, (checkMode & SignatureCheckMode.StrictArity) | (strictVariance ? SignatureCheckMode.StrictCallback : SignatureCheckMode.BivariantCallback), reportErrors, errorReporter, incompatibleErrorReporter, compareTypes, reportUnreliableMarkers) :
compareSignaturesRelated(targetSig, sourceSig, (checkMode & SignatureCheckMode.StrictArity) | (strictVariance ? SignatureCheckMode.StrictCallback : SignatureCheckMode.BivariantCallback), reportErrors, errorReporter, incompatibleErrorReporter, compareTypes, reportUnreliableMarkers, relation) :
!(checkMode & SignatureCheckMode.Callback) && !strictVariance && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors);
// With strict arity, (x: number | undefined) => void is a subtype of (x?: number | undefined) => void
if (related && checkMode & SignatureCheckMode.StrictArity && i >= getMinArgumentCount(source) && i < getMinArgumentCount(target) && compareTypes(sourceType, targetType, /*reportErrors*/ false)) {
@@ -20327,7 +20328,7 @@ namespace ts {
*/
function signatureRelatedTo(source: Signature, target: Signature, erase: boolean, reportErrors: boolean, incompatibleReporter: (source: Type, target: Type) => void): Ternary {
return compareSignaturesRelated(erase ? getErasedSignature(source) : source, erase ? getErasedSignature(target) : target,
relation === strictSubtypeRelation ? SignatureCheckMode.StrictArity : 0, reportErrors, reportError, incompatibleReporter, isRelatedToWorker, makeFunctionTypeMapper(reportUnreliableMarkers));
relation === strictSubtypeRelation ? SignatureCheckMode.StrictArity : 0, reportErrors, reportError, incompatibleReporter, isRelatedToWorker, makeFunctionTypeMapper(reportUnreliableMarkers), relation);
}

function signaturesIdenticalTo(source: Type, target: Type, kind: SignatureKind): Ternary {
Original file line number Diff line number Diff line change
@@ -36,8 +36,8 @@ var cs = [a, b, c]; // { x: number; y?: number };[]
>c : { x: number; a?: number; }

var ds = [(x: Object) => 1, (x: string) => 2]; // { (x:Object) => number }[]
>ds : ((x: Object) => number)[]
>[(x: Object) => 1, (x: string) => 2] : ((x: Object) => number)[]
>ds : ((x: string) => number)[]
Copy link
Member

Choose a reason for hiding this comment

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

This is a pretty big correctness improvement, so we should document it as a breaking change

>[(x: Object) => 1, (x: string) => 2] : ((x: string) => number)[]
>(x: Object) => 1 : (x: Object) => number
>x : Object
>1 : 1
14 changes: 7 additions & 7 deletions tests/baselines/reference/arrayOfFunctionTypes3.types
Original file line number Diff line number Diff line change
@@ -50,28 +50,28 @@ var c: { (x: number): number; (x: any): any; };
>x : any

var z = [a, b, c];
>z : { (x: number): number; (x: any): any; }[]
>[a, b, c] : { (x: number): number; (x: any): any; }[]
>z : ({ (x: number): number; (x: string): string; } | { (x: number): number; (x: any): any; })[]
>[a, b, c] : ({ (x: number): number; (x: string): string; } | { (x: number): number; (x: any): any; })[]
>a : { (x: number): number; (x: string): string; }
>b : { (x: number): number; (x: string): string; }
>c : { (x: number): number; (x: any): any; }

var r4 = z[0];
>r4 : { (x: number): number; (x: any): any; }
>z[0] : { (x: number): number; (x: any): any; }
>z : { (x: number): number; (x: any): any; }[]
>r4 : { (x: number): number; (x: string): string; } | { (x: number): number; (x: any): any; }
>z[0] : { (x: number): number; (x: string): string; } | { (x: number): number; (x: any): any; }
>z : ({ (x: number): number; (x: string): string; } | { (x: number): number; (x: any): any; })[]
>0 : 0

var r5 = r4(''); // any not string
>r5 : any
>r4('') : any
>r4 : { (x: number): number; (x: any): any; }
>r4 : { (x: number): number; (x: string): string; } | { (x: number): number; (x: any): any; }
>'' : ""

var r5b = r4(1);
>r5b : number
>r4(1) : number
>r4 : { (x: number): number; (x: any): any; }
>r4 : { (x: number): number; (x: string): string; } | { (x: number): number; (x: any): any; }
>1 : 1

var a2: { <T>(x: T): number; (x: string): string;};
18 changes: 6 additions & 12 deletions tests/baselines/reference/bestChoiceType.symbols
Original file line number Diff line number Diff line change
@@ -2,14 +2,12 @@
// Repro from #10041

(''.match(/ /) || []).map(s => s.toLowerCase());
>(''.match(/ /) || []).map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>(''.match(/ /) || []).map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>''.match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))
>match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 2, 26))
>s.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 2, 26))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))

// Similar cases

@@ -27,13 +25,11 @@ function f1() {

let z = y.map(s => s.toLowerCase());
>z : Symbol(z, Decl(bestChoiceType.ts, 9, 7))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>y : Symbol(y, Decl(bestChoiceType.ts, 8, 7))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 9, 18))
>s.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 9, 18))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
}

function f2() {
@@ -51,12 +47,10 @@ function f2() {

let z = y.map(s => s.toLowerCase());
>z : Symbol(z, Decl(bestChoiceType.ts, 15, 7))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>y : Symbol(y, Decl(bestChoiceType.ts, 14, 7))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 15, 18))
>s.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 15, 18))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
}

74 changes: 37 additions & 37 deletions tests/baselines/reference/bestChoiceType.types
Original file line number Diff line number Diff line change
@@ -2,23 +2,23 @@
// Repro from #10041

(''.match(/ /) || []).map(s => s.toLowerCase());
>(''.match(/ /) || []).map(s => s.toLowerCase()) : string[]
>(''.match(/ /) || []).map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>(''.match(/ /) || []) : RegExpMatchArray
>''.match(/ /) || [] : RegExpMatchArray
>(''.match(/ /) || []).map(s => s.toLowerCase()) : any[]
>(''.match(/ /) || []).map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>(''.match(/ /) || []) : RegExpMatchArray | never[]
>''.match(/ /) || [] : RegExpMatchArray | never[]
>''.match(/ /) : RegExpMatchArray | null
>''.match : (regexp: string | RegExp) => RegExpMatchArray | null
>'' : ""
>match : (regexp: string | RegExp) => RegExpMatchArray | null
>/ / : RegExp
>[] : never[]
>map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>s => s.toLowerCase() : (s: string) => string
>s : string
>s.toLowerCase() : string
>s.toLowerCase : () => string
>s : string
>toLowerCase : () => string
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
>s.toLowerCase : any
>s : any
>toLowerCase : any

// Similar cases

@@ -34,23 +34,23 @@ function f1() {
>/ / : RegExp

let y = x || [];
>y : RegExpMatchArray
>x || [] : RegExpMatchArray
>y : RegExpMatchArray | never[]
>x || [] : RegExpMatchArray | never[]
>x : RegExpMatchArray | null
>[] : never[]

let z = y.map(s => s.toLowerCase());
>z : string[]
>y.map(s => s.toLowerCase()) : string[]
>y.map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>y : RegExpMatchArray
>map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>s => s.toLowerCase() : (s: string) => string
>s : string
>s.toLowerCase() : string
>s.toLowerCase : () => string
>s : string
>toLowerCase : () => string
>z : any[]
>y.map(s => s.toLowerCase()) : any[]
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>y : RegExpMatchArray | never[]
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
>s.toLowerCase : any
>s : any
>toLowerCase : any
}

function f2() {
@@ -65,23 +65,23 @@ function f2() {
>/ / : RegExp

let y = x ? x : [];
>y : RegExpMatchArray
>x ? x : [] : RegExpMatchArray
>y : RegExpMatchArray | never[]
>x ? x : [] : RegExpMatchArray | never[]
>x : RegExpMatchArray | null
>x : RegExpMatchArray
>[] : never[]

let z = y.map(s => s.toLowerCase());
>z : string[]
>y.map(s => s.toLowerCase()) : string[]
>y.map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>y : RegExpMatchArray
>map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>s => s.toLowerCase() : (s: string) => string
>s : string
>s.toLowerCase() : string
>s.toLowerCase : () => string
>s : string
>toLowerCase : () => string
>z : any[]
>y.map(s => s.toLowerCase()) : any[]
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>y : RegExpMatchArray | never[]
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
>s.toLowerCase : any
>s : any
>toLowerCase : any
}

Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@ class C extends A {
}

var xs = [(x: A) => { }, (x: B) => { }, (x: C) => { }];
>xs : ((x: A) => void)[]
>[(x: A) => { }, (x: B) => { }, (x: C) => { }] : ((x: A) => void)[]
>xs : (((x: B) => void) | ((x: C) => void))[]
>[(x: A) => { }, (x: B) => { }, (x: C) => { }] : (((x: B) => void) | ((x: C) => void))[]
>(x: A) => { } : (x: A) => void
>x : A
>(x: B) => { } : (x: B) => void
Original file line number Diff line number Diff line change
@@ -36,8 +36,8 @@ const repro_43249 = (value: unknown) => {
>"No" : "No"
}
const match = value.match(/anything/) || [];
>match : RegExpMatchArray
>value.match(/anything/) || [] : RegExpMatchArray
>match : RegExpMatchArray | never[]
>value.match(/anything/) || [] : RegExpMatchArray | never[]
>value.match(/anything/) : RegExpMatchArray | null
>value.match : { (regexp: string | RegExp): RegExpMatchArray | null; (matcher: { [Symbol.match](string: string): RegExpMatchArray | null; }): RegExpMatchArray | null; }
>value : string
@@ -48,7 +48,7 @@ const repro_43249 = (value: unknown) => {
const [, extracted] = match;
> : undefined
>extracted : string
>match : RegExpMatchArray
>match : RegExpMatchArray | never[]

};

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ const [, a = ''] = ''.match('') || [];
> : undefined
>a : string
>'' : ""
>''.match('') || [] : RegExpMatchArray
>''.match('') || [] : RegExpMatchArray | undefined[]
>''.match('') : RegExpMatchArray
>''.match : (regexp: string | RegExp) => RegExpMatchArray
>'' : ""
61 changes: 61 additions & 0 deletions tests/baselines/reference/strictSubtypeReduction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//// [strictSubtypeReduction.ts]
// Repro from #41977

class S1 {
static f(a: number | string): void { }
}

class S2 {
static f(a: number): void { }
static g(a: number): void { }
}

function f(a: number): void { }
function g(a: number): void { }

// Declaring the following type aliases should have no effect

type T1 = typeof S2.g;
type T2 = typeof g;

// All should have type ((a: number) => void)[]

const y1 = [S1.f, f];
const y2 = [S1.f, g];
const y3 = [S1.f, S2.f];
const y4 = [S1.f, S2.g];

// All assignments should be errors in strict mode, but won't be without strict function types on

const x1: ((ctrl: number | string) => void)[] = y1;
const x2: ((ctrl: number | string) => void)[] = y2;
const x3: ((ctrl: number | string) => void)[] = y3;
const x4: ((ctrl: number | string) => void)[] = y4;

//// [strictSubtypeReduction.js]
// Repro from #41977
var S1 = /** @class */ (function () {
function S1() {
}
S1.f = function (a) { };
return S1;
}());
var S2 = /** @class */ (function () {
function S2() {
}
S2.f = function (a) { };
S2.g = function (a) { };
return S2;
}());
function f(a) { }
function g(a) { }
// All should have type ((a: number) => void)[]
var y1 = [S1.f, f];
var y2 = [S1.f, g];
var y3 = [S1.f, S2.f];
var y4 = [S1.f, S2.g];
// All assignments should be errors in strict mode, but won't be without strict function types on
var x1 = y1;
var x2 = y2;
var x3 = y3;
var x4 = y4;
Loading
Oops, something went wrong.