Fix replacements of types nested within nested constraints (#1731)

Also, adjust tests
This commit is contained in:
Daniel Chao authored and GitHub committed 2026-07-06 08:58:56 -07:00
1 parent 26c115aea6
commit 2ec83198f5
5 files changed
+32 -34

No files matched your search

@@ -39,7 +39,7 @@ public abstract class UnresolvedTypeNode extends PklNode {
private final VmLanguage language;
@Child UnresolvedTypeNode childNode;
TypeConstraintNode[] constraintCheckNodes;
@Children TypeConstraintNode[] constraintCheckNodes;
public Constrained(
SourceSection sourceSection,
@@ -184,7 +184,7 @@ public final class VmTypeAlias extends VmValue {
// * Fewer root nodes to call
// * ControlFlowException used to implement union types doesn't escape root node
if (typeParameters.isEmpty()) return (TypeNode) typeNode.deepCopy();
if (typeParameters.isEmpty()) return deepCopy(typeNode);
// handle if alias root is itself a type variable (https://github.com/apple/pkl/issues/1711)
if (typeNode instanceof TypeVariableNode typeVarNode) {
@@ -198,13 +198,11 @@ public final class VmTypeAlias extends VmValue {
clone.accept(
node -> {
if (node instanceof TypeVariableNode typeVarNode) {
int index = typeVarNode.getTypeParameterIndex();
// should not need to clone type argument node because it is not used by its original
// root node
var index = typeVarNode.getTypeParameterIndex();
node.replace(
typeArgumentNodes.length == 0
? new UnknownTypeNode(sourceSection)
: typeArgumentNodes[index]);
: deepCopy(typeArgumentNodes[index]));
} else if (node instanceof UnresolvedTypeNode.TypeVariable unresolvedTypeVar) {
// Type variables inside constraint expressions (e.g. `every((it) -> it is T)`)
// are still unresolved at instantiation time. Replace them with a resolved
@@ -213,7 +211,8 @@ public final class VmTypeAlias extends VmValue {
node.replace(
typeArgumentNodes.length == 0
? new UnresolvedTypeNode.Unknown(sourceSection)
: new UnresolvedTypeNode.Resolved(sourceSection, typeArgumentNodes[index]));
: new UnresolvedTypeNode.Resolved(
sourceSection, deepCopy(typeArgumentNodes[index])));
}
return true;
});
@@ -311,4 +310,8 @@ public final class VmTypeAlias extends VmValue {
public String toString() {
return qualifiedName.startsWith("pkl.base#") ? simpleName : qualifiedName;
}
private static TypeNode deepCopy(TypeNode typeArgumentNode) {
return (TypeNode) typeArgumentNode.deepCopy();
}
}