Skip to content

Commit

Permalink
fix missing operand in LdObj instruction in some scenarios (#256)
Browse files Browse the repository at this point in the history
also refactor the test code to better convey intent and avoid code duplication
  • Loading branch information
adrianoc committed Sep 30, 2024
1 parent d81642e commit a63011e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,46 +72,39 @@ public void ImplicitNumericConversions_Are_Applied([Values("List<long>", "long[]
}

[Test]
public void ImplicitTypeConversions_Are_Applied([Values("List<Foo>", "Foo[]", "Span<Foo>")] string targetType, [Values("[2, 1]", "[5, 4, 3, 2, 1]")] string items)
public void ImplicitUserDefinedConversions_Are_Applied([Values("List<Foo>", "Foo[]", "Span<Foo>")] string targetType, [Values("[2, 1]", "[5, 4, 3, 2, 1]")] string items)
{
var (lengthExtractor, expectedILError) = targetType == "Span<Foo>" ? ("items.Length", "[ReturnPtrToStack]") : ("((ICollection<Foo>) items).Count", null);
AssertConversionIsApplied(targetType, items, "Foo");
}

[Test]
public void BoxConversions_Are_Applied([Values("List<object>", "object[]", "Span<object>")] string targetType, [Values("[2, 1]", "[5, 4, 3, 2, 1]")] string items)
{
AssertConversionIsApplied(targetType, items, "object");
}

void AssertConversionIsApplied(string targetType, string items, string elementType)
{
var (lengthExtractor, expectedILError) = targetType == $"Span<{elementType}>" ? ("items.Length", "[ReturnPtrToStack]") : ("((ICollection) items).Count", null);
AssertOutput(
$$"""
using System.Collections.Generic;
using System.Collections;
using System;
{{targetType}} items = {{items}};
// We can´t use a foreach (to simplify the code) due to issue #306
for(var i = 0; i < {{lengthExtractor}}; i++) System.Console.Write(items[i].Value);
for(var i = 0; i < {{lengthExtractor}}; i++) System.Console.Write(items[i]);
struct Foo
{
public Foo(int i) => Value = i;
public static implicit operator Foo(int i) => new Foo(i);
public static implicit operator int(Foo f) => f.Value;
public int Value;
}
""",
Regex.Replace(items, @"[\[\],\s+]", ""),
expectedILError);
}

[Test]
public void BoxConversions_Are_Applied([Values("List<object>", "object[]", "Span<object>")] string targetType, [Values("[2, 1]", "[5, 4, 3, 2, 1]")] string items)
{
var (lengthExtractor, expectedILError) = targetType == "Span<object>" ? ("items.Length", "[ReturnPtrToStack]") : ("((ICollection<object>) items).Count", null);
AssertOutput(
$$"""
using System.Collections.Generic;
using System;
{{targetType}} items = {{items}};
// We can´t usa a foreach (to simplify the code) due to issue #306
for(var i = 0; i < {{lengthExtractor}}; i++)
{
System.Console.Write(items[i]);
}
""",
Regex.Replace(items, @"[\[\],\s+]", ""),
expectedILError);
}
}
3 changes: 2 additions & 1 deletion Cecilifier.Core/AST/SyntaxWalkerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ protected void HandlePotentialRefLoad(string ilVar, SyntaxNode expression, IType

if (needsLoadIndirect)
{
Context.EmitCilInstruction(ilVar, type.LdindOpCodeFor());
var opCode = type.LdindOpCodeFor();
Context.EmitCilInstruction(ilVar, opCode, opCode == OpCodes.Ldobj ? Context.TypeResolver.Resolve(type) : null);
}
}

Expand Down

0 comments on commit a63011e

Please sign in to comment.