From 142aa71fc7611f0ec95c46e1c52e24d87208cdad Mon Sep 17 00:00:00 2001 From: "Congcong Cai (EE-CN-42)" Date: Fri, 12 Aug 2022 13:59:18 +0800 Subject: [PATCH 1/3] fix: infer return type in implicit return arrow function --- src/resolver.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/resolver.ts b/src/resolver.ts index 46d5d16570..03d4f8c9fe 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -79,7 +79,9 @@ import { isTypeOmitted, FunctionExpression, NewExpression, - ArrayLiteralExpression + ArrayLiteralExpression, + ArrowKind, + ExpressionStatement } from "./ast"; import { @@ -2613,7 +2615,25 @@ export class Resolver extends DiagnosticEmitter { /** How to proceed with eventual diagnostics. */ reportMode: ReportMode = ReportMode.REPORT ): Type | null { - return this.resolveFunctionType(node.declaration.signature, ctxFlow.actualFunction, ctxFlow.contextualTypeArguments, reportMode); + const declaration = node.declaration; + const signature = declaration.signature; + const body = declaration.body; + let functionType = this.resolveFunctionType(signature, ctxFlow.actualFunction, ctxFlow.contextualTypeArguments, reportMode); + if ( + functionType && + declaration.arrowKind != ArrowKind.NONE && + body && body.kind == NodeKind.EXPRESSION && + isTypeOmitted(signature.returnType) + ) { + // (x) => ret, infer return type accordingt to `ret` + const expr = (body).expression; + const type = this.resolveExpression(expr, ctxFlow, ctxType, ReportMode.SWALLOW); + if (type) { + let signatureReference = assert(functionType.getSignature()); + signatureReference.returnType = type; + } + } + return functionType; } // ==================================================== Elements ===================================================== From 6517cf475c36af952a2a1147196cbb1778c9dff0 Mon Sep 17 00:00:00 2001 From: "Congcong Cai (EE-CN-42)" Date: Fri, 12 Aug 2022 17:22:21 +0800 Subject: [PATCH 2/3] add testcase --- tests/compiler/std/array.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/compiler/std/array.ts b/tests/compiler/std/array.ts index 50a937347c..46b4f5d1ce 100644 --- a/tests/compiler/std/array.ts +++ b/tests/compiler/std/array.ts @@ -780,7 +780,7 @@ var i: i32; // Array#map /////////////////////////////////////////////////////////////////////////////////////// { - let newArr = arr.map((value: i32) => value); + let newArr = arr.map((value: i32, index: i32, arr: Array) => value); assert(newArr.length == 4); assert(newArr[0] == arr[0]); From bb620db4a5004f6cd7bbf952b1a6de1a8a94e2e3 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Sun, 21 Aug 2022 07:03:14 +0800 Subject: [PATCH 3/3] Update src/resolver.ts Co-authored-by: dcode --- src/resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resolver.ts b/src/resolver.ts index 03d4f8c9fe..523d1d8d44 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -2627,7 +2627,7 @@ export class Resolver extends DiagnosticEmitter { ) { // (x) => ret, infer return type accordingt to `ret` const expr = (body).expression; - const type = this.resolveExpression(expr, ctxFlow, ctxType, ReportMode.SWALLOW); + const type = this.resolveExpression(expr, ctxFlow, ctxType, reportMode); if (type) { let signatureReference = assert(functionType.getSignature()); signatureReference.returnType = type;