C#: Fix template substitution for method names and variadic args#6940
Merged
knutwannheden merged 1 commit intomainfrom Mar 11, 2026
Merged
C#: Fix template substitution for method names and variadic args#6940knutwannheden merged 1 commit intomainfrom
knutwannheden merged 1 commit intomainfrom
Conversation
…adic args SubstitutionVisitor.VisitIdentifier was the only substitution path, but base visitors for MethodInvocation and FieldAccess never visit their Name children, leaving placeholders unresolved in those positions. Variadic captures stored as IReadOnlyList<object> also failed the IReadOnlyList<T> type check in MatchResult.GetList, returning empty and leaving placeholders. - Override VisitMethodInvocation/VisitFieldAccess to substitute name placeholders - Add ExpandVariadicArgs to replace variadic placeholder with captured arg list - Fix GetList<T> to handle IReadOnlyList<object> via Cast<T>() (fails-fast on type mismatch instead of silently dropping with OfType) - Handle empty variadic captures by removing placeholder rather than keeping it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
CSharpTemplate.Apply()outputs placeholder names instead of captured values when captures appear in method-name or variadic-argument positions:When matching
new Random().Next(10), the template produces:instead of:
Summary
SubstitutionVisitorto substitute placeholders in method-name positions (MethodInvocation.Name) and field-name positions (FieldAccess.Name), which the base visitor never visitsMatchResult.GetList<T>to handleIReadOnlyList<object>(as stored by the pattern matcher) viaCast<T>()instead of failing the type check silentlyCast<T>()instead ofOfType<T>()to fail fast on type mismatchesTest plan
SubstitutesMethodNameCapture: pattern captures method name, template substitutes it into a different targetSubstitutesVariadicArgs: variadic capture of multiple args, template substitutes into different methodSubstitutesMethodNameAndVariadicArgs: combined method name + variadic args (new Random().Next(10)→Random.Shared.Next(10))SubstitutesFieldNameCapture: isolated field-name substitution (DateTime.Now→DateTimeOffset.Now)