Browse Source

Fix approximate member choosing when Inherited methods are involved

Background: Approximate member choosing is based only on the types, so an overridden method would match twice, but with different priority. Fixes this by including a priority check before deciding whether or not to throw an error
kindlich 4 years ago
parent
commit
73cccd9a35
No known key found for this signature in database

+ 10
- 6
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberGroup.java View File

@@ -359,7 +359,7 @@ public class TypeMemberGroup {
359 359
                 return selectedMethod.member;
360 360
         }
361 361
 		// try to match with approximate types
362
-		FunctionalMemberRef selected = null;
362
+		TypeMember<FunctionalMemberRef> selected = null;
363 363
 		for (TypeMember<FunctionalMemberRef> method : methods) {
364 364
 			if (!(method.member.isStatic() ? allowStatic : allowNonStatic))
365 365
 				continue;
@@ -372,15 +372,19 @@ public class TypeMemberGroup {
372 372
 			if (!header.matchesImplicitly(position, arguments, scope))
373 373
 				continue;
374 374
 			
375
-			if (selected != null) {
375
+			if (selected == null) {
376
+				selected = method;
377
+			} else if(selected.priority == method.priority){
376 378
 				StringBuilder explanation = new StringBuilder();
377
-				FunctionHeader selectedHeader = selected.getHeader().instanceForCall(position, scope.getTypeRegistry(), arguments);
379
+				FunctionHeader selectedHeader = selected.member.getHeader().instanceForCall(position, scope.getTypeRegistry(), arguments);
378 380
 				explanation.append("Function A: ").append(selectedHeader.toString()).append("\n");
379 381
 				explanation.append("Function B: ").append(header.toString());
380 382
 				throw new CompileException(position, CompileExceptionCode.CALL_AMBIGUOUS, "Ambiguous call; multiple methods match:\n" + explanation.toString());
383
+			} else {
384
+				//For example:
385
+				//Child overrides parent: Priority.Specified vs. Priority.Inherited
386
+				selected = selected.resolve(method);
381 387
 			}
382
-			
383
-			selected = method.member;
384 388
 		}
385 389
 		
386 390
 		if (selected == null) {
@@ -403,7 +407,7 @@ public class TypeMemberGroup {
403 407
 			throw new CompileException(position, CompileExceptionCode.CALL_NO_VALID_METHOD, "No matching method found for " + name + ":\n" + message.toString());
404 408
 		}
405 409
 		
406
-		return selected;
410
+		return selected.member;
407 411
 	}
408 412
 	
409 413
 	public FunctionalMemberRef getOverride(CodePosition position, TypeScope scope, FunctionalMember member) throws CompileException {

Loading…
Cancel
Save