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

Nullable Reference Types: CS8618 and/or CS8625 should be consistently reported for static fields/properties. #27014

Closed
PathogenDavid opened this issue May 21, 2018 · 3 comments
Labels
Area-Compilers Bug New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Milestone

Comments

@PathogenDavid
Copy link
Contributor

PathogenDavid commented May 21, 2018

Version Used: 05/14/18 Nullable Reference Types Preview (csc reports 2.8.0.62830 (e595ee27)) with Visual Studio 15.7.1

Demonstration Code:

(For your convenience, here's all of the examples in this issue in one project: PathogenPlayground/InconsistentWarningsOnStaticFieldsAndProperties)

public class TestClass
{
    public string TestProperty { get; }
    public string testField;

    public static string TestStaticProperty { get; }
    public static string testStaticField;
}

Expected Behavior:

A CS8618 warning is reported for all four fields/properties.

Actual Behavior:

CS8618 is only reported for the instance field/property:

warning CS8618: Non-nullable property 'TestProperty' is uninitialized.
warning CS8618: Non-nullable field 'testField' is uninitialized.

Variations

While reporting this issue, I ended up finding out that adding null initializers and a static constructor changed the behavior as observed in the examples below:


Adding a static constructor does not change anything:

// (No warnings for TestStaticProperty or testStaticField)
public class TestClass
{
    public static string TestStaticProperty { get; }
    public static string testStaticField;

    static TestClass() { }
}

Adding null initializers does not change anything:

// (No warnings for TestStaticProperty or testStaticField)
public class TestClass
{
    public static string TestStaticProperty { get; } = null;
    public static string testStaticField = null;
}

Adding both null initializers and a static constructor does change things:

public class TestClass
{
    public static string TestStaticProperty { get; } = null; // warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
    public static string testStaticField = null; // warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.

    static TestClass() { }
}
@sharwell
Copy link
Member

Possible duplicate of #26651

@PathogenDavid
Copy link
Contributor Author

I've created PathogenPlayground/InconsistentWarningsOnStaticFieldsAndProperties to demonstrate the inconsistency all in one project.

Also I forgot to mention, this issue might also be related to #26628 (or #26651 and it had a baby and made this bug.)

@jaredpar
Copy link
Member

Looks like a dupe of #26651 as @sharwell indicated.

@jaredpar jaredpar added the Resolution-Duplicate The described behavior is tracked in another issue label Jan 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

4 participants