```#nullable enable``` causes unexpected compilation errors
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
Compile the following code with ```#nullable enable```:
```
public class C1 where T : notnull
{
public T F => throw null!;
public static C1 operator &(C1 x, C1 y)
{
return x;
}
public static bool operator true(C1 x) => true;
public static bool operator false(C1 x) => false;
}
public class C2
{}
class Program
{
static void Main()
{
var x = Get((C2?)null);
var y = Get(new C2());
(x && y).F.ToString();
(y && x).F.ToString();
(y && y).F.ToString();
}
static C1 Get(T x)
{
throw null!;
}
}
```
Observed the following unexpected errors:
```
// /Program.cs(25,10): error CS0217: In order to be applicable as a short circuit operator a user-defined logical operator ('C1.operator &(C1, C1)') must have the same return type and parameter types
// (x && y).F.ToString();
Diagnostic(ErrorCode.ERR_BadBoolOp, "x && y").WithArguments("C1.operator &(C1, C1)").WithLocation(25, 10),
// /Program.cs(26,10): error CS0217: In order to be applicable as a short circuit operator a user-defined logical operator ('C1.operator &(C1, C1)') must have the same return type and parameter types
// (y && x).F.ToString();
Diagnostic(ErrorCode.ERR_BadBoolOp, "y && x").WithArguments("C1.operator &(C1, C1)").WithLocation(26, 10),
// /Program.cs(27,10): error CS0217: In order to be applicable as a short circuit operator a user-defined logical operator ('C1.operator &(C1, C1)') must have the same return type and parameter types
// (y && y).F.ToString();
Diagnostic(ErrorCode.ERR_BadBoolOp, "y && y").WithArguments("C1.operator &(C1, C1)").WithLocation(27, 10)
```
The errors go away when compiled with ```#nullable disable```.
Expected: The errors should not be reported. The operator satisfy the requirements about which the errors are complaining.
Contributor guide
Research direction
Start by compiling the issue's Program.cs example with nullable enabled and disabled, then trace the C# compiler's handling of user-defined && operators and nullable type inference. Add a regression test for the sample and verify that the three expressions compile without CS0217 while the operator requirements remain enforced.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100