Skip to content

Commit

Permalink
[c#] laxer extension method matching (#5253)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierpinho authored Jan 24, 2025
1 parent b2bf9bd commit 133df78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ class CSharpScope(summary: CSharpProgramSummary)
name: String,
argTypes: List[String]
): CSharpMethod => Boolean = { m =>
m.isStatic && m.name == name && m.parameterTypes.map(_._2) == thisType :: argTypes
// TODO: we should also compare argTypes, however we first need to account for:
// a) default valued parameters in CSharpMethod, to account for different arities
// b) compatible/sub types, i.e. System.String should unify with System.Object.
m.isStatic && m.name == name && m.parameterTypes.map(_._2).headOption.contains(thisType)
}

/** Tries to find an extension method for [[baseTypeFullName]] with the given [[callName]] and [[argTypes]] in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ class ExtensionMethodTests extends CSharpCode2CpgFixture {

cpg.call.nameExact("DoStuff").callee.l shouldBe cpg.literal("2").method.l
}

"resolve correctly if the extra argument is type-compatible with the extension method's extra parameter" in {
val cpg = code("""
|using System.Collections.Generic;
|
|var x = new List<string>();
|x.DoStuff(null);
|
|static class Extensions
|{
| public static int DoStuff(this List<string> myList, object x) { return 2; }
|}
|""".stripMargin)

cpg.call.nameExact("DoStuff").callee.l shouldBe cpg.literal("2").method.l
}
}

"consecutive unary extension method calls" should {
Expand Down

0 comments on commit 133df78

Please sign in to comment.