Simplify branching in ILImporter.Scanner.cs with Debug.Assert #123235
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Replace explicit
else if (targetMethod.AcquiresInstMethodTableFromThis())withelse { Debug.Assert(...); }to tighten the invariant that shared generic methods acquire context via exactly one of three mutually exclusive paths: method dictionary, type dictionary, orthispointer.Changes
File:
src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.csIn the
directCallpath withinexactContextNeedsRuntimeLookup:Aligns with existing pattern in
CorInfoImpl.GetGenericRuntimeLookupKind. No functional behavior change.Testing
Original prompt
Goal: Simplify branching in ILImporter.Scanner.cs by replacing the explicit
else if (targetMethod.AcquiresInstMethodTableFromThis())with a plainelsethat begins withDebug.Assert(targetMethod.AcquiresInstMethodTableFromThis()). This matches the invariant that, in shared generic code, if a method does not require a method dictionary or a type (method table) dictionary argument, it must acquire the generic context fromthis.Background:
RequiresInstMethodDescArg()for method dictionaryRequiresInstMethodTableArg()for type/method table dictionaryAcquiresInstMethodTableFromThis()forthispointerMethodDesccomments) and mirrored in managedTypeExtensions. Other places in the codebase (e.g.,CorInfoImpl.GetGenericRuntimeLookupKind) already use theelse + Debug.Assert(...)pattern for this invariant.Change scope:
directCallpath, inside theexactContextNeedsRuntimeLookupbranch, under the conditionif (targetMethod.IsSharedByGenericInstantiations && !resolvedConstraint && !referencingArrayAddressMethod).else if (targetMethod.AcquiresInstMethodTableFromThis()) { _dependencies.Add(_factory.ShadowNonConcreteMethod(concreteMethod), reason); }with:
else { Debug.Assert(targetMethod.AcquiresInstMethodTableFromThis()); _dependencies.Add(_factory.ShadowNonConcreteMethod(concreteMethod), reason); }Rationale:
CorInfoImpl.GetGenericRuntimeLookupKind).Exact edit (before/after):
Before:
After: