Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Neme12 opened this issue Oct 8, 2018 · 1 comment · Fixed by #37596
Closed

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

Neme12 opened this issue Oct 8, 2018 · 1 comment · Fixed by #37596
Assignees
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Milestone

Comments

@Neme12
Copy link
Contributor

Neme12 commented Oct 8, 2018

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

@jcouv jcouv self-assigned this Oct 8, 2018
@jcouv jcouv added this to the 16.0 milestone Oct 8, 2018
@Neme12
Copy link
Contributor Author

Neme12 commented Oct 10, 2018

There is a similar issue here. GetConstantValue isn't working either for default literals in C# 7.0, even in simple cases. For example:

class C
{
    void M()
    {
        int i = default;
    }
}

Calling GetConstantValue on the default literal returns no value. This is blocking work on another feature (converting default to another literal). Please let me know if this is unrelated and I should file a separate issue. Thanks.

@jcouv jcouv modified the milestones: 16.0, 16.0.P2 Nov 12, 2018
@jcouv jcouv modified the milestones: 16.0.P2, 16.0 Jan 5, 2019
@jcouv jcouv modified the milestones: 16.0, 16.1, 16.2 Apr 18, 2019
@jcouv jcouv modified the milestones: 16.2, 16.3, Compiler.Next Jun 25, 2019
@jcouv jcouv added the 4 - In Review A fix for the issue is submitted for review. label Aug 20, 2019
@jcouv jcouv modified the milestones: Compiler.Next, 16.4 Aug 21, 2019
@jcouv jcouv added the Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented label Aug 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants