Skip to content

Commit 149aaff

Browse files
committed
Fixed some generator type conversion safety bugs, and upped package versions.
1 parent c98eb62 commit 149aaff

5 files changed

Lines changed: 21 additions & 15 deletions

File tree

DomainModeling.CodeFixProviders/SyntaxNodeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public static SyntaxTrivia GetNewlineTrivia(this SyntaxNode node)
2424
var length = trivia.Span.Length;
2525
var lengthIsOne = length == 1;
2626
var lengthIsTwo = length == 2;
27-
nCount += Unsafe.As<bool, int>(ref lengthIsOne);
28-
rnCount += Unsafe.As<bool, int>(ref lengthIsTwo);
27+
nCount += Unsafe.As<bool, byte>(ref lengthIsOne);
28+
rnCount += Unsafe.As<bool, byte>(ref lengthIsTwo);
2929
}
3030

3131
return rnCount > nCount

DomainModeling.Example/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using Newtonsoft.Json;
23

34
namespace Architect.DomainModeling.Example;
45

56
public static class Program
67
{
8+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Merely demoing JSON serialization.")]
79
public static void Main()
810
{
911
// ValueObject

DomainModeling.Generator/GeneratableExtensions.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ internal static class GeneratableExtensions
99
/// </summary>
1010
public static bool GetBit(this uint bits, int position)
1111
{
12-
var result = (bits >> position) & 1U;
13-
return Unsafe.As<uint, bool>(ref result);
12+
var result = (bits >> position) & 1U;
13+
var bitValue = (byte)result;
14+
return Unsafe.As<byte, bool>(ref bitValue);
1415
}
1516

1617
/// <summary>
@@ -19,13 +20,16 @@ public static bool GetBit(this uint bits, int position)
1920
public static void SetBit(ref this uint bits, int position, bool value)
2021
{
2122
// Create a mask to unset the target bit: 1 << position
22-
// Unset the target bit: & (1 << position)
23+
// Unset the target bit: & ~(1 << position)
2324

24-
// Create a mask to write the target bit value: 1 << value
25-
// Write the target bit: | (1 << value)
25+
// Create a mask to write the target bit value: value << position
26+
// Write the target bit: | (1 << value)
27+
28+
var intendedBit = Unsafe.As<bool, byte>(ref value);
29+
var intendedBitInPosition = (uint)intendedBit << position;
2630

2731
bits = bits
2832
& ~(1U << position)
29-
| (Unsafe.As<bool, uint>(ref value) << position);
33+
| intendedBitInPosition;
3034
}
3135
}

DomainModeling.Tests/DomainModeling.Tests.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
@@ -23,11 +23,11 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
27-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.8" />
28-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
26+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.14" />
27+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.14" />
28+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
2929
<PackageReference Include="xunit" Version="2.9.3" />
30-
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
30+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
3131
<PrivateAssets>all</PrivateAssets>
3232
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3333
</PackageReference>

DomainModeling/DomainModeling.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Misc:
7171
- Feature: Non-generic Wrapper/Identity interfaces.
7272
- Feature: DummyBuilder records cloned on each step, for reuse.
7373
- Feature: Analyzer warns when '==' or similar operator implicitly casts some IValueObject to something else. Avoids accidentally comparing unrelated types.
74-
- Feature: Analyzer warns when '>' or similar operator risks unintended null handling.
74+
- Feature: Analyzer warns when '&gt;' or similar operator risks unintended null handling.
7575
- Feature: Analyzer warns if field initializers might be skipped.
7676
- Fixed: Source-generated default ctor should not exist in case of primary ctor or lacking base default ctor.
7777
- Fixed: Source-generated records would ignore hand-written ToString()/Equals()/GetHashCode().
@@ -97,7 +97,7 @@ Misc:
9797
</ItemGroup>
9898

9999
<ItemGroup>
100-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
100+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
101101
</ItemGroup>
102102

103103
<ItemGroup>

0 commit comments

Comments
 (0)