mirror of
https://github.com/apple/pkl.git
synced 2026-07-10 23:22:46 +02:00
Correctly handle type aliases with a type variable as the root (#1714)
This commit is contained in:
@@ -182,10 +182,18 @@ public final class VmTypeAlias extends VmValue {
|
|||||||
// which should be good for interpreted and compiled performance alike:
|
// which should be good for interpreted and compiled performance alike:
|
||||||
// * Fewer root nodes to call
|
// * Fewer root nodes to call
|
||||||
// * ControlFlowException used to implement union types doesn't escape root node
|
// * 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();
|
var clone = (TypeNode) typeNode.deepCopy();
|
||||||
|
|
||||||
if (typeParameters.isEmpty()) return clone;
|
|
||||||
|
|
||||||
clone.accept(
|
clone.accept(
|
||||||
node -> {
|
node -> {
|
||||||
if (node instanceof TypeVariableNode typeVarNode) {
|
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)
|
||||||
Reference in New Issue
Block a user