Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ private static MemberExpression CreateMemberAccess(Expression? expression, Membe
/// <remarks>
/// As in C#, the common type is the type of one of the branches, so it is <see cref="object" /> only
/// when a branch is of that type, for example after an explicit conversion. The null literal takes the type
/// of the other branch if that type accepts <see langword="null" />.
/// of the other branch if that type accepts <see langword="null" />. A branch of type <see langword="void" />
/// is rejected.
/// </remarks>
private static Type ApplyConditionalConversions(Expr.Conditional expr, ref Expression ifTrue, ref Expression ifFalse)
{
Expand All @@ -131,6 +132,12 @@ private static Type ApplyConditionalConversions(Expr.Conditional expr, ref Expre
isFalseNull ? null : ifFalse.Type);
}

//
// A branch of type void has no value, so the conditional expression would have none either.
//
if (ifTrue.Type == typeof(void) || ifFalse.Type == typeof(void))
Error.NonApplicableConditionalOperator(ifTrue.Type, ifFalse.Type);

if (ifTrue.Type == ifFalse.Type)
return ifTrue.Type;

Expand Down
3 changes: 3 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,8 @@ Operator '?:' cannot be applied to operands of type 'System.Int32' and '<null>'.
true ? null : null
Operator '?:' cannot be applied to operands of type '<null>' and '<null>'.

true ? TimeZoneInfo.ClearCachedData() : TimeZoneInfo.ClearCachedData()
Operator '?:' cannot be applied to operands of type 'System.Void' and 'System.Void'.

(true ? "abc" : null :> object).Length
Cannot resolve symbol 'Length'.