Skip to content

Fix MVAR resolution in MethodDef_Instance::ResolveToken#3398

Merged
josesimoes merged 2 commits into
nanoframework:developfrom
josesimoes:fix-mvar-resolution
May 29, 2026
Merged

Fix MVAR resolution in MethodDef_Instance::ResolveToken#3398
josesimoes merged 2 commits into
nanoframework:developfrom
josesimoes:fix-mvar-resolution

Conversation

@josesimoes

Copy link
Copy Markdown
Member
  • MVAR (!!0) resolution now checks the MethodSpec first (authoritative IL-encoded generic argument) and only falls back to arrayElementType when no MethodSpec exists.

Description

Motivation and Context

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

-  MVAR (!!0) resolution now checks the MethodSpec first (authoritative IL-encoded generic argument) and only falls back to arrayElementType when no MethodSpec exists.
@josesimoes josesimoes added the Area: Common libs Everything related with common libraries label May 29, 2026
@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: d5746b15-d4ad-4ebc-bdfc-b12575c71a12

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The PR tightens generic method argument resolution in CLR_RT_MethodDef_Instance::ResolveToken(...) by reordering how DATATYPE_MVAR entries are handled. When a MethodSpec is present, it now authorizes argument resolution directly; otherwise, fallback to caller->arrayElementType is restricted to position 0 only.

Changes

MVAR Resolution Logic

Layer / File(s) Summary
Generic argument resolution for DATATYPE_MVAR
src/CLR/Core/TypeSystem.cpp
When hasMethodSpec is true, msInst.GetGenericArgument(...) resolves DATATYPE_MVAR directly from MethodSpec instead of preferring caller's arrayElementType first. Fallback to caller->arrayElementType is restricted to GenericParamPosition 0; other positions now fail resolution by setting allResolved = false.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing MVAR resolution in a specific method, staying within the 50-character guideline at 55 characters.
Description check ✅ Passed The description is directly related to the changeset, explaining the MVAR resolution fix, its motivation from failing unit tests, and testing approach.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@josesimoes

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/CLR/Core/TypeSystem.cpp (1)

2265-2288: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Fail unresolved MethodSpec-to-VAR chains instead of storing an empty class token.

If GetGenericArgument() returns DATATYPE_VAR and the caller type context is missing or still doesn't resolve to a concrete type, this path currently falls back to msArgElem.Class / paramElem.Class. Those fields are not guaranteed to be populated for open VAR/GENERICINST, so resolvedArgs[a] can stay empty and later match the wrong closed TypeSpec instead of failing resolution.

Suggested fix
-                                                if (msInst.GetGenericArgument(argElem.GenericParamPosition, msArgElem))
+                                                if (msInst.GetGenericArgument(argElem.GenericParamPosition, msArgElem))
                                                 {
                                                     if (msArgElem.DataType == DATATYPE_VAR &&
                                                         caller->genericType != nullptr &&
                                                         NANOCLR_INDEX_IS_VALID(*caller->genericType))
                                                     {
@@
                                                         if (callerTsInst.InitializeFromIndex(*caller->genericType) &&
                                                             callerTsInst.GetGenericParam(
                                                                 msArgElem.GenericParamPosition,
                                                                 paramElem))
                                                         {
-                                                            resolvedArgs[a] = paramElem.Class;
+                                                            if (NANOCLR_INDEX_IS_VALID(paramElem.Class))
+                                                            {
+                                                                resolvedArgs[a] = paramElem.Class;
+                                                            }
+                                                            else
+                                                            {
+                                                                allResolved = false;
+                                                            }
                                                         }
                                                         else
                                                         {
                                                             allResolved = false;
                                                         }
                                                     }
-                                                    else
+                                                    else if (NANOCLR_INDEX_IS_VALID(msArgElem.Class))
                                                     {
                                                         resolvedArgs[a] = msArgElem.Class;
                                                     }
+                                                    else
+                                                    {
+                                                        allResolved = false;
+                                                    }
                                                 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/CLR/Core/TypeSystem.cpp` around lines 2265 - 2288, When
GetGenericArgument() yields DATATYPE_VAR but the caller type context is missing
or cannot resolve to a concrete type, don't fall back to using msArgElem.Class
or paramElem.Class (which can be empty); instead mark resolution as failed. In
the block checking msArgElem.DataType == DATATYPE_VAR and caller->genericType,
if callerTsInst.InitializeFromIndex(...) or callerTsInst.GetGenericParam(...)
fails, set allResolved = false and do not assign resolvedArgs[a]; also if
msArgElem.DataType == DATATYPE_VAR and the caller context is null/invalid, set
allResolved = false rather than assigning msArgElem.Class. This ensures
unresolved VAR/GENERICINST chains cause resolution to fail (use symbols:
GetGenericArgument, DATATYPE_VAR, caller->genericType, CLR_RT_TypeSpec_Instance,
InitializeFromIndex, GetGenericParam, resolvedArgs[a], allResolved).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/CLR/Core/TypeSystem.cpp`:
- Around line 2265-2288: When GetGenericArgument() yields DATATYPE_VAR but the
caller type context is missing or cannot resolve to a concrete type, don't fall
back to using msArgElem.Class or paramElem.Class (which can be empty); instead
mark resolution as failed. In the block checking msArgElem.DataType ==
DATATYPE_VAR and caller->genericType, if callerTsInst.InitializeFromIndex(...)
or callerTsInst.GetGenericParam(...) fails, set allResolved = false and do not
assign resolvedArgs[a]; also if msArgElem.DataType == DATATYPE_VAR and the
caller context is null/invalid, set allResolved = false rather than assigning
msArgElem.Class. This ensures unresolved VAR/GENERICINST chains cause resolution
to fail (use symbols: GetGenericArgument, DATATYPE_VAR, caller->genericType,
CLR_RT_TypeSpec_Instance, InitializeFromIndex, GetGenericParam, resolvedArgs[a],
allResolved).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5a3dcec8-58cd-48e1-aaca-8e27b445c95f

📥 Commits

Reviewing files that changed from the base of the PR and between bc59b2b and 5ca5929.

📒 Files selected for processing (1)
  • src/CLR/Core/TypeSystem.cpp

@josesimoes
josesimoes enabled auto-merge (squash) May 29, 2026 13:49
@josesimoes
josesimoes disabled auto-merge May 29, 2026 14:28
@josesimoes
josesimoes merged commit 081d6ed into nanoframework:develop May 29, 2026
23 checks passed
@josesimoes
josesimoes deleted the fix-mvar-resolution branch May 29, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Common libs Everything related with common libraries Type: bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants