Skip to content

[TrimmableTypeMap] Prefer non-generic aliases - #13749

Merged
simonrozsival merged 6 commits into
mainfrom
simonrozsival-generic-typemap-aliases
Nov 28, 2026
Merged

simonrozsival merged 6 commits into
mainfrom
simonrozsival-generic-typemap-aliases

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: fed399804a93e3838a11deda38d3ce0f4c7909c0

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ Generator tests and all three trimming configurations passed locally.
↑ Both Debug reference orders passed in the previous CI run.
↓ CI for the assessed revision is still running.
Diagnosis confidence 9/10 ↑ A two-library reproduction selected an open-generic peer before the fix and the concrete peer afterward.
Evidence fit 7.5/10 ↑ Both scenarios involve untyped activation selecting an open-generic alias.
↓ The original report concerns SparseArray in Debug, not the two-library trimming reproduction.

Context: #13674
Context: #13658

When multiple libraries bind the same Java class, an open generic peer can sort before a concrete peer. Untyped Java activation then selects the open generic proxy, which cannot be instantiated. This is dependent on generated ordering; the current SparseArray ordering only masks the problem.

Order non-generic peers before generic definitions in generated alias groups, while retaining generic aliases for typed closed-generic lookups. Debug combines proxies from separate per-assembly universes, so the runtime cache builder applies the same preference there.

Add a two-library regression case to the isolated TrimmingTests app suite for Release full/partial trimming and NativeAOT. The case uses instrumentation rather than a launcher activity, so configurations do not compete for the foreground activity. Keep Debug coverage in MSBuildDeviceIntegration and vary project-reference order.

Validation

  • make leeroy CONFIGURATION=Debug MSBUILD_ARGS='-m:4' — passed after regenerating a stale local AssemblyInfo.cs; the initial build failed with IL3058 because that generated file lacked current AOT metadata.
  • dotnet test src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/Microsoft.Android.Build.Tasks.Tests.csproj --filter 'FullyQualifiedName~GenerateTrimmableTypeMapTests' -v:q -nologo — 68 passed on an earlier revision; the generator and its tests are unchanged since then.
  • ./dotnet-local.sh build tests/TrimmingTests/TrimmingTests.csproj -v:q -nologo — passed.
  • ADB_TARGET='-s emulator-5558' TEST_ANDROID_JDK_PATH="$HOME/android-toolchain/jdk-25" TEST_ANDROID_SDK_PATH="$HOME/android-toolchain/sdk" ./dotnet-local.sh test bin/TestDebug/TrimmingTests/net11.0/TrimmingTests.dll --filter 'Name~UntypedJavaActivationPrefersNonGenericBindingWithoutWarnings' --logger 'console;verbosity=normal' — 3 passed on an arm64 emulator: CoreCLR full trimming, CoreCLR partial trimming, and NativeAOT.
  • ./dotnet-local.sh build tests/MSBuildDeviceIntegration/MSBuildDeviceIntegration.csproj -v:q -nologo — passed on an earlier revision.
  • The file-based case built for CoreCLR full and partial trimming with warnings treated as errors; the generated Release alias table places Z.NonGenericPeer at slot 0 and retains A.GenericPeer<T> at slot 1.
  • CI build 1703345 passed both Debug reference-order device tests on the previous head; those tests were not rerun locally after the SDK rebuild.

@simonrozsival
simonrozsival force-pushed the simonrozsival-generic-typemap-aliases branch from 3edd91b to 1039399 Compare November 27, 2026 12:14
Base automatically changed from dev/simonrozsival/trimming-regression-tests to main November 27, 2026 21:39
simonrozsival and others added 4 commits November 27, 2026 16:39
Prefer non-generic peers in generated alias groups and when combining per-assembly universes, while retaining generic peers for typed lookups.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Move Release trimming coverage for duplicate generic bindings into the isolated TrimmingTests apps. Keep Debug coverage in MSBuildDeviceIntegration, where cross-universe proxy ordering is exercised.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
javac rejects the UTF-8 byte order mark emitted by the default project item encoding. Write the regression test source as UTF-8 without a BOM.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Share the peer sources between the TrimmingTests case and the Debug
device test, create Java instances with JNIEnv.CreateInstance (), and
build the generator test's peers with a local helper.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@simonrozsival
simonrozsival force-pushed the simonrozsival-generic-typemap-aliases branch from 1039399 to 373b7d8 Compare November 27, 2026 21:39
@simonrozsival
simonrozsival marked this pull request as ready for review November 27, 2026 22:45
Copilot AI balanced review requested due to automatic review settings November 27, 2026 22:45
@simonrozsival

Copy link
Copy Markdown
Member Author

@dalexsoto review

@simonrozsival
simonrozsival enabled stacked auto-merge (squash) November 27, 2026 22:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The activity-based test runs configuration cases in parallel, allowing them to interfere with activity startup and cause spurious logcat timeouts; it should be marked NonParallelizable or moved to instrumentation.

1 open issue
What changed in this PR

This PR makes untyped Java activation prefer non-generic bindings when multiple managed types bind the same Java class, addressing the activation failure reported in #13674 while retaining generic aliases for typed lookups.

Changes:

  • Prioritizes non-generic aliases in generated type maps and Debug’s runtime cache.
  • Adds ordering tests for the generator and cross-assembly runtime lookup.
  • Adds a two-library regression case for trimming and NativeAOT, plus Debug device coverage with reversed reference order.
File Description
tests/​TrimmingTests/​Tests/​JavaPeerActivationTests.cs Registers the new activation regression case.
tests/​TrimmingTests/​Cases/​GenericBindingAliases/​ZNonGenericBinding/​ZNonGenericBinding.csproj Defines the non-generic binding library.
tests/​TrimmingTests/​Cases/​GenericBindingAliases/​ZNonGenericBinding/​NonGenericPeer.cs Provides the concrete Java peer binding.
tests/​TrimmingTests/​Cases/​GenericBindingAliases/​GenericBindingAliases.cs Checks untyped activation and closed-generic lookup.
tests/​TrimmingTests/​Cases/​GenericBindingAliases/​Case.targets Disables automatic binding for the Java fixture.
tests/​TrimmingTests/​Cases/​GenericBindingAliases/​AliasPeer.java Supplies the Java class shared by both bindings.
tests/​TrimmingTests/​Cases/​GenericBindingAliases/​AGenericBinding/​GenericPeer.cs Provides the generic Java peer binding.
tests/​TrimmingTests/​Cases/​GenericBindingAliases/​AGenericBinding/​AGenericBinding.csproj Defines the generic binding library.
tests/​MSBuildDeviceIntegration/​Tests/​InstallAndRunTests.cs Adds Debug coverage with both project-reference orders.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​TrimmableTypeMapTypeManagerTests.cs Tests non-generic preference across type-map universes.
src/​Mono.Android/​Microsoft.Android.Runtime/​TrimmableTypeMap.cs Places a non-generic proxy first in the runtime cache.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​ModelBuilder.cs Sorts non-generic aliases before generic definitions.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​GenerateTrimmableTypeMapTests.cs Verifies alias ordering and generic-binding retention.

Review effort: Balanced

@dalexsoto

Copy link
Copy Markdown
Member

In response to @simonrozsival's request: My static review is still in progress. Before a complete verdict I need to finish the InstallAndRunTests and binding-project/Java patches, trace AggregateTypeMap/SingleUniverseTypeMap proxy insertion and TrimmableTypeMap peer creation/TargetTypeMatches, and check the removed native alias[0] alignment requirement. These are unfinished reviewer-coverage items within this tick, not defects established in the PR. The separate final whole-PR blocker pass is also still pending; the next scheduled tick will resume from the retained reads rather than restart.

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new activity-based trimming test needs serialization. In tests/TrimmingTests/JavaPeerActivationTests.cs:21-26, the new test inherits [Parallelizable(ParallelScope.Children)] but runs GenericBindingAliases, which is a [Activity(MainLauncher = true)] case. TrimmingTestBase.cs:69-72 explicitly requires activity cases to be [NonParallelizable]: otherwise concurrent cases can interfere with the foreground activity and make this test fail nondeterministically.

Please mark this test [NonParallelizable], or make the case use CaseInstrumentation and perform its checks in CallApplicationOnCreate(); it does not need an activity window. This is the single remaining blocker from the static review.

@PeppersMuse

Copy link
Copy Markdown

Reviewed as PeppersMuse. The one open concern — the new activity-based TrimmingTests case running under [Parallelizable (ParallelScope.Children)] — is already flagged by Copilot and @dalexsoto, so I am not repeating it. What I checked:

  • ModelBuilder.CompareAliasesForRuntimeResolution: the new sort key is a valid total order — non-generic before generic definition, then Mono.Android among equally activatable aliases, then managed type name, then assembly. Mono.Android is still preferred whenever both peers are equally activatable; only a generic Mono.Android peer vs. a concrete peer from another assembly changes slot 0, which is exactly the bug being fixed. BuildNativeRegistrations iterates all proxies and does not depend on alias [0].
  • TrimmableTypeMap.JniProxyCacheBuilder.Add: inserting a non-generic proxy at 0 only when multiple [0] is a generic definition covers both universe orders, and a later non-generic appends after an already-first non-generic, so untyped lookup still lands on a concrete peer. Confirmed TryGetTargetType returns proxies [0].TargetType, so slot 0 is what untyped activation reads.
  • Tests: the generator test asserts Z.NonGenericPeer lands in slot 0 for both reference orders and both generic assemblies (AGenericBinding, Mono.Android) while keeping both proxies; the runtime test covers both universe orders and asserts the closed-generic proxy stays available at slot 1. The TrimmingTests case checks untyped activation, untyped peer creation, and closed-generic lookup together, and follows the file-based case conventions (#:project refs, KeptTypeInAssembly, single PASS/FAIL log line).
  • No other issues found in the diff.

simonrozsival and others added 2 commits November 28, 2026 11:21
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Run the alias checks without a launcher activity, so configuration cases can execute in parallel without competing for the foreground activity.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@simonrozsival

Copy link
Copy Markdown
Member Author

@dalexsoto The activity-race concern is addressed in the pushed head: the trimming case now runs as instrumentation, so I removed [NonParallelizable]. The focused device test passed in CoreCLR full trimming, CoreCLR partial trimming, and NativeAOT (3/3). I also replied to the inline thread, which is now resolved. The remaining review coverage can continue against this head.

@dalexsoto

Copy link
Copy Markdown
Member

In response to @simonrozsival's request: The instrumentation conversion addresses the activity-race concern from my previous review. I still need to trace the full CompareAliasesForRuntimeResolution implementation and its generator callers in src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/ModelBuilder.cs at fed399804a93; only its changed hunk has been covered. This is a reviewer coverage gap, not a defect identified in the PR. The inspected changes have no additional high-confidence blocker.

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity-race blocker from my previous review is resolved: GenericBindingAliases now uses CaseInstrumentation.CallApplicationOnCreate, matching the harness contract without requiring NonParallelizable. The generator sorts non-generic aliases first after cross-assembly merging, and the runtime cache preserves that order while retaining generic proxies for closed-generic lookups. The added generator, runtime-cache and instrumentation cases cover both alias orders and assemblies. No remaining blocking issue found in the complete static review; no builds or device tests were executed.

@simonrozsival
simonrozsival merged commit 3e61ef1 into main Nov 28, 2026
41 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-generic-typemap-aliases branch November 28, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants