Skip to content

Commit 9afb86f

Browse files
authored
Merge pull request #88 from azeno/master
Explicitly cast recursive field to IEnumerable in generated GetEnumerator methods
2 parents f707f60 + d9b0501 commit 9afb86f

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/ImmutableObjectGraph.Generation.Tests/CodeGenTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public async Task ByteArray_CanBuild()
7474
var result = await this.GenerateFromStreamAsync("ByteArray");
7575
}
7676

77+
[Fact]
78+
public async Task ImmutableArray_CanBuild()
79+
{
80+
var result = await this.GenerateFromStreamAsync("ImmutableArray");
81+
}
82+
7783
[Fact]
7884
public async Task OneScalarFieldAndEmptyDerived_HasCreateMethod()
7985
{

src/ImmutableObjectGraph.Generation.Tests/ImmutableObjectGraph.Generation.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
5959
</Compile>
6060
<EmbeddedResource Include="TestSources\ByteArray.cs" />
61+
<EmbeddedResource Include="TestSources\ImmutableArray.cs" />
6162
<Compile Include="TestSources\Generations.cs">
6263
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
6364
</Compile>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace ImmutableObjectGraph.Generation.Tests.TestSources
2+
{
3+
using System.Collections.Immutable;
4+
5+
[GenerateImmutable]
6+
partial class Node
7+
{
8+
readonly ImmutableArray<Node> children;
9+
}
10+
}

src/ImmutableObjectGraph.Generation/CodeGen+EnumerableRecursiveParentGen.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ private void ImplementIEnumerableInterfaces()
7474
{
7575
this.baseTypes.Add(SyntaxFactory.SimpleBaseType(Syntax.IEnumerableOf(this.generator.applyToMetaType.RecursiveType.TypeSyntax)));
7676

77-
// return this.<#=templateType.RecursiveField.NameCamelCase#>.GetEnumerator();
77+
// return ((IEnumerable<RecursiveType>)this.<#=templateType.RecursiveField.NameCamelCase#>).GetEnumerator();
7878
var body = SyntaxFactory.Block(
7979
SyntaxFactory.ReturnStatement(
8080
SyntaxFactory.InvocationExpression(
8181
SyntaxFactory.MemberAccessExpression(
8282
SyntaxKind.SimpleMemberAccessExpression,
83-
Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name)),
84-
SyntaxFactory.IdentifierName(nameof(IEnumerable<int>.GetEnumerator))),
85-
SyntaxFactory.ArgumentList())));
83+
SyntaxFactory.ParenthesizedExpression(
84+
SyntaxFactory.CastExpression(
85+
Syntax.IEnumerableOf(GetFullyQualifiedSymbolName(this.generator.applyToMetaType.RecursiveField.ElementType)),
86+
Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name)))),
87+
SyntaxFactory.IdentifierName(nameof(IEnumerable<int>.GetEnumerator))),
88+
SyntaxFactory.ArgumentList())));
8689

8790
// public System.Collections.Generic.IEnumerator<RecursiveType> GetEnumerator()
8891
this.innerMembers.Add(

0 commit comments

Comments
 (0)