Skip to content

Fix stack overflow in indexed access relation #14874

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 3 commits into from
Mar 28, 2017
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
16 changes: 8 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7942,16 +7942,9 @@ namespace ts {
}
}
else if (target.flags & TypeFlags.IndexedAccess) {
// if we have indexed access types with identical index types, see if relationship holds for
// the two object types.
if (source.flags & TypeFlags.IndexedAccess && (<IndexedAccessType>source).indexType === (<IndexedAccessType>target).indexType) {
if (result = isRelatedTo((<IndexedAccessType>source).objectType, (<IndexedAccessType>target).objectType, reportErrors)) {
return result;
}
}
// A type S is related to a type T[K] if S is related to A[K], where K is string-like and
// A is the apparent type of S.
const constraint = getBaseConstraintOfType(target);
const constraint = getConstraintOfType(<IndexedAccessType>target);
if (constraint) {
if (result = isRelatedTo(source, constraint, reportErrors)) {
errorInfo = saveErrorInfo;
Expand Down Expand Up @@ -7998,6 +7991,13 @@ namespace ts {
return result;
}
}
else if (target.flags & TypeFlags.IndexedAccess && (<IndexedAccessType>source).indexType === (<IndexedAccessType>target).indexType) {
// if we have indexed access types with identical index types, see if relationship holds for
// the two object types.
if (result = isRelatedTo((<IndexedAccessType>source).objectType, (<IndexedAccessType>target).objectType, reportErrors)) {
return result;
}
}
}
else {
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (<TypeReference>source).target === (<TypeReference>target).target) {
Expand Down
56 changes: 56 additions & 0 deletions tests/baselines/reference/indexedAccessRelation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//// [indexedAccessRelation.ts]
// Repro from #14723

class Component<S> {
setState<K extends keyof S>(state: Pick<S, K>) {}
}

export interface State<T> {
a?: T;
}

class Foo {}

class Comp<T extends Foo, S> extends Component<S & State<T>>
{
foo(a: T) {
this.setState({ a: a });
}
}


//// [indexedAccessRelation.js]
// Repro from #14723
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
var Component = (function () {
function Component() {
}
Component.prototype.setState = function (state) { };
return Component;
}());
var Foo = (function () {
function Foo() {
}
return Foo;
}());
var Comp = (function (_super) {
__extends(Comp, _super);
function Comp() {
return _super !== null && _super.apply(this, arguments) || this;
}
Comp.prototype.foo = function (a) {
this.setState({ a: a });
};
return Comp;
}(Component));
53 changes: 53 additions & 0 deletions tests/baselines/reference/indexedAccessRelation.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== tests/cases/compiler/indexedAccessRelation.ts ===
// Repro from #14723

class Component<S> {
>Component : Symbol(Component, Decl(indexedAccessRelation.ts, 0, 0))
>S : Symbol(S, Decl(indexedAccessRelation.ts, 2, 16))

setState<K extends keyof S>(state: Pick<S, K>) {}
>setState : Symbol(Component.setState, Decl(indexedAccessRelation.ts, 2, 20))
>K : Symbol(K, Decl(indexedAccessRelation.ts, 3, 13))
>S : Symbol(S, Decl(indexedAccessRelation.ts, 2, 16))
>state : Symbol(state, Decl(indexedAccessRelation.ts, 3, 32))
>Pick : Symbol(Pick, Decl(lib.d.ts, --, --))
>S : Symbol(S, Decl(indexedAccessRelation.ts, 2, 16))
>K : Symbol(K, Decl(indexedAccessRelation.ts, 3, 13))
}

export interface State<T> {
>State : Symbol(State, Decl(indexedAccessRelation.ts, 4, 1))
>T : Symbol(T, Decl(indexedAccessRelation.ts, 6, 23))

a?: T;
>a : Symbol(State.a, Decl(indexedAccessRelation.ts, 6, 27))
>T : Symbol(T, Decl(indexedAccessRelation.ts, 6, 23))
}

class Foo {}
>Foo : Symbol(Foo, Decl(indexedAccessRelation.ts, 8, 1))

class Comp<T extends Foo, S> extends Component<S & State<T>>
>Comp : Symbol(Comp, Decl(indexedAccessRelation.ts, 10, 12))
>T : Symbol(T, Decl(indexedAccessRelation.ts, 12, 11))
>Foo : Symbol(Foo, Decl(indexedAccessRelation.ts, 8, 1))
>S : Symbol(S, Decl(indexedAccessRelation.ts, 12, 25))
>Component : Symbol(Component, Decl(indexedAccessRelation.ts, 0, 0))
>S : Symbol(S, Decl(indexedAccessRelation.ts, 12, 25))
>State : Symbol(State, Decl(indexedAccessRelation.ts, 4, 1))
>T : Symbol(T, Decl(indexedAccessRelation.ts, 12, 11))
{
foo(a: T) {
>foo : Symbol(Comp.foo, Decl(indexedAccessRelation.ts, 13, 1))
>a : Symbol(a, Decl(indexedAccessRelation.ts, 14, 8))
>T : Symbol(T, Decl(indexedAccessRelation.ts, 12, 11))

this.setState({ a: a });
>this.setState : Symbol(Component.setState, Decl(indexedAccessRelation.ts, 2, 20))
>this : Symbol(Comp, Decl(indexedAccessRelation.ts, 10, 12))
>setState : Symbol(Component.setState, Decl(indexedAccessRelation.ts, 2, 20))
>a : Symbol(a, Decl(indexedAccessRelation.ts, 15, 23))
>a : Symbol(a, Decl(indexedAccessRelation.ts, 14, 8))
}
}

55 changes: 55 additions & 0 deletions tests/baselines/reference/indexedAccessRelation.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
=== tests/cases/compiler/indexedAccessRelation.ts ===
// Repro from #14723

class Component<S> {
>Component : Component<S>
>S : S

setState<K extends keyof S>(state: Pick<S, K>) {}
>setState : <K extends keyof S>(state: Pick<S, K>) => void
>K : K
>S : S
>state : Pick<S, K>
>Pick : Pick<T, K>
>S : S
>K : K
}

export interface State<T> {
>State : State<T>
>T : T

a?: T;
>a : T
>T : T
}

class Foo {}
>Foo : Foo

class Comp<T extends Foo, S> extends Component<S & State<T>>
>Comp : Comp<T, S>
>T : T
>Foo : Foo
>S : S
>Component : Component<S & State<T>>
>S : S
>State : State<T>
>T : T
{
foo(a: T) {
>foo : (a: T) => void
>a : T
>T : T

this.setState({ a: a });
>this.setState({ a: a }) : void
>this.setState : <K extends keyof (S & State<T>)>(state: Pick<S & State<T>, K>) => void
>this : this
>setState : <K extends keyof (S & State<T>)>(state: Pick<S & State<T>, K>) => void
>{ a: a } : { a: T; }
>a : T
>a : T
}
}

Loading