Skip to content

fix: preserve init accessor on init-only properties#717

Merged
vbreuss merged 1 commit into
mainfrom
topic/preserve-init-accessor
Apr 29, 2026
Merged

fix: preserve init accessor on init-only properties#717
vbreuss merged 1 commit into
mainfrom
topic/preserve-init-accessor

Conversation

@vbreuss
Copy link
Copy Markdown
Member

@vbreuss vbreuss commented Apr 29, 2026

This pull request adds support for C# init-only properties in the source generator, ensuring that properties declared with the init accessor are correctly recognized and emitted in generated code. The changes update both the code generation logic and the method/property metadata, and include a new test to verify this functionality.

Support for init-only properties:

  • Added an IsInitOnly property to the Method entity to track whether a method or property accessor is init-only.
  • Updated property code generation to emit init instead of set for init-only setters in generated mock classes.
  • Modified the logic for generating property setters to skip certain code paths for init-only properties, ensuring correct behavior.

@vbreuss vbreuss self-assigned this Apr 29, 2026
@vbreuss vbreuss added the bug Something isn't working label Apr 29, 2026
@vbreuss vbreuss enabled auto-merge (squash) April 29, 2026 17:54
@vbreuss vbreuss force-pushed the topic/preserve-init-accessor branch from 82bfc57 to a16407d Compare April 29, 2026 17:59
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 29, 2026

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 9V74 2.60GHz, 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 361.9 ns 6.82 ns 6.38 ns 1.00 0.02 0.1254 0.0005 2.05 KB 1.00
Event_Moq 13,778.9 ns 176.72 ns 156.66 ns 38.09 0.77 0.7324 - 12.51 KB 6.09
Event_NSubstitute 5,598.8 ns 74.99 ns 70.15 ns 15.48 0.32 0.5493 0.0076 9.05 KB 4.40
Event_FakeItEasy 229,565.8 ns 515.36 ns 482.07 ns 634.57 10.92 0.7324 0.4883 15.39 KB 7.49
Event_Imposter 1,490.0 ns 45.31 ns 42.38 ns 4.12 0.13 0.5379 0.0153 8.8 KB 4.29
Event_TUnitMocks 186.8 ns 4.92 ns 4.60 ns 0.52 0.02 0.0837 0.0002 1.37 KB 0.67
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Intel Xeon Platinum 8370C CPU 2.80GHz (Max: 2.77GHz), 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 N Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Indexer_Mockolate 1 1.036 μs 0.0221 μs 0.0207 μs 1.00 0.03 0.1640 - 4.03 KB 1.00
Indexer_Moq 1 162.116 μs 0.9731 μs 0.9103 μs 156.59 3.12 0.4883 - 20.36 KB 5.05
Indexer_NSubstitute 1 9.040 μs 0.0649 μs 0.0607 μs 8.73 0.18 0.5188 - 12.78 KB 3.17
Indexer_FakeItEasy 1 11.295 μs 0.0616 μs 0.0576 μs 10.91 0.22 0.5646 0.0153 13.87 KB 3.44
Indexer_Imposter 1 1.002 μs 0.0229 μs 0.0214 μs 0.97 0.03 0.2098 0.0019 5.16 KB 1.28
Indexer_Mockolate 10 2.898 μs 0.0175 μs 0.0164 μs 1.00 0.01 0.2060 - 5.09 KB 1.00
Indexer_Moq 10 171.928 μs 0.8577 μs 0.7603 μs 59.34 0.41 0.9766 0.7324 29.89 KB 5.88
Indexer_NSubstitute 10 21.352 μs 0.1276 μs 0.1131 μs 7.37 0.06 1.0376 0.0305 25.58 KB 5.03
Indexer_FakeItEasy 10 23.009 μs 0.0877 μs 0.0777 μs 7.94 0.05 1.4343 0.0610 35.48 KB 6.98
Indexer_Imposter 10 2.189 μs 0.0315 μs 0.0280 μs 0.76 0.01 0.3242 - 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 445.9 ns 2.98 ns 2.49 ns 1.00 0.01 0.1416 0.0010 2.32 KB 1.00
Method_Moq 1 183,153.5 ns 314.15 ns 262.33 ns 410.72 2.29 0.4883 - 14.58 KB 6.28
Method_NSubstitute 1 5,518.4 ns 19.44 ns 17.23 ns 12.37 0.08 0.5569 0.0076 9.12 KB 3.93
Method_FakeItEasy 1 6,038.2 ns 16.96 ns 15.03 ns 13.54 0.08 0.4959 - 8.11 KB 3.50
Method_Imposter 1 634.6 ns 5.00 ns 4.43 ns 1.42 0.01 0.2470 0.0029 4.04 KB 1.74
Method_TUnitMocks 1 651.4 ns 5.01 ns 4.18 ns 1.46 0.01 0.1774 0.0010 2.9 KB 1.25
Method_Mockolate 10 743.0 ns 5.32 ns 4.98 ns 1.00 0.01 0.1545 0.0010 2.53 KB 1.00
Method_Moq 10 189,569.3 ns 508.32 ns 450.61 ns 255.13 1.76 0.9766 0.7324 18.48 KB 7.30
Method_NSubstitute 10 8,408.7 ns 21.77 ns 18.18 ns 11.32 0.08 0.7324 0.0153 12.07 KB 4.77
Method_FakeItEasy 10 9,301.6 ns 48.44 ns 42.94 ns 12.52 0.10 0.9766 0.0305 16.05 KB 6.34
Method_Imposter 10 1,267.8 ns 5.15 ns 4.30 ns 1.71 0.01 0.3376 0.0057 5.52 KB 2.18
Method_TUnitMocks 10 1,659.4 ns 12.84 ns 11.38 ns 2.23 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 578.4 ns 11.16 ns 10.44 ns 1.00 0.02 0.1574 0.0010 2.58 KB 1.00
Property_Moq 1 11,750.3 ns 84.68 ns 70.71 ns 20.32 0.37 0.6104 - 10.39 KB 4.03
Property_NSubstitute 1 7,708.8 ns 29.84 ns 26.45 ns 13.33 0.24 0.6866 - 11.45 KB 4.44
Property_FakeItEasy 1 8,749.2 ns 33.72 ns 29.89 ns 15.13 0.27 0.6866 - 11.24 KB 4.36
Property_Imposter 1 440.9 ns 11.87 ns 11.11 ns 0.76 0.02 0.1912 0.0019 3.13 KB 1.21
Property_TUnitMocks 1 763.8 ns 3.27 ns 3.06 ns 1.32 0.02 0.1526 0.0010 2.51 KB 0.97
Property_Mockolate 10 1,067.2 ns 7.08 ns 6.27 ns 1.00 0.01 0.1869 - 3.07 KB 1.00
Property_Moq 10 18,679.4 ns 223.28 ns 208.85 ns 17.50 0.21 1.0986 - 18.28 KB 5.95
Property_NSubstitute 10 17,953.4 ns 67.08 ns 56.02 ns 16.82 0.11 1.2817 0.0305 21.08 KB 6.87
Property_FakeItEasy 10 20,938.0 ns 67.92 ns 60.21 ns 19.62 0.12 1.8616 - 30.81 KB 10.03
Property_Imposter 10 1,156.6 ns 9.62 ns 9.00 ns 1.08 0.01 0.2842 0.0019 4.67 KB 1.52
Property_TUnitMocks 10 2,154.4 ns 6.16 ns 4.81 ns 2.02 0.01 0.2823 - 4.66 KB 1.52
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 Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Callback_Mockolate 396.3 ns 1.90 ns 1.59 ns 1.00 0.01 0.1197 - 1.96 KB 1.00
Callback_Moq 96,821.0 ns 253.57 ns 237.19 ns 244.30 1.11 0.4883 0.2441 8.88 KB 4.53
Callback_NSubstitute 4,320.4 ns 9.90 ns 8.77 ns 10.90 0.05 0.4730 0.0076 7.74 KB 3.95
Callback_FakeItEasy 4,723.0 ns 15.92 ns 14.12 ns 11.92 0.06 0.4120 0.0153 6.81 KB 3.47
Callback_Imposter 391.9 ns 2.86 ns 2.68 ns 0.99 0.01 0.1454 0.0010 2.38 KB 1.22
Callback_TUnitMocks 591.1 ns 5.14 ns 4.81 ns 1.49 0.01 0.1602 0.0010 2.63 KB 1.34

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 29, 2026

Test Results

    21 files  ±0      21 suites  ±0   9m 30s ⏱️ -24s
 3 847 tests +1   3 845 ✅ ±0  1 💤 ±0  1 ❌ +1 
25 055 runs  +3  25 052 ✅ +1  1 💤 ±0  2 ❌ +2 

For more details on these failures, see this check.

Results for commit a58449d. ± Comparison against base commit 22dfeb1.

♻️ This comment has been updated with latest results.

@vbreuss vbreuss force-pushed the topic/preserve-init-accessor branch from a16407d to a58449d Compare April 29, 2026 18:16
@vbreuss vbreuss force-pushed the topic/preserve-init-accessor branch from a58449d to 24dc68a Compare April 29, 2026 18:25
@vbreuss vbreuss disabled auto-merge April 29, 2026 18:29
@vbreuss vbreuss merged commit d8827bb into main Apr 29, 2026
12 checks passed
@vbreuss vbreuss deleted the topic/preserve-init-accessor branch April 29, 2026 18:29
@sonarqubecloud
Copy link
Copy Markdown

@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