Skip to content

Unused byref dereference is removed despite NRE as a side effect #72165

@EgorBo

Description

@EgorBo

Roslyn should not optimize unused byref dereferences due to possible side effect of NRE. E.g. https://github.com/dotnet/runtime/pull/98547/files PR used such dereferences intentionally to throw NRE before we enter unmanaged code.

Version Used:
Reproduces in .NET 8.0 and in Main on Sharplab.

Steps to Reproduce:

static void Test(ref byte b)
{
    _ = b; // dereference, hence, NRE as a side effect
}

Expected Behavior:

.method assembly hidebysig static Test (uint8& b)
{
    .maxstack 8
    ldind.u1
    pop
    ret
}

Actual Behavior:

.method assembly hidebysig static Test (uint8& b)
{
    .maxstack 8
    ret
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CompilersuntriagedIssues and PRs which have not yet been triaged by a lead

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions