No nullable warning emmited for non-nullable properties on structs. #74715
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
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:
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
The text was updated successfully, but these errors were encountered: