Skip to content

Requires on NativeAOT has problems with CompilerGeneratedCode #68786

@tlakollo

Description

@tlakollo

Description

Doing some testing with the Requires attribute on CompilerGeneratedCode I realized that NativeAOT has differences with linker, it will not produce warnings for code using ldftn for example:

//[ExpectedWarning("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)]
//[ExpectedWarning("IL3050", "--MethodWithRequires--")]
static IEnumerable<int> TestLdftn()
{
    yield return 0;
    yield return 1;
    var action = new Action(MethodWithRequires);
}
[RequiresUnreferencedCode("--MethodWithRequires--")]
[RequiresDynamicCode("--MethodWithRequires--")]
static void MethodWithRequires()
{
}

It will have problems with Lazy Delegates too

// Cannot annotate fields either with RUC nor RAF therefore the warning persists
//[ExpectedWarning("IL2026", "Message from --MethodWithRequiresAndReturns--", CompilerGeneratedCode = true)]
//[ExpectedWarning("IL3050", "Message from --MethodWithRequiresAndReturns--")]
public static Lazy<string> _default = new Lazy<string>(MethodWithRequiresAndReturns);

static IEnumerable<int> TestLazyDelegate()
{
    yield return 0;
    yield return 1;
    _ = _default.Value;
}
[RequiresUnreferencedCode("Message from --MethodWithRequiresAndReturns--")]
[RequiresDynamicCode("Message from --MethodWithRequiresAndReturns--")]
public static string MethodWithRequiresAndReturns()
{
    return "string";
}

Suppression inside CompilerGeneratedCode is also not working

Reproduction Steps

Expected behavior

The warnings generated by dotnet/linker and nativeAOT should be the same

Actual behavior

NativeAOT sometimes misses generating some of the warnings and some other times it generates extra warnings

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

High Priority

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions