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 faster, stricter prop type comparison when merging props in union prop creation #43696

Merged
merged 4 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 7 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11662,7 +11662,7 @@ namespace ts {
// If the symbols are instances of one another with identical types - consider the symbols
// equivalent and just use the first one, which thus allows us to avoid eliding private
// members when intersecting a (this-)instantiations of a class with it's raw base or another instance
if (isInstantiation && isPropertyIdenticalTo(singleProp, prop)) {
if (isInstantiation && compareProperties(singleProp, prop, (a, b) => a === b ? Ternary.True : Ternary.False) === Ternary.True) {
// If we merged instantiations of a generic type, we replicate the symbol parent resetting behavior we used
// to do when we recorded multiple distinct symbols so that we still get, eg, `Array<T>.length` printed
// back and not `Array<string>.length` when we're looking at a `.length` access on a `string[] | number[]`
Expand Down Expand Up @@ -15860,8 +15860,8 @@ namespace ts {
outerTypeParameters = addRange(outerTypeParameters, templateTagParameters);
}
typeParameters = outerTypeParameters || emptyArray;
typeParameters = (target.objectFlags & ObjectFlags.Reference || target.symbol.flags & SymbolFlags.TypeLiteral) && !target.aliasTypeArguments ?
filter(typeParameters, tp => isTypeParameterPossiblyReferenced(tp, declaration)) :
typeParameters = (target.objectFlags & ObjectFlags.Reference || target.symbol.flags & SymbolFlags.Method || target.symbol.flags & SymbolFlags.TypeLiteral) && !target.aliasTypeArguments ?
filter(typeParameters, tp => isTypeParameterPossiblyReferenced(tp, declaration) || !(type.objectFlags & ObjectFlags.Reference) && some(type.symbol.declarations, d => isTypeParameterPossiblyReferenced(tp, d))) :
Copy link
Member

Choose a reason for hiding this comment

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

why skip object references here? are references really the only object types that won't work?

I'd suggest instead declaring

const allDeclarations = type.objectFlags & ObjectFlags.Reference ? [(<TypeReference>type).node!] : type.symbol.declarations! just below declarations, then:

Suggested change
filter(typeParameters, tp => isTypeParameterPossiblyReferenced(tp, declaration) || !(type.objectFlags & ObjectFlags.Reference) && some(type.symbol.declarations, d => isTypeParameterPossiblyReferenced(tp, d))) :
filter(typeParameters, tp => some(allDeclarations, d => isTypeParameterPossiblyReferenced(tp, d)) :

Copy link
Member Author

Choose a reason for hiding this comment

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

Because References use their .node to track their content, not their declarations. Because reasons.

Copy link
Member

Choose a reason for hiding this comment

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

I think I'd still prefer
const allDeclarations = type.objectFlags & ObjectFlags.Reference ? [] : type.symbol.declarations!
then

typeParameters;
links.outerTypeParameters = typeParameters;
}
Expand Down Expand Up @@ -15908,7 +15908,7 @@ namespace ts {
return true;
}
}
return !!forEachChild(node, containsReference);
return containsReference(node);
}
return true;
function containsReference(node: Node): boolean {
Expand All @@ -15920,6 +15920,9 @@ namespace ts {
getTypeFromTypeNodeWorker(<TypeNode>node) === tp; // use worker because we're looking for === equality
case SyntaxKind.TypeQuery:
return true;
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
return (!(node as FunctionLikeDeclaration).type && !!(node as FunctionLikeDeclaration).body) || !!forEachChild(node, containsReference);
sandersn marked this conversation as resolved.
Show resolved Hide resolved
}
return !!forEachChild(node, containsReference);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/bivariantInferences.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ declare const b: (string | number)[] | null[] | undefined[] | {}[];

let x = a.equalsShallow(b);
>x : Symbol(x, Decl(bivariantInferences.ts, 9, 3))
>a.equalsShallow : Symbol(Array.equalsShallow, Decl(bivariantInferences.ts, 2, 20))
sandersn marked this conversation as resolved.
Show resolved Hide resolved
>a.equalsShallow : Symbol(Array.equalsShallow, Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20))
>a : Symbol(a, Decl(bivariantInferences.ts, 6, 13))
>equalsShallow : Symbol(Array.equalsShallow, Decl(bivariantInferences.ts, 2, 20))
>equalsShallow : Symbol(Array.equalsShallow, Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20))
>b : Symbol(b, Decl(bivariantInferences.ts, 7, 13))

4 changes: 2 additions & 2 deletions tests/baselines/reference/bivariantInferences.types
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ declare const b: (string | number)[] | null[] | undefined[] | {}[];
let x = a.equalsShallow(b);
>x : boolean
>a.equalsShallow(b) : boolean
>a.equalsShallow : <T>(this: readonly T[], other: readonly T[]) => boolean
>a.equalsShallow : (<T>(this: readonly T[], other: readonly T[]) => boolean) | (<T>(this: readonly T[], other: readonly T[]) => boolean) | (<T>(this: readonly T[], other: readonly T[]) => boolean) | (<T>(this: readonly T[], other: readonly T[]) => boolean)
>a : (string | number)[] | null[] | undefined[] | {}[]
>equalsShallow : <T>(this: readonly T[], other: readonly T[]) => boolean
>equalsShallow : (<T>(this: readonly T[], other: readonly T[]) => boolean) | (<T>(this: readonly T[], other: readonly T[]) => boolean) | (<T>(this: readonly T[], other: readonly T[]) => boolean) | (<T>(this: readonly T[], other: readonly T[]) => boolean)
>b : (string | number)[] | null[] | undefined[] | {}[]

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
},
{
"text": "toString",
"kind": "methodName"
"kind": "propertyName"
},
{
"text": "(",
Expand Down Expand Up @@ -198,7 +198,7 @@
},
{
"text": "toLocaleString",
"kind": "methodName"
"kind": "propertyName"
},
{
"text": "(",
Expand Down Expand Up @@ -981,7 +981,7 @@
},
{
"text": "join",
"kind": "methodName"
"kind": "propertyName"
},
{
"text": "(",
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/mixinAccessModifiers.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ function f9(x: ProtectedGeneric<{a: void;}> & ProtectedGeneric<{a:void;b:void;}>
>b : Symbol(b, Decl(mixinAccessModifiers.ts, 127, 71))

x.privateMethod(); // Error, private constituent makes method inaccessible
>x.privateMethod : Symbol(ProtectedGeneric.privateMethod, Decl(mixinAccessModifiers.ts, 107, 27))
>x.privateMethod : Symbol(ProtectedGeneric.privateMethod, Decl(mixinAccessModifiers.ts, 107, 27), Decl(mixinAccessModifiers.ts, 107, 27))
>x : Symbol(x, Decl(mixinAccessModifiers.ts, 127, 12))
>privateMethod : Symbol(ProtectedGeneric.privateMethod, Decl(mixinAccessModifiers.ts, 107, 27))
>privateMethod : Symbol(ProtectedGeneric.privateMethod, Decl(mixinAccessModifiers.ts, 107, 27), Decl(mixinAccessModifiers.ts, 107, 27))

x.protectedMethod(); // Error, protected when all constituents are protected
>x.protectedMethod : Symbol(ProtectedGeneric.protectedMethod, Decl(mixinAccessModifiers.ts, 108, 27))
>x.protectedMethod : Symbol(ProtectedGeneric.protectedMethod, Decl(mixinAccessModifiers.ts, 108, 27), Decl(mixinAccessModifiers.ts, 108, 27))
>x : Symbol(x, Decl(mixinAccessModifiers.ts, 127, 12))
>protectedMethod : Symbol(ProtectedGeneric.protectedMethod, Decl(mixinAccessModifiers.ts, 108, 27))
>protectedMethod : Symbol(ProtectedGeneric.protectedMethod, Decl(mixinAccessModifiers.ts, 108, 27), Decl(mixinAccessModifiers.ts, 108, 27))
}

8 changes: 4 additions & 4 deletions tests/baselines/reference/mixinAccessModifiers.types
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,14 @@ function f9(x: ProtectedGeneric<{a: void;}> & ProtectedGeneric<{a:void;b:void;}>

x.privateMethod(); // Error, private constituent makes method inaccessible
>x.privateMethod() : void
>x.privateMethod : () => void
>x.privateMethod : (() => void) & (() => void)
>x : ProtectedGeneric<{ a: void; }> & ProtectedGeneric<{ a: void; b: void; }>
>privateMethod : () => void
>privateMethod : (() => void) & (() => void)

x.protectedMethod(); // Error, protected when all constituents are protected
>x.protectedMethod() : void
>x.protectedMethod : () => void
>x.protectedMethod : (() => void) & (() => void)
>x : ProtectedGeneric<{ a: void; }> & ProtectedGeneric<{ a: void; b: void; }>
>protectedMethod : () => void
>protectedMethod : (() => void) & (() => void)
}

4 changes: 2 additions & 2 deletions tests/baselines/reference/sliceResultCast.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare var x: [number, string] | [number, string, string];
>x : Symbol(x, Decl(sliceResultCast.ts, 0, 11))

x.slice(1) as readonly string[];
>x.slice : Symbol(Array.slice, Decl(lib.es5.d.ts, --, --))
>x.slice : Symbol(Array.slice, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(sliceResultCast.ts, 0, 11))
>slice : Symbol(Array.slice, Decl(lib.es5.d.ts, --, --))
>slice : Symbol(Array.slice, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

4 changes: 2 additions & 2 deletions tests/baselines/reference/sliceResultCast.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ declare var x: [number, string] | [number, string, string];
x.slice(1) as readonly string[];
>x.slice(1) as readonly string[] : readonly string[]
>x.slice(1) : (string | number)[]
>x.slice : (start?: number, end?: number) => (string | number)[]
>x.slice : ((start?: number, end?: number) => (string | number)[]) | ((start?: number, end?: number) => (string | number)[])
>x : [number, string] | [number, string, string]
>slice : (start?: number, end?: number) => (string | number)[]
>slice : ((start?: number, end?: number) => (string | number)[]) | ((start?: number, end?: number) => (string | number)[])
>1 : 1

47 changes: 47 additions & 0 deletions tests/baselines/reference/stackDepthLimitCastingType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [tests/cases/compiler/stackDepthLimitCastingType.ts] ////

//// [index.d.ts]
declare global {
interface JQueryXHR { }
class Model<T = any, E = {}> {
initialize(attributes?: T, options?: CombinedModelConstructorOptions<E, this>): void;
fetch(options?: any): JQueryXHR;
}
interface ModelConstructorOptions<TModel extends Model = Model> {
collection?: Collection<TModel>;
}
class Collection<TModel extends Model = Model> {
without(...values: TModel[]): TModel[];
}
type CombinedModelConstructorOptions<E, M extends Model<any, E> = Model> = ModelConstructorOptions<M> & E;
}

export {
Model
};
export as namespace Backbone;

//// [index.d.ts]
import * as Backbone from "backbone";
declare module "backbone" {
interface ModelWithCache extends Model {
fetch(options?: any): JQueryXHR;
}
}
export {};
export as namespace BackboneFetchCache;

//// [index.ts]
import * as Backbone from "backbone";
import * as BackboneFetchCache from "backbone-fetch-cache";


const hoge = new Backbone.Model() as Backbone.ModelWithCache;
hoge.fetch(null as any);

//// [index.js]
"use strict";
exports.__esModule = true;
var Backbone = require("backbone");
var hoge = new Backbone.Model();
hoge.fetch(null);
110 changes: 110 additions & 0 deletions tests/baselines/reference/stackDepthLimitCastingType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
=== tests/cases/compiler/node_modules/backbone/index.d.ts ===
declare global {
>global : Symbol(global, Decl(index.d.ts, 0, 0))

interface JQueryXHR { }
>JQueryXHR : Symbol(JQueryXHR, Decl(index.d.ts, 0, 16))

class Model<T = any, E = {}> {
>Model : Symbol(Model, Decl(index.d.ts, 1, 27))
>T : Symbol(T, Decl(index.d.ts, 2, 16))
>E : Symbol(E, Decl(index.d.ts, 2, 24))

initialize(attributes?: T, options?: CombinedModelConstructorOptions<E, this>): void;
>initialize : Symbol(Model.initialize, Decl(index.d.ts, 2, 34))
>attributes : Symbol(attributes, Decl(index.d.ts, 3, 19))
>T : Symbol(T, Decl(index.d.ts, 2, 16))
>options : Symbol(options, Decl(index.d.ts, 3, 34))
>CombinedModelConstructorOptions : Symbol(CombinedModelConstructorOptions, Decl(index.d.ts, 11, 5))
>E : Symbol(E, Decl(index.d.ts, 2, 24))

fetch(options?: any): JQueryXHR;
>fetch : Symbol(Model.fetch, Decl(index.d.ts, 3, 93))
>options : Symbol(options, Decl(index.d.ts, 4, 14))
>JQueryXHR : Symbol(JQueryXHR, Decl(index.d.ts, 0, 16))
}
interface ModelConstructorOptions<TModel extends Model = Model> {
>ModelConstructorOptions : Symbol(ModelConstructorOptions, Decl(index.d.ts, 5, 5))
>TModel : Symbol(TModel, Decl(index.d.ts, 6, 38))
>Model : Symbol(Model, Decl(index.d.ts, 1, 27))
>Model : Symbol(Model, Decl(index.d.ts, 1, 27))

collection?: Collection<TModel>;
>collection : Symbol(ModelConstructorOptions.collection, Decl(index.d.ts, 6, 69))
>Collection : Symbol(Collection, Decl(index.d.ts, 8, 5))
>TModel : Symbol(TModel, Decl(index.d.ts, 6, 38))
}
class Collection<TModel extends Model = Model> {
>Collection : Symbol(Collection, Decl(index.d.ts, 8, 5))
>TModel : Symbol(TModel, Decl(index.d.ts, 9, 21))
>Model : Symbol(Model, Decl(index.d.ts, 1, 27))
>Model : Symbol(Model, Decl(index.d.ts, 1, 27))

without(...values: TModel[]): TModel[];
>without : Symbol(Collection.without, Decl(index.d.ts, 9, 52))
>values : Symbol(values, Decl(index.d.ts, 10, 16))
>TModel : Symbol(TModel, Decl(index.d.ts, 9, 21))
>TModel : Symbol(TModel, Decl(index.d.ts, 9, 21))
}
type CombinedModelConstructorOptions<E, M extends Model<any, E> = Model> = ModelConstructorOptions<M> & E;
>CombinedModelConstructorOptions : Symbol(CombinedModelConstructorOptions, Decl(index.d.ts, 11, 5))
>E : Symbol(E, Decl(index.d.ts, 12, 41))
>M : Symbol(M, Decl(index.d.ts, 12, 43))
>Model : Symbol(Model, Decl(index.d.ts, 1, 27))
>E : Symbol(E, Decl(index.d.ts, 12, 41))
>Model : Symbol(Model, Decl(index.d.ts, 1, 27))
>ModelConstructorOptions : Symbol(ModelConstructorOptions, Decl(index.d.ts, 5, 5))
>M : Symbol(M, Decl(index.d.ts, 12, 43))
>E : Symbol(E, Decl(index.d.ts, 12, 41))
}

export {
Model
>Model : Symbol(Model, Decl(index.d.ts, 15, 8))

};
export as namespace Backbone;
>Backbone : Symbol(Backbone, Decl(index.d.ts, 17, 2))

=== tests/cases/compiler/node_modules/backbone-fetch-cache/index.d.ts ===
import * as Backbone from "backbone";
>Backbone : Symbol(Backbone, Decl(index.d.ts, 0, 6))

declare module "backbone" {
>"backbone" : Symbol(Backbone, Decl(index.d.ts, 0, 0), Decl(index.d.ts, 0, 37))

interface ModelWithCache extends Model {
>ModelWithCache : Symbol(ModelWithCache, Decl(index.d.ts, 1, 27))
>Model : Symbol(Backbone.Model, Decl(index.d.ts, 1, 27))

fetch(options?: any): JQueryXHR;
>fetch : Symbol(ModelWithCache.fetch, Decl(index.d.ts, 2, 44))
>options : Symbol(options, Decl(index.d.ts, 3, 14))
>JQueryXHR : Symbol(JQueryXHR, Decl(index.d.ts, 0, 16))
}
}
export {};
export as namespace BackboneFetchCache;
>BackboneFetchCache : Symbol(BackboneFetchCache, Decl(index.d.ts, 6, 10))

=== tests/cases/compiler/index.ts ===
import * as Backbone from "backbone";
>Backbone : Symbol(Backbone, Decl(index.ts, 0, 6))

import * as BackboneFetchCache from "backbone-fetch-cache";
>BackboneFetchCache : Symbol(BackboneFetchCache, Decl(index.ts, 1, 6))


const hoge = new Backbone.Model() as Backbone.ModelWithCache;
>hoge : Symbol(hoge, Decl(index.ts, 4, 5))
>Backbone.Model : Symbol(Backbone.Model, Decl(index.d.ts, 15, 8))
>Backbone : Symbol(Backbone, Decl(index.ts, 0, 6))
>Model : Symbol(Backbone.Model, Decl(index.d.ts, 15, 8))
>Backbone : Symbol(Backbone, Decl(index.ts, 0, 6))
>ModelWithCache : Symbol(Backbone.ModelWithCache, Decl(index.d.ts, 1, 27))

hoge.fetch(null as any);
>hoge.fetch : Symbol(Backbone.ModelWithCache.fetch, Decl(index.d.ts, 2, 44))
>hoge : Symbol(hoge, Decl(index.ts, 4, 5))
>fetch : Symbol(Backbone.ModelWithCache.fetch, Decl(index.d.ts, 2, 44))

82 changes: 82 additions & 0 deletions tests/baselines/reference/stackDepthLimitCastingType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
=== tests/cases/compiler/node_modules/backbone/index.d.ts ===
declare global {
>global : typeof global

interface JQueryXHR { }
class Model<T = any, E = {}> {
>Model : Model<T, E>

initialize(attributes?: T, options?: CombinedModelConstructorOptions<E, this>): void;
>initialize : (attributes?: T | undefined, options?: CombinedModelConstructorOptions<E, this> | undefined) => void
>attributes : T | undefined
>options : CombinedModelConstructorOptions<E, this> | undefined

fetch(options?: any): JQueryXHR;
>fetch : (options?: any) => JQueryXHR
>options : any
}
interface ModelConstructorOptions<TModel extends Model = Model> {
collection?: Collection<TModel>;
>collection : Collection<TModel> | undefined
}
class Collection<TModel extends Model = Model> {
>Collection : Collection<TModel>

without(...values: TModel[]): TModel[];
>without : (...values: TModel[]) => TModel[]
>values : TModel[]
}
type CombinedModelConstructorOptions<E, M extends Model<any, E> = Model> = ModelConstructorOptions<M> & E;
>CombinedModelConstructorOptions : CombinedModelConstructorOptions<E, M>
}

export {
Model
>Model : typeof Model

};
export as namespace Backbone;
>Backbone : typeof import("tests/cases/compiler/node_modules/backbone/index")

=== tests/cases/compiler/node_modules/backbone-fetch-cache/index.d.ts ===
import * as Backbone from "backbone";
>Backbone : typeof Backbone

declare module "backbone" {
>"backbone" : typeof Backbone

interface ModelWithCache extends Model {
fetch(options?: any): JQueryXHR;
>fetch : (options?: any) => JQueryXHR
>options : any
}
}
export {};
export as namespace BackboneFetchCache;
>BackboneFetchCache : typeof import("tests/cases/compiler/node_modules/backbone-fetch-cache/index")

=== tests/cases/compiler/index.ts ===
import * as Backbone from "backbone";
>Backbone : typeof Backbone

import * as BackboneFetchCache from "backbone-fetch-cache";
>BackboneFetchCache : typeof BackboneFetchCache


const hoge = new Backbone.Model() as Backbone.ModelWithCache;
>hoge : Backbone.ModelWithCache
>new Backbone.Model() as Backbone.ModelWithCache : Backbone.ModelWithCache
>new Backbone.Model() : Backbone.Model<any, ModelConstructorOptions<Backbone.ModelWithCache>>
>Backbone.Model : typeof Backbone.Model
>Backbone : typeof Backbone
>Model : typeof Backbone.Model
>Backbone : any

hoge.fetch(null as any);
>hoge.fetch(null as any) : JQueryXHR
>hoge.fetch : (options?: any) => JQueryXHR
>hoge : Backbone.ModelWithCache
>fetch : (options?: any) => JQueryXHR
>null as any : any
>null : null

Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function f<T extends Cat | Dog>(a: T) {
>T : Symbol(T, Decl(typeParameterExtendingUnion1.ts, 8, 11))

a.run();
>a.run : Symbol(Animal.run, Decl(typeParameterExtendingUnion1.ts, 0, 14))
>a.run : Symbol(Animal.run, Decl(typeParameterExtendingUnion1.ts, 0, 14), Decl(typeParameterExtendingUnion1.ts, 0, 14))
>a : Symbol(a, Decl(typeParameterExtendingUnion1.ts, 8, 32))
>run : Symbol(Animal.run, Decl(typeParameterExtendingUnion1.ts, 0, 14))
>run : Symbol(Animal.run, Decl(typeParameterExtendingUnion1.ts, 0, 14), Decl(typeParameterExtendingUnion1.ts, 0, 14))

run(a);
>run : Symbol(run, Decl(typeParameterExtendingUnion1.ts, 2, 33))
Expand Down
Loading