diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-10 15:42:31 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-10 17:24:24 +0100 |
| commit | c386126bdb87705c73379b3e6678680b4c0253cd (patch) | |
| tree | 92e0af809b2c4f5323fc998432ffb902df06fcbf /src/main | |
| parent | 31e56d743cdca4d6ba0b3f6050351c2c5e3be7e9 (diff) | |
Filter unused type params from arbitrary value function
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java | 21 |
1 files changed, 14 insertions, 7 deletions
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<TypeParam>(); 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<SimpleStructType> 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> type, + Set<TypeParam> 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<TypeParam> 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 { .<JavaExpression>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"), |
