Skip to content

Remove nullability annotation from MetadataLoadContext.CoreAssembly.#126142

Open
teo-tsirpanis wants to merge 4 commits intodotnet:mainfrom
teo-tsirpanis:mlc-core-assembly-nullability
Open

Remove nullability annotation from MetadataLoadContext.CoreAssembly.#126142
teo-tsirpanis wants to merge 4 commits intodotnet:mainfrom
teo-tsirpanis:mlc-core-assembly-nullability

Conversation

@teo-tsirpanis
Copy link
Copy Markdown
Contributor

This property will never be null, because failing to resolve the core assembly would throw when creating the MLC. We update the property's nullability annotation, and simplify code flow to make this evident.

Copilot AI review requested due to automatic review settings March 26, 2026 02:54
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 26, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates MetadataLoadContext.CoreAssembly to be non-nullable by making core-assembly resolution an up-front, throwing operation, and refactors related initialization to reflect that invariant.

Changes:

  • Change CoreAssembly from Assembly? to Assembly (including the ref assembly).
  • Resolve and store the core assembly during MetadataLoadContext construction via a new LoadCoreAssembly helper.
  • Refactor CoreTypes initialization to accept a resolved RoAssembly directly.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/CoreTypes.cs Refactors CoreTypes ctor to take a resolved core RoAssembly and simplifies initialization loop.
src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/MetadataLoadContext.CoreAssembly.cs Replaces TryGetCoreAssembly with LoadCoreAssembly and changes _coreAssembly to non-nullable readonly.
src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/MetadataLoadContext.Apis.cs Loads core assembly up-front in the constructor and updates CoreAssembly property signature to non-nullable.
src/libraries/System.Reflection.MetadataLoadContext/ref/System.Reflection.MetadataLoadContext.cs Updates public API surface for CoreAssembly nullability in the reference assembly.
Comments suppressed due to low confidence (1)

src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/MetadataLoadContext.Apis.cs:248

  • The XML doc comment for CoreAssembly still states that the value may be null and that the core assembly is loaded lazily. With the property now non-nullable and the constructor resolving the core assembly up-front (throwing if it can’t be found), the docs should be updated to match the actual behavior/contract.
        public Assembly CoreAssembly
        {
            get
            {
                if (IsDisposed)

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@teo-tsirpanis teo-tsirpanis requested a review from jkotas April 15, 2026 14:42
Copilot AI review requested due to automatic review settings April 15, 2026 18:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 15, 2026

Could you please address copilot feedback?

Copilot AI review requested due to automatic review settings April 15, 2026 21:49
@teo-tsirpanis teo-tsirpanis force-pushed the mlc-core-assembly-nullability branch from c07808a to cde9080 Compare April 15, 2026 21:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment on lines 223 to 224
/// If a core assembly cannot be found or if the core assembly is missing types, this will affect the behavior of the MetadataLoadContext as follows:
///
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The XML docs still describe behavior when a core assembly cannot be found, but MetadataLoadContext now resolves the core assembly during construction and throws if it can’t be resolved. Please update this section to reflect that failure to resolve the core assembly results in an exception at construction time, and keep the remaining guidance focused on the “core assembly missing types” case (which can still happen).

Copilot uses AI. Check for mistakes.
Comment on lines 211 to 212
/// The CoreAssembly is determined by passing the coreAssemblyName parameter passed to the MetadataAssemblyResolver constructor
/// to the MetadataAssemblyResolver's Resolve method.
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs say the coreAssemblyName parameter is “passed to the MetadataAssemblyResolver constructor”, but coreAssemblyName is a MetadataLoadContext constructor parameter and is what gets passed to MetadataAssemblyResolver.Resolve. Please correct this wording to avoid confusing API consumers.

Copilot uses AI. Check for mistakes.
Comment on lines +14 to +16
internal RoAssembly LoadCoreAssembly(string? coreAssemblyName)
{
Debug.Assert(_coreAssembly == null);
RoAssembly? coreAssembly;
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoadCoreAssembly no longer uses Debug/System.Diagnostics in this file. Please remove the now-unused using System.Diagnostics; directive to avoid CS8019 warnings (which may be treated as errors in this repo’s build settings).

Copilot uses AI. Check for mistakes.
@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 16, 2026

More copilot nits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Reflection community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants