Warn on cctors annotated with RUC if a field triggers its marking#1889
Warn on cctors annotated with RUC if a field triggers its marking#1889mateoatr merged 5 commits intodotnet:mainfrom
Conversation
| { | ||
| } | ||
|
|
||
| public static int field = 0; |
There was a problem hiding this comment.
I think it'd be better not to use static fields to check that types without beforefieldinit modifiers work as expected.
There was a problem hiding this comment.
I think moving the field initialization into the static .cctor should be enough, right?
There was a problem hiding this comment.
I think the test coverage should be better. Does the code print the warning in this case?
new C ().Foo ();
class C
{
[RequiresUnreferencedCode ("Message")]
static C ()
{
}
public void Foo ()
{
}
}There was a problem hiding this comment.
Also, AFAIK Roslyn won't make the type beforefieldinit in any of the above cases - the explicit static ctor in C# doesn't have the relaxed semantics allowed by beforefieldinit. To test with beforefieldinit you need to avoid writing a static ctor and let Roslyn generate the .cctor from the static field initializers:
class C {
static int field = RUC();
[RequiresUnreferencedCode]
static int RUC() => 0;
}| public static int AnnotatedMethod () => 42; | ||
| } | ||
|
|
||
| [LogContains ("IL2026: Mono.Linker.Tests.Cases.RequiresCapability.RequiresUnreferencedCodeCapability.TypeIsBeforeFieldInit..cctor():" + |
There was a problem hiding this comment.
Why do we use LogContains here?
There was a problem hiding this comment.
As @sbomer puts above, Roslyn generates a .cctor and annotates the type with beforefieldinit, this static constructor initializes the field and thus calls AnnotatedMethod, triggering the warning from within the .cctor. Because of this, there's no place where we could put the ExpectedWarning attribute (the call is made from generated code).
…tnet/linker#1889) * Cctor marked because of accessed field should trigger warning * Ignore testcase in analyzer * Add tests Move field initialization to cctor * Add beforefieldinit test to list of tests ignored by the analyzer * Fix testname Commit migrated from dotnet/linker@252c665
Whenever a static member is referenced, static constructors on the declared type are called. If the cctor is annotated with
RequiresUnreferencedCode, this should warn.