Don't count failed signature matches as autocast matches #12337
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supersedes #10701. The following description is adopted from that PR.
Consider the following:
Each overload set is checked in that order, because neither overload is more restricted than the other. What happens here is:
1
successfully matchesInt8
because1
is withinInt8
's range. The compiler adds this to a list of partial autocast matches.'a'
successfully matchesChar
, so there is a successful signature match. But the compiler must also check other defs to detect ambiguous autocasts.1
successfully matchesUInt8
because1
is withinUInt8
's range. The partial autocast match list now contains both integer types.'a'
does not matchString
, so this signature match fails, but the compiler does not reset the list of partial autocast matches. Thus1
is considered to be ambiguous, even though theUInt8
autocast match is associated with a call that fails.This PR makes it so that autocast matches are added only after a signature match succeeds completely (with
AutocastType#add_autocast_matches
). Thus the autocasting behavior is no longer dependent on the argument order, and both error calls above become unambiguous:Does not fix #8600 (review), since in that case all signature matches succeed.
Note that splat restrictions never autocast:
So two of the calls to
Crystal::Type#restrict
insideCrystal::CallSignature#match
are not associated with any autocast checks.