Skip to content

Commit 072f770

Browse files
fix(generator): guard newer refs for netstandard2.1 codegen
1 parent 57a4f6b commit 072f770

File tree

9 files changed

+21
-7
lines changed

9 files changed

+21
-7
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ src/Nino.sln.DotSettings.user
1010
src/Nino.Shared/Nino.Shared.csproj.user
1111
**/BenchmarkDotNet.Artifacts
1212
src/Nino.Core/.AssemblyAttributes
13-
src/Nino.UnitTests/build.binlog
14-
src/Nino.UnitTests/args.rsp
13+
src/Nino.UnitTests/*.binlog
14+
src/Nino.UnitTests/*.rsp
1515

1616
# LLM Agent directories and configuration
1717
.claude/

src/Nino.Generator/BuiltInType/ArrayGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected override void GenerateSerializer(ITypeSymbol typeSymbol, Writer writer
9090
writer.AppendLine();
9191

9292
// Both value and reference types benefit from ref iteration - eliminates bounds checks
93-
writer.AppendLine("#if !UNITY_2020_2_OR_NEWER");
93+
writer.AppendLine("#if NET5_0_OR_GREATER");
9494
writer.AppendLine(" ref var cur = ref System.Runtime.InteropServices.MemoryMarshal.GetArrayDataReference(value);");
9595
writer.AppendLine("#else");
9696
writer.AppendLine(" ref var cur = ref value[0];");

src/Nino.Generator/BuiltInType/DictionaryGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected override void GenerateSerializer(ITypeSymbol typeSymbol, Writer writer
114114
writer.AppendLine(" return;");
115115
writer.AppendLine(" }");
116116
writer.AppendLine(" // Iterate entries via direct ref to avoid bounds checks");
117-
writer.AppendLine("#if !UNITY_2020_2_OR_NEWER");
117+
writer.AppendLine("#if NET5_0_OR_GREATER");
118118
writer.AppendLine(" ref var entryRef = ref System.Runtime.InteropServices.MemoryMarshal.GetArrayDataReference(entries);");
119119
writer.AppendLine("#else");
120120
writer.AppendLine(" ref var entryRef = ref entries[0];");

src/Nino.Generator/BuiltInType/HashSetGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected override void GenerateSerializer(ITypeSymbol typeSymbol, Writer writer
106106
writer.AppendLine(" return;");
107107
writer.AppendLine(" }");
108108
writer.AppendLine(" // Iterate entries via direct ref to avoid bounds checks");
109-
writer.AppendLine("#if !UNITY_2020_2_OR_NEWER");
109+
writer.AppendLine("#if NET5_0_OR_GREATER");
110110
writer.AppendLine(" ref var entryRef = ref System.Runtime.InteropServices.MemoryMarshal.GetArrayDataReference(entries);");
111111
writer.AppendLine("#else");
112112
writer.AppendLine(" ref var entryRef = ref entries[0];");

src/Nino.Generator/BuiltInType/StackGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected override void GenerateSerializer(ITypeSymbol typeSymbol, Writer writer
108108

109109
// Use ref iteration for all types to eliminate bounds checks
110110
writer.AppendLine(" // Stack serializes from top to bottom with ref iteration");
111-
writer.AppendLine("#if !UNITY_2020_2_OR_NEWER");
111+
writer.AppendLine("#if NET5_0_OR_GREATER");
112112
writer.AppendLine(" ref var cur = ref System.Runtime.CompilerServices.Unsafe.Add(ref System.Runtime.InteropServices.MemoryMarshal.GetArrayDataReference(array), cnt - 1);");
113113
writer.AppendLine(" ref var end = ref System.Runtime.CompilerServices.Unsafe.Subtract(ref System.Runtime.InteropServices.MemoryMarshal.GetArrayDataReference(array), 1);");
114114
writer.AppendLine("#else");

src/Nino.UnitTests/AdvancedCollectionTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void TestNewCollectionTypes()
4343

4444
Console.WriteLine("SortedSet tests passed!");
4545

46+
#if NET6_0_OR_GREATER
4647
// Test PriorityQueue<TElement, TPriority>
4748
// 1. Primitive unmanaged
4849
PriorityQueue<int, int> priorityQueueInt = new PriorityQueue<int, int>();
@@ -80,6 +81,7 @@ public void TestNewCollectionTypes()
8081
Assert.AreEqual(2, priorityQueueStructResult.Count);
8182

8283
Console.WriteLine("PriorityQueue tests passed!");
84+
#endif
8385

8486
// Test SortedDictionary<TKey, TValue>
8587
// 1. Primitive unmanaged key and value
@@ -264,6 +266,7 @@ public void TestNewCollectionTypesComplex()
264266
Assert.AreEqual(1, immutableListOf2DArraysResult[0][0, 0]);
265267
Assert.AreEqual(8, immutableListOf2DArraysResult[1][1, 1]);
266268

269+
#if NET6_0_OR_GREATER
267270
// 5. PriorityQueue with nested generic type parameters
268271
PriorityQueue<List<string>, int> priorityQueueNested = new PriorityQueue<List<string>, int>();
269272
priorityQueueNested.Enqueue(new List<string> { "a", "b" }, 2);
@@ -276,6 +279,7 @@ public void TestNewCollectionTypesComplex()
276279
List<string> firstItem = priorityQueueNestedResult.Dequeue();
277280
Assert.AreEqual(2, firstItem.Count);
278281
Assert.AreEqual("c", firstItem[0]); // Priority 1 comes first
282+
#endif
279283

280284
// 6. ReadOnlyDictionary with complex keys and values
281285
Dictionary<int, ImmutableArray<TestStruct3>> innerComplexDict =

src/Nino.UnitTests/Nino.UnitTests.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<IsPackable>false</IsPackable>
77
<LangVersion>10</LangVersion>
88
<OutputType>Library</OutputType>
9-
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
9+
<TargetFrameworks>net6.0;net8.0;netstandard2.1</TargetFrameworks>
1010
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
1111
<ReportAnalyzer>true</ReportAnalyzer>
1212
<EnableNETAnalyzers>false</EnableNETAnalyzers>
@@ -24,6 +24,10 @@
2424
<ItemGroup>
2525
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" PrivateAssets="all"/>
2626
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
27+
<PackageReference Include="PolySharp" Version="1.15.0">
28+
<PrivateAssets>all</PrivateAssets>
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
</PackageReference>
2731
<PackageReference Include="System.Buffers" Version="4.6.0" />
2832
<PackageReference Include="System.Memory" Version="4.6.0" />
2933
</ItemGroup>

src/Nino.UnitTests/RefOverloadTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ public void TestNewCollectionTypesRefOverload()
313313
Assert.IsTrue(existingSortedSet.Contains(1));
314314
Assert.IsFalse(existingSortedSet.Contains(10)); // Should be cleared
315315

316+
#if NET6_0_OR_GREATER
316317
// Test ref overload for PriorityQueue
317318
PriorityQueue<string, int> priorityQueue = new PriorityQueue<string, int>();
318319
priorityQueue.Enqueue("first", 1);
@@ -324,6 +325,7 @@ public void TestNewCollectionTypesRefOverload()
324325
NinoDeserializer.Deserialize(bytes, ref existingPriorityQueue);
325326
Assert.AreEqual(2, existingPriorityQueue.Count);
326327
Assert.AreEqual("first", existingPriorityQueue.Dequeue());
328+
#endif
327329

328330
// Test ref overload for SortedDictionary
329331
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>

src/Nino.UnitTests/TestClass.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ namespace Nino.UnitTests
247247

248248
public TestPrivateMemberClass()
249249
{
250+
#if NET6_0_OR_GREATER
250251
Id = Random.Shared.Next();
252+
#else
253+
Id = new Random().Next();
254+
#endif
251255
}
252256
}
253257

0 commit comments

Comments
 (0)