Skip to content

fix: support generic methods with allows ref struct constraint#724

Merged
vbreuss merged 1 commit into
mainfrom
topic/support-generic-methods-with-allows-ref-struct-constraint
Apr 30, 2026
Merged

fix: support generic methods with allows ref struct constraint#724
vbreuss merged 1 commit into
mainfrom
topic/support-generic-methods-with-allows-ref-struct-constraint

Conversation

@vbreuss
Copy link
Copy Markdown
Member

@vbreuss vbreuss commented Apr 30, 2026

A generic method whose type parameter declares allows ref struct and uses that parameter in its return type or any parameter type cannot be wired
through the regular setup pipeline: ReturnMethodSetup / IReturnMethodSetup
do not declare the same anti-constraint, so the generated source fails with CS9244 at every reference to the setup type.

The generator now treats these methods like the existing unsupported ref-struct shapes: the override body throws NotSupportedException, and no setup-interface declaration or explicit implementation is emitted. Verify remains available because it does not parameterize on T.

A generic method whose type parameter declares `allows ref struct` and uses
that parameter in its return type or any parameter type cannot be wired
through the regular setup pipeline: ReturnMethodSetup<T> / IReturnMethodSetup<T>
do not declare the same anti-constraint, so the generated source fails with
CS9244 at every reference to the setup type.

The generator now treats these methods like the existing unsupported
ref-struct shapes: the override body throws NotSupportedException, and no
setup-interface declaration or explicit implementation is emitted. Verify
remains available because it does not parameterize on T.
@vbreuss vbreuss self-assigned this Apr 30, 2026
@vbreuss vbreuss added the bug Something isn't working label Apr 30, 2026
@sonarqubecloud
Copy link
Copy Markdown

@vbreuss vbreuss enabled auto-merge (squash) April 30, 2026 05:41
@github-actions
Copy link
Copy Markdown

Test Results

    21 files  ±0      21 suites  ±0   9m 50s ⏱️ +43s
 3 851 tests +1   3 850 ✅ +1  1 💤 ±0  0 ❌ ±0 
25 067 runs  +3  25 066 ✅ +3  1 💤 ±0  0 ❌ ±0 

Results for commit 558057d. ± Comparison against base commit 74753ac.

@vbreuss vbreuss merged commit 9ccc594 into main Apr 30, 2026
16 checks passed
@vbreuss vbreuss deleted the topic/support-generic-methods-with-allows-ref-struct-constraint branch April 30, 2026 05:43
@github-actions
Copy link
Copy Markdown

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.90GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.203
[Host] : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Event_Mockolate 343.4 ns 2.94 ns 2.60 ns 1.00 0.01 0.1254 0.0005 2.05 KB 1.00
Event_Moq 15,894.6 ns 70.78 ns 66.21 ns 46.29 0.39 0.7324 - 12.51 KB 6.09
Event_NSubstitute 5,654.2 ns 18.48 ns 17.29 ns 16.47 0.13 0.5493 0.0076 9.05 KB 4.40
Event_FakeItEasy 210,030.5 ns 838.49 ns 654.64 ns 611.73 4.84 0.7324 0.4883 15.39 KB 7.49
Event_Imposter 1,359.1 ns 5.82 ns 4.86 ns 3.96 0.03 0.5379 0.0153 8.8 KB 4.29
Event_TUnitMocks 190.2 ns 2.04 ns 1.81 ns 0.55 0.01 0.0837 0.0002 1.37 KB 0.67
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.87GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.203
[Host] : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3

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

Method N Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Indexer_Mockolate 1 1.054 μs 0.0066 μs 0.0058 μs 1.00 0.01 0.2460 0.0019 4.03 KB 1.00
Indexer_Moq 1 218.193 μs 1.2803 μs 1.1350 μs 207.05 1.52 0.9766 0.4883 20.25 KB 5.02
Indexer_NSubstitute 1 9.545 μs 0.0536 μs 0.0501 μs 9.06 0.07 0.7782 - 12.78 KB 3.17
Indexer_FakeItEasy 1 12.275 μs 0.0344 μs 0.0305 μs 11.65 0.07 0.8392 0.0305 13.88 KB 3.44
Indexer_Imposter 1 1.069 μs 0.0069 μs 0.0064 μs 1.01 0.01 0.3147 0.0038 5.16 KB 1.28
Indexer_Mockolate 10 2.570 μs 0.0081 μs 0.0072 μs 1.00 0.00 0.3090 - 5.09 KB 1.00
Indexer_Moq 10 229.743 μs 1.1869 μs 1.1102 μs 89.41 0.48 1.7090 1.4648 28.64 KB 5.63
Indexer_NSubstitute 10 23.421 μs 0.0543 μs 0.0453 μs 9.11 0.03 1.5564 0.0305 25.58 KB 5.03
Indexer_FakeItEasy 10 26.156 μs 0.1415 μs 0.1323 μs 10.18 0.06 2.1667 0.0610 35.47 KB 6.97
Indexer_Imposter 10 2.309 μs 0.0142 μs 0.0126 μs 0.90 0.01 0.4845 0.0076 7.97 KB 1.57
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.45GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.203
[Host] : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3

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

Method N Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Method_Mockolate 1 486.7 ns 12.84 ns 12.01 ns 1.00 0.03 0.1416 0.0010 2.32 KB 1.00
Method_Moq 1 182,555.9 ns 496.83 ns 414.88 ns 375.30 9.22 0.4883 - 14.58 KB 6.28
Method_NSubstitute 1 6,017.5 ns 23.05 ns 20.43 ns 12.37 0.31 0.5569 0.0076 9.12 KB 3.93
Method_FakeItEasy 1 6,190.2 ns 33.44 ns 31.28 ns 12.73 0.32 0.4959 - 8.11 KB 3.50
Method_Imposter 1 652.0 ns 14.55 ns 13.61 ns 1.34 0.04 0.2470 0.0029 4.04 KB 1.74
Method_TUnitMocks 1 707.6 ns 13.49 ns 12.62 ns 1.45 0.04 0.1774 0.0010 2.9 KB 1.25
Method_Mockolate 10 797.9 ns 5.98 ns 5.59 ns 1.00 0.01 0.1545 0.0010 2.53 KB 1.00
Method_Moq 10 187,723.8 ns 1,041.35 ns 974.08 ns 235.28 1.98 0.9766 0.7324 18.48 KB 7.30
Method_NSubstitute 10 8,829.5 ns 48.27 ns 45.15 ns 11.07 0.09 0.7324 0.0153 12.07 KB 4.77
Method_FakeItEasy 10 9,702.1 ns 49.32 ns 43.72 ns 12.16 0.10 0.9766 0.0305 16.05 KB 6.34
Method_Imposter 10 1,316.0 ns 19.73 ns 17.49 ns 1.65 0.02 0.3376 0.0057 5.52 KB 2.18
Method_TUnitMocks 10 1,769.7 ns 10.64 ns 9.95 ns 2.22 0.02 0.2747 0.0019 4.49 KB 1.77
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.45GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.203
[Host] : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3

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

Method N Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Property_Mockolate 1 541.8 ns 7.53 ns 7.04 ns 1.00 0.02 0.1574 0.0010 2.58 KB 1.00
Property_Moq 1 11,700.2 ns 128.22 ns 119.94 ns 21.60 0.35 0.6104 - 10.39 KB 4.03
Property_NSubstitute 1 7,381.2 ns 33.54 ns 29.73 ns 13.63 0.18 0.6943 0.0076 11.45 KB 4.44
Property_FakeItEasy 1 8,377.5 ns 53.59 ns 50.13 ns 15.46 0.21 0.6866 - 11.24 KB 4.36
Property_Imposter 1 444.4 ns 4.44 ns 4.15 ns 0.82 0.01 0.1912 0.0019 3.13 KB 1.21
Property_TUnitMocks 1 728.3 ns 4.47 ns 3.96 ns 1.34 0.02 0.1526 0.0010 2.51 KB 0.97
Property_Mockolate 10 1,080.6 ns 8.98 ns 8.40 ns 1.00 0.01 0.1869 - 3.07 KB 1.00
Property_Moq 10 18,251.7 ns 59.60 ns 55.75 ns 16.89 0.14 1.0986 - 18.28 KB 5.95
Property_NSubstitute 10 16,820.3 ns 47.54 ns 39.70 ns 15.57 0.12 1.2817 0.0305 21.08 KB 6.87
Property_FakeItEasy 10 19,783.6 ns 151.85 ns 142.04 ns 18.31 0.19 1.8616 - 30.81 KB 10.03
Property_Imposter 10 1,159.5 ns 11.10 ns 9.84 ns 1.07 0.01 0.2842 0.0019 4.67 KB 1.52
Property_TUnitMocks 10 2,122.0 ns 17.38 ns 15.41 ns 1.96 0.02 0.2823 - 4.66 KB 1.52
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.203
[Host] : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v4

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Callback_Mockolate 416.0 ns 3.61 ns 3.38 ns 1.00 0.01 0.0796 0.0005 1.96 KB 1.00
Callback_Moq 70,383.5 ns 185.53 ns 154.92 ns 169.21 1.37 0.2441 - 8.88 KB 4.53
Callback_NSubstitute 4,095.1 ns 19.40 ns 18.14 ns 9.84 0.09 0.3128 0.0076 7.74 KB 3.95
Callback_FakeItEasy 4,693.8 ns 23.52 ns 20.85 ns 11.28 0.10 0.2747 - 6.91 KB 3.52
Callback_Imposter 407.7 ns 1.75 ns 1.55 ns 0.98 0.01 0.0968 0.0005 2.38 KB 1.22
Callback_TUnitMocks 639.7 ns 3.07 ns 2.72 ns 1.54 0.01 0.1068 0.0010 2.63 KB 1.34

@github-actions github-actions Bot added the state: released The issue is released label Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant