Improve evaluation of typealiases (#1796)

This simplifies the resolution of names in typealias constraints.
This removes the existing logic around swapping out the frame's
owner/receiver, and instead favors resolving the variable at parse time.

* During variable resolution, create read variable nodes that read off of
  a receiver node
* Introduce `GetTypeAliasModuleNode` that provides the typealias's
  enclosing module
* Inject the enclosing module during typealias instantiation
This commit is contained in:
Daniel Chao
2026-07-30 17:22:45 +00:00
committed by GitHub
parent 2ca2aaf9ec
commit a55a3d7c33
39 changed files with 634 additions and 139 deletions
@@ -0,0 +1 @@
typealias Id<T> = T
@@ -0,0 +1 @@
name: String = "base"
@@ -0,0 +1,9 @@
amends "typeAliasAmendBase.pkl"
local const function isValid(s: String): Boolean = s.startsWith("Bob")
local typealias ValidString4 = String(isValid(this))
local myProp: ValidString4 = "Bob Marley"
name = myProp
@@ -0,0 +1,3 @@
const function isValid(s: String): Boolean = s.startsWith("Bob")
typealias ValidString2 = String(isValid(this))
@@ -0,0 +1,3 @@
local const function isValid(s: String): Boolean = s.startsWith("Bob")
typealias ValidString3 = String(isValid(this))
@@ -0,0 +1 @@
typealias MyModule = module
@@ -0,0 +1,5 @@
open module typeAliasOpenModule
const function isValid(s: String): Boolean = s.startsWith("Bob")
typealias ValidString = String(isValid(this))
@@ -0,0 +1,3 @@
const name = "Bob"
typealias Inner = String(startsWith(name))
@@ -0,0 +1,5 @@
import "typealias1.pkl"
const name = "Alice"
typealias Outer = typealias1.Inner
@@ -0,0 +1,8 @@
import "../../input-helper/types/idType.pkl"
hidden modules: Listing<idType.Id<module>> = new {
new module {}
new module {}
}
res = modules.length
@@ -0,0 +1,3 @@
import "../../input-helper/types/typeAliasModule.pkl"
res: typeAliasModule = module
@@ -0,0 +1,3 @@
import "../../input-helper/types/typealias2.pkl"
myProp: typealias2.Outer = "Alice Jenkins"
@@ -0,0 +1,5 @@
import "../../input-helper/types/typeAliasOpenModule.pkl"
const function isValid(s: String): Boolean = s.startsWith("Alice")
myProp: typeAliasOpenModule.ValidString = "Alice Jenkins"
@@ -0,0 +1,5 @@
import "../../input-helper/types/typeAliasClosedModule.pkl"
const function isValid(s: String): Boolean = s.startsWith("Alice")
myProp: typeAliasClosedModule.ValidString2 = "Alice Jenkins"
@@ -0,0 +1,5 @@
import "../../input-helper/types/typeAliasLocalMethod.pkl"
local const function isValid(s: String): Boolean = s.startsWith("Alice")
myProp: typeAliasLocalMethod.ValidString3 = "Alice Jenkins"
@@ -0,0 +1,5 @@
import "../../input-helper/types/typeAliasAmendModule.pkl"
local const function isValid(s: String): Boolean = s.startsWith("Alice")
res = typeAliasAmendModule.name
@@ -0,0 +1 @@
res = 2
@@ -0,0 +1,15 @@
–– Pkl Error ––
Expected value of type `typeAliasModule`, but got type `typeAlias6`.
Value: new ModuleClass { res = ? }
x | res: typeAliasModule = module
^^^^^^^^^^^^^^^
at typeAlias6#res (file:///$snippetsDir/input/types/typeAlias6.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)
@@ -0,0 +1,23 @@
–– Pkl Error ––
Type constraint `startsWith(name)` violated.
Value: "Alice Jenkins"
startsWith(name)
│ │
false "Bob"
x | typealias Inner = String(startsWith(name))
^^^^^^^^^^^^^^^^
at typeAliasConstraint4#myProp (file:///$snippetsDir/input-helper/types/typealias1.pkl)
x | myProp: typealias2.Outer = "Alice Jenkins"
^^^^^^^^^^^^^^^
at typeAliasConstraint4#myProp (file:///$snippetsDir/input/types/typeAliasConstraint4.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)
@@ -0,0 +1,23 @@
–– Pkl Error ––
Type constraint `isValid(this)` violated.
Value: "Alice Jenkins"
isValid(this)
│ │
false "Alice Jenkins"
x | typealias ValidString = String(isValid(this))
^^^^^^^^^^^^^
at typeAliasConstraint5#myProp (file:///$snippetsDir/input-helper/types/typeAliasOpenModule.pkl)
x | myProp: typeAliasOpenModule.ValidString = "Alice Jenkins"
^^^^^^^^^^^^^^^
at typeAliasConstraint5#myProp (file:///$snippetsDir/input/types/typeAliasConstraint5.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)
@@ -0,0 +1,23 @@
–– Pkl Error ––
Type constraint `isValid(this)` violated.
Value: "Alice Jenkins"
isValid(this)
│ │
false "Alice Jenkins"
x | typealias ValidString2 = String(isValid(this))
^^^^^^^^^^^^^
at typeAliasConstraint6#myProp (file:///$snippetsDir/input-helper/types/typeAliasClosedModule.pkl)
x | myProp: typeAliasClosedModule.ValidString2 = "Alice Jenkins"
^^^^^^^^^^^^^^^
at typeAliasConstraint6#myProp (file:///$snippetsDir/input/types/typeAliasConstraint6.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)
@@ -0,0 +1,23 @@
–– Pkl Error ––
Type constraint `isValid(this)` violated.
Value: "Alice Jenkins"
isValid(this)
│ │
false "Alice Jenkins"
x | typealias ValidString3 = String(isValid(this))
^^^^^^^^^^^^^
at typeAliasConstraint7#myProp (file:///$snippetsDir/input-helper/types/typeAliasLocalMethod.pkl)
x | myProp: typeAliasLocalMethod.ValidString3 = "Alice Jenkins"
^^^^^^^^^^^^^^^
at typeAliasConstraint7#myProp (file:///$snippetsDir/input/types/typeAliasConstraint7.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)
@@ -0,0 +1 @@
res = "Bob Marley"