Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Correctly perform analysis for compiler-generated methods #2001

Closed

Description

@agocke

This a blanket issue for analyzing compiler generated code, like async/iterator state machines and nested functions (lambdas, local functions).

The first task is to design the expected user behavior. For instance, if you call a method annotated with RequiresUnreferencedCode in a local function, does placing RequiresUnreferencedCode on the containing method suppress the warning? Or do you need to place RequiresUnreferencedCode on the local function? The C# compiler is somewhat inconsistent about requirements here -- if you place unsafe on a method, the local function is considered unsafe as well. However, if you use await in a local function, making the parent method async is not sufficient; the local function must also be declared async.

It's up to us to decide the expected user experience.

For data flow analysis, we need to decide if the user experience should be closer to the source code representation, or to the behavior between methods. The linker doesn't normally do interprocedural analysis, and neither does the C# compiler, but for local functions C# does do interprocedural flow analysis for definite assignment. If we want the experience of captured variables to be closer to the experience of local variables, we likely need to do interprocedural analysis. If we think the experience of fields is sufficient, then we don't.

Once we decide on a user experience then need to examine technical requirements: do we think that attributes can be propagated automatically? Or do we want to try to rebuild some of the knowledge of source code from the IL? If we do need to rebuild some knowledge of source code, is there something we need or want from the C# compiler?

We also need to plan what's necessary for 6.0 vs later. We may want a short and long term solution.

Activity

  1. added this to the .NET 6.0 milestone on Apr 28, 2021
  2. marek-safar commented on Apr 29, 2021

    @marek-safar
    Contributor

    Please keep in mind that the linker is not the C# compiler post analyzer but a generic IL linking/trimming tool. The same experience should be available to developers writing F# code.

  3. agocke commented on Apr 29, 2021

    @agocke
    MemberAuthor

    Sure -- once we decide on the user experience we can design a technical solution which will hopefully be implementable by both the C# and F# compilers. Of course, support for any particular language will be dependent on the language's compiler implementing the feature.

  4. MichalStrehovsky commented on Apr 30, 2021

    @MichalStrehovsky
    Member

    The .NET Native analyzer hardcoded the shape of e.g. async state machines and was able to propagate dataflow through that. Given .NET Native is where the dataflow analysis originated, we could very cheaply steal that. I intentionally didn't port that part over because I pretty strongly think we should not do that. But it's temptingly cheap, so calling this out.

  5. vitek-karas commented on Jun 17, 2021

    @vitek-karas
    Member

    #2075 implements support for warning suppression via UnconditionalSuppressMessage or RequiresUnreferencedCode on iterator and async method bodies.

  6. modified the milestones: .NET 6.0, .NET 7.0 on Sep 28, 2021
  7. jtschuster commented on Jan 28, 2022

    @jtschuster
    Member

    In discussion and according to the docs Requires.. attributes should propagate to local functions and lambdas.

  8. sbomer commented on Jun 23, 2022

    @sbomer
    Member

    Some updates on what's done and what's left:

    Completed:

    In progress/remaining:

    Known issues:

  9. agocke commented on Jul 27, 2022

    @agocke
    MemberAuthor

    @sbomer Is this good to be closed out?

  10. sbomer commented on Jul 27, 2022

    @sbomer
    Member

    Yup I think so!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

  • Status
    No status

Relationships

None yet

Development

No branches or pull requests

Issue actions