Skip to content

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

Closed

Description

@akravch

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

Activity

  1. added
    Area: App+Library BuildIssues when building Library projects or Application projects.
    needs-triageIssues that need to be assigned.
    on Dec 12, 2022
  2. changed the title [-].NET 7 AOT x86/x64 Unable to build a method of a generic type that returns Type[/-] [+].NET 7 AOT x86/x64 Unable to build a method of a generic class that returns Type[/+] on Dec 12, 2022
  3. jonathanpeppers commented on Dec 15, 2022

    @jonathanpeppers
    Member

    This issue was moved to dotnet/runtime#79710

  4. ghost locked as resolved and limited conversation to collaborators on Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area: App+Library BuildIssues when building Library projects or Application projects.needs-triageIssues that need to be assigned.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions