Skip to content

.NET 7 AOT x86/x64 Unable to build a method of a generic class that returns Type #79710

Closed
@jonathanpeppers

Description

@jonathanpeppers

From @akravch on Mon, 12 Dec 2022 15:50:00 GMT

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

.NET 7.0.100

Description

Can't build an app for x86 or x64 if there is a method that satisfies the following conditions:

  • It is a method of a generic class
  • It returns Type
  • It has a conditional structure inside (e.g. if, switch or a ternary operator)
  • One of the branches returns typeof(T)

Same code compiles just fine for ARM/ARM64 with .NET 7, or any runtime including x86/x64 with .NET 6.

Repro steps below or just see this repo: https://github.com/akravch/net_7_aot_issue

Steps to Reproduce

  1. Create a new android applicaiton for .NET 7
  2. Configure it to build for x86 and/or x64 runtime identifiers: <RuntimeIdentifiers>android-x64</RuntimeIdentifiers>
  3. Make sure AOT is enabled: <RunAOTCompilation>true</RunAOTCompilation>
  4. Add the following code anywhere in the project:
    public class ProblematicClass<T>
    {
        public Type ProblematicMethod(int i)
        {
            return i == 0 ? typeof(T) : null;
        }
    }
  5. Run the build in Release configuration

Expected:
The build has succeeded
Actual:
AOT compilation failed

Did you find any workaround?

Any of the following methods would work:

public class ProblematicClass<T>
{
    private readonly Type _type = typeof(T);
    private readonly Type _nullType = null;

    public Type WorkingMethod1(int i)
    {
        return i == 0 ? _type : null;
    }

    public Type WorkingMethod2(int i)
    {
        return i == 0 ? typeof(T) : _nullType;
    }
}

Although, in my case the issue actually comes from a NuGet package, so applying the workaround is somewhat complicated.

Relevant log output

Microsoft.Android.Sdk.Aot.targets(91,5): error : Precompiling failed for net_7_aot_issue\Net7AotIssue.Android\obj\Release\net7.0-android\android-x64\linked\Net7AotIssue.Android.dll with exit code -1073740791.
Microsoft.Android.Sdk.Aot.targets(91,5): error : Mono Ahead of Time compiler - compiling assembly net_7_aot_issue\Net7AotIssue.Android\obj\Release\net7.0-android\android-x64\linked\Net7AotIssue.Android.dll
Microsoft.Android.Sdk.Aot.targets(91,5): error : AOTID 6426D756-4265-334D-C2CE-551A78CD5437
Microsoft.Android.Sdk.Aot.targets(91,5): error : Opcode 'rttype' missing from machine description file.

Also attaching a binlog: binlog.zip

Copied from original issue dotnet/android#7631

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions