From c386126bdb87705c73379b3e6678680b4c0253cd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 10 Aug 2026 15:42:31 +0100 Subject: Filter unused type params from arbitrary value function --- .../generators/javatypes/JavaTypesGenerator.java | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java index 1034a98..4f4f648 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java @@ -184,17 +184,18 @@ public class JavaTypesGenerator implements Generator { generateTypeRef(type.value(), context); }; + var typeParamsUsedInArbitraryValue = new HashSet(); var arbitraryValueExpression = new JavaNewExpression( builderJavaTypeRef, structDefinition.fields().orElse(List.of()).stream() - .map(field -> arbitraryValue(field.type(), context)) + .map(field -> arbitraryValue(field.type(), typeParamsUsedInArbitraryValue, context)) .toList() ); - // TODO: don't require arbitrary values for option, list, etc. var arbitraryValueParams = switch (structDefinition.typeOrConstructor()) { case TypeOrConstructor.Constructor constructor -> constructor.value().params().stream() + .filter(typeParamsUsedInArbitraryValue::contains) .map(typeParam -> new JavaParam( JavaTypeRef.supplier(this.javaGenerator.generateTypeRef(typeParam)), arbitraryValueSupplier(typeParam) @@ -631,16 +632,21 @@ public class JavaTypesGenerator implements Generator { private JavaExpression arbitraryValue( TypedTypeLevelExpressionNode type, + Set typeParamsUsedInArbitraryValue, Context context ) { - return arbitraryValue(type.value(), context); + return arbitraryValue(type.value(), typeParamsUsedInArbitraryValue, context); } - private JavaExpression arbitraryValue(Type type, Context context) { + private JavaExpression arbitraryValue( + Type type, + Set typeParamsUsedInArbitraryValue, + Context context + ) { // TODO: unify logic for constructed types. return switch (this.javaGenerator.collapseType(type)) { case ConstructedNativeType constructedType -> { - yield arbitraryValue(constructedType.constructor().genericType(), context); + yield arbitraryValue(constructedType.constructor().genericType(), typeParamsUsedInArbitraryValue, context); } case ConstructedStructType constructedType -> { @@ -652,7 +658,7 @@ public class JavaTypesGenerator implements Generator { .map(typeArg -> new JavaLambdaExpression( List.of(), new JavaBlock(List.of( - new JavaReturn(arbitraryValue(typeArg, context)) + new JavaReturn(arbitraryValue(typeArg, typeParamsUsedInArbitraryValue, context)) )) )) .toList() @@ -693,7 +699,7 @@ public class JavaTypesGenerator implements Generator { case SumType sumType -> { var firstVariant = selectArbitraryVariant(sumType, context.typesInfo); - yield arbitraryValue(firstVariant, context); + yield arbitraryValue(firstVariant, typeParamsUsedInArbitraryValue, context); } case TypeLevelValueType typeLevelValueType -> { @@ -701,6 +707,7 @@ public class JavaTypesGenerator implements Generator { } case TypeParam typeParam -> { + typeParamsUsedInArbitraryValue.add(typeParam); yield new JavaMethodCall( new JavaRef(arbitraryValueSupplier(typeParam)), JavaIdentifier.of("get"), -- cgit v1.2.3