Open
Description
for instance, the IL for
class Foo<T>
{
}
is missing the attribute. Below is the IL generated by the compiler for the same code:
.class private auto ansi beforefieldinit Foo`1<T>
extends [System.Runtime]System.Object
{
// Missing attribute
.param type T
.custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableAttribute::.ctor(uint8) = (
01 00 02 00 00
)
// ...
} // end of class Foo`1
There are many more scenarios that need to be handled as well.
References
- https://github.com/dotnet/roslyn/blob/main/docs/features/nullable-metadata.md
- https://codeblog.jonskeet.uk/2019/02/10/nullableattribute-and-c-8/
- System.Runtime.CompilerServices.NullableAttribute is not returning if there are no even properties with null and without null. dotnet/roslyn#39133
- https://dev.to/maartenba/internals-of-c-nullable-reference-types-migrating-to-nullable-reference-types-part-2-gmd
Notes
- This task also includes emitting
NullableContextAttribute
. - For generic type parameters the argument to the attribute constructor is defined by the presence/absence of
notnull
constraint - One of the following values should be provided for
each
type parameter:- 0 meaning the
type parameter
is oblivious in regards to nullability (not sure how to construct a test scenario for this) - 1 if the type parameter is non nullable (i.e, has the
notnull
constraint) - 2 if the type parameter is nullable
- 0 meaning the
- For generic types the
NullableAttribute
constructor is passed an array of the values above, one for eachtype parameter
. NullableContextAttribute
/NullableAttribute
are applied in a way to minimize the number of attributes required, so in general,NullableContextAttribute
is applied with whichever value is more common in the method andNullableAttribute
is applied for elements that differs.#nullable
pre-processor directive set toenable
is equivalent do no preprocessor
Activity