Skip to content

Commit

Permalink
Fix #46 Call static field with generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
virzak committed Jan 21, 2024
1 parent e596a52 commit 23e00d5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ internal sealed class AsyncToSyncRewriter(SemanticModel semanticModel) : CSharpS
SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers |
SymbolDisplayMiscellaneousOptions.UseSpecialTypes);

private static readonly SymbolDisplayFormat GlobalDisplayFormatWithTypeParameters = new(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Included,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
miscellaneousOptions:
SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers |
SymbolDisplayMiscellaneousOptions.UseSpecialTypes);

private static readonly SymbolDisplayFormat NamespaceDisplayFormat = new(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.None,
Expand Down Expand Up @@ -254,7 +262,7 @@ static BinaryExpressionSyntax CheckNull(ExpressionSyntax expr) => BinaryExpressi
else if (node.Parent is not MemberAccessExpressionSyntax
&& GetSymbol(node) is IFieldSymbol { IsStatic: true } fieldSymbol)
{
var typeString = fieldSymbol.ContainingType.ToDisplayString(GlobalDisplayFormat);
var typeString = fieldSymbol.ContainingType.ToDisplayString(GlobalDisplayFormatWithTypeParameters);
return @base.WithIdentifier(Identifier($"{typeString}.{fieldSymbol.Name}"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<NoWarn>$(NoWarn);CS0219;CA1812;CA1852;SA1402;CA1822;SA1201;CA1303;SA1124;IDE0035;CS0162;RS1035;CS8619;CS8603;CA5394</NoWarn>
<NoWarn>$(NoWarn);CS0219;CA1812;CA1852;SA1402;CA1822;SA1201;CA1303;SA1124;IDE0035;CS0162;RS1035;CS8619;CS8603;CS1998;CA5394</NoWarn>
<ImplicitUsings>false</ImplicitUsings>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: Test.GenericClass.MethodAsync.g.cs
// <auto-generated/>
#nullable enable
namespace Test;
partial class GenericClass<T>
{
public void Method()
{
_ = global::Test.GenericClass<T>.Bar;
}
}
16 changes: 16 additions & 0 deletions tests/Generator.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ async void EmptyAsync()
{
}
}
""".Verify(sourceType: SourceType.Full);

[Fact]
public Task GenericClassMemberAccess() => """
namespace Test;
partial class GenericClass<T>
{
private const string Bar = "Bar";
[CreateSyncVersion]
public async Task MethodAsync()
{
_ = Bar;
}
}
""".Verify(sourceType: SourceType.Full);

[Fact]
Expand Down

0 comments on commit 23e00d5

Please sign in to comment.