Skip to content

Ref safety diagnostics downgraded to warnings even in some safe contexts #83564

@jjonescz

Description

@jjonescz

Version Used: 5.7.0-1.26217.5 (77dc97b)

Steps to Reproduce:

.NET Lab

using System.Collections.Generic;
using System.Runtime.CompilerServices;

class C
{
    unsafe void M1()
    {
        int p = *default(int*); // no error
        ref int outside = ref Unsafe.NullRef<int>();
        {
            int local = 1;
            outside = ref local; // warning
        }
    }

    unsafe IEnumerable<int> M2()
    {
        int p = *default(int*); // error
        ref int outside = ref Unsafe.NullRef<int>();
        {
            int local = 1;
            outside = ref local; // warning
        }
        yield return 1;
    }
}

Expected Behavior: the ref safety violation in M2 should be an error, since it's in a safe context

Actual Behavior: it's a warning (it thinks it's in unsafe context since M2 is declared as unsafe but doesn't consider that it's an iterator)

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions