-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
It seems the type for a default literal is not correctly inferred when using C# 7.0 or lower. In these cases:
class C
{
int M()
{
return default;
}
}class C
{
void M(int a)
{
M(default);
}
}class C
{
void M()
{
var i = true ? 0 : default;
}
}class C
{
void M()
{
var i = (int)default;
}
}calling GetTypeInfo().ConvertedType on the default literal results in an error type. This works as expected (returns int type) when using C# 7.1. Some simple cases work in C# 7.0 too such as int i = default; and switch (0) { case default: }; (both return int for converted type on the default literal).
Is this by design? I think it would be good for the compiler to infer the type even when the language version isn't right, for good error recovery in the IDE. If that's not possible or desired, would it at least be possible to somehow find out what the type would have been if there wasn't an error? Thanks.
(Discovered in #30359 (comment))
Tagging @jcouv