From 2d407369ec549914d5c078312d5080051d62e1ac Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 14 Sep 2018 15:46:37 -0700 Subject: [PATCH 1/2] Use head message at top level of elaboration if elaborating via possible call --- src/compiler/checker.ts | 16 ++++++++-------- ...nsForExpressionsWhichCouldBeCalled.errors.txt | 4 ++-- ...eStillReferencesArgumentAtTopLevel.errors.txt | 10 ++++++++++ ...lableTypeStillReferencesArgumentAtTopLevel.js | 7 +++++++ ...TypeStillReferencesArgumentAtTopLevel.symbols | 12 ++++++++++++ ...leTypeStillReferencesArgumentAtTopLevel.types | 13 +++++++++++++ .../baselines/reference/parser536727.errors.txt | 12 ++++++------ ...lableTypeStillReferencesArgumentAtTopLevel.ts | 3 +++ 8 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt create mode 100644 tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js create mode 100644 tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.symbols create mode 100644 tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.types create mode 100644 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4e31e285e2de3..2a8b0ce43147e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10592,7 +10592,7 @@ namespace ts { function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean { if (isTypeRelatedTo(source, target, relation)) return true; - if (!errorNode || !elaborateError(expr, source, target, relation)) { + if (!errorNode || !elaborateError(expr, source, target, relation, headMessage)) { return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain); } return false; @@ -10602,20 +10602,20 @@ namespace ts { return !!(type.flags & TypeFlags.Conditional || (type.flags & TypeFlags.Intersection && some((type as IntersectionType).types, isOrHasGenericConditional))); } - function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map): boolean { + function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map, headMessage: DiagnosticMessage | undefined): boolean { if (!node || isOrHasGenericConditional(target)) return false; - if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation)) { + if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation, headMessage)) { return true; } switch (node.kind) { case SyntaxKind.JsxExpression: case SyntaxKind.ParenthesizedExpression: - return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation); + return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation, headMessage); case SyntaxKind.BinaryExpression: switch ((node as BinaryExpression).operatorToken.kind) { case SyntaxKind.EqualsToken: case SyntaxKind.CommaToken: - return elaborateError((node as BinaryExpression).right, source, target, relation); + return elaborateError((node as BinaryExpression).right, source, target, relation, headMessage); } break; case SyntaxKind.ObjectLiteralExpression: @@ -10628,7 +10628,7 @@ namespace ts { return false; } - function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map): boolean { + function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map, headMessage: DiagnosticMessage | undefined): boolean { const callSignatures = getSignaturesOfType(source, SignatureKind.Call); const constructSignatures = getSignaturesOfType(source, SignatureKind.Construct); for (const signatures of [constructSignatures, callSignatures]) { @@ -10637,7 +10637,7 @@ namespace ts { return !(returnType.flags & (TypeFlags.Any | TypeFlags.Never)) && checkTypeRelatedTo(returnType, target, relation, /*errorNode*/ undefined); })) { const resultObj: { error?: Diagnostic } = {}; - checkTypeAssignableTo(source, target, node, /*errorMessage*/ undefined, /*containingChain*/ undefined, resultObj); + checkTypeAssignableTo(source, target, node, headMessage, /*containingChain*/ undefined, resultObj); const diagnostic = resultObj.error!; addRelatedInfo(diagnostic, createDiagnosticForNode( node, @@ -10663,7 +10663,7 @@ namespace ts { const sourcePropType = getIndexedAccessType(source, nameType, /*accessNode*/ undefined, errorType); const targetPropType = getIndexedAccessType(target, nameType, /*accessNode*/ undefined, errorType); if (sourcePropType !== errorType && targetPropType !== errorType && !isTypeAssignableTo(sourcePropType, targetPropType)) { - const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation); + const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation, /*headMessage*/ undefined); if (elaborated) { reportedError = true; } diff --git a/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt b/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt index 9f1f60b9d1e30..4e86397c36e24 100644 --- a/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt +++ b/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt @@ -2,7 +2,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(1 Property 'x' is missing in type 'typeof Bar'. tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(11,8): error TS2322: Type 'DateConstructor' is not assignable to type 'Date'. Property 'toDateString' is missing in type 'DateConstructor'. -tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2322: Type '() => number' is not assignable to type 'number'. +tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'. tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(26,5): error TS2322: Type '() => number' is not assignable to type 'number'. @@ -33,7 +33,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(2 y: new Date() }, getNum); ~~~~~~ -!!! error TS2322: Type '() => number' is not assignable to type 'number'. +!!! error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'. !!! related TS6212 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:17:4: Did you mean to call this expression? diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt new file mode 100644 index 0000000000000..084baeb0689d9 --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts(3,4): error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'. + + +==== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts (1 errors) ==== + declare var ohno: new () => number; + declare function ff(t: number): void; + ff(ohno) + ~~~~ +!!! error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'. +!!! related TS6213 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts:3:4: Did you mean to use `new` with this expression? \ No newline at end of file diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js new file mode 100644 index 0000000000000..bf7e6a98bbd1a --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js @@ -0,0 +1,7 @@ +//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts] +declare var ohno: new () => number; +declare function ff(t: number): void; +ff(ohno) + +//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js] +ff(ohno); diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.symbols b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.symbols new file mode 100644 index 0000000000000..8913bb59c9e2f --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts === +declare var ohno: new () => number; +>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11)) + +declare function ff(t: number): void; +>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35)) +>t : Symbol(t, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 1, 20)) + +ff(ohno) +>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35)) +>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11)) + diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.types b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.types new file mode 100644 index 0000000000000..cb47b67754ff6 --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts === +declare var ohno: new () => number; +>ohno : new () => number + +declare function ff(t: number): void; +>ff : (t: number) => void +>t : number + +ff(ohno) +>ff(ohno) : void +>ff : (t: number) => void +>ohno : new () => number + diff --git a/tests/baselines/reference/parser536727.errors.txt b/tests/baselines/reference/parser536727.errors.txt index 6cdf152582f38..8c2cb059a78a7 100644 --- a/tests/baselines/reference/parser536727.errors.txt +++ b/tests/baselines/reference/parser536727.errors.txt @@ -1,6 +1,6 @@ -tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. +tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. Type '(x: string) => string' is not assignable to type 'string'. -tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. +tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. Type '(x: string) => string' is not assignable to type 'string'. @@ -13,12 +13,12 @@ tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): foo(g); foo(() => g); ~~~~~~~ -!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. -!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'. +!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. +!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'. !!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:7:5: Did you mean to call this expression? foo(x); ~ -!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. -!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'. +!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. +!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'. !!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:8:5: Did you mean to call this expression? \ No newline at end of file diff --git a/tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts b/tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts new file mode 100644 index 0000000000000..f2b964fadbc51 --- /dev/null +++ b/tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts @@ -0,0 +1,3 @@ +declare var ohno: new () => number; +declare function ff(t: number): void; +ff(ohno) \ No newline at end of file From 7ba12a98f9b196a2c56f1f08ec4f5eafa0a09737 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 14 Sep 2018 16:00:17 -0700 Subject: [PATCH 2/2] Accept updated baseline --- ...iblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt index 084baeb0689d9..b8400f3b76b70 100644 --- a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt @@ -7,4 +7,4 @@ tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAt ff(ohno) ~~~~ !!! error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'. -!!! related TS6213 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts:3:4: Did you mean to use `new` with this expression? \ No newline at end of file +!!! related TS6213 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts:3:4: Did you mean to use 'new' with this expression? \ No newline at end of file