dotnet / dotnet/roslyn

Unexpected indirect load instruction is emitted in release mode

Open
#84,547 4 comments 0 reactions 1 assignee Claimed by @AlekseyTs View on GitHub
Area-Compilers
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

```
[Fact]
public void Retrack_PointerToRefLocal_Nested_02()
{
var source = """
class C
{
unsafe void M(byte* p)
{
ref byte b1 = ref GetRef();
ref byte local = ref b1;
{
ref byte b2 = ref *p;
local = ref b2;
}
local.ToString();
}

ref byte GetRef() => throw null;
}
""";
CompileAndVerify(source, verify: Verification.Fails, options: TestOptions.UnsafeDebugDll).VerifyIL("C.M", """
{
// Code size 24 (0x18)
.maxstack 1
.locals init (byte& V_0, //b1
byte& V_1, //local
byte& V_2) //b2
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call "ref byte C.GetRef()"
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: stloc.1
IL_000a: nop
IL_000b: ldarg.1
IL_000c: stloc.2
IL_000d: ldloc.2
IL_000e: stloc.1
IL_000f: nop
IL_0010: ldloc.1
IL_0011: call "string byte.ToString()"
IL_0016: pop
IL_0017: ret
}
""");
CompileAndVerify(source, verify: Verification.Fails, options: TestOptions.UnsafeReleaseDll).VerifyIL("C.M", """
{
// Code size 18 (0x12)
.maxstack 1
.locals init (byte& V_0) //b2
IL_0000: ldarg.0
IL_0001: call "ref byte C.GetRef()"
IL_0006: ldind.u1
IL_0007: pop
IL_0008: ldarg.1
IL_0009: stloc.0
IL_000a: ldloc.0
IL_000b: call "string byte.ToString()"
IL_0010: pop
IL_0011: ret
}
""");
}
```

Observed: ```IL_0006: ldind.u1``` is unexpected

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.