Skip to content

Commit 36a85b0

Browse files
authored
Propagate the 'this' keyword in LibraryImports (#102793)
Since Roslyn just lowers the extension methods to a call to the static method there's no reason we shouldn't support creating extension methods that are also LibraryImport methods. This adds the IsExplicitThis property to TypePositionInfo and copies this to the generated signature if it is true.
1 parent 3393b4d commit 36a85b0

File tree

6 files changed

+325
-3
lines changed

6 files changed

+325
-3
lines changed

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ public static SyntaxTokenList GetManagedParameterModifiers(TypePositionInfo type
426426
}
427427
}
428428

429+
if (typeInfo.IsExplicitThis)
430+
{
431+
tokens = tokens.Add(Token(SyntaxKind.ThisKeyword));
432+
}
433+
429434
return tokens;
430435
}
431436

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypePositionInfo.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
using System;
55
using System.Collections.Generic;
6-
6+
using System.Linq;
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.CSharp.Syntax;
910
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1011

1112
namespace Microsoft.Interop
@@ -77,6 +78,7 @@ public static int IncrementIndex(int index)
7778

7879
public int ManagedIndex { get; init; } = UnsetIndex;
7980
public int NativeIndex { get; init; } = UnsetIndex;
81+
public bool IsExplicitThis { get; init; }
8082

8183
public static TypePositionInfo CreateForParameter(IParameterSymbol paramSymbol, MarshallingInfo marshallingInfo, Compilation compilation)
8284
{
@@ -88,7 +90,8 @@ public static TypePositionInfo CreateForParameter(IParameterSymbol paramSymbol,
8890
RefKind = paramSymbol.RefKind,
8991
ByValueContentsMarshalKind = byValueContentsMarshalKind,
9092
ByValueMarshalAttributeLocations = (inLocation, outLocation),
91-
ScopedKind = paramSymbol.ScopedKind
93+
ScopedKind = paramSymbol.ScopedKind,
94+
IsExplicitThis = ((ParameterSyntax)paramSymbol.DeclaringSyntaxReferences[0].GetSyntax()).Modifiers.Any(SyntaxKind.ThisKeyword)
9295
};
9396

9497
return typeInfo;

0 commit comments

Comments
 (0)