Annotate ServiceDescriptor.IsKeyedService for nullability flow and sync ref assembly#124493
Annotate ServiceDescriptor.IsKeyedService for nullability flow and sync ref assembly#124493danmoseley merged 5 commits intomainfrom
ServiceDescriptor.IsKeyedService for nullability flow and sync ref assembly#124493Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
ServiceDescriptor.IsKeyedService for nullability flow and add regression coverage
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
ServiceDescriptor.IsKeyedService for nullability flow and add regression coverageServiceDescriptor.IsKeyedService for nullability flow, sync ref assembly, and add regression coverage
...Extensions.DependencyInjection/tests/DI.Tests/ServiceCollectionKeyedServiceExtensionsTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
This PR addresses a nullability flow analysis issue by adding [MemberNotNullWhen(true, nameof(ServiceKey))] to ServiceDescriptor.IsKeyedService. This annotation tells the C# compiler that when IsKeyedService returns true, the ServiceKey property is guaranteed to be non-null, eliminating the need for null-forgiving operators (!) in code that checks this property.
Changes:
- Added
[MemberNotNullWhen(true, nameof(ServiceKey))]attribute toServiceDescriptor.IsKeyedServicein the implementation - Synced the annotation to the reference assembly
- Added regression test to verify the attribute is present and correctly configured
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs | Added MemberNotNullWhen attribute to IsKeyedService property to improve nullability flow analysis |
| src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/ref/Microsoft.Extensions.DependencyInjection.Abstractions.cs | Synced the MemberNotNullWhen attribute to the reference assembly |
| src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceCollectionKeyedServiceExtensionsTest.cs | Added reflection-based test to ensure the attribute is present with correct parameters |
...ependencyInjection.Abstractions/ref/Microsoft.Extensions.DependencyInjection.Abstractions.cs
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Head branch was pushed to by a user without write access
ServiceDescriptor.IsKeyedService for nullability flow, sync ref assembly, and add regression coverageServiceDescriptor.IsKeyedService for nullability flow and sync ref assembly
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection |
|
/tmp/helix/working/B086093A/p/build/apple/AppleBuild.targets(260,5): error : LLVM ERROR: IO failure on output stream: No space left on device |
ServiceDescriptor.ServiceKeyis nullable (object?), so static analysis did not infer non-nullness afterif (descriptor.IsKeyedService). This change makes that contract explicit so keyed-service branches no longer require null-forgiving operators.Nullability contract (implementation)
[MemberNotNullWhen(true, nameof(ServiceKey))]toServiceDescriptor.IsKeyedServiceinMicrosoft.Extensions.DependencyInjection.Abstractions.Nullability contract (reference assembly)
[MemberNotNullWhen(true, "ServiceKey")]annotation toServiceDescriptor.IsKeyedServiceinMicrosoft.Extensions.DependencyInjection.Abstractions/refto keep ref/src API annotations in sync.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.