Repository navigation
parser: convert conditional branches to their common type - #17
Merged
Merged
Conversation
CanConvertPrimitive used the widening rules of the reflection binder, which do not convert integer types and char to decimal. It also took an enum target for its underlying type, so it reported that any number converts to any enum. Allow the implicit conversions to decimal and reject conversions to an enum. The method resolver uses the table only for primitive parameter types, and decimal and enums are not primitive, so overload resolution does not change.
The branches of a conditional expression were passed to Expression.Condition as they were, so branches of different types, as in 'flag ? 1 : 2.0' or 'flag ? name : null', failed with "Argument types do not match". Convert one branch to the type of the other when only one of them converts implicitly, as C# does. The common type is always the type of a branch, so the result is object only when a branch is of that type. The null literal takes the type of the other branch if that type accepts null, and 'flag ? null : null' now fails because neither branch has a type. Branches without a common type fail with an error that names both types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The branches of a conditional expression were passed to
Expression.Conditionas they were, so expressions such asflag ? 1 : 2.0andflag ? name : nullfailed withArgument types do not match.This PR converts one branch to the type of the other when only one of them converts implicitly, as C# does. The common type is always the type of a branch, so the result is
objectonly when a branch is of that type, as inflag ? 1 : x :> object. The null literal takes the type of the other branch if that type acceptsnull. Branches without a common type, such as inflag ? 1 : "a", fail withOperator '?:' cannot be applied to operands of type 'System.Int32' and 'System.String'.To convert numbers as C# does,
TypeUtils.CanConvertPrimitivenow allows the implicit conversions of integer types andchartodecimaland rejects conversions to an enum. The method resolver uses it only for primitive parameter types, so overload resolution does not change.flag ? null : nullnow fails because neither branch has a type.