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

No nullable warning emmited for non-nullable properties on structs. #74715

Closed
KimBrusevold opened this issue Aug 12, 2024 · 2 comments
Closed
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@KimBrusevold
Copy link

Version Used:
Using SDK .Net 8.0.107, x64

Steps to Reproduce:
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZQAs6AsgBQCU+hxTAbgIYBO6ALgKYDOXdAF50AOx4B3dAGUu7AK4BjLgBV+XOgG5GTIlDgBOarwEA6NQA8u5gPYBVAA4Oe7OrW1Idutp15c+AELCYpI4ADasfHxqAlrIXkz6Rn6B5jxWto7OrrTuXgC+yIWeJfEoGAIKyjJySqrqyARIiaRwAAzoloJ46ADmPFya6HwDQ8XFyGiYMOGR0Q1ITS2Y7Z3p3X2jw1vjyEA===

Enable nullable in project file
<Nullable>enable</Nullable>

Create one struct and one class:

struct StructTest
{
    public string Text { get; set; }
}

class ClassTest
{
    public string Text { get; set; }
}

Expected Behavior:

A nullable warning that the non-nullable property 'Text' cannot be initialized to null on both the struct and class.

Actual Behavior:

Only the class property emits the error:
"Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.CS8618"

If we treat this warning as an error, the compiler generates invalid code. By obtaining a reference to the struct property Text, we may get an null reference error, even though we declared the property as non-nullable. The empty initializer for the struct (new StructTest()) which calls default(StructTest) sets null in the non-nullable fields

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 12, 2024
@RikkiGibson
Copy link
Contributor

Unfortunately, structs have known/expected gaps with null safety. There have been various proposals to address this over the years such as dotnet/csharplang#146.

But, the struct scenario you have shown is not expected to issue any warnings under the current design. One way to work around this and get a certain (limited) degree of nullability checking is to explicitly declare a constructor in struct StructTest. There will still be null safety gaps when the default value is used, however.

@RikkiGibson RikkiGibson added Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design labels Aug 12, 2024
@KimBrusevold
Copy link
Author

Thanks. I'm not at liberty to say anything about what is expected and not.

I got around this by creating properties and marking them as required. The compiler recognized that de default "empty" constructor is invalid (which is a good thing).
The nullable feature is such a good thing, and seems so close to being good enough.

I really want to be confident in the compiler checking the nullable stuff for me.

@jaredpar jaredpar closed this as not planned Won't fix, can't repro, duplicate, stale Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants