From bc7f70a241abc82e4e9fc975b1e3fe613dcd52c5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Nov 2026 22:13:35 +0000 Subject: Add function expression params to scope --- .../TypeCheckerFunctionExpressionTests.java | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/test') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java index 0dde897..a03beae 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java @@ -19,8 +19,7 @@ import static org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNo import static org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher.isTypedParamNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNodeMatcher.isTypedReferenceNode; import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.equalTo; -import static org.zwobble.precisely.Matchers.isSequence; +import static org.zwobble.precisely.Matchers.*; public class TypeCheckerFunctionExpressionTests { @Test @@ -74,4 +73,33 @@ public class TypeCheckerFunctionExpressionTests { ))) ); } + + @Test + public void paramsAreAvailableInFunctionExpressionBody() { + var untyped = UntypedFunctionExpressionNode.arbitrary() + .addParam(UntypedParamNode.arbitrary().withName("a")) + .withBody(UntypedReferenceNode.arbitrary().withName("a")) + .build(); + var globalContext = TypeCheckerContextArb.globalContext(); + var namespaceName = NamespaceName.of("App"); + var namespaceContext = globalContext.enterNamespace(namespaceName); + + var typeHint = new FunctionType( + List.of(NativeTypes.INT_32), + NativeTypes.INT_32 + ); + var typed = TypeChecker.typeCheckExpression(untyped, Optional.of(typeHint), namespaceContext); + + assertThat(typed, isTypedFunctionExpressionNode() + .withParams(isSequence( + isTypedParamNode().withName(equalTo("a")).withType(equalTo(NativeTypes.INT_32)) + )) + .withBody(isTypedReferenceNode().withName(equalTo("a")).withType(equalTo(NativeTypes.INT_32))) + .withType(equalTo(new FunctionType( + List.of(NativeTypes.INT_32), + NativeTypes.INT_32 + ))) + ); + assertThat(namespaceContext.lookupType("a"), isOptionalEmpty()); + } } -- cgit v1.2.3