Fix enforcement of const member access inside methods (#1945)

This commit is contained in:
Daniel Chao authored and GitHub committed 2026-10-30 16:57:49 +00:00
1 parent ec2d12a654
commit 99c2d84c43
7 files changed
+84 -2

No files matched your search

@@ -357,8 +357,9 @@ public final class SymbolTable {
var depth = -1;
var lexicalScope = getLexicalScope();
while (lexicalScope.getConstLevel() == ConstLevel.ALL) {
// LambdaScope inherits constLevel but doesn't create a const scope barrier
if (!(lexicalScope instanceof LambdaScope)) {
// LambdaScope inherits constLevel but doesn't create a const scope barrier.
// Method scopes don't level up.
if (!(lexicalScope instanceof LambdaScope) && !(lexicalScope instanceof MethodScope)) {
depth += 1;
}
var parent = lexicalScope.getParent();
@@ -0,0 +1,5 @@
x = 1
const function f() = x
r = f()
@@ -0,0 +1,5 @@
obj {
x = 1
local const function f() = x
r = f()
}
@@ -0,0 +1,5 @@
const function a() = b()
function b() = 3
res = a()
@@ -0,0 +1,22 @@
–– Pkl Error ––
Cannot reference property `x` from here because it is not `const`.
x | const function f() = x
^
at constMethod3#f (file:///$snippetsDir/input/errors/const/constMethod3.pkl)
To fix, do either of:
1. Add modifier `const` to property `x`
2. Self-import this module, and reference this property from the import.
x | r = f()
^^^
at constMethod3#r (file:///$snippetsDir/input/errors/const/constMethod3.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,22 @@
–– Pkl Error ––
Cannot reference property `x` from here because it is not `const`.
x | local const function f() = x
^
at constMethod4#obj.f (file:///$snippetsDir/input/errors/const/constMethod4.pkl)
To fix, do either of:
1. Add modifier `const` to property `x`
2. Self-import this module, and reference this property from the import.
x | r = f()
^^^
at constMethod4#obj.r (file:///$snippetsDir/input/errors/const/constMethod4.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,22 @@
–– Pkl Error ––
Cannot call method `b` from here because it is not `const`.
x | const function a() = b()
^^^
at constMethod5#a (file:///$snippetsDir/input/errors/const/constMethod5.pkl)
To fix, do either of:
1. Add modifier `const` to method `b`
2. Self-import this module, and reference this method from the import.
x | res = a()
^^^
at constMethod5#res (file:///$snippetsDir/input/errors/const/constMethod5.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)