Turn module type usage warnings into errors (#1977)

This commit is contained in:
Jen Basch authored and GitHub committed 2026-11-10 09:59:40 -07:00
1 parent 5b53816463
commit 155fc5a867
39 files changed
+97 -162

No files matched your search

@@ -209,7 +209,6 @@ import org.pkl.core.stdlib.registry.ExternalMemberRegistry;
import org.pkl.core.stdlib.registry.MemberRegistryFactory;
import org.pkl.core.util.CollectionUtils;
import org.pkl.core.util.EconomicMaps;
import org.pkl.core.util.ErrorMessages;
import org.pkl.core.util.IoUtils;
import org.pkl.core.util.Pair;
import org.pkl.parser.Span;
@@ -394,14 +393,20 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
if (currentScope.getConstLevel() == ConstLevel.MODULE) {
for (var scope = currentScope; scope != null; scope = scope.getParent()) {
if (scope.isAnnotationScope()) {
errorMessage = ErrorMessages.create("invalidModuleTypeInAnnotation");
break;
throw exceptionBuilder()
.withSourceSection(sourceSection)
.evalError("invalidModuleTypeInAnnotation")
.build();
} else if (scope.isClassScope()) {
errorMessage = ErrorMessages.create("invalidModuleTypeInClass");
break;
throw exceptionBuilder()
.withSourceSection(sourceSection)
.evalError("invalidModuleTypeInClass")
.build();
} else if (scope.isTypeAliasScope()) {
errorMessage = ErrorMessages.create("invalidModuleTypeInTypeAlias");
break;
throw exceptionBuilder()
.withSourceSection(sourceSection)
.evalError("invalidModuleTypeInTypeAlias")
.build();
}
}
}
@@ -418,18 +423,16 @@ public class AstBuilder extends AbstractAstBuilder<Object> {
}
var message =
scope.isPropertyScope() ? "invalidModuleTypeInProperty" : "invalidModuleTypeInMethod";
errorMessage = ErrorMessages.create(message, scope.getQualifiedName());
break;
throw exceptionBuilder()
.withSourceSection(sourceSection)
.evalError(message, scope.getQualifiedName())
.build();
}
}
}
assert errorMessage != null;
// TODO: when making this an error, update comment on moduleClass in ReferenceTypeNode.eval
VmContext.get(null)
.getLogger()
.warn(
errorMessage + " This will be an error in a future release.",
VmUtils.createStackFrame(sourceSection, null));
// we should always throw a real exception before we get here!
throw PklBugException.unreachableCode();
}
@Override
@@ -1 +0,0 @@
typealias MyModule = module
@@ -18,26 +18,8 @@ res6a: module = res6[0]
function fun(m: module): List<module> = List((m) { x = 2 })
class Bad {
res7: module = "abc"
res8: module = new Person {}
res9: module? = new Person {}
res10: List<module> = List(new Person {})
}
class Person
res7 = test.catch(() -> new Bad {}.res7)
res8 = test.catch(() -> new Bad {}.res8)
res9 = test.catch(() -> new Bad {}.res9)
res10 = test.catch(() -> new Bad {}.res10)
output {
// force eval but don't render to prevent recursion
value =
@@ -48,9 +30,5 @@ output {
let (r5 = res5)
let (r6 = res6)
let (r6a = res6a)
let (r7 = res7)
let (r8 = res8)
let (r9 = res9)
let (r10 = res10)
new Dynamic { result = "ok" }
}
@@ -20,26 +20,8 @@ res6a: module = res6[0]
function fun(m: module): List<module> = List((m) { x = 2 })
class Bad {
res7: module = "abc"
res8: module = new Person {}
res9: module? = new Person {}
res10: List<module> = List(new Person {})
}
class Person
res7 = test.catch(() -> new Bad {}.res7)
res8 = test.catch(() -> new Bad {}.res8)
res9 = test.catch(() -> new Bad {}.res9)
res10 = test.catch(() -> new Bad {}.res10)
output {
// force eval but don't render to prevent recursion
value =
@@ -50,9 +32,5 @@ output {
let (r5 = res5)
let (r6 = res6)
let (r6a = res6a)
let (r7 = res7)
let (r8 = res8)
let (r9 = res9)
let (r10 = res10)
new Dynamic { result = "ok" }
}
@@ -1,21 +0,0 @@
typealias Foo = module
@Baz { valid = true is module }
class Bar {
baz: module
}
class Baz extends Annotation {
valid: Boolean
}
const qux = true is module
const function quux() = true is module
const corge = new {
grault {
garply = true is module
}
}
hidden const waldo = (x: module) -> true
@@ -0,0 +1 @@
typealias Foo = module
@@ -0,0 +1,6 @@
@Baz { valid = true is module }
class Bar
class Baz extends Annotation {
valid: Boolean
}
@@ -0,0 +1,3 @@
class Bar {
baz: module
}
@@ -0,0 +1 @@
const qux = true is module
@@ -0,0 +1 @@
const function quux() = true is module
@@ -0,0 +1,5 @@
const corge = new {
grault {
garply = true is module
}
}
@@ -0,0 +1 @@
hidden const waldo = (x: module) -> true
@@ -4,8 +4,6 @@ import ".../input-helper/types/moduleTypeProperty.pkl"
class ModuleSubclass extends moduleTypeProperty {
x = 2
hidden mod2: module
mod2x = mod2.x
}
x = -1
@@ -1,3 +0,0 @@
import "../../input-helper/types/typeAliasModule.pkl"
res: typeAliasModule = module
@@ -1,5 +0,0 @@
result = "ok"
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType1.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType1.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType1.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType1.pkl)
@@ -0,0 +1 @@
result = "ok"
@@ -1,5 +0,0 @@
result = "ok"
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
@@ -0,0 +1 @@
result = "ok"
@@ -1,5 +0,0 @@
result = "ok"
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType2.pkl)
@@ -0,0 +1 @@
result = "ok"
@@ -1,13 +0,0 @@
qux = false
corge {
grault {
garply = false
}
}
pkl: WARN: Cannot reference `module` type within an annotation body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType4.pkl)
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType4.pkl)
pkl: WARN: Cannot reference `module` type within a type alias body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType4.pkl)
pkl: WARN: Cannot reference `module` type from const property `currentModuleType4#qux`. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType4.pkl)
pkl: WARN: Cannot reference `module` type from const property `currentModuleType4#corge`. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType4.pkl)
pkl: WARN: Cannot reference `module` type from const property `currentModuleType4#waldo`. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType4.pkl)
pkl: WARN: Cannot reference `module` type from const method `currentModuleType4#quux`. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType4.pkl)
@@ -0,0 +1,6 @@
–– Pkl Error ––
Cannot reference `module` type within a type alias body.
x | typealias Foo = module
^^^^^^
at currentModuleType4a#Foo (file:///$snippetsDir/input/types/currentModuleType4a.pkl)
@@ -0,0 +1,6 @@
–– Pkl Error ––
Cannot reference `module` type within an annotation body.
x | @Baz { valid = true is module }
^^^^^^
at currentModuleType4b#Bar.valid (file:///$snippetsDir/input/types/currentModuleType4b.pkl)
@@ -0,0 +1,6 @@
–– Pkl Error ––
Cannot reference `module` type within a class body.
x | baz: module
^^^^^^
at currentModuleType4c#Bar.baz (file:///$snippetsDir/input/types/currentModuleType4c.pkl)
@@ -0,0 +1,6 @@
–– Pkl Error ––
Cannot reference `module` type from const property `currentModuleType4d#qux`.
x | const qux = true is module
^^^^^^
at currentModuleType4d#qux (file:///$snippetsDir/input/types/currentModuleType4d.pkl)
@@ -0,0 +1,6 @@
–– Pkl Error ––
Cannot reference `module` type from const method `currentModuleType4e#quux`.
x | const function quux() = true is module
^^^^^^
at currentModuleType4e#quux (file:///$snippetsDir/input/types/currentModuleType4e.pkl)
@@ -0,0 +1,6 @@
–– Pkl Error ––
Cannot reference `module` type from const property `currentModuleType4f#corge`.
x | garply = true is module
^^^^^^
at currentModuleType4f#corge.grault.garply (file:///$snippetsDir/input/types/currentModuleType4f.pkl)
@@ -0,0 +1,6 @@
–– Pkl Error ––
Cannot reference `module` type from const property `currentModuleType4g#waldo`.
x | hidden const waldo = (x: module) -> true
^^^^^^
at currentModuleType4g#waldo.<function#1> (file:///$snippetsDir/input/types/currentModuleType4g.pkl)
@@ -13,7 +13,5 @@ res3 {
x = 2
y = 2
modName = "currentModuleType6#ModuleSubclass"
mod2x = -1
}
res4 = "Expected value of type `currentModuleType6#ModuleSubclass`, but got type `moduleTypeProperty`. Value: new moduleTypeProperty { x = ?; y = ?; modName = ? }"
pkl: WARN: Cannot reference `module` type within a class body. This will be an error in a future release. (file:///$snippetsDir/input/types/currentModuleType6.pkl)
@@ -1,16 +0,0 @@
pkl: WARN: Cannot reference `module` type within a type alias body. This will be an error in a future release. (file:///$snippetsDir/input-helper/types/typeAliasModule.pkl)
–– Pkl Error ––
Expected value of type `typeAliasModule`, but got type `typeAlias6`.
Value: new typeAlias6 { 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)