Skip to content

Commit 50fe92e

Browse files
committed
refine domain rendering to string, remove default rendering behaviors
1 parent 8fa396a commit 50fe92e

27 files changed

Lines changed: 82 additions & 64 deletions

pkl-core/src/main/java/org/pkl/core/PClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void initSupertype(PType supertype, PClass superclass) {
6060

6161
/**
6262
* Returns the name of the module that this class is declared in. Note that a module name is not
63-
* guaranteed to be unique, especially if it not declared but inferred from the module URI.
63+
* guaranteed to be unique, especially if it is not declared but inferred from the module URI.
6464
*/
6565
public String getModuleName() {
6666
return classInfo.getModuleName();

pkl-core/src/main/java/org/pkl/core/PType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ public String toString() {
125125
}
126126
return result;
127127
}
128+
129+
@Override
130+
public boolean equals(Object obj) {
131+
if (this == obj) return true;
132+
if (!(obj instanceof Class clazz)) return false;
133+
return pClass == clazz.getPClass() && typeArguments.equals(clazz.getTypeArguments());
134+
}
128135
}
129136

130137
public static final class Nullable extends PType {

pkl-core/src/main/java/org/pkl/core/runtime/VmReference.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.pkl.core.runtime;
1717

18-
import com.oracle.truffle.api.nodes.DirectCallNode;
1918
import java.util.ArrayList;
2019
import java.util.Collections;
2120
import java.util.Comparator;
@@ -43,7 +42,8 @@ public final class VmReference extends VmValue {
4342

4443
private boolean forced = false;
4544

46-
private static final PType nullType = new PType.Class(BaseModule.getNullClass().export());
45+
private static final PType.Class nullType = new PType.Class(BaseModule.getNullClass().export());
46+
private static final PType.Class anyType = new PType.Class(BaseModule.getAnyClass().export());
4747
private static final Set<TypeAlias> intAliasTypes = new HashSet<>();
4848
private static final Set<TypeAlias> preservedAliasTypes = new HashSet<>();
4949

@@ -54,14 +54,6 @@ public final class VmReference extends VmValue {
5454
}
5555
}
5656

57-
private static final DirectCallNode toStringCallNode;
58-
59-
static {
60-
var toStringMethod = RefModule.getReferenceClass().getMethod(Identifier.TO_STRING);
61-
assert toStringMethod != null;
62-
toStringCallNode = DirectCallNode.create(toStringMethod.getCallTarget());
63-
}
64-
6557
private static VmTyped newAccess(@Nullable String property, @Nullable Object key) {
6658
return new VmObjectBuilder()
6759
.addProperty(Identifier.PROPERTY, property == null ? VmNull.withoutDefault() : property)
@@ -106,11 +98,13 @@ public List<VmTyped> getPath() {
10698
private static Set<PType> normalizeTypes(PType type, PClass moduleClass) {
10799
var types = new HashSet<PType>();
108100
normalizeTypes(type, moduleClass, types);
101+
if (types.contains(PType.UNKNOWN)) return Set.of(PType.UNKNOWN);
102+
if (containsClass(types, anyType.getPClass())) return Set.of(anyType);
109103
return types;
110104
}
111105

112106
private static void normalizeTypes(PType type, PClass moduleClass, Set<PType> result) {
113-
if (type == PType.UNKNOWN || type instanceof PType.StringLiteral) {
107+
if (type == PType.UNKNOWN || type == PType.NOTHING || type instanceof PType.StringLiteral) {
114108
result.add(type);
115109
} else if (type instanceof PType.Class clazz) {
116110
if (clazz.getTypeArguments().isEmpty()) {
@@ -257,12 +251,20 @@ private static void getCandidateSubscriptType(PType type, Object key, Set<PType>
257251
* Tells if this reference's referent type is a subtype of {@code type}. Does not check domain.
258252
*/
259253
public boolean referentTypeIsSubtypeOf(PType type, PClass moduleClass) {
260-
// fast path: if this could be unknown, any type is accepted
254+
// fast path: if referent is unknown it can match any type check
261255
if (candidateTypes.contains(PType.UNKNOWN)) {
262256
return true;
263257
}
264258

265259
var checkTypes = normalizeTypes(type, moduleClass);
260+
// fast path: short circuit if any referent is accepted
261+
if (checkTypes.contains(PType.UNKNOWN) || containsClass(checkTypes, anyType.getPClass())) {
262+
return true;
263+
}
264+
// fast path: short circuit if nothing is accepted
265+
if (checkTypes.size() == 1 && checkTypes.contains(PType.NOTHING)) {
266+
return false;
267+
}
266268

267269
// all candidate types must be subtypes of at least one target type
268270
candidate:
@@ -275,6 +277,13 @@ public boolean referentTypeIsSubtypeOf(PType type, PClass moduleClass) {
275277
return true;
276278
}
277279

280+
private static boolean containsClass(Set<PType> types, PClass pClass) {
281+
for (var t : types) {
282+
if (t instanceof PType.Class clazz && clazz.getPClass() == pClass) return true;
283+
}
284+
return false;
285+
}
286+
278287
private static boolean isSubtype(PType a, PType b) {
279288
// checks if A is a subtype of B
280289
// cases (A -> B)
@@ -435,8 +444,4 @@ public int hashCode() {
435444
result = 31 * result + candidateTypes.hashCode();
436445
return result;
437446
}
438-
439-
public String toPklString() {
440-
return (String) toStringCallNode.call(this, getVmClass().getPrototype());
441-
}
442447
}

pkl-core/src/main/java/org/pkl/core/stdlib/AbstractStringRenderer.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.pkl.core.runtime.VmClass;
1919
import org.pkl.core.runtime.VmFunction;
20-
import org.pkl.core.runtime.VmReference;
2120
import org.pkl.core.runtime.VmTypeAlias;
2221

2322
/** Base class for renderers that are part of the standard library. */
@@ -52,11 +51,6 @@ protected void decreaseIndent() {
5251
currIndent.setLength(currIndent.length() - indent.length());
5352
}
5453

55-
@Override
56-
public void visitReference(VmReference value) {
57-
visitString(value.toPklString());
58-
}
59-
6054
// override these to mark them final
6155

6256
@Override

pkl-core/src/test/files/LanguageSnippetTests/input/api/reference.pkl

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import "pkl:math"
22
import "pkl:ref"
33

44
class D extends ref.Domain {
5-
function referenceToString(data: Any, path: List<ref.Access>): String =
6-
if (data is Resource)
7-
let (path = path.map((elem) -> if (elem.isProperty) ".\(elem.property)" else "[\(elem.key)]"))
8-
"${\(data.name)\(path.join(""))}"
9-
else
10-
throw("can only render references rooted to Resource instances")
5+
function renderReference(reference: ref.Reference<D, Any>): String =
6+
let (data = reference.getData())
7+
if (data is Resource)
8+
let (
9+
path =
10+
reference
11+
.getPath()
12+
.map((elem) -> if (elem.isProperty) ".\(elem.property)" else "[\(elem.key)]")
13+
)
14+
"${\(data.name)\(path.join(""))}"
15+
else
16+
throw("can only render references rooted to Resource instances")
1117
}
1218

13-
const local d: D = new {}
19+
local const d: D = new {}
1420
typealias Ref<T> = ref.Reference<D, T>
1521

1622
abstract class Resource {
@@ -51,7 +57,7 @@ class BProperties {
5157

5258
class MapKey {
5359
k: Int
54-
60+
5561
function toString(): String = "key:\(k)"
5662
}
5763

@@ -114,3 +120,11 @@ j: K = new {
114120
refInterpolation = "\(aRef.outputs.someListing[1])"
115121
kInterpolation = "\(k)"
116122
aValuesJoined = k.aValues.join("\n").replaceAll(Regex("@[a-z0-9]+"), "@<addr>")
123+
124+
output {
125+
renderer {
126+
converters {
127+
[ref.Reference] = (it) -> it.toString()
128+
}
129+
}
130+
}

pkl-core/src/test/files/LanguageSnippetTests/input/errors/reference1.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "pkl:ref"
22

33
class D extends ref.Domain {
4-
function referenceToString(data: Any, path: List<ref.Access>): String = throw("not supported")
4+
function renderReference(reference: ref.Reference<D, Any>): String = throw("not supported")
55
}
66
local d: D = new {}
77
typealias Ref<T> = ref.Reference<D, T>

pkl-core/src/test/files/LanguageSnippetTests/input/errors/reference10.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "pkl:ref"
22

33
class D extends ref.Domain {
4-
function referenceToString(data: Any, path: List<ref.Access>): String = throw("not supported")
4+
function renderReference(reference: ref.Reference<D, Any>): String = throw("not supported")
55
}
66
typealias Ref<T> = ref.Reference<D, T>
77
local d: D = new {}

pkl-core/src/test/files/LanguageSnippetTests/input/errors/reference11.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "pkl:ref"
22

33
class D extends ref.Domain {
4-
function referenceToString(data: Any, path: List<ref.Access>): String = throw("not supported")
4+
function renderReference(reference: ref.Reference<D, Any>): String = throw("not supported")
55
}
66
typealias Ref<T> = ref.Reference<D, T>
77
local d: D = new {}

pkl-core/src/test/files/LanguageSnippetTests/input/errors/reference12.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "pkl:ref"
22

33
class D extends ref.Domain {
4-
function referenceToString(data: Any, path: List<ref.Access>): String = throw("not supported")
4+
function renderReference(reference: ref.Reference<D, Any>): String = throw("not supported")
55
}
66
typealias RefAlias1 = ref.Reference<D, Alias1?>
77
local d: D = new {}

pkl-core/src/test/files/LanguageSnippetTests/input/errors/reference13.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "pkl:ref"
22

33
class D extends ref.Domain {
4-
function referenceToString(data: Any, path: List<ref.Access>): String = throw("not supported")
4+
function renderReference(reference: ref.Reference<D, Any>): String = throw("not supported")
55
}
66
local d: D = new {}
77

0 commit comments

Comments
 (0)