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 analysis] Assigning default to struct variable with non-nulable refs should produce nullability warning #77130

Open
petrroll opened this issue Feb 10, 2025 · 0 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@petrroll
Copy link
Contributor

petrroll commented Feb 10, 2025

Version Used:
Latest

Steps to Reproduce:

  1. Create a struct MyStruct with non-nullable field / property object MyProperty
  2. Create a variable MyVar of type MyStruct
  3. Assign default to MyVar

A minimal repro, with source-code provided, is ideal:
https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYCMBYAUKgZgAJVMA2E9IgYSIG88inKqHdmOiA3AQzCIBGRALxE4AUwBmPCABtgACgBCASgDcjTkwEA6ALIBPAApgA9gAdxYYAZ0AVUwGVgYAJZQA5gvWamAX18iPADcPDBxHjhTKFkDInCAY1MwOCIAZxcIBOAiJQVTAQArcWyiQxMLKxt1IA=

Expected Behavior:
Some sort of warning happens.

Actual Behavior:
it's all good, acessing MyProperty trows NRE.

Notes
I understand that by definition nullability analysis has holes. One could e.g. create an array of such structs (or generally initialization of arrays is a problem), we could be in generic type situation, etc.

And also understand that plugging even this generally would probably require adding a notion of non-default-initialized struct and flow that through analysis or some way to disallow usage of default generated constructor (maybe warn if there's one autogenerated on struct with non-nullable fields?) to fix situations s.a. with initialization of field on class.

But plugging at least the case where we explicitly pass default to such variable should be possible .

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

1 participant