-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
NullabilityInfoContext does not report the correct nullability for method in generic base class. #63555
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsDescriptionLet's say I have the following class defined in nullable enabled context:
If I try to add a null value to the object, I get a compiler error:
However, I can't replicate this compiler behavior with Reproduction Steps
Expected behaviorI would expect the code above to print "NotNull" instead. Actual behaviorPrints "Nullable". Regression?No response Known WorkaroundsNo response ConfigurationVS 17.0.2, .NET 6, Windows 10 x64. I don't have any reason to believe this is configuration specific. Other informationNo response
|
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsDescriptionLet's say I have the following class defined in nullable enabled context:
If I try to add a null value to the object, I get a compiler error:
However, I can't replicate this compiler behavior with Reproduction Steps
Expected behaviorI would expect the code above to print "NotNull" instead. Actual behaviorPrints "Nullable". Regression?No response Known WorkaroundsNo response ConfigurationVS 17.0.2, .NET 6, Windows 10 x64. I don't have any reason to believe this is configuration specific. Other informationNo response
|
cc: @buyaa-n |
This is by design, at runtime there is no way to distinguish if the Therefore in order to determine nullability of the |
@buyaa-n if there is no way to tell, how does the compiler differentiate (or can the compiler only do it if the derived type is defined in the same compilation?)? |
I guess i should have said that there is no way to distinguish that in reflection, because compiler does not provide any nullability info for those generic parameters (reflection uses compiler provided The compiler nullability analysis and this reflection API are do not work same |
@buyaa-n but when I decompile my assembly with dotpeek it looks like the nullable metadata is there: Original: internal class NonNullableValuesDictionary : Dictionary<string, object> { }
internal class NullableValuesDictionary : Dictionary<string, object?> { } Decompiled: [Nullable(new byte[] {(byte) 0, (byte) 1, (byte) 1})]
internal class NonNullableValuesDictionary : Dictionary<string, object>
{
}
[Nullable(new byte[] {(byte) 0, (byte) 1, (byte) 2})]
internal class NullableValuesDictionary : Dictionary<string, object>
{
} Aren't those attributes describing the nullability of the generic parameters for |
Hm, that actually looks feasible compared to, reopening the issue, thanks! var nonNull= typeof(Dictionary<string, object>);
var Nullable = typeof(Dictionary<string, object?>); |
In several cases, NullabilityInfoContext would return values which were out of sync with the C# compiler's interpretation of the nullability metadata. This fixes the following cases: * The `NullablePublicOnly` attribute was not considered when analyzing private constructor parameters. * CodeAnalysis attributes on indexer properties were not recognized. * CodeAnalysis attributes were not recognized in a `#nullable disabled` context. * Private value-typed members lacking nullable annotations were not properly marked as nullable/notnull. * Mixing CodeAnalysis attributes with opposite meanings (e. g. `AllowNull` and `DisallowNull` produced the wrong result. * Analysis of members inherited from generic base types did not incorporate the nullable metadata associated with the inheritance. Fix dotnet#63555 Fix dotnet#63846 Fix dotnet#63847 Fix dotnet#63848 Fix dotnet#63849 Fix dotnet#63853
Description
Let's say I have the following class defined in nullable enabled context:
If I try to add a null value to the object, I get a compiler error:
However, I can't replicate this compiler behavior with
NullabilityInfoContext
even though it seems to me that the compiler must be reading the same metadata in order to issue that warning.Reproduction Steps
Expected behavior
I would expect the code above to print "NotNull" instead.
Actual behavior
Prints "Nullable".
Regression?
No response
Known Workarounds
No response
Configuration
VS 17.0.2, .NET 6, Windows 10 x64.
I don't have any reason to believe this is configuration specific.
Other information
Some other fun cases:
The text was updated successfully, but these errors were encountered: