Skip to content

Type for default literal not inferred in C# 7.0 #30384

@Neme12

Description

@Neme12

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

Metadata

Metadata

Assignees

Labels

4 - In ReviewA fix for the issue is submitted for review.Area-CompilersBugResolution-FixedThe bug has been fixed and/or the requested behavior has been implemented

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions