From 772010bc72d1ca746a17e2c750fc0642007829a0 Mon Sep 17 00:00:00 2001 From: Yoav Steinberg Date: Sun, 7 Apr 2024 13:13:37 +0300 Subject: [PATCH 1/3] don't recurse into closures when searching for `return` statements --- examples/tests/invalid/missing_return.test.w | 8 ++++++++ libs/wingc/src/type_check/has_type_stmt.rs | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/tests/invalid/missing_return.test.w b/examples/tests/invalid/missing_return.test.w index 558f5d1d705..0a18c1a221e 100644 --- a/examples/tests/invalid/missing_return.test.w +++ b/examples/tests/invalid/missing_return.test.w @@ -23,3 +23,11 @@ let returnString2 = inflight (): str => { return "hi"; } }; + +// Ignore return statements in inner closures when searching for return statements +let returnString3 = (): str => { + let x = (): str => { + return "what?"; // This should be ignored and we should produce a missing return error + }; +}; +//^ A function whose return type is "str" must return a value. diff --git a/libs/wingc/src/type_check/has_type_stmt.rs b/libs/wingc/src/type_check/has_type_stmt.rs index 363e97fa63c..e28edc8cef6 100644 --- a/libs/wingc/src/type_check/has_type_stmt.rs +++ b/libs/wingc/src/type_check/has_type_stmt.rs @@ -1,5 +1,5 @@ use crate::{ - ast::{Stmt, StmtKind}, + ast::{Expr, ExprKind, Stmt, StmtKind}, visit::{self, Visit}, }; @@ -27,4 +27,12 @@ impl Visit<'_> for HasStatementVisitor { } visit::visit_stmt(self, node); } + + fn visit_expr(&mut self, node: &'_ Expr) { + // Don't recurse into closures. This way our search will ignore stmts in inner closures. + if matches!(node.kind, ExprKind::FunctionClosure(_)) { + return; + } + visit::visit_expr(self, node); + } } From be801e0e51d46f299fe4bb61ba8d6f489ae49320 Mon Sep 17 00:00:00 2001 From: Yoav Steinberg Date: Sun, 7 Apr 2024 14:46:11 +0300 Subject: [PATCH 2/3] fix old test --- examples/tests/valid/while_loop_await.test.w | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tests/valid/while_loop_await.test.w b/examples/tests/valid/while_loop_await.test.w index f7e032126cf..4ba2bc9a549 100644 --- a/examples/tests/valid/while_loop_await.test.w +++ b/examples/tests/valid/while_loop_await.test.w @@ -2,7 +2,7 @@ bring cloud; let queue = new cloud.Queue(); -let handler = inflight (body: str): str => { +let handler = inflight (body: str): void => { let i = 0; let iterator = inflight (j: num): num => { return j+1; From 621e316dafc8241b0d806abff376afd24bca9578 Mon Sep 17 00:00:00 2001 From: "monada-bot[bot]" Date: Sun, 7 Apr 2024 11:55:25 +0000 Subject: [PATCH 3/3] chore: self mutation (e2e-2of2.diff) Signed-off-by: monada-bot[bot] --- tools/hangar/__snapshots__/invalid.ts.snap | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/hangar/__snapshots__/invalid.ts.snap b/tools/hangar/__snapshots__/invalid.ts.snap index 6cf66d7d09a..a44e6108b80 100644 --- a/tools/hangar/__snapshots__/invalid.ts.snap +++ b/tools/hangar/__snapshots__/invalid.ts.snap @@ -2712,6 +2712,18 @@ exports[`missing_return.test.w 1`] = ` | \\\\-^ A function whose return type is \\"str\\" must return a value. +error: A function whose return type is \\"str\\" must return a value. + --> ../../../examples/tests/invalid/missing_return.test.w:28:32 + | +28 | let returnString3 = (): str => { + | /--------------------------------^ +29 | | let x = (): str => { +30 | | return \\"what?\\"; // This should be ignored and we should produce a missing return error +31 | | }; +32 | | }; + | \\\\-^ A function whose return type is \\"str\\" must return a value. + + Tests 1 failed (1)