Skip to content

fix: register custom exceptions despite the target site - #1873

Merged
vbreuss merged 1 commit into
mainfrom
fix/register-custom-exceptions
Oct 18, 2026
Merged

vbreuss merged 1 commit into
mainfrom
fix/register-custom-exceptions

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 18, 2026

Copy link
Copy Markdown
Member

Every type derived from System.Exception inherits TargetSite, which requires unreferenced code, so the generator never registered a custom exception. Under Native AOT, a snapshot of a custom exception therefore lost its own members, and comparing two of them failed because reflection is switched off.

TargetSite only returns null where the metadata of the method was trimmed, as it always does under Native AOT, so reading it is safe. The generator now registers such an exception with all its members, like reflection finds them, and suppresses the trimming warning on the registration that reads TargetSite. An exception with its own unreferenceable member is still skipped.

Every type derived from `System.Exception` inherits `TargetSite`, which requires unreferenced code, so the generator never registered a custom exception. Under Native AOT, a snapshot of a custom exception therefore lost its own members, and comparing two of them failed because reflection is switched off.

`TargetSite` only returns null where the metadata of the method was trimmed, as it always does under Native AOT, so reading it is safe. The generator now registers such an exception with all its members, like reflection finds them, and suppresses the trimming warning on the registration that reads `TargetSite`. An exception with its own unreferenceable member is still skipped.
@vbreuss vbreuss self-assigned this Oct 18, 2026
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss enabled auto-merge (squash) October 18, 2026 15:03
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     92 files       92 suites   2h 16m 14s ⏱️
 38 393 tests  38 392 ✅ 1 💤 0 ❌
223 516 runs  223 515 ✅ 1 💤 0 ❌

Results for commit 042d0f5.

@vbreuss
vbreuss merged commit 0d03437 into main Oct 18, 2026
17 checks passed
@vbreuss
vbreuss deleted the fix/register-custom-exceptions branch October 18, 2026 15:37
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
AMD EPYC 9V45 2.60GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.303
[Host] : .NET 8.0.32 (8.0.32, 8.0.3226.50405), X64 RyuJIT x86-64-v4

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean vs aweXpect Error StdDev Gen0 Gen1 Allocated vs aweXpect
Contains_aweXpect 183.51 ns 5.492 ns 4.869 ns 0.0443 - 744 B
Contains_FluentAssertions 205.66 ns +12% 10.523 ns 9.843 ns 0.0961 0.0002 1608 B +116%
Contains_TUnit 208.08 ns +13% 4.933 ns 3.852 ns 0.0505 - 848 B +14%
Equivalency_aweXpect 6,536.93 ns 96.553 ns 85.592 ns 0.2289 - 3880 B
Equivalency_FluentAssertions 169,517.77 ns +2493% 2,821.503 ns 2,639.235 ns 35.1563 1.9531 590288 B +15114%
Equivalency_TUnit 9,960.56 ns +52% 320.909 ns 284.478 ns 0.9308 - 15680 B +304%
EventTriggered_aweXpect 392.65 ns 12.987 ns 12.148 ns 0.1106 0.0005 1856 B
EventTriggered_FluentAssertions 30,589.40 ns +7691% 1,059.727 ns 991.269 ns 0.3052 0.2747 5256 B +183%
Int_GreaterThan_aweXpect 77.99 ns 3.539 ns 3.137 ns 0.0319 - 536 B
Int_GreaterThan_FluentAssertions 129.38 ns +66% 2.632 ns 2.333 ns 0.0730 - 1224 B +128%
Int_GreaterThan_TUnit 124.21 ns +59% 4.075 ns 3.812 ns 0.0463 - 776 B +45%
ItemsCount_AtLeast_aweXpect 151.70 ns 3.250 ns 2.881 ns 0.0358 - 600 B
ItemsCount_AtLeast_FluentAssertions 254.31 ns +68% 7.716 ns 7.217 ns 0.1197 - 2008 B +235%
ItemsCount_AtLeast_TUnit 224.97 ns +48% 6.732 ns 6.297 ns 0.0634 - 1064 B +77%
Multiple_aweXpect 443.05 ns 7.151 ns 6.339 ns 0.1006 0.0005 1688 B
Multiple_FluentAssertions 1,113.14 ns +151% 32.250 ns 30.167 ns 0.4025 - 6744 B +300%
Multiple_TUnit 503.90 ns +14% 4.460 ns 4.172 ns 0.1335 - 2240 B +33%
Satisfies_aweXpect 101.41 ns 1.122 ns 0.937 ns 0.0329 - 552 B
Satisfies_FluentAssertions 10,127.51 ns +9887% 196.140 ns 153.133 ns 0.3357 0.2899 5615 B +917%
Satisfies_TUnit 136.90 ns +35% 3.083 ns 2.733 ns 0.0424 - 712 B +29%
String_aweXpect 154.74 ns 1.440 ns 1.202 ns 0.0343 - 576 B
String_FluentAssertions 293.56 ns +90% 5.718 ns 5.069 ns 0.1373 - 2304 B +300%
String_TUnit 189.13 ns +22% 3.565 ns 2.977 ns 0.0544 - 912 B +58%
StringArray_aweXpect 342.05 ns 6.242 ns 4.874 ns 0.0520 - 872 B
StringArray_FluentAssertions 732.07 ns +114% 19.388 ns 18.136 ns 0.2480 - 4152 B +376%
StringArray_TUnit 332.99 ns -3% 8.550 ns 7.140 ns 0.0753 - 1264 B +45%
StringArrayInAnyOrder_aweXpect 336.84 ns 14.679 ns 13.731 ns 0.0548 - 920 B
StringArrayInAnyOrder_FluentAssertions 10,245.11 ns +2942% 371.853 ns 347.832 ns 1.9989 0.0610 33471 B +3538%
StringArrayInAnyOrder_TUnit 449.91 ns +34% 10.883 ns 9.088 ns 0.0839 - 1408 B +53%
Throws_aweXpect 6,588.00 ns 32.202 ns 26.890 ns 0.0992 - 1736 B
Throws_FluentAssertions 19,600.82 ns +198% 101.509 ns 94.952 ns 1.0986 - 18609 B +972%
Throws_TUnit 6,458.62 ns -2% 51.329 ns 45.502 ns 0.1068 - 1824 B +5%
ThrowsAsync_aweXpect 6,212.93 ns 107.840 ns 95.598 ns 0.1297 - 2216 B
ThrowsAsync_FluentAssertions 28,303.89 ns +356% 194.353 ns 181.798 ns 1.4038 - 23553 B +963%
ThrowsAsync_TUnit 12,586.19 ns +103% 121.239 ns 101.240 ns 0.1678 - 2920 B +32%

github-actions Bot added a commit that referenced this pull request Oct 18, 2026
github-actions Bot added a commit that referenced this pull request Oct 18, 2026
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.

1 participant