Skip to content

Use head message at top level of elaboration if elaborating via possible call #27107

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
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 @@ -10592,7 +10592,7 @@ namespace ts {

function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, 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;
Expand All @@ -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<RelationComparisonResult>): boolean {
function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map<RelationComparisonResult>, 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:
Expand All @@ -10628,7 +10628,7 @@ namespace ts {
return false;
}

function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map<RelationComparisonResult>): boolean {
function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map<RelationComparisonResult>, headMessage: DiagnosticMessage | undefined): boolean {
const callSignatures = getSignaturesOfType(source, SignatureKind.Call);
const constructSignatures = getSignaturesOfType(source, SignatureKind.Construct);
for (const signatures of [constructSignatures, callSignatures]) {
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'.


Expand Down Expand Up @@ -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?


Expand Down
Original file line number Diff line number Diff line change
@@ -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?
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts]
declare var ohno: new () => number;
declare function ff(t: number): void;
ff(ohno)

//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js]
ff(ohno);
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

12 changes: 6 additions & 6 deletions tests/baselines/reference/parser536727.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.


Expand All @@ -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?

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare var ohno: new () => number;
declare function ff(t: number): void;
ff(ohno)