C#: Add semantic matching to pattern comparator#7069
Merged
knutwannheden merged 4 commits intomainfrom Mar 20, 2026
Merged
Conversation
…d type references Enable type-aware pattern matching that matches across syntactic forms when both sides resolve to the same symbol: - Static method invocations: Math.Abs(x) matches Abs(x) via using static when both have the same MethodType declaring type - Static fields: Math.PI matches PI via using static when both reference the same Variable with the same owner - Type references: Console matches System.Console when both resolve to the same FullyQualified type Also fixes Variable.FlagsBitMap not being populated in CSharpTypeMapping, and enables semantic model creation for template scaffolds when usings are provided.
…ching - Implicit this: Foo() matches this.Foo() and vice versa — when one MethodInvocation has no Select and the other's Select is "this", skip receiver comparison and match by name + arguments - Using aliases: Con.WriteLine() matches Console.WriteLine() when using Con = System.Console — this works automatically through the existing static method MethodType.DeclaringType comparison since Roslyn resolves aliases to canonical FQNs
- Compare TypeParameters in both semantic method invocation paths (static match and implicit-this) to prevent false matches on generic methods like Max<int>() vs Max<double>() - Check IsStatic on both sides in MatchFieldAccessToIdentifier for defensive correctness
…claring types - Use typed capture in InstanceMethodWithReceiverDoesNotMatchWithoutReceiver test so both sides get MethodType attribution, proving the IsStatic guard is load-bearing (red-green verified) - Unwrap JavaType.Parameterized when comparing declaring type FQNs — generic types like List<int> have Parameterized declaring types that need unwrapping to get the underlying Class FQN
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.
Summary
Math.Abs(x)matchesAbs(x)viausing static, and vice versa — comparesMethodType.DeclaringTypeFQN instead of receiver syntax when the method is static. Same for static fields (Math.PI↔PI)ConsolematchesSystem.Consolewhen both resolve to the sameJavaType.FullyQualifiedCon.WriteLine()matchesConsole.WriteLine()whenusing Con = System.Console— works automatically via Roslyn's alias resolution into canonical FQNsthismatching:Foo()matchesthis.Foo()for instance methodsJavaType.Variable.FlagsBitMapwas not populated inCSharpTypeMapping.MapVariable— now callsMapFlags(symbol)so static/public/etc. flags are setSemanticModelwhenusingsare provided, enabling type attribution on pattern treesTest plan