Skip to content

[TrimmableTypeMap] Fix generated constructor UCO methods #11295

Closed

Description

@simonrozsival

I noticed we're invoking the wrong constructors in the generated IL, for example:

// ILSpy output:

// _TypeMap.Proxies.Android_Runtime_JavaProxyThrowable_Proxy
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Android.Runtime;
using Java.Interop;
using Java.Lang;

[UnmanagedCallersOnly]
public static void nctor_1_uco(System.IntPtr P_0, System.IntPtr P_1, System.IntPtr P_2, System.IntPtr P_3, byte P_4, byte P_5)
{
	//IL_0029: Unknown result type (might be due to invalid IL or missing references)
	JniTransition val = default(JniTransition);
	JniRuntime val2 = default(JniRuntime);
	if (!JniEnvironment.BeginMarshalMethod(P_0, ref val, ref val2))
	{
		return;
	}
	try
	{
		if (!JavaPeerProxy.ShouldSkipActivation(P_1))
		{
			// we are not resolving P_1, P_2, P_3!
			((Error)(JavaProxyThrowable)RuntimeHelpers.GetUninitializedObject(typeof(JavaProxyThrowable)))..ctor(P_1, (JniHandleOwnership)0); // we are calling the wrong constructor! we should be calling the one with the 5 parameters here!
		}
	}
	catch (System.Exception ex)
	{
		if (val2 != null)
		{
			val2.OnUserUnhandledException(ref val, ex);
		}
	}
	finally
	{
		JniEnvironment.EndMarshalMethod(ref val);
	}
}

Activity

  1. added
    bug
    on May 6, 2026
  2. added this to the .NET 11 milestone on May 6, 2026
  3. added
    needs-triage
    on May 6, 2026
  4. simonrozsival commented on May 25, 2026

    @simonrozsival
    MemberAuthor

    This appears to be fixed in main now.

    Evidence:

    • The related PRs are all merged and contained in origin/main:
    • The current trimmable typemap code no longer blindly calls the (IntPtr, JniHandleOwnership) activation constructor when a matching managed constructor exists. It now tracks HasMatchingManagedCtor / ManagedParameterTypes, emits RuntimeHelpers.GetUninitializedObject, sets the activation peer reference, marshals JNI constructor arguments, and calls the user-visible managed constructor.
    • Object-reference constructor parameters are marshalled through Java.Lang.Object.GetObject, which addresses the original issue's example where constructor arguments were not resolved before invoking the managed constructor.
    • Regression coverage exists for this behavior, including:
      • ActivatedDirectThrowableSubclasses_ThrowableCtor_ShouldForwardArgs
      • ActivatedDirectThrowableSubclasses_MultipleCtors_ShouldDispatchToCorrectCtor
      • generator tests asserting object-ref UCO constructor arguments use GetObject and invoke the user-visible constructor.

    Closing as fixed by the merged constructor-activation work, primarily #11275 and #11123, with #11461 as a related follow-up.

  5. locked and limited conversation to collaborators on Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugneeds-triagetrimmable-type-map

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions