Registry Mirror
Skip to content

Conversation

@dualfroz

Copy link
Copy Markdown
Contributor

_ = (Action<int>)M; never reads the cache field back, and the transform required exactly one read, so the output kept <>O.<0>__M, which does not compile.
With zero reads, the cache check is now replaced by the delegate construction it guarded.

Closes #3965

@siegfriedpammer

Copy link
Copy Markdown
Member

Did you use AI to implement this? Please read https://github.com/icsharpcode/ILSpy/blob/master/CONTRIBUTING.md#contributing especially bullet 2.

@dualfroz

Copy link
Copy Markdown
Contributor Author

Hi. I didn't use the AI to write the code itself. I did however use AI to make it easier to read. Same with the comment - i wrote it myself and then told ai to please make it look nicer. Will copy-paste this response in the other PR.

// delegate without ever reading the cache back. Keep the conversion,
// which still allocates, and drop the caching around it -- but only
// once the rest of the method is known not to read the field either.
if (context.Function.Descendants.Any(i => i != left && i.MatchLdsFld(field)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this check is not necessary (or rather harmful)...

I asked Claude about it, and it produced the following table:

Image

Would you be able to add more test cases to the pretty test?

dualfroz and others added 2 commits September 19, 2026 11:14
CachedDelegateInitializationWithField only fired when the instruction
after the caching `if` read the cache field exactly once. A discarded
conversion -- `_ = (Action<int>)M;` -- reads it zero times: Roslyn emits
the null check and the store, and nothing else. The transform bailed out,
so the `<>O` cache class and its `<0>__M` field survived into the output
under names no C# compiler will accept.

Zero usages is now handled: the `if` is replaced by the delegate
construction it guarded, which is what the source expressed, and the
caching disappears with the field. The rest of the method is checked for
loads of the same field first, since only then is this `if` the sole
initialization.

Closes #3965
Cover the method body shapes from the PR review and remove the extra field-load guard so discarded conversions are kept as allocations without preserving the cache check.

Assisted-by: OpenCode:openai/gpt-5.5:OpenCode
@siegfriedpammer
siegfriedpammer force-pushed the fix-discarded-method-group-cache branch from acdd004 to 1b2d77a Compare September 19, 2026 11:12
@siegfriedpammer

Copy link
Copy Markdown
Member

LGTM thanks!

@siegfriedpammer
siegfriedpammer merged commit f75fa75 into icsharpcode:master Sep 19, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discarded method-group conversion leaks the <>O delegate cache into the output

2 participants