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/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; 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); + } } 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)