Skip to content

[typemap] Diagnose unsupported constructor shapes - #12567

Open
simonrozsival wants to merge 3 commits into
mainfrom
simonrozsival-constructor-signature-diagnostics
Open

[typemap] Diagnose unsupported constructor shapes#12567
simonrozsival wants to merge 3 commits into
mainfrom
simonrozsival-constructor-signature-diagnostics

Conversation

@simonrozsival

@simonrozsivalsimonrozsival commented Aug 28, 2026

Copy link
Copy Markdown
Member

Summary

  • add localized XA4259-XA4262 diagnostics for constructor collisions, unsupported shapes, missing Java base constructors, and invalid ordinary SuperArgumentsString parameter references
  • collect independent constructor and reserved-Java-name diagnostics before returning without outputs
  • match implicit derived constructors to registered base constructors by computed JNI signature
  • preserve XA4263 suppression and valid Export plus Register/JniConstructorSignature metadata regardless of attribute order
  • use full managed type identity for primitives, activation discovery, overload comparison, and JNI descriptor resolution
  • conservatively validate SuperArgumentsString: tokenize literals/comments and defer any expression containing Java lambda (->) or method-reference (::) syntax entirely to javac; retain XA4262 for ordinary bare/call/arithmetic pN references
  • deduplicate XA4259 per peer/JNI signature and assert complete measured legacy constructor collections
  • preserve legacy framework-constructor compatibility and stop before partial Java/typemap output

Validation

  • standalone trimmable typemap suite: 870/870
  • trimmable typemap integration suite: 41/41
  • conservative lambda/method-reference javac matrix: 42/42
  • combined llvm-ir/trimmable CoreCLR/trimmable NativeAOT diagnostic and no-output matrices: 60/60
  • trimmable successful-build baseline: 3 passed, 1 unsupported configuration skipped
  • final independent high-confidence review: clean
  • git diff --check; acceptance commit adds 33 lines (within 180)

Part of #12561

CopilotAI 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.

Copilot review overview

Review tier: Lite
Findings: 1 High severity · 1 Low severity

New issues introduced by this change (2)
SeverityFinding
Low severitytests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Scanner/​ConstructorDetectionTests.cs — 💡 suggestion — ScanPeer opens and reads both fixture assemblies from disk on every test invocation.…
High severitysrc/​Microsoft.Android.Sdk.TrimmableTypeMap/​Scanner/​JavaPeerScanner.cs — ❌ error — Base-constructor compatibility for non-explicit constructors is currently checked via…
What changed in this PR

Adds first-class constructor-shape validation to the trimmable typemap pipeline, surfacing new localized XA4259–XA4262 diagnostics and ensuring generation stops before emitting partial Java/type-map output when constructors are not representable or are ambiguous.

Changes:

  • Introduces constructor diagnostics (collision, unsupported parameter shapes, missing compatible base ctor, invalid SuperArgumentsString) and wires them into the generator/Build.Tasks logger with localized resources.
  • Expands test coverage with new “invalid constructor” fixture assembly and adds focused unit/integration/build tests to validate diagnostics and “no partial output” behavior.
  • Documents new error codes (XA4259–XA4262) and adds them to the docs index/TOC.
FileDescription
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​TestFixtures/​StubAttributes.csExtends stub ExportAttribute to support ctor usage and SuperArgumentsString for scanner tests.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Scanner/​ConstructorDetectionTests.csAdds targeted tests for new constructor diagnostics and representable/explicit cases.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csprojAdds a new fixture project and copies its output alongside existing fixtures.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​InvalidConstructorFixtures/​InvalidConstructors.csNew fixture types that intentionally trigger (or avoid) constructor diagnostics.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​InvalidConstructorFixtures/​InvalidConstructorFixtures.csprojNew fixture assembly project (unsafe enabled) used as scanner/generator input.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Generator/​TrimmableTypeMapGeneratorTests.csVerifies generator logs coded errors and returns no partial outputs when ctor diagnostics exist.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests/​UserTypesFixture/​UserTypesFixture.csprojEnables unsafe to support new user fixture types using pointers/function pointers.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests/​UserTypesFixture/​UserTypes.csAdds user fixture types mirroring ctor-collision/unrepresentable/super-args scenarios.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests/​ScannerRunner.csAdds legacy constructor extraction to support ctor parity tests.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests/​ScannerComparisonTests.csExcludes ctor-diagnostic fixtures from legacy↔new marshal-method parity comparisons.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests/​ConstructorParityTests.csNew integration tests that pin legacy behavior around ctor collisions/unrepresentable ctors/super args.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​TrimmableTypeMapBuildTests.csAdds a build-level regression ensuring XA4259 fails before any partial typemap Java is written.
src/​Xamarin.Android.Build.Tasks/​Tasks/​GenerateTrimmableTypeMap.csImplements new logger hooks for XA4259–XA4262 in the MSBuild task.
src/​Xamarin.Android.Build.Tasks/​Properties/​Resources.resxAdds localized strings for XA4259–XA4262.
src/​Xamarin.Android.Build.Tasks/​Properties/​Resources.Designer.csUpdates generated resource accessors for XA4259–XA4262.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​TrimmableTypeMapGenerator.csValidates constructor diagnostics early and returns no generated outputs on failure.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Scanner/​JavaPeerScanner.csComputes ctor diagnostics during scanning, including signature collapse, parameter-shape validation, base-ctor compatibility, and SuperArgumentsString validation.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Scanner/​JavaPeerInfo.csAdds ConstructorDiagnostics model plus diagnostic kind/info types.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​ITrimmableTypeMapLogger.csExtends logger interface with ctor diagnostic logging hooks.
Documentation/​docs-mobile/​TOC.ymlAdds entries for XA4259–XA4262 docs.
Documentation/​docs-mobile/​messages/​xa4259.mdNew documentation page for XA4259.
Documentation/​docs-mobile/​messages/​xa4260.mdNew documentation page for XA4260.
Documentation/​docs-mobile/​messages/​xa4261.mdNew documentation page for XA4261.
Documentation/​docs-mobile/​messages/​xa4262.mdNew documentation page for XA4262.
Documentation/​docs-mobile/​messages/​index.mdAdds XA4259–XA4262 to the message index listing.
Files not reviewed (1)
  • src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs: Generated file

Comment threadsrc/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerScanner.cs Outdated
@simonrozsival
simonrozsivalforce-pushed the simonrozsival-constructor-signature-diagnostics branch from e03c49d to c385503CompareAugust 31, 2026 14:46
@simonrozsival
simonrozsival changed the base branch from main to simonrozsival-unsupported-export-signaturesAugust 31, 2026 14:46
@simonrozsival
simonrozsivalforce-pushed the simonrozsival-constructor-signature-diagnostics branch 2 times, most recently from 765d233 to 8936f2aCompareSeptember 1, 2026 12:50
@simonrozsival
simonrozsivalforce-pushed the simonrozsival-constructor-signature-diagnostics branch 2 times, most recently from 9d75936 to 7854663CompareSeptember 1, 2026 13:15
@simonrozsival
simonrozsivalforce-pushed the simonrozsival-constructor-signature-diagnostics branch from 7854663 to 8b98f5fCompareSeptember 1, 2026 13:33
@simonrozsival
simonrozsivalforce-pushed the simonrozsival-constructor-signature-diagnostics branch 2 times, most recently from 3ea14be to 663a4abCompareSeptember 1, 2026 15:16
Base automatically changed from simonrozsival-unsupported-export-signatures to mainSeptember 4, 2026 07:20
simonrozsival added a commit that referenced this pull request Sep 4, 2026
## Summary
- reject unsupported `[Export]`/`[ExportField]` signatures during trimmable scanning with localized XA4263 before typemap, Java, or ACW-map output
- preserve supported Java peers/interfaces, primitives, strings, ordinary arrays, enums, collections, and valid scalar `[ExportParameter]` mappings
- match Export attributes by full `Java.Interop` identity and resolve peer/enum/special framework types by assembly identity through forwarders
- validate exported constructors while preserving constructor identity and scalar adapter dispatch
## Final type-identity behavior
Special mappings require canonical framework identity, not just a managed full name:
- `System.IO.Stream`: `System.Runtime` facade or resolved `System.Private.CoreLib`
- `System.Xml.XmlReader`: `System.Xml.ReaderWriter` facade or resolved `System.Private.Xml`
- multi-hop facades such as `netstandard → System.Xml.ReaderWriter → System.Private.Xml` are accepted through the existing cycle-safe forwarder resolver
- user assemblies defining the same full names are rejected with XA4263 for method parameter/return, ExportField return, and constructor parameter paths
Tests use minimal metadata assemblies for direct `System.Private.Xml`, the two-hop facade chain, and a resolved `User.Xml` assembly containing `System.Xml.XmlReader`. A separately compiled Android fixture and end-to-end builds retain the user-collision/no-output controls.
## Constructor diagnostics ownership
PR #12567 owns XA4260 for generic, byref, pointer, function-pointer, rectangular-array constructor parameters, including nested SZ-array forms. XA4263 owns unresolved managed types and invalid `[ExportParameter]` kind/type/identity pairs. This PR does not duplicate XA4259/XA4261 analysis.
An isolated composition with the latest #12567 reproduces its remaining follow-up: two XA4263-rejected overloads on the same type with default/missing `SuperArgumentsString` also receive secondary XA4259 and XA4261. The coordinator will make that analyzer skip XA4263-rejected constructors after rebasing #12567 above this PR.
## Validation
- 802 standalone trimmable tests
- 27 integration tests, including semantic classfile comparison, javac, peer/enum collisions, and Stream/XmlReader collisions
- 66 export-signature/runtime-identity and no-output build tests
- valid llvm-ir CoreCLR, trimmable CoreCLR, and trimmable NativeAOT fixture builds
- legacy `GenerateExportedMembers`
- latest #12567 composition: stronger two-overload secondary-diagnostic reproduction and 838 full combined standalone tests
- `git diff --check`; every added line at most 180 characters; full-name attribute audit clean
Stacked on #12596. Tracks #12561.
Validate Java-visible constructor signatures, base forwarding, collisions, and SuperArgumentsString values while preserving legacy handling for managed-only constructors.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@simonrozsival
simonrozsivalforce-pushed the simonrozsival-constructor-signature-diagnostics branch from 573889f to 1ef61f1CompareSeptember 4, 2026 10:08
simonrozsivaland others added 2 commits September 4, 2026 14:05
Match legacy JCW generation by reserving XA4261 for explicit registered or exported constructors while silently skipping implicit constructors without a compatible Java base constructor.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Allow Java constructor wrappers without a matching public managed constructor to use the existing activation-constructor path instead of failing model generation.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/review

@github-actions

github-actionsBot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #12567

@github-actionsgithub-actionsBot 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.

LGTM — No correctness issues found. The constructor diagnostics preserve the intended legacy behavior for implicit constructors while producing focused errors for explicit unsupported shapes, signature collisions, missing base constructors, and invalid super arguments. The scanner, generator, model-builder, integration, and build coverage is strong, and all 44 checks in Azure DevOps/GitHub passed.

One non-blocking inline suggestion requests MSBuild-level coverage for XA4261.

Generated by Android PR Reviewer for #12567 · gpt56 · 430.3 AIC · ⌖ 8.85 AIC · ⊞ 25.7K
Comment /review to run again

}

[Theory]
[InlineData ("MyApp.RegisteredMissingBaseCtorActivity")]

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.

🤖 💡 Testing — Please add an end-to-end build case for XA4261, similar to the new XA4259/XA4260/XA4262 cases in TrimmableTypeMapBuildTests. These scanner and generator tests prove detection and mock logging, but they do not exercise GenerateTrimmableTypeMap's real resource mapping or verify that a missing-base-constructor error suppresses partial typemap/Java outputs at the MSBuild boundary.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@simonrozsival