Correctly type check Class<T> (#1698)

This commit is contained in:
Jen Basch authored and GitHub committed 2026-08-26 05:29:34 +00:00
1 parent a90eb74c2d
commit cb01d6275b
39 files changed
+503 -13

No files matched your search

@@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.jspecify.annotations.NonNull;
@@ -61,7 +62,8 @@ import org.pkl.core.util.MutableReference;
public abstract class TypeNode extends PklNode {
public interface ClassTypeNode {
/** Type node that corresponds to a user-defined class (or module class). */
public interface UserClassTypeNode {
VmClass getVmClass();
}
@@ -410,8 +412,7 @@ public abstract class TypeNode extends PklNode {
}
/** The `module` type for a final module. */
public static final class FinalModuleTypeNode extends ObjectSlotTypeNode
implements ClassTypeNode {
public static final class FinalModuleTypeNode extends ObjectSlotTypeNode {
private final VmClass moduleClass;
public FinalModuleTypeNode(SourceSection sourceSection, VmClass moduleClass) {
@@ -465,8 +466,7 @@ public abstract class TypeNode extends PklNode {
}
/** The `module` type for an open module. */
public static final class NonFinalModuleTypeNode extends ObjectSlotTypeNode
implements ClassTypeNode {
public static final class NonFinalModuleTypeNode extends ObjectSlotTypeNode {
private final VmClass moduleClass; // only used by getVmClass()
@Child private ExpressionNode getModuleNode;
@@ -651,7 +651,8 @@ public abstract class TypeNode extends PklNode {
* String/Boolean/Int/Float and their supertypes, only `VmValue`s can possibly pass its type
* check.
*/
public static final class FinalClassTypeNode extends ObjectSlotTypeNode implements ClassTypeNode {
public static final class FinalClassTypeNode extends ObjectSlotTypeNode
implements UserClassTypeNode {
private final VmClass clazz;
public FinalClassTypeNode(SourceSection sourceSection, VmClass clazz) {
@@ -708,7 +709,7 @@ public abstract class TypeNode extends PklNode {
* check.
*/
public abstract static class NonFinalClassTypeNode extends ObjectSlotTypeNode
implements ClassTypeNode {
implements UserClassTypeNode {
protected final VmClass clazz;
public NonFinalClassTypeNode(SourceSection sourceSection, VmClass clazz) {
@@ -3164,6 +3165,97 @@ public abstract class TypeNode extends PklNode {
}
}
public abstract static class ClassClassTypeNode extends ObjectSlotTypeNode {
@Child private TypeNode typeNode;
@CompilationFinal private boolean initialized = false;
@CompilationFinal private @Nullable VmClass clazz = null;
public ClassClassTypeNode(SourceSection sourceSection, TypeNode typeNode) {
super(sourceSection);
this.typeNode = typeNode;
}
private void initVmClass() {
if (initialized) return;
CompilerDirectives.transferToInterpreterAndInvalidate();
initialized = true;
var node = typeNode;
while (node instanceof TypeAliasTypeNode typeAliasTypeNode) {
node = typeAliasTypeNode.getAliasedTypeNode();
}
if (node instanceof UnknownTypeNode || node instanceof TypeVariableNode) {
clazz = BaseModule.getAnyClass();
} else if (!node.isParametric()) {
clazz = node.getVmClass();
}
}
@Override
public VmClass getVmClass() {
return BaseModule.getClassClass();
}
@Specialization
protected Object eval(VmClass value) {
// safe to init clazz here (instead of on init and typealias instantiate)
// because in the typealias case this node will never execute prior to instantiation
initVmClass();
// Fast path: all classes match Class<Any> / Class<unknown> / Class<type arg>.
// In this case, skip the subclass check and behave like a bare `Class` type annotation.
if (clazz == BaseModule.getAnyClass()) {
return value;
}
// clazz will be null iff the type arg is a not a valid class type
if (clazz == null) {
throw new VmTypeMismatchException.ClassType(sourceSection, value, typeNode.doExport());
}
if (!value.isSubclassOf(clazz)) {
throw new VmTypeMismatchException.ClassType(sourceSection, value, clazz);
}
return value;
}
@Fallback
protected Object fallback(Object value) {
throw typeMismatch(value, BaseModule.getClassClass());
}
@Override
protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {
if (visitTypeArguments) {
return consumer.accept(this) && typeNode.acceptTypeNode(visitTypeArguments, consumer);
}
return consumer.accept(this);
}
@Override
protected boolean doIsEquivalentTo(TypeNode other) {
if (!(other instanceof ClassClassTypeNode classClassTypeNode)) {
return false;
}
return Objects.equals(clazz, classClassTypeNode.clazz);
}
@Override
public VmList getTypeArgumentMirrors() {
return VmList.of(typeNode.getMirror());
}
@Override
protected PType doExport() {
return new PType.Class(BaseModule.getClassClass().export(), typeNode.doExport());
}
}
public abstract static class ValidatingObjectSlotTypeNode extends ObjectSlotTypeNode {
protected ValidatingObjectSlotTypeNode(SourceSection sourceSection) {
@@ -278,9 +278,8 @@ public abstract class UnresolvedTypeNode extends PklNode {
return FunctionNClassTypeNodeGen.create(sourceSection, resolvedTypeArgumentNodes);
}
// erase `x: Class<Foo>` to `x: Class` for now (cf. function types)
if (clazz.isClassClass()) {
return new FinalClassTypeNode(sourceSection, clazz);
return ClassClassTypeNodeGen.create(sourceSection, typeArgumentNodes[0].execute(frame));
}
if (clazz.isVarArgsClass()) {
@@ -169,6 +169,69 @@ public abstract class VmTypeMismatchException extends ControlFlowException {
}
}
public static final class ClassType extends VmTypeMismatchException {
private final String renderedExpected;
private final @Nullable VmClass expectedClass;
public ClassType(SourceSection sourceSection, VmClass actualClass, VmClass expectedClass) {
super(sourceSection, actualClass);
this.expectedClass = expectedClass;
renderedExpected = "Class<" + expectedClass + ">";
}
public ClassType(SourceSection sourceSection, VmClass actualClass, PType expectedType) {
super(sourceSection, actualClass);
this.expectedClass = null;
renderedExpected = "Class<" + expectedType + ">";
}
@Override
@TruffleBoundary
public void buildMessage(
AnsiStringBuilder builder, String indent, boolean withPowerAssertions) {
var actualClass = (VmClass) actualValue;
var renderedActualClass = "Class<" + actualClass + ">";
// give better error than "expected Class<foo.Bar>, but got Class<foo.Bar>" in case of naming
// conflict
if (expectedClass != null
&& actualClass.getQualifiedName().equals(expectedClass.getQualifiedName())) {
var actualModuleUri = actualClass.getModule().getModuleInfo().getModuleKey().getUri();
var expectedModuleUri = expectedClass.getModule().getModuleInfo().getModuleKey().getUri();
builder
.append(
ErrorMessages.createIndented(
actualClass.getPClassInfo().isModuleClass()
? "typeMismatchVersionConflict1"
: "typeMismatchVersionConflict2",
indent,
renderedExpected,
expectedModuleUri,
actualModuleUri))
.append("\n");
return;
}
builder.append(
ErrorMessages.createIndented(
"typeMismatch", indent, renderedExpected, renderedActualClass));
}
@Override
protected Boolean hasHint() {
return expectedClass == null;
}
@Override
@TruffleBoundary
public void buildHint(AnsiStringBuilder builder, String indent, boolean withPowerAssertions) {
if (expectedClass != null) return;
builder.append(ErrorMessages.createIndented("classTypeMismatchHint", indent));
}
}
public static final class Constraint extends VmTypeMismatchException {
private final SourceSection constraintBodySourceSection;
@@ -173,7 +173,7 @@ public final class CommandSpecParser {
if (optionsTypeNode instanceof TypeNode.TypedTypeNode) {
return BaseModule.getTypedClass();
}
if (!(optionsTypeNode instanceof TypeNode.ClassTypeNode node)) {
if (!(optionsTypeNode instanceof TypeNode.UserClassTypeNode node)) {
throw exceptionBuilder()
.withSourceSection(optionsTypeNode.getSourceSection())
.evalError(
@@ -358,6 +358,9 @@ Expected value of type `{0}`, but got a different `{1}`.
typeMismatchBecause=\
* Value is not of type `{0}` because:
classTypeMismatchHint=\
A `Class` type check can only succeed when its type argument is an un-parameterized class, `unknown`, `module`, or an alias to one of those types.
typeConstraintViolated=\
Type constraint `{0}` violated.