From 3885e3fcda213ec8c5f21849d6b8ddf8a5357fa7 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 14 May 2019 16:58:06 -0700 Subject: [PATCH] Fix error message regressed by #30916 (#31276) --- src/compiler/checker.ts | 20 +++++++++++++++---- ...gningFunctionToTupleIssuesError.errors.txt | 8 ++++++++ .../assigningFunctionToTupleIssuesError.js | 6 ++++++ ...ssigningFunctionToTupleIssuesError.symbols | 8 ++++++++ .../assigningFunctionToTupleIssuesError.types | 8 ++++++++ .../assigningFunctionToTupleIssuesError.ts | 2 ++ 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.js create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.types create mode 100644 tests/cases/compiler/assigningFunctionToTupleIssuesError.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9a64dce125591..0f18afa77410c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12290,7 +12290,7 @@ namespace ts { let depth = 0; let expandingFlags = ExpandingFlags.None; let overflow = false; - let suppressNextError = false; + let overrideNextErrorInfo: DiagnosticMessageChain | undefined; Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); @@ -12571,10 +12571,14 @@ namespace ts { } if (!result && reportErrors) { - const maybeSuppress = suppressNextError; - suppressNextError = false; + let maybeSuppress = overrideNextErrorInfo; + overrideNextErrorInfo = undefined; if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) { + const currentError = errorInfo; tryElaborateArrayLikeErrors(source, target, reportErrors); + if (errorInfo !== currentError) { + maybeSuppress = errorInfo; + } } if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Primitive) { tryElaborateErrorsForPrimitivesAndObjects(source, target); @@ -13506,9 +13510,10 @@ namespace ts { if (unmatchedProperty) { if (reportErrors) { const props = arrayFrom(getUnmatchedProperties(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false)); + let shouldSkipElaboration = false; if (!headMessage || (headMessage.code !== Diagnostics.Class_0_incorrectly_implements_interface_1.code && headMessage.code !== Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass.code)) { - suppressNextError = true; // Retain top-level error for interface implementing issues, otherwise omit it + shouldSkipElaboration = true; // Retain top-level error for interface implementing issues, otherwise omit it } if (props.length === 1) { const propName = symbolToString(unmatchedProperty); @@ -13516,6 +13521,9 @@ namespace ts { if (length(unmatchedProperty.declarations)) { associateRelatedInfo(createDiagnosticForNode(unmatchedProperty.declarations[0], Diagnostics._0_is_declared_here, propName)); } + if (shouldSkipElaboration) { + overrideNextErrorInfo = errorInfo; + } } else if (tryElaborateArrayLikeErrors(source, target, /*reportErrors*/ false)) { if (props.length > 5) { // arbitrary cutoff for too-long list form @@ -13524,7 +13532,11 @@ namespace ts { else { reportError(Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, typeToString(source), typeToString(target), map(props, p => symbolToString(p)).join(", ")); } + if (shouldSkipElaboration) { + overrideNextErrorInfo = errorInfo; + } } + // ELSE: No array like or unmatched property error - just issue top level error (errorInfo = undefined) } return Ternary.False; } diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt b/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt new file mode 100644 index 0000000000000..ca1af66c0873c --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt @@ -0,0 +1,8 @@ +tests/cases/compiler/assigningFunctionToTupleIssuesError.ts(2,5): error TS2322: Type '() => void' is not assignable to type '[string]'. + + +==== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts (1 errors) ==== + declare let a: () => void; + let b: [string] = a; + ~ +!!! error TS2322: Type '() => void' is not assignable to type '[string]'. \ No newline at end of file diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.js b/tests/baselines/reference/assigningFunctionToTupleIssuesError.js new file mode 100644 index 0000000000000..3de3da7b3cc41 --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.js @@ -0,0 +1,6 @@ +//// [assigningFunctionToTupleIssuesError.ts] +declare let a: () => void; +let b: [string] = a; + +//// [assigningFunctionToTupleIssuesError.js] +var b = a; diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols b/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols new file mode 100644 index 0000000000000..7a499662aeffb --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts === +declare let a: () => void; +>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11)) + +let b: [string] = a; +>b : Symbol(b, Decl(assigningFunctionToTupleIssuesError.ts, 1, 3)) +>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11)) + diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.types b/tests/baselines/reference/assigningFunctionToTupleIssuesError.types new file mode 100644 index 0000000000000..6c7d57f8483f2 --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts === +declare let a: () => void; +>a : () => void + +let b: [string] = a; +>b : [string] +>a : () => void + diff --git a/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts b/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts new file mode 100644 index 0000000000000..befea50f81d94 --- /dev/null +++ b/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts @@ -0,0 +1,2 @@ +declare let a: () => void; +let b: [string] = a; \ No newline at end of file