Skip to content

CallerMemberNameAttribute on non-function member #1549

@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

Describe the bug

The standard does not specify what becomes the default value of a parameter decorated with CallerMemberNameAttribute when the parameter belongs to a constructor of an attribute, and a property or an indexer (or a type parameter of an indexer) is decorated with this attribute. (The standard specifies this if an accessor of a property or indexer is so decorated.)

C# 9 draft section 23.5.6.4 The CallerMemberName attribute.

Example

using System;
using System.Runtime.CompilerServices;

public class MAttribute : Attribute {
    public MAttribute(
        [CallerMemberName]string? name = null) {
    }
}

class C {
    [M] object o = new MAttribute();
    
    [M] object P {
        [M] get => new MAttribute();
    }

    [M] object this[[M]int index] {
        [M] get => new MAttribute();
    }

    [M] delegate void D<[M]T>();
}

Expected behavior

Should be equivalent to:

class C {
    [M(null)] object o = new MAttribute("o");
    
    [M("P")] object P {
        [M("P")] get => new MAttribute("P");
    }

    [M("Item")] object this[[M("Item")]int index] {
        [M("Item")] get => new MAttribute("Item");
    }

    [M(null)] delegate void D<[M(null)]T>();
}

Additional context

Noticed from #1548.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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