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

Codegen not compatible with all "nullable contexts" #158

Open
JustALawnGnome7 opened this issue Apr 28, 2022 · 2 comments
Open

Codegen not compatible with all "nullable contexts" #158

JustALawnGnome7 opened this issue Apr 28, 2022 · 2 comments

Comments

@JustALawnGnome7
Copy link

The default "nullable context" for newly-generated C# projects targeting .NET 6 is now set to "enable" at the project level; this means all code generated by the dotnet-dbus codegen command will receive the context that is set for the entire project unless it is overwritten at the source file level.

This means that every time the generated code tries to assign null to a reference type, we get a compiler warning. When following along with the example included in the README.md file, for example, I observed the same warning 81 times:

warning CS8625: Cannot convert null literal to non-nullable reference type.

For reference, I have included the following to provide more information on this issue:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types#setting-the-nullable-context

@JustALawnGnome7
Copy link
Author

JustALawnGnome7 commented Apr 28, 2022

Given that the codegen command has no knowledge of the C# project it will be generating code for, I propose we fix this issue with one of the following solutions:

  1. Disable the nullable context for all codegen-generated source files by prepending "#nullable disable" to the top of each file
  2. Add a --nullable option for the codegen command that optimizes the generated code to take advantage of new nullable context features. (This obviously requires more work)

@tmds
Copy link
Owner

tmds commented May 3, 2022

I prefer the second option.

Do you want to make a PR for one of these options?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants