Skip to content

parser: support nullable value types in operators - #30

Merged
rameel merged 1 commit into
mainfrom
parser/nullable-operators
Nov 18, 2026
Merged

rameel merged 1 commit into
mainfrom
parser/nullable-operators

Conversation

@rameel

@rameel rameel commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Operators accepted operands of a nullable value type only when both operands had the same type, as in x + y for two int? values. Mixed operands were rejected: for an int? x, x ?? 5, x + 1, x > 1, and Flag ? x : 1 all failed, as did x == DayOfWeek.Friday for a DayOfWeek? x and x > Date for a DateTime? x. The unary ~ did not promote a byte? operand, so ~x for x = 3 returned 252 as byte? instead of -4 as int?.

This PR follows the C# rules for nullable value types:

  • Adds the implicit conversions from S and S? to T? for each identity or numeric conversion from S to T. The conditional operator uses them to find the common type of its branches.
  • Determines the result type of ?? in the order of the specification: A₀, A, then B. For example, x ?? 1 is an int, x ?? 1L is a long, and x ?? y is a long? for a long? y.
  • Lifts the unary and binary operators, including shifts, enum operators, and user-defined operators such as DateTime subtraction: the operator is selected by the underlying types of the operands, and the operands are converted to the nullable forms of its operand types.
  • Gives the null literal the type of the nullable operand on the other side, as in x == null or x ?? null.

The unary ~ on a nullable operand of a type smaller than int, such as byte?, now returns int?. Other expressions that parsed before keep their result type and value.

?? with a left operand of a reference type is not changed: name ?? obj for a string and an object is still rejected, though C# accepts it. Constants are still converted by their type, so x + 1 is rejected for a ulong? x, as it is for a ulong.

Lifted enum operators follow the existing rules for enums. For example, x - DayOfWeek.Monday for a DayOfWeek? x returns a DayOfWeek?, as it does for a DayOfWeek.

Operators accepted operands of a nullable value type only when both
operands had the same type, as in x + y for two int? values. Mixed
operands were rejected: for an int? x, x ?? 5, x + 1, x > 1, and
Flag ? x : 1 all failed, as did x == DayOfWeek.Friday for a DayOfWeek?
x and x > Date for a DateTime? x. The unary '~' did not promote a byte?
operand, so ~x for x = 3 returned 252 as byte? instead of -4 as int?.

Follow the C# rules for nullable value types:

- Add the implicit conversions from S and S? to T? for each identity
  or numeric conversion from S to T. The conditional operator uses
  them to find the common type of its branches.
- Determine the result type of '??' in the order of the specification:
  A0, A, then B. For example, x ?? 1 is an int, x ?? 1L is a long, and
  x ?? y is a long? for a long? y.
- Lift the unary and binary operators, including shifts, enum
  operators, and user-defined operators such as DateTime subtraction:
  select the operator by the underlying types of the operands and
  convert the operands to the nullable forms of its operand types.
- Give the null literal the type of the nullable operand on the other
  side, as in x == null or x ?? null.
@rameel
rameel merged commit 8aad428 into main Nov 18, 2026
1 check passed
@rameel
rameel deleted the parser/nullable-operators branch November 18, 2026 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant