Skip to content

Mark methods of automatically implemented properties with CompilerGenerated attribute #11535

Closed

Description

@BoundedChenn31

Is your feature request related to a problem? Please describe.
C# compilers mark methods of auto-implemented properties with CompilerGenerated attribute (example) and F# doesn't (example). Libraries can rely on the presence of this attribute to detect such properties. In particular, realm-dotnet rewrites methods of auto-implemented properties using Fody and ignores properties with explicit backing field.

I think, it's worth it to improve compatibility with C#-oriented libraries and allow to differentiate auto-implemented properties from explicit ones using reflection at cost of a few bytes of IL-code.

Describe the solution you'd like
Mark methods of auto-implemented properties.

Describe alternatives you've considered

  • On library side detection of auto-implemented properties can be extended by checking the presence of backing field with name <PropertyName>@. It's a hack but covers 99.9% of cases. Technically, detection can be broken by declaring field <PropertyName>@, but names with @ are reserved for compiler, so it's not really an issue.
  • Library consumers can use C# or implement explicit properties with backing field and mark property methods manually.

Additional context
.

Activity

  1. smoothdeveloper commented on May 6, 2021

    @smoothdeveloper
    Contributor

    Here is an example of a weaver (not related with realm AFAIU):

    https://github.com/robinzevallos/kasay-propertychanged/blob/1b86901da71b6767c21bc164072bc6b0b0d5d1e3/Kasay.PropertyChanged.Fody/ModuleWeaver/PropertyChangedWeaver.cs

    this one seem to rely on the C# backing field name, which wouldn't work with VB.NET (https://sharplab.io/#v2:DYLgbgRgNAJiDUAfAwsAhgZwwAgCoE8AHAUwFgAobK7ABQCcB7EugF32wDEBLOjFgOTQBbYtgCCOAMos6XAHYBzCgFE5MbKkwYgA).

    So it seems the weavers are all going with their own approach at identifying things they want to weave.

    It makes sense to add the attribute to align with the two other languages.

    allow to differentiate auto-implemented properties from explicit ones using reflection

    Is presence of the CompilerGenerated attribute on a property 100%/only means it is an auto-implemented property?

    It seems to me the weavers would be better at looking at the IL for those scenarios to be robust, not just look at the attribute.

  2. BoundedChenn31 commented on May 7, 2021

    @BoundedChenn31
    ContributorAuthor

    Is presence of the CompilerGenerated attribute on a property 100%/only means it is an auto-implemented property?

    Looks like it, yes. There is a check that property is auto-implemented in Roslyn.

    It seems to me the weavers would be better at looking at the IL for those scenarios to be robust, not just look at the attribute.

    Well, auto-implemented property is just a 'syntax sugar'. It's IL-implementation does not differ that much from explicit.
    As you said, backing fields can be named differently, the same thing applies to property accessors (at least, this comment implies it) and method bodies are simple. In In this circumstance I see attribute as a universal contract that library authors can rely on instead of adjusting to every language.

    As a side note, methods that compiler generates for records (Equals(), ToString() and so on) are marked as CompilerGenerated, but properties and backing fields are not. Not sure if it's intentional design or just shared implementation.

  3. smoothdeveloper commented on May 7, 2021

    @smoothdeveloper
    Contributor

    Looks like it, yes. There is a check that property is auto-implemented in Roslyn.

    If I read that roslyn code correctly, it is the compiler writing the IL adding the attribute on the property, I don't take this as an immutable and unambiguous statement that the attribute "is only ever going to be used on properties when those are auto implemented", when this somehow changes, this is going to break some weavers 🙂.

    In In this circumstance I see attribute as a universal contract that library authors can rely on instead of adjusting to every language.

    I'd say a thing which holds true for 100% of current C# codebases, but not sure what the weavers are used for, hard for me to trust the attribute on a property will always mean what it is used for in that context.

    Nonetheless, by all means, if you want to contribute to the compiler to emit it, it seems the AutoProperty carries into the AST, and figuring out the code gen part for them shouldn't be too hard.

    I hope you can get confirmation from the team that such PR with few tests and required changes would be merged, but you may hack the implementation yourself to see what it takes and open a PR.

  4. dsyme commented on Apr 20, 2022

    @dsyme
    Contributor

    We should do this, it's a good first issue for people actually

  5. added
    Area-Compiler-CodeGen
    on Apr 20, 2022
  6. self-assigned this
    on Jul 7, 2022
  7. added this to the July-2022 milestone on Jul 7, 2022
  8. moved this from Not Planned to Planned in F# Compiler and Toolingon Jul 7, 2022
  9. Repository owner moved this from Planned to Done in F# Compiler and Toolingon Jul 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Compiler-CodeGenFeature Requestgood first issue

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions