Skip to content

Implicitly typed lambdas with [UnscopedRef] don't match their own anonymous delegate types #63565

@TessenR

Description

@TessenR

Version Used:

commit e041ae9802b68f7c6f6903ec92b56d27ff0caec3
Author: Youssef Victor <youssefvictor00@gmail.com>
Date:   Wed Aug 24 16:00:43 2022 +0200

    Delete extra character in Syntax.xml (#63561)

Steps to Reproduce:

using System.Diagnostics.CodeAnalysis;

public ref struct RefStruct { }
public struct RegularStruct { }

delegate void D1([UnscopedRef] ref RefStruct s);
delegate void D2([UnscopedRef] out RegularStruct s);

public class C
{
  public void M()
  {
    var x = ([UnscopedRef] ref RefStruct s) => { }; // error CS8986: The 'scoped' modifier of parameter 's' doesn't match target '<anonymous delegate>'.
    var y = ([UnscopedRef] out RegularStruct s) => { }; // error CS8986: The 'scoped' modifier of parameter 's' doesn't match target '<anonymous delegate>'.
    D1 q = ([UnscopedRef] ref RefStruct s) => { };
    D2 w = ([UnscopedRef] out RegularStruct s) => { };
  }
}

namespace System.Diagnostics.CodeAnalysis
{
  [AttributeUsage(AttributeTargets.All, Inherited = false)]
  internal sealed class UnscopedRefAttribute : Attribute
  {
    public UnscopedRefAttribute()
    {
    }
  }
}

Expected Behavior:
No errors for lambda expressions assigned to var variables. The anonymous delegates created for them should have [UnscopedRef] annotations on their parameters

Actual Behavior:
Lambda expressions with completely valid signatures (as demonstrated by D1, D2 and corresponding lambda expressions) produce errors due to mismatches with their own implicit types

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions