Skip to content

Commit

Permalink
Fix #74 Prevent a crash when there's a complex expression to the left…
Browse files Browse the repository at this point in the history
… of the dot in an extension method
  • Loading branch information
virzak committed Apr 26, 2024
1 parent b1178fa commit 2a3c703
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static BinaryExpressionSyntax CheckNull(ExpressionSyntax expr) => BinaryExpressi
CastExpression(NullableType(IdentifierName(SystemObject)), expr).AppendSpace(),
LiteralExpression(SyntaxKind.NullLiteralExpression).PrependSpace());

var argumentType = GetSymbol(leftOfTheDot) ?? throw new InvalidOperationException("Can't process");
var argumentType = GetSymbol(node.Expression) ?? throw new InvalidOperationException("Can't process");
var funcArgumentType = GetReturnType(argumentType);

IdentifierNameSyntax toCheckForNullExpr;
Expand Down
73 changes: 73 additions & 0 deletions tests/Generator.Tests/ExtensionMethodTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace Generator.Tests;

public class ExtensionMethodTests
{
[Fact]
public Task UnwrapGenericExtensionMethod() => """
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;

namespace Zomp.SyncMethodGenerator.IntegrationTests
{
using Extensi.ons123;
partial class Extensions
{
[CreateSyncVersion]
public static async Task HasGenericExtensionAsync(object o, CancellationToken ct)
{
var z = o.TryGetValue<Point>(out var item);
}

[CreateSyncVersion]
public static async Task HasGeneric2ExtensionAsync(object o, CancellationToken ct)
{
var z = o.TryGetValue<Point, PointF>(out var _, out var _1);
}
}
}

namespace Extensi.ons123
{
internal static class MyExtensionClass
{
public static bool TryGetValue<T>(this object _, out T? item)
{
item = default;
return false;
}

public static bool TryGetValue<T1, T2>(this object _, out T1? item1, out T2? item2)
{
item1 = default;
item2 = default;
return false;
}
}
}
""".Verify(sourceType: SourceType.Full);

[Fact]
public Task LeftOfTheDotTest() => """
namespace Tests;

internal class Bar
{
public static Bar Create() => new Bar();
}

partial class Class
{
[Zomp.SyncMethodGenerator.CreateSyncVersion]
public async Task MethodAsync()
{
_ = Bar.Create()?.DoSomething();
}
}

internal static class BarExtension
{
public static Bar DoSomething(this Bar bar) => bar;
}
""".Verify(sourceType: SourceType.Full);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: Tests.Class.MethodAsync.g.cs
// <auto-generated/>
#nullable enable
namespace Tests;
partial class Class
{
public void Method()
{
_ = ((global::System.Func<global::Tests.Bar?,global::Tests.Bar?>)((param)=>(object?)param == null?(global::Tests.Bar?)null: global::Tests.BarExtension.DoSomething(param)))(global::Tests.Bar.Create());
}
}
45 changes: 0 additions & 45 deletions tests/Generator.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,51 +643,6 @@ These comments shouldn't show
#endregion
}

""".Verify(sourceType: SourceType.Full);

[Fact]
public Task UnwrapGenericExtensionMethod() => """
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;

namespace Zomp.SyncMethodGenerator.IntegrationTests
{
using Extensi.ons123;
partial class Extensions
{
[CreateSyncVersion]
public static async Task HasGenericExtensionAsync(object o, CancellationToken ct)
{
var z = o.TryGetValue<Point>(out var item);
}

[CreateSyncVersion]
public static async Task HasGeneric2ExtensionAsync(object o, CancellationToken ct)
{
var z = o.TryGetValue<Point, PointF>(out var _, out var _1);
}
}
}

namespace Extensi.ons123
{
internal static class MyExtensionClass
{
public static bool TryGetValue<T>(this object _, out T? item)
{
item = default;
return false;
}

public static bool TryGetValue<T1, T2>(this object _, out T1? item1, out T2? item2)
{
item1 = default;
item2 = default;
return false;
}
}
}
""".Verify(sourceType: SourceType.Full);

#if NETCOREAPP1_0_OR_GREATER
Expand Down

0 comments on commit 2a3c703

Please sign in to comment.