IL2CPP error on a struct generic method with a in generic parameter

Hello there, I am trying to use my internal framework inside Unity and things work correctly in the editor but fail to build on the IL2CPP scripting backend.

The method is a simple ‘public void Set(in T component = default)’ defined on a struct (so it should not be the interface generic method limitation of IL2CPP) and if its usage is removed (while keeping usage of other generic methods of this struct without the in parameter) the project build successfully. I tried adding a link.xml file to preserve all my external assembly but it gives the same error.
I am on Unity 2020.2.2f1 using .NET Standard 2.0 api compatibility level by the way.

So either Unity doesn’t like generic in parameters or I am missing something else :confused: Thank you for your help.

Not sure if the in keyword is supported… also on older Unity the naked default is also not supported, so it would need to be default(T).

Just a few things to tinker with.

I have other non generic methods with in parameter used and IL2CPP doesn’t seem to complain. Also default or default(T) should just be syntactic sugar, since my framework is referenced as a dll the IL should be the same.

Just to be sure I will try to make a empty unity project with such methods to see how it behave.

So I finally took some time to do a small code repro and it seems IL2CPP doesn’t like neither default nor default(T). Is this expected?

using UnityEngine;

public static class Container<T>
{
    private static T _value;

    // uncomment this and the build fail
    //public static void Set1(in T value = default(T)) => Set2(value);

    public static void Set2(in T value)
    {
        _value = value;
    }

    public static void Set3() => Set2(default);

    public static ref T Get() => ref _value;
}

public class Test : MonoBehaviour
{
    private SpriteRenderer _renderer;

    // Start is called before the first frame update
    void Start()
    {
        _renderer = GetComponent<SpriteRenderer>();

        Container<Color>.Set3();
        Container<Color>.Set2(Color.red);
    }

    // Update is called once per frame
    void Update()
    {
        _renderer.color = Container<Color>.Get();
    }
}

Tested on both 2020.2.3f1 and 2021.1.0b5

Did a little more digging and found this issue Unity Issue Tracker - [IL2CPP] Build fails if trying to assign a default value to a function&#39;s reference parameter (&quot;in&quot; keyword)
Tested with a private static void SetColor(in Color value = default) => Container<Color>.Set2(value); and sure enough it works so I guess the problem is with generic in parameter with default value.

1 Like

@Doraku : Can you submit a bug report for this issue? We should correct the behavior of IL2CPP. Unity QA: Building quality with passion

Just did, case 1313460 if that’s any help :slight_smile:

Thanks!