You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Correctly perform analysis for compiler-generated methods #2001
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.
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.
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.
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.
Scan compiler-generated methods as a group #2792
This put the architecture in place to do interprocedural analysis for groups of methods that are part of the compiler-generated code for a method in user code. It depended on architectural changes and related bugfixes in the linker:
Add compiler-generated code dataflow analysis #2842
This is flowing annotated values into lamdas and local functions, and across await/yield points in iterator state machines. This approach leaves some analysis holes that I intend to fix before merging the change:
Activity
marek-safar commentedon Apr 29, 2021
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.
agocke commentedon Apr 29, 2021
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.
MichalStrehovsky commentedon Apr 30, 2021
The .NET Native analyzer hardcoded the shape of e.g.
asyncstate 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.Warning suppressions in compiler generated code (#2075)
vitek-karas commentedon Jun 17, 2021
#2075 implements support for warning suppression via
UnconditionalSuppressMessageorRequiresUnreferencedCodeon iterator and async method bodies.jtschuster commentedon Jan 28, 2022
In discussion and according to the docs Requires.. attributes should propagate to local functions and lambdas.
sbomer commentedon Jun 23, 2022
Some updates on what's done and what's left:
Completed:
Lambda and local function suppressions #2689
This allows RUC and UnconditionalSuppressMessage to work more or less as described in https://github.com/dotnet/linker/blob/main/docs/design/compiler-generated-code-handling.md, on lambdas and local functions.
Related:
Track attributes through Roslyn-generated types #2786
This flows DAM annotations on type parameters to usages in state machine methods, as described in https://github.com/dotnet/linker/blob/main/docs/design/compiler-generated-code-handling.md#b5---generic-parameter-data-flow-in-iterator-body (for iterators, same for async methods). Lambdas and local functions are still not fully supported.
Related:
Scan compiler-generated methods as a group #2792
This put the architecture in place to do interprocedural analysis for groups of methods that are part of the compiler-generated code for a method in user code. It depended on architectural changes and related bugfixes in the linker:
Related:
In progress/remaining:
This is flowing annotated values into lamdas and local functions, and across await/yield points in iterator state machines. This approach leaves some analysis holes that I intend to fix before merging the change:
This will make the linker behavior more consistent,
and simplify the analyzer so that it doesn't have to match the linker behavior.Would be good to fix, but not necessary.Known issues:
agocke commentedon Jul 27, 2022
@sbomer Is this good to be closed out?
sbomer commentedon Jul 27, 2022
Yup I think so!
Warning suppressions in compiler generated code (dotnet/linker#2075)