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 resetErrorInfo if structuredTypeRelatedTo succeeds #49718

Merged
merged 3 commits into from
Jul 6, 2022
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
26 changes: 9 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19287,13 +19287,21 @@ namespace ts {
}

function structuredTypeRelatedTo(source: Type, target: Type, reportErrors: boolean, intersectionState: IntersectionState): Ternary {
const saveErrorInfo = captureErrorCalculationState();
const result = structuredTypeRelatedToWorker(source, target, reportErrors, intersectionState, saveErrorInfo);
if (result) {
resetErrorInfo(saveErrorInfo);
}
return result;
}

function structuredTypeRelatedToWorker(source: Type, target: Type, reportErrors: boolean, intersectionState: IntersectionState, saveErrorInfo: ReturnType<typeof captureErrorCalculationState>): Ternary {
Copy link
Member Author

Choose a reason for hiding this comment

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

saveErrorInfo is kept because there are still some calls which explicitly reset it in order to hide certain errors that would be less helpful than others.

if (intersectionState & IntersectionState.PropertyCheck) {
return propertiesRelatedTo(source, target, reportErrors, /*excludedProperties*/ undefined, IntersectionState.None);
}
let result: Ternary;
let originalErrorInfo: DiagnosticMessageChain | undefined;
let varianceCheckFailed = false;
const saveErrorInfo = captureErrorCalculationState();
let sourceFlags = source.flags;
const targetFlags = target.flags;
if (relation === identityRelation) {
Expand Down Expand Up @@ -19357,7 +19365,6 @@ namespace ts {
if (constraint && everyType(constraint, c => c !== source)) { // Skip comparison if expansion contains the source itself
// TODO: Stack errors so we get a pyramid for the "normal" comparison above, _and_ a second for this
if (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand Down Expand Up @@ -19491,7 +19498,6 @@ namespace ts {
result &= isRelatedTo((source as IndexedAccessType).indexType, (target as IndexedAccessType).indexType, RecursionFlags.Both, reportErrors);
}
if (result) {
resetErrorInfo(saveErrorInfo);
return result;
}
if (reportErrors) {
Expand Down Expand Up @@ -19596,7 +19602,6 @@ namespace ts {
// If we reach 10 levels of nesting for the same conditional type, assume it is an infinitely expanding recursive
// conditional type and bail out with a Ternary.Maybe result.
if (isDeeplyNestedType(target, targetStack, targetDepth, 10)) {
resetErrorInfo(saveErrorInfo);
return Ternary.Maybe;
}
const c = target as ConditionalType;
Expand All @@ -19611,7 +19616,6 @@ namespace ts {
if (result = skipTrue ? Ternary.True : isRelatedTo(source, getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false)) {
result &= skipFalse ? Ternary.True : isRelatedTo(source, getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false);
if (result) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand Down Expand Up @@ -19644,12 +19648,10 @@ namespace ts {
const constraint = getConstraintOfType(source as TypeVariable) || unknownType;
// hi-speed no-this-instantiation check (less accurate, but avoids costly `this`-instantiation when the constraint will suffice), see #28231 for report on why this is needed
if (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
resetErrorInfo(saveErrorInfo);
return result;
}
// slower, fuller, this-instantiated check (necessary when comparing raw `this` types from base classes), see `subclassWithPolymorphicThisIsAssignable.ts` test for example
else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, RecursionFlags.Source, reportErrors && constraint !== unknownType && !(targetFlags & sourceFlags & TypeFlags.TypeParameter), /*headMessage*/ undefined, intersectionState)) {
resetErrorInfo(saveErrorInfo);
return result;
}
if (isMappedTypeGenericIndexedAccess(source)) {
Expand All @@ -19658,7 +19660,6 @@ namespace ts {
const indexConstraint = getConstraintOfType((source as IndexedAccessType).indexType);
if (indexConstraint) {
if (result = isRelatedTo(getIndexedAccessType((source as IndexedAccessType).objectType, indexConstraint), target, RecursionFlags.Source, reportErrors)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand All @@ -19667,15 +19668,13 @@ namespace ts {
}
else if (sourceFlags & TypeFlags.Index) {
if (result = isRelatedTo(keyofConstraintType, target, RecursionFlags.Source, reportErrors)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
else if (sourceFlags & TypeFlags.TemplateLiteral && !(targetFlags & TypeFlags.Object)) {
if (!(targetFlags & TypeFlags.TemplateLiteral)) {
const constraint = getBaseConstraintOfType(source);
if (constraint && constraint !== source && (result = isRelatedTo(constraint, target, RecursionFlags.Source, reportErrors))) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand All @@ -19686,14 +19685,12 @@ namespace ts {
return Ternary.False;
}
if (result = isRelatedTo((source as StringMappingType).type, (target as StringMappingType).type, RecursionFlags.Both, reportErrors)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
else {
const constraint = getBaseConstraintOfType(source);
if (constraint && (result = isRelatedTo(constraint, target, RecursionFlags.Source, reportErrors))) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand All @@ -19702,7 +19699,6 @@ namespace ts {
// If we reach 10 levels of nesting for the same conditional type, assume it is an infinitely expanding recursive
// conditional type and bail out with a Ternary.Maybe result.
if (isDeeplyNestedType(source, sourceStack, sourceDepth, 10)) {
resetErrorInfo(saveErrorInfo);
return Ternary.Maybe;
}
if (targetFlags & TypeFlags.Conditional) {
Expand All @@ -19725,7 +19721,6 @@ namespace ts {
result &= isRelatedTo(getFalseTypeFromConditionalType(source as ConditionalType), getFalseTypeFromConditionalType(target as ConditionalType), RecursionFlags.Both, reportErrors);
}
if (result) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand All @@ -19736,7 +19731,6 @@ namespace ts {
const distributiveConstraint = hasNonCircularBaseConstraint(source) ? getConstraintOfDistributiveConditionalType(source as ConditionalType) : undefined;
if (distributiveConstraint) {
if (result = isRelatedTo(distributiveConstraint, target, RecursionFlags.Source, reportErrors)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand All @@ -19747,7 +19741,6 @@ namespace ts {
const defaultConstraint = getDefaultConstraintOfConditionalType(source as ConditionalType);
if (defaultConstraint) {
if (result = isRelatedTo(defaultConstraint, target, RecursionFlags.Source, reportErrors)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand All @@ -19760,7 +19753,6 @@ namespace ts {
if (isGenericMappedType(target)) {
if (isGenericMappedType(source)) {
if (result = mappedTypeRelatedTo(source, target, reportErrors)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/baselines/reference/relatedViaDiscriminatedTypeNoError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [relatedViaDiscriminatedTypeNoError.ts]
class Model {
constructor(public flag: boolean) {}
}

type DiscriminatedUnion = { flag: true } | { flag: false };
class A<T extends DiscriminatedUnion> {
constructor(public model: T) { }
}

class B extends A<Model> { }


//// [relatedViaDiscriminatedTypeNoError.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Model = /** @class */ (function () {
function Model(flag) {
this.flag = flag;
}
return Model;
}());
var A = /** @class */ (function () {
function A(model) {
this.model = model;
}
return A;
}());
var B = /** @class */ (function (_super) {
__extends(B, _super);
function B() {
return _super !== null && _super.apply(this, arguments) || this;
}
return B;
}(A));
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/relatedViaDiscriminatedTypeNoError.ts ===
class Model {
>Model : Symbol(Model, Decl(relatedViaDiscriminatedTypeNoError.ts, 0, 0))

constructor(public flag: boolean) {}
>flag : Symbol(Model.flag, Decl(relatedViaDiscriminatedTypeNoError.ts, 1, 16))
}

type DiscriminatedUnion = { flag: true } | { flag: false };
>DiscriminatedUnion : Symbol(DiscriminatedUnion, Decl(relatedViaDiscriminatedTypeNoError.ts, 2, 1))
>flag : Symbol(flag, Decl(relatedViaDiscriminatedTypeNoError.ts, 4, 27))
>flag : Symbol(flag, Decl(relatedViaDiscriminatedTypeNoError.ts, 4, 44))

class A<T extends DiscriminatedUnion> {
>A : Symbol(A, Decl(relatedViaDiscriminatedTypeNoError.ts, 4, 59))
>T : Symbol(T, Decl(relatedViaDiscriminatedTypeNoError.ts, 5, 8))
>DiscriminatedUnion : Symbol(DiscriminatedUnion, Decl(relatedViaDiscriminatedTypeNoError.ts, 2, 1))

constructor(public model: T) { }
>model : Symbol(A.model, Decl(relatedViaDiscriminatedTypeNoError.ts, 6, 16))
>T : Symbol(T, Decl(relatedViaDiscriminatedTypeNoError.ts, 5, 8))
}

class B extends A<Model> { }
>B : Symbol(B, Decl(relatedViaDiscriminatedTypeNoError.ts, 7, 1))
>A : Symbol(A, Decl(relatedViaDiscriminatedTypeNoError.ts, 4, 59))
>Model : Symbol(Model, Decl(relatedViaDiscriminatedTypeNoError.ts, 0, 0))

26 changes: 26 additions & 0 deletions tests/baselines/reference/relatedViaDiscriminatedTypeNoError.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/relatedViaDiscriminatedTypeNoError.ts ===
class Model {
>Model : Model

constructor(public flag: boolean) {}
>flag : boolean
}

type DiscriminatedUnion = { flag: true } | { flag: false };
>DiscriminatedUnion : { flag: true; } | { flag: false; }
>flag : true
>true : true
>flag : false
>false : false

class A<T extends DiscriminatedUnion> {
>A : A<T>

constructor(public model: T) { }
>model : T
}

class B extends A<Model> { }
>B : B
>A : A<Model>

10 changes: 10 additions & 0 deletions tests/cases/compiler/relatedViaDiscriminatedTypeNoError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Model {
constructor(public flag: boolean) {}
}

type DiscriminatedUnion = { flag: true } | { flag: false };
class A<T extends DiscriminatedUnion> {
constructor(public model: T) { }
}

class B extends A<Model> { }