-
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 properly respect CodeAnalysis attribute on indexer property #63849
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsDescriptionIn most cases code analysis attributes like AllowNull are respected, but this is not true for indexer properties. Reproduction Stepspublic void Main()
{
var context = new NullabilityInfoContext();
var indexerInfo = context.Create(typeof(HasIndexer).GetProperty("Item")!);
Console.WriteLine(indexerInfo.WriteState); // NotNull (should be Nullable)
}
public class HasIndexer
{
[AllowNull] public string this[string? s]
{
get => default!;
set { }
}
} Expected behaviorThe write state should be nullable because the following compiles without warnings:
Actual behaviorWrite state is not null as if the attribute were not present. 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 informationCC @Shane32
|
Thanks for opening these nullable annotation bugs @madelson . Are you interested in offering a PR for any of them? |
@danmoseley yes I'm working on fixes for some of them with the idea of sending a PR. |
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
In most cases code analysis attributes like AllowNull are respected, but this is not true for indexer properties.
Reproduction Steps
Expected behavior
The write state should be nullable because the following compiles without warnings:
Actual behavior
Write state is not null as if the attribute were not present.
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
CC @Shane32
The text was updated successfully, but these errors were encountered: