Skip to content

Commit b38a607

Browse files
committed
smaller test
1 parent 08c9cf8 commit b38a607

2 files changed

Lines changed: 50 additions & 10 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/names/NameResolution.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private static <T extends NameLink> ImmutableCollection<T> removeDuplicates(List
117117

118118
public static ImmutableCollection<FuncLink> lookupMemberFuncs(Element node, WurstType receiverType, String name, boolean showErrors) {
119119
if (!showErrors) {
120-
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType, GlobalCaches.LookupType.MEMBER_FUNC);
120+
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType.getFullName(), GlobalCaches.LookupType.MEMBER_FUNC);
121121
@SuppressWarnings("unchecked")
122122
ImmutableCollection<FuncLink> cached = (ImmutableCollection<FuncLink>) GlobalCaches.lookupCache.get(key);
123123
if (cached != null) {
@@ -155,7 +155,7 @@ public static ImmutableCollection<FuncLink> lookupMemberFuncs(Element node, Wurs
155155
ImmutableCollection<FuncLink> immutableResult = removeDuplicates(result);
156156

157157
if (!showErrors) {
158-
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType, GlobalCaches.LookupType.MEMBER_FUNC);
158+
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType.getFullName(), GlobalCaches.LookupType.MEMBER_FUNC);
159159
GlobalCaches.lookupCache.put(key, immutableResult);
160160
}
161161

@@ -252,7 +252,7 @@ public static NameLink lookupVarNoConfig(Element node, String name, boolean show
252252

253253
public static NameLink lookupMemberVar(Element node, WurstType receiverType, String name, boolean showErrors) {
254254
if (!showErrors) {
255-
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType, GlobalCaches.LookupType.MEMBER_VAR);
255+
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType.getFullName(), GlobalCaches.LookupType.MEMBER_VAR);
256256
NameLink cached = (NameLink) GlobalCaches.lookupCache.get(key);
257257
if (cached != null) {
258258
return cached;
@@ -286,7 +286,7 @@ public static NameLink lookupMemberVar(Element node, WurstType receiverType, Str
286286

287287
if (bestMatch != null) {
288288
if (!showErrors) {
289-
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType, GlobalCaches.LookupType.MEMBER_VAR);
289+
GlobalCaches.CacheKey key = new GlobalCaches.CacheKey(node, name + "@" + receiverType.getFullName(), GlobalCaches.LookupType.MEMBER_VAR);
290290
GlobalCaches.lookupCache.put(key, bestMatch.link);
291291
}
292292
return bestMatch.link;
@@ -413,13 +413,30 @@ private DefLinkMatch(DefLink link, int distance) {
413413

414414
public static DefLink matchDefLinkReceiver(DefLink n, WurstType receiverType, Element node, boolean showErrors) {
415415
WurstType n_receiverType = n.getReceiverType();
416-
if (n_receiverType == null) {
417-
return null;
418-
}
419-
VariableBinding mapping = receiverType.matchAgainstSupertype(n_receiverType, node, VariableBinding.emptyMapping().withTypeVariables(n.getTypeParams()), VariablePosition.RIGHT);
420-
if (mapping == null) {
421-
return null;
416+
if (n_receiverType == null) return null;
417+
418+
VariableBinding vb = VariableBinding.emptyMapping();
419+
420+
// 1) include type params from the receiver type (class/interface/module instantiation)
421+
if (n_receiverType instanceof WurstTypeClassOrInterface wtc) {
422+
if (wtc.getDef() instanceof AstElementWithTypeParameters tpOwner) {
423+
vb = vb.withTypeVariables(tpOwner.getTypeParameters());
424+
}
425+
} else if (n_receiverType instanceof WurstTypeModuleInstanciation mins) {
426+
ClassDef cd = mins.getDef().attrNearestClassDef();
427+
if (cd != null) {
428+
vb = vb.withTypeVariables(cd.getTypeParameters());
429+
}
422430
}
431+
432+
// 2) include function/extension type params as before
433+
vb = vb.withTypeVariables(n.getTypeParams());
434+
435+
VariableBinding mapping =
436+
receiverType.matchAgainstSupertype(n_receiverType, node, vb, VariablePosition.RIGHT);
437+
438+
if (mapping == null) return null;
439+
423440
if (showErrors) {
424441
if (n.getVisibility() == Visibility.PRIVATE_OTHER) {
425442
node.addError(Utils.printElement(n.getDef()) + " is private and cannot be used here.");

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GenericsWithTypeclassesTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,29 @@ public void genericClassWithLLModule2() {
22002200
);
22012201
}
22022202

2203+
@Test
2204+
public void genericModuleThistypeSmall() {
2205+
testAssertOkLines(true,
2206+
"package test",
2207+
" native testSuccess()",
2208+
" module LLM",
2209+
" static thistype t",
2210+
" construct()",
2211+
" t = this",
2212+
" function iterator() returns Iterator",
2213+
" return new Iterator()",
2214+
" static class Iterator",
2215+
" function next() returns LLM.thistype",
2216+
" return t",
2217+
" class A<T:>",
2218+
" use LLM",
2219+
" init",
2220+
" let a = new A<int>",
2221+
" if a.iterator().next() == a",
2222+
" testSuccess()",
2223+
"endpackage"
2224+
);
2225+
}
22032226

22042227

22052228
}

0 commit comments

Comments
 (0)