Fix interactions between method type parameters and for generators (#2022)

This commit is contained in:
Jen Basch authored and GitHub committed 2026-12-02 23:09:43 +00:00
1 parent 654ae454a2
commit 9af2385e37
7 files changed
+157 -26

No files matched your search

@@ -207,6 +207,7 @@ import org.pkl.core.runtime.VmUtils;
import org.pkl.core.stdlib.LanguageAwareNode;
import org.pkl.core.stdlib.registry.ExternalMemberRegistry;
import org.pkl.core.stdlib.registry.MemberRegistryFactory;
import org.pkl.core.util.ArrayUtils;
import org.pkl.core.util.CollectionUtils;
import org.pkl.core.util.EconomicMaps;
import org.pkl.core.util.IoUtils;
@@ -1364,7 +1365,8 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
frameDescriptorBuilder.addSlot(
FrameSlotKind.Illegal,
toIdentifier(par.getIdentifier().getValue()),
LetExpressionScope.LET_BINDING_SLOT);
LetExpressionScope.LET_BINDING_SLOT,
false);
}
var bindingExpr = visitExpr(letExpr.getBindingExpr());
var t = typeNode;
@@ -1612,12 +1614,11 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
return symbolTable
.getCurrentScope()
.frameDescriptorBuilder
.addSlot(FrameSlotKind.Illegal, toIdentifier(name), null);
.addSlot(FrameSlotKind.Illegal, toIdentifier(name), null, false);
}
@Override
public GeneratorMemberNode visitForGenerator(ForGenerator ctx) {
var keyParameter = ctx.getP2() == null ? null : ctx.getP1();
var valueParameter = ctx.getP2() == null ? ctx.getP1() : ctx.getP2();
var keyBinding = makeBinding(keyParameter);
@@ -1640,23 +1641,31 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
.build();
}
var unresolvedKeyTypeNode =
keyIdentifier == null ? null : visitTypeAnnotation(keyIdentifier.getTypeAnnotation());
var unresolvedValueTypeNode =
valueIdentifier == null ? null : visitTypeAnnotation(valueIdentifier.getTypeAnnotation());
var header =
symbolTable.enterEagerGenerator(
scope ->
new ForGeneratorHeader(
keyIdentifier == null
? null
: visitTypeAnnotation(keyIdentifier.getTypeAnnotation()),
valueIdentifier == null
? null
: visitTypeAnnotation(valueIdentifier.getTypeAnnotation()),
visitExpr(ctx.getExpr())));
// if possible, initialize immediately to avoid later insert
var keyTypeNode =
unresolvedKeyTypeNode == null && keyBinding != null
header.unresolvedKeyTypeNode == null && keyBinding != null
? new TypeNode.UnknownTypeNode(VmUtils.unavailableSourceSection())
.initWriteSlotNode(keyBinding.slot())
: null;
// if possible, initialize immediately to avoid later insert
var valueTypeNode =
unresolvedValueTypeNode == null && valueBinding != null
header.unresolvedValueTypeNode == null && valueBinding != null
? new TypeNode.UnknownTypeNode(VmUtils.unavailableSourceSection())
.initWriteSlotNode(valueBinding.slot())
: null;
var iterableNode = symbolTable.enterEagerGenerator(scope -> visitExpr(ctx.getExpr()));
var outerScope = symbolTable.getCurrentScope();
return symbolTable.enterForGenerator(
keyBinding,
@@ -1667,9 +1676,9 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
return GeneratorForNodeGen.create(
createSourceSection(ctx),
scope.frameDescriptorBuilder.build(),
iterableNode,
unresolvedKeyTypeNode,
unresolvedValueTypeNode,
header.iterableNode,
header.unresolvedKeyTypeNode,
header.unresolvedValueTypeNode,
memberNodes,
keyTypeNode,
valueTypeNode,
@@ -2200,16 +2209,15 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
var descriptorBuilder = descriptorBuilderAndBindings.builder;
var bindings = descriptorBuilderAndBindings.bindings;
var typeParameters =
visitTypeParameterList(
entry.getTypeParameterList(), descriptorBuilderAndBindings.typeBindings);
var typeBindings = descriptorBuilderAndBindings.typeBindings;
var typeParameters = visitTypeParameterList(entry.getTypeParameterList(), typeBindings);
var annotations = doVisitAnnotations(entry.getAnnotations(), methodName);
return symbolTable.enterMethod(
methodName,
getConstLevel(modifiers),
bindings,
ArrayUtils.concat(bindings, typeBindings),
descriptorBuilder,
typeParameters,
scope -> {
@@ -2889,13 +2897,13 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
var frameDescriptorBuilder = frameDescriptorBuilderAndBindings.builder;
var bindings = frameDescriptorBuilderAndBindings.bindings;
var typeParameters =
visitTypeParameterList(typeParamList, frameDescriptorBuilderAndBindings.typeBindings);
var typeBindings = frameDescriptorBuilderAndBindings.typeBindings;
var typeParameters = visitTypeParameterList(typeParamList, typeBindings);
return symbolTable.enterMethod(
methodName,
getConstLevel(modifiers),
bindings,
ArrayUtils.concat(bindings, typeBindings),
frameDescriptorBuilder,
typeParameters,
scope -> {
@@ -3227,7 +3235,8 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
} else {
identifier = org.pkl.core.runtime.Identifier.ILLEGAL;
}
slotVariables[i] = frameDescriptorBuilder.addSlot(FrameSlotKind.Illegal, identifier, null);
slotVariables[i] =
frameDescriptorBuilder.addSlot(FrameSlotKind.Illegal, identifier, null, false);
}
return slotVariables;
}
@@ -3240,7 +3249,7 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
var param = typeParams.get(i);
slotVariables[i] =
frameDescriptorBuilder.addSlot(
FrameSlotKind.Illegal, toIdentifier(param.getIdentifier().getValue()), null);
FrameSlotKind.Object, toIdentifier(param.getIdentifier().getValue()), null, true);
}
return slotVariables;
}
@@ -3446,4 +3455,9 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
}
public record ArgInfo(ExpressionNode[] arguments, int methodSlot) {}
private record ForGeneratorHeader(
@Nullable UnresolvedTypeNode unresolvedKeyTypeNode,
@Nullable UnresolvedTypeNode unresolvedValueTypeNode,
ExpressionNode iterableNode) {}
}
@@ -686,6 +686,8 @@ public final class SymbolTable {
return new VariableResolution.LexicalProperty(false, prop.modifiers, levelsUp);
}
for (var binding : bindings) {
// not possible for an object body to declare type parameters
assert !binding.isTypeParameter();
if (binding.name().equals(name)) {
// params are on a higher level than the properties
return new VariableResolution.Parameter(binding.slot(), levelsUp + 1);
@@ -1180,7 +1182,7 @@ public final class SymbolTable {
private static @Nullable VariableResolution resolveParameter(
String name, FrameSlotVariable[] bindings, int levelsUp) {
for (var binding : bindings) {
if (name.equals(binding.name())) {
if (!binding.isTypeParameter() && name.equals(binding.name())) {
return new Parameter(binding.slot(), levelsUp);
}
}
@@ -51,13 +51,14 @@ public class FrameDescriptorBuilder {
}
}
public FrameSlotVariable addSlot(FrameSlotKind kind, Identifier name, @Nullable Object info) {
public FrameSlotVariable addSlot(
FrameSlotKind kind, Identifier name, @Nullable Object info, boolean isTypeParameter) {
ensureCapacity(1);
names[size] = name;
size++;
state++;
var slot = underlying.addSlot(kind, name, info);
return new FrameSlotVariable(name.toString(), slot);
return new FrameSlotVariable(name.toString(), slot, isTypeParameter);
}
/**
@@ -15,4 +15,4 @@
*/
package org.pkl.core.runtime;
public record FrameSlotVariable(String name, int slot) {}
public record FrameSlotVariable(String name, int slot, boolean isTypeParameter) {}
@@ -47,4 +47,10 @@ public final class ArrayUtils {
System.arraycopy(array2, 0, result, array1.length, array2.length);
return result;
}
public static <T> T[] concat(T[] array1, T[] array2) {
var result = Arrays.copyOf(array1, array1.length + array2.length);
System.arraycopy(array2, 0, result, array1.length, array2.length);
return result;
}
}
@@ -0,0 +1,82 @@
import "pkl:test"
function key<T>() = new Mapping {
["a" as T] = 1
}
function forValue<T>() = new Mapping {
for (i in List(1)) {
[i] = "a" as T
}
}
function forKey<T>() = new Mapping {
for (i in List(1)) {
["a" as T] = i
}
}
function forWhen<T>() = new Listing {
for (i in List(1)) {
when ("a" is T) {
"is T"
} else {
"is not T"
}
}
}
function forAnnotation<T>() = new Listing {
for (i: T in List(1)) { i }
}
function forNested<T>() = new Mapping {
for (a in List(1, 2)) {
for (b in List("x")) {
[b as T] = a
}
}
}
function forNestedAnnotation<T>() = new Listing {
for (a in List(1)) {
for (b: T in List(a)) { b }
}
}
function forLambda<T>() = new Listing {
for (i in List(1)) {
List("a").map((x) -> x as T).first
}
}
function forIterable<T>() = new Listing {
// noinspection ReplaceForGeneratorWithSpread
for (i in List("a" as T)) { i }
}
function forKeyAnnotation<T>() = new Mapping {
for (k: T, v in Map(1, "a")) {
[k] = v
}
}
function forTwoTypeParams<K, V>() = new Mapping {
for (k: K, v: V in Map(1, "a")) {
[k] = v
}
}
a = test.catch(() -> key<Int>())
b = test.catch(() -> forValue<Int>())
c = test.catch(() -> forKey<Int>())
d = test.catch(() -> forKey<String(length > 5)>())
e = forWhen<Int>()
f = test.catch(() -> forAnnotation<String>())
g = test.catch(() -> forNested<Int>())
h = test.catch(() -> forNestedAnnotation<String>())
i = forNestedAnnotation<Int>()
j = test.catch(() -> forLambda<Int>())
k = forLambda<String>()
l = test.catch(() -> forIterable<Int>())
m = forIterable<String>()
n = test.catch(() -> forKeyAnnotation<String>())
o = forTwoTypeParams<Int, String>()
p = test.catch(() -> forTwoTypeParams<String, Int>())
@@ -0,0 +1,26 @@
a = "Expected value of type `Int`, but got type `String`. Value: \"a\""
b = "Expected value of type `Int`, but got type `String`. Value: \"a\""
c = "Expected value of type `Int`, but got type `String`. Value: \"a\""
d = "Type constraint `length > 5` violated. Value: \"a\""
e {
"is not T"
}
f = "Expected value of type `String`, but got type `Int`. Value: 1"
g = "Expected value of type `Int`, but got type `String`. Value: \"x\""
h = "Expected value of type `String`, but got type `Int`. Value: 1"
i {
1
}
j = "Expected value of type `Int`, but got type `String`. Value: \"a\""
k {
"a"
}
l = "Expected value of type `Int`, but got type `String`. Value: \"a\""
m {
"a"
}
n = "Expected value of type `String`, but got type `Int`. Value: 1"
o {
[1] = "a"
}
p = "Expected value of type `String`, but got type `Int`. Value: 1"