Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit f471f10

Browse files
Make it an error to have ambiguous overloads in handler groups
1 parent 9595ca9 commit f471f10

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

libscript/src/script-bytecode.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ struct MCScriptBytecodeOp_Invoke
357357
// We use a simple scoring mechanism to determine which method to use. If
358358
// input type for an argument is equal to expected type, then this is a
359359
// score of zero. If input type for an argument conforms to expected type, then
360-
// this is a score of 1. The method with the lowest score is chosen, with
361-
// priority given to the first in the list in the case of a tie.
360+
// this is a score of 1. The method with the lowest score is chosen
361+
// unless multiple variants have the same score, which is an error.
362362

363363
uindex_t t_min_score = UINDEX_MAX;
364364
MCScriptDefinition *t_min_score_definition = nil;
365365
MCScriptInstanceRef t_min_score_instance = nil;
366+
bool t_min_score_ambiguous = false;
367+
366368
for(uindex_t t_handler_idx = 0; t_handler_idx < p_group -> handler_count; t_handler_idx++)
367369
{
368370
MCScriptInstanceRef t_current_instance;
@@ -419,14 +421,22 @@ struct MCScriptBytecodeOp_Invoke
419421
t_current_score += 1;
420422
}
421423

422-
if (t_current_score < t_min_score)
424+
if (t_current_score == t_min_score)
425+
{
426+
// The minimum score is ambiguous - multiple alternatives have
427+
// equal scores.
428+
t_min_score_ambiguous = true;
429+
}
430+
else if (t_current_score < t_min_score)
423431
{
424-
t_min_score = t_current_score;
432+
// A new minimum score has been found
433+
t_min_score = t_current_score;
425434
t_min_score_definition = t_current_definition;
426435
t_min_score_instance = t_current_instance;
436+
t_min_score_ambiguous = false;
427437
}
428438
}
429-
if (t_min_score_definition == NULL)
439+
if (t_min_score_ambiguous || t_min_score_definition == NULL)
430440
{
431441
ctxt.ThrowUnableToResolveMultiInvoke(p_group,
432442
p_arguments,

0 commit comments

Comments
 (0)