Add support for const object members (#678)

This adds support for adding the `const` modifier to object members.

Such object members are also required to have the `local` modifier applied.

This follows SPICE-0011.
This commit is contained in:
Daniel Chao
2024-10-21 22:00:12 -07:00
committed by GitHub
parent 5057bb5b17
commit 0ee3d37524
13 changed files with 104 additions and 5 deletions
@@ -0,0 +1,5 @@
module Birds2
abstract class Bird
bird: Bird
@@ -0,0 +1,15 @@
const baz = 15
foo {
const local bar = baz
const local bar2 = biz()
const local function biz() = baz
qux = bar
corge = biz()
corge2 = bar2
}
@@ -0,0 +1,9 @@
amends ".../input-helper/modules/Birds2.pkl"
const local myBirdName = "Birdy"
local class MyBird extends Bird {
name: String = myBirdName
}
bird = new MyBird {}
@@ -0,0 +1,7 @@
bar = 15
obj {
const local function foo() = bar
res = foo()
}
@@ -0,0 +1,5 @@
foo {
res1 = 15
const local qux = res1
res2 = qux
}
@@ -0,0 +1,6 @@
baz = 15
foo {
qux = 15
corge = 15
corge2 = 15
}
@@ -0,0 +1,3 @@
bird {
name = "Birdy"
}
@@ -0,0 +1,18 @@
–– Pkl Error ––
Cannot reference property `bar` from here because it is not `const`.
x | const local function foo() = bar
^^^
at constLocalMethod#obj.foo (file:///$snippetsDir/input/errors/const/constLocalMethod.pkl)
To fix, do either of:
1. Add modifier `const` to property `bar`
2. Self-import this module, and reference this property from the import.
x | res = foo()
^^^^^
at constLocalMethod#obj.res (file:///$snippetsDir/input/errors/const/constLocalMethod.pkl)
xxx | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (pkl:base)
@@ -0,0 +1,18 @@
–– Pkl Error ––
Cannot reference property `res1` from here because it is not `const`.
x | const local qux = res1
^^^^
at constLocalProperty#foo.qux (file:///$snippetsDir/input/errors/const/constLocalProperty.pkl)
To fix, do either of:
1. Add modifier `const` to property `res1`
2. Self-import this module, and reference this property from the import.
x | res2 = qux
^^^
at constLocalProperty#foo.res2 (file:///$snippetsDir/input/errors/const/constLocalProperty.pkl)
xxx | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (pkl:base)
@@ -1,5 +1,5 @@
–– Pkl Error ––
Modifier `const` is not applicable to object members.
Modifier `const` can only be applied to object members that are also `local`.
x | const foo = 10
^^^^^