Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CodeConverter/CSharp/VbNameExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static bool IsReceiverTypeOrDerivedTypeEnclosingType(SyntaxNode node, IM
{
var classType = (ITypeSymbol)node.GetEnclosingDeclaredTypeSymbol(semanticModel);

return classType.InheritsFromOrEquals(symbol.ReceiverType);
return classType.InheritsFromOrEquals(symbol.ReceiverType, includeInterfaces: true);
}

/// <summary>
Expand Down Expand Up @@ -122,9 +122,13 @@ private static bool NameCanBeExpanded(SyntaxNode node)
private static INamedTypeSymbol GetDefaultImplicitInstance(SimpleNameSyntax node, ISymbol symbol, SemanticModel semanticModel, CancellationToken cancellationToken = default) =>
IsQualifiableInstanceReference(symbol) && IsLeftMostQualifier(node) ? semanticModel.GetEnclosingSymbol<INamedTypeSymbol>(node.SpanStart, cancellationToken) : null;

/// <remarks>
/// Reduced extension methods are never members of the qualifying type, but that's not type promotion.
/// Treating them as such replaces the whole access with its receiver, skipping the expansion of an implicit Me receiver within it.
/// </remarks>
private static bool IsTypePromotion(SyntaxNode node, ISymbol symbol, SemanticModel semanticModel)
{
if (IsQualifiableInstanceReference(symbol) && node is MemberAccessExpressionSyntax maes && maes.Expression != null) {
if (!symbol.IsReducedExtension() && IsQualifiableInstanceReference(symbol) && node is MemberAccessExpressionSyntax maes && maes.Expression != null) {
var qualifyingType = semanticModel.GetTypeInfo(maes.Expression).Type;
return qualifyingType == null || !qualifyingType.ContainsMember(symbol);
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/CSharp/MemberTests/ExtensionMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ public static void TestMethod2Parameters(this string str, string other)
}");
}

[Fact]
public async Task ImplicitMeExtensionCallKeepsReceiverAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Imports System.Collections.Generic
Imports System.Linq

Public Class MyList
Inherits List(Of Integer)

Public Function Uniques() As List(Of Integer)
Return Distinct().ToList()
End Function
End Class", @"using System.Collections.Generic;
using System.Linq;

public partial class MyList : List<int>
{

public List<int> Uniques()
{
return this.Distinct().ToList();
}
}");
}

[Fact]
public async Task TestExtensionMethodWithExistingImportAsync()
{
Expand Down
Loading