Tmds.DBus icon indicating copy to clipboard operation
Tmds.DBus copied to clipboard

Codegen not compatible with all "nullable contexts"

Open JustALawnGnome7 opened this issue 2 years ago • 2 comments

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 avatar Apr 28 '22 18:04 JustALawnGnome7

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)

JustALawnGnome7 avatar Apr 28 '22 18:04 JustALawnGnome7

I prefer the second option.

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

tmds avatar May 03 '22 10:05 tmds