Correctly handle type aliases with a type variable as the root (#1714)

This commit is contained in:
Jen Basch
2026-06-29 23:49:21 -07:00
committed by GitHub
parent 139d1ae8ec
commit 70fc1d4ba3
5 changed files with 35 additions and 3 deletions
@@ -182,10 +182,18 @@ public final class VmTypeAlias extends VmValue {
// which should be good for interpreted and compiled performance alike:
// * Fewer root nodes to call
// * ControlFlowException used to implement union types doesn't escape root node
if (typeParameters.isEmpty()) return (TypeNode) typeNode.deepCopy();
// handle if alias root is itself a type variable (https://github.com/apple/pkl/issues/1711)
if (typeNode instanceof TypeVariableNode typeVarNode) {
// no need to run validation since the arg itself has already been checked
return typeArgumentNodes.length == 0
? new UnknownTypeNode(sourceSection)
: typeArgumentNodes[typeVarNode.getTypeParameterIndex()];
}
var clone = (TypeNode) typeNode.deepCopy();
if (typeParameters.isEmpty()) return clone;
clone.accept(
node -> {
if (node instanceof TypeVariableNode typeVarNode) {
@@ -0,0 +1,2 @@
typealias Id<T> = T
foo: Id<Int> = 1
@@ -0,0 +1,2 @@
typealias Id<T> = T
foo: Id<Int> = "oops"
@@ -0,0 +1 @@
foo = 1
@@ -0,0 +1,19 @@
–– Pkl Error ––
Expected value of type `Int`, but got type `String`.
Value: "oops"
x | foo: Id<Int> = "oops"
^^^
at typeAlias3a#foo (file:///$snippetsDir/input/types/typeAlias3a.pkl)
x | foo: Id<Int> = "oops"
^^^^^^
at typeAlias3a#foo (file:///$snippetsDir/input/types/typeAlias3a.pkl)
xxx | renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (pkl:base)
xxx | if (renderer is BytesRenderer) renderer.renderDocument(value) else text.encodeToBytes("UTF-8")
^^^^
at pkl.base#Module.output.bytes (pkl:base)