Repository navigation
Warning suppressions in compiler generated code - #2075
Conversation
| case Language.FSharp: | ||
| if (memberReference.Name.Contains ('@')) | ||
| return true; | ||
| break; |
There was a problem hiding this comment.
Do we have a way to detect VB or compiler-generated code in VB assemblies?
There was a problem hiding this comment.
I'll double check, but my understanding is that the compiler will use the same naming scheme for VB and C#...
|
Does this work if I nest local functions? Level1();
static void Level1()
{
Level2();
static void Level2()
{
Level3();
static void Level3()
{
}
}
}I general, I would kind of prefer we do something more conservative, like look for AsyncStateMachineAttribute. I know that wouldn't solve local functions. But it would make the compiler feature request more urgent. If we add a workaround, however miserable it is, it is easy to brush off the compiler feature request with "they're unblocked and shipped this way". If I understand this correctly - we figure out the "owning user code" by looking at the code that triggered need for the method body we're looking at - I'm a bit worried about whether a situation can occur where a single generated method is referred to from multiple user method bodies - would the annotation propagate from user code we saw first only? I'm not confident I know all the situations where C# generates these helpers to be able to say these assumptions are safe. And I know nothing about the F# compiler. |
|
The intent is definitely for it to work in nested local functions - I just haven't written all the tests yet. Your other points are all valid:
|
|
Will this work if the reason why we're marking the compiler-generated thing is reflection/DynamicDependency/ILLinkTrim? I'm thinking of scenario where we include an entire type with all its compiler-generated members because it was subject to something like |
|
Looking for |
|
Ah, but if we don't need to identify the caller and only need to know that the method was generated by the compiler, that's different... We would need a new attribute though, because the |
This change implements the following high-level functionality: * UnconditionalSuppressMessage applies to the entire method body even for iterators, async methods and location functions and lambdas * RequiresUnreferencedCode automatically suppresses trim analysis warnings from entire method body for iterators, async methods and local functions and lambdas. Solution approach: * Detect compiler generated code - heuristic for .NET 6 * If in C# assembly, anything with `<` in its name is compiler generated * If in F# assembly, anything with `@` in its name is compiler generated * Looks for UnconditionalSuppressMessage and RequriesUnreferencedCode in: * If the warning origin is not a compiler generated code - simply use the warning origin (method) - existing behavior * If the warning origin is compiler generated use both the origin as well as the user defined method (non-compiler-generated) which is the source of the code for the compiler generated origin This is done by storing additional SuppressionContextMember on MessageOrigin which should always point to non-compiler-generated item. It's propagated using the scope stack in MarkStep and compiler-generated-code inherits the suppression context from its parent on the stack. Added lot of new tests for these scenarios.
e85e463 to
43c07c7
Compare
…butes (#10) Changes the implementation to not use marking stack to deduce suppression context. Instead it relies on state machine attributes (`IteratorStateMachine`, `AsyncStateMachine`, ...) - these are generated by the compiler onto the original user method and point to the state machine type (compiler generated) which implements the body of the method. This makes the behavior much more robust and predictable. The downside is that suppressions only propagate to iterator and async method bodies, they don't propagate to local functions and lambdas. The information about the mapping between methods and state machine types is cached to avoid rescans which are relatively expensive. Adapted the tests to the new behavior.
|
I think this is now ready for final review. The actually implemented solution is not based on heuristics as that lead to lot of trouble when code is accessed via reflection (in which case we can't rely on mark ordering to determine the "user method"). This now only works on async and iterator method bodies. Lambdas and local functions are not handled. |
| class <compiler_generated_state_machine>_type { | ||
| void MoveNext () | ||
| { | ||
| // This should normally produce IL2026 | ||
| CallSomethingWhichRequiresUnreferencedCode (); | ||
| } | ||
| } |
There was a problem hiding this comment.
Since this is public facing documentation, we should format this the way normal people expect C# code to be formatted.
| // Check both the origin's member (the direct method which contains the call to RUC) | ||
| // as well as the suppression context member (the "user code" source in case of compiler generated code) | ||
| IMemberDefinition suppressionContextMember = currentOrigin.SuppressionContextMember; | ||
| if (suppressionContextMember != null && | ||
| Annotations.HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (suppressionContextMember)) | ||
| return; | ||
|
|
||
| IMemberDefinition originMember = currentOrigin.MemberDefinition; | ||
| if (suppressionContextMember != originMember && originMember != null && | ||
| Annotations.HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (originMember)) |
There was a problem hiding this comment.
Can these two checks be extracted into a helper and shared with https://github.com/mono/linker/pull/2075/files#diff-565c4d3a1fda40fe49e36a001479e217a6a484722f6e97ee132c3ddcefdbd585R64?
| } | ||
|
|
||
| static bool HasRoslynCompilerGeneratedName (IMemberDefinition member) => | ||
| member.Name.Contains ('<') || member.DeclaringType == null || HasRoslynCompilerGeneratedName (member.DeclaringType); |
There was a problem hiding this comment.
This can be changed to take a TypeDefinition I think. Also, I'm not sure I understand the DeclaringType logic - should it be this instead?
| member.Name.Contains ('<') || member.DeclaringType == null || HasRoslynCompilerGeneratedName (member.DeclaringType); | |
| member.Name.Contains ('<') || (member.DeclaringType != null && HasRoslynCompilerGeneratedName (member.DeclaringType)); |
There was a problem hiding this comment.
You're right on both accounts - fixed.
This change implements the following high-level functionality: * `UnconditionalSuppressMessage` applies to the entire method body even for iterators, async methods * `RequiresUnreferencedCode` automatically suppresses trim analysis warnings from entire method body for iterators, async methods Solution approach: * Detect compiler generated code by using the `IteratorStateMachineAttribute`, `AsyncStateMachineAttribute` and `AyncIteratorStateMachineAttribute`. * When a method which is compiler generated (detected by looing for `<` in its name) is processed the original "user method" is determined by looking for a method with the "state machine" attribute pointing to the compiler generated type. * This information is cached to avoid duplication of relatively expensive detection logic. * Looks for `UnconditionalSuppressMessage` and `RequriesUnreferencedCode` in: * If the warning origin is not a compiler generated code - simply use the warning origin (method) - existing behavior * If the warning origin is compiler generated use both the origin as well as the user defined method (non-compiler-generated) which is the source of the code for the compiler generated origin This is done by storing additional `SuppressionContextMember` on `MessageOrigin` which should always point to non-compiler-generated item. Added lot of new tests for these scenarios. This implements warning suppression part of dotnet/linker#2001 for state machines. Commit migrated from dotnet/linker@bee7f73
This change implements the following high-level functionality:
UnconditionalSuppressMessageapplies to the entire method body even for iterators, async methodsRequiresUnreferencedCodeautomatically suppresses trim analysis warnings from entire method body for iterators, async methodsSolution approach:
IteratorStateMachineAttribute,AsyncStateMachineAttributeandAyncIteratorStateMachineAttribute.<in its name) is processed the original "user method" is determined by looking for a method with the "state machine" attribute pointing to the compiler generated type.UnconditionalSuppressMessageandRequriesUnreferencedCodein:This is done by storing additional
SuppressionContextMemberonMessageOriginwhich should always point to non-compiler-generated item.Added lot of new tests for these scenarios.
This implements warning suppression part of #2001 for state machines.