From ec7c801a8dbad307b76b0a18f819da3ea06bb16e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Mar 2024 09:34:44 +0000 Subject: Support calling functions with explicit type-level args --- .../compiler/typechecker/TypeCheckerCallTests.java | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java index c7bbafaf..21a3e718 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java @@ -466,6 +466,125 @@ public class TypeCheckerCallTests { ); } + @Test + public void canCallGenericFunctionWithExplicitTypeArg() { + var untyped = UntypedCallNode.builder() + .withReceiver(Untyped.reference("identity")) + .withTypeLevelArgs(List.of( + Untyped.typeLevelReference("Int") + )) + .addPositionalArg(Untyped.intLiteral(42)) + .build(); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map( + Map.entry( + "identity", + FunctionType.builder() + .withTypeLevelParams(List.of( + TypeParam.forFunction(NamespaceName.of("Example"), "identity", "T") + )) + .addPositionalParam(TypeParam.forFunction(NamespaceName.of("Example"), "identity", "T")) + .withReturnType(TypeParam.forFunction(NamespaceName.of("Example"), "identity", "T")) + .build() + ), + Map.entry("Int", Types.metaType(Types.INT)) + )); + + var typed = typeCheckExpression(untyped, context); + + assertThat( + typed.value(), + isTypedCallNode() + .withType(Types.INT) + ); + } + + @Test + public void canCallGenericFunctionWithImplicitTypeArg() { + var untyped = UntypedCallNode.builder() + .withReceiver(Untyped.reference("identity")) + .addPositionalArg(Untyped.intLiteral(42)) + .build(); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map( + Map.entry( + "identity", + FunctionType.builder() + .withTypeLevelParams(List.of( + TypeParam.forFunction(NamespaceName.of("Example"), "identity", "T") + )) + .addPositionalParam(TypeParam.forFunction(NamespaceName.of("Example"), "identity", "T")) + .withReturnType(TypeParam.forFunction(NamespaceName.of("Example"), "identity", "T")) + .build() + ), + Map.entry("Int", Types.metaType(Types.INT)) + )); + + var typed = typeCheckExpression(untyped, context); + + assertThat( + typed.value(), + isTypedCallNode() + .withType(Types.INT) + ); + } + + @Test + public void canCallGenericFunctionWithExplicitEffectArg() { + var untyped = UntypedCallNode.builder() + .withReceiver(Untyped.reference("twice")) + .withHasEffects(true) + .withTypeLevelArgs(List.of(Untyped.typeLevelReference("Io"))) + .addPositionalArg(Untyped.reference("f")) + .build(); + var twiceType = FunctionType.builder() + .withTypeLevelParams(List.of( + EffectParam.forFunction(NamespaceName.of("Example"), "twice", "E") + )) + .addPositionalParam( + FunctionType.builder() + .withEffect(EffectParam.forFunction(NamespaceName.of("Example"), "twice", "E")) + .build() + ) + .withEffect(EffectParam.forFunction(NamespaceName.of("Example"), "twice", "E")) + .build(); + var context = TypeCheckerContext.EMPTY + .enterFunction( + P.map( + Map.entry( + "twice", + Variable.defined(twiceType, VariableType.VAL) + ), + Map.entry( + "f", + Variable.defined( + FunctionType.builder() + .withEffect(Effects.IO) + .build(), + VariableType.VAL + ) + ), + Map.entry( + "Io", + Variable.defined( + Types.effectType(Effects.IO), + VariableType.CONSTANT + ) + ) + ), + Effects.IO, + Types.UNIT + ); + + var typed = typeCheckExpression(untyped, context); + + assertThat( + typed.value(), + isTypedCallNode() + .withHasEffects(true) + ); + } + @Test public void canCallGenericFunctionWithImplicitEffectArg() { var untyped = UntypedCallNode.builder() -- cgit v1.2.3