1515 */
1616package org .pkl .core .runtime ;
1717
18- import com .oracle .truffle .api .nodes .DirectCallNode ;
1918import java .util .ArrayList ;
2019import java .util .Collections ;
2120import 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}
0 commit comments