Allow DoesNotReturn methods in conditional expressions #3079
-
For now the compiler cannot determine the type of a conditional expression when one of it's branches calls a method marked as Steps to reproduce #nullable enable
private static string? Method(object? value) =>
value is null? ThrowException() : value.ToString(); // error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'void' and 'string'
private static string? MethodWithoutError(object? value) =>
value is null? throw new Exception() : value.ToString(); // compiled without issues
[DoesNotReturn]
private static void ThrowException() =>
throw new Exception(); Compiler version: '3.3.1-beta4-19462-11 (66a912c9)'. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
There is no guarantee of this, as the compiler does not enforce that this method actually does not return. For this, we'd need something like #538. |
Beta Was this translation helpful? Give feedback.
-
This makes sense to me since we don't want nullable attributes to change behavior of code. I would personally prefer a more full-fledged feature to support this like #538 instead of attributes changing type resolution behavior. |
Beta Was this translation helpful? Give feedback.
There is no guarantee of this, as the compiler does not enforce that this method actually does not return. For this, we'd need something like #538.