Skip to content

Properties with mixed readonly accessors should skip escape checks where possible #35606

@RikkiGibson

Description

@RikkiGibson

Related to #35146 and #35597

In some cases involving mixed readonly/non-readonly properties and indexers, we should be able to tell that passing a ref-like variable to a readonly member will not allow that variable to escape, but we're currently pessimistic and require that all accessors be 'readonly' in order to decide the variable won't escape.

using System;

public ref struct S
{
    int this[Span<byte> span] { readonly get => 0; set {} }

    static void M(S s)
    {
        Span<byte> span = stackalloc byte[10];
        // We know 'span' won't escape because the getter is readonly
        // but we still give an error today.
        _ = s[span];
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions