Skip to content

Commit

Permalink
Fix assembly default to nullable, remove pointer support, bump to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikernet committed Oct 25, 2024
1 parent 260949f commit 5025ef5
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 178 deletions.
2 changes: 1 addition & 1 deletion Source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Nullable>enable</Nullable>
<AnnotatedReferenceAssemblyVersion>6.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>1.0.6</Version>
<Version>2.0.0</Version>
<DefineConstants Condition="'$(IsCI)' == 'true'">$(DefineConstants);CI</DefineConstants>
</PropertyGroup>

Expand Down
29 changes: 4 additions & 25 deletions Source/RuntimeNullables.Fody.Tests/PointerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestAssemblyPointers;

namespace RuntimeNullables.Fody.Tests;
Expand All @@ -10,31 +9,11 @@ public class PointerTests
private readonly int _value = 5;

[TestMethod]
public unsafe void ReturnSameValueNullableParameter()
public unsafe void PointerIgnored()
{
fixed (int* value = &_value) {
Pointers.ReturnSameValueNullableParameter(value);
var ex = Assert.ThrowsException<NullReferenceException>(() => Pointers.ReturnSameValueNullableParameter(null));
Assert.IsTrue(ex.Message.Contains("return", StringComparison.OrdinalIgnoreCase), "Exception message did not apply to return value.");
}
}

[TestMethod]
public unsafe void AllowNullOnlyNoOp()
{
fixed (int* value = &_value) {
Pointers.AllowNullOnlyNoOp(value);
var ex = Assert.ThrowsException<NullReferenceException>(() => Pointers.AllowNullOnlyNoOp(null));
Assert.IsTrue(ex.Message.Contains("output", StringComparison.OrdinalIgnoreCase), "Exception message did not apply to output value.");
}
}

[TestMethod]
public unsafe void NonNullParameter()
{
fixed (int* value = &_value) {
Pointers.NonNullParameter(value);
Assert.ThrowsException<ArgumentNullException>(() => Pointers.NonNullParameter(null));
Pointers.Ignored(value);
Pointers.Ignored(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FodyHelpers" Version="6.8.0" />
<PackageReference Include="FodyHelpers" Version="6.8.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,15 @@
extends [System.Runtime]System.Object
{
.method public hidebysig static int32*
ReturnSameValueNullableParameter(int32* 'value') cil managed
Ignored(int32* 'value') cil managed
{
.param [1]
.maxstack 8
IL_0000: ldarg.0
IL_0001: dup
IL_0002: brtrue.s IL_000e
IL_0004: ldstr "Return value nullability contract was broken."
IL_0009: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000e: ret
}
.method public hidebysig static void AllowNullOnlyNoOp(int32* 'value') cil managed
{
.param [1]
.maxstack 8
.maxstack 1
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: brtrue.s IL_000e
IL_0004: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0009: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000e: ret
}
.method public hidebysig static void NonNullParameter(int32* 'value') cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000d
IL_0003: ldstr "value"
IL_0008: call void RuntimeNullables.ThrowHelpers::ThrowArgumentNull(string)
IL_000d: nop
IL_000e: ldarg.0
IL_000f: brtrue.s IL_001b
IL_0011: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0016: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_001b: ret
IL_0002: stloc.0
IL_0003: br.s IL_0005
IL_0005: ldloc.0
IL_0006: ret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,15 @@
extends [System.Runtime]System.Object
{
.method public hidebysig static int32*
ReturnSameValueNullableParameter(int32* 'value') cil managed
Ignored(int32* 'value') cil managed
{
.param [1]
.maxstack 8
IL_0000: ldarg.0
IL_0001: dup
IL_0002: brtrue.s IL_000e
IL_0004: ldstr "Return value nullability contract was broken."
IL_0009: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000e: ret
}
.method public hidebysig static void AllowNullOnlyNoOp(int32* 'value') cil managed
{
.param [1]
.maxstack 8
.maxstack 1
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: brtrue.s IL_000e
IL_0004: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0009: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000e: ret
}
.method public hidebysig static void NonNullParameter(int32* 'value') cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000d
IL_0003: ldstr "value"
IL_0008: call void RuntimeNullables.ThrowHelpers::ThrowArgumentNull(string)
IL_000d: nop
IL_000e: ldarg.0
IL_000f: brtrue.s IL_001b
IL_0011: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0016: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_001b: ret
IL_0002: stloc.0
IL_0003: br.s IL_0005
IL_0005: ldloc.0
IL_0006: ret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,10 @@
extends [System.Runtime]System.Object
{
.method public hidebysig static int32*
ReturnSameValueNullableParameter(int32* 'value') cil managed
Ignored(int32* 'value') cil managed
{
.param [1]
.maxstack 8
IL_0000: ldarg.0
IL_0001: dup
IL_0002: brtrue.s IL_000e
IL_0004: ldstr "Return value nullability contract was broken."
IL_0009: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000e: ret
}
.method public hidebysig static void AllowNullOnlyNoOp(int32* 'value') cil managed
{
.param [1]
.maxstack 8
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000d
IL_0003: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0008: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000d: ret
}
.method public hidebysig static void NonNullParameter(int32* 'value') cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000d
IL_0003: ldstr "value"
IL_0008: call void RuntimeNullables.ThrowHelpers::ThrowArgumentNull(string)
IL_000d: ldarg.0
IL_000e: brtrue.s IL_001a
IL_0010: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0015: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_001a: ret
IL_0001: ret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,10 @@
extends [System.Runtime]System.Object
{
.method public hidebysig static int32*
ReturnSameValueNullableParameter(int32* 'value') cil managed
Ignored(int32* 'value') cil managed
{
.param [1]
.maxstack 8
IL_0000: ldarg.0
IL_0001: dup
IL_0002: brtrue.s IL_000e
IL_0004: ldstr "Return value nullability contract was broken."
IL_0009: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000e: ret
}
.method public hidebysig static void AllowNullOnlyNoOp(int32* 'value') cil managed
{
.param [1]
.maxstack 8
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000d
IL_0003: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0008: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_000d: ret
}
.method public hidebysig static void NonNullParameter(int32* 'value') cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000d
IL_0003: ldstr "value"
IL_0008: call void RuntimeNullables.ThrowHelpers::ThrowArgumentNull(string)
IL_000d: ldarg.0
IL_000e: brtrue.s IL_001a
IL_0010: ldstr "Output parameter 'value' nullability contract was "
+ "broken."
IL_0015: call void RuntimeNullables.ThrowHelpers::ThrowOutputNull(string)
IL_001a: ret
IL_0001: ret
}
}
2 changes: 1 addition & 1 deletion Source/RuntimeNullables.Fody/Contexts/NullableContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal NullableContext(ICustomAttributeProvider attributeProvider, WeavingCont
{
WeavingContext = weavingContext;

_isNullable = false;
_isNullable = true;
_nullChecksEnabled = true;

ProcessAttributes(attributeProvider, ref _isNullable, ref _nullChecksEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ internal static class TypeReferenceExtensions
{
public static bool IsReferenceType(this TypeReference type)
{
if (type.IsPointer || type.IsArray)
if (type.IsPointer)
return false;

if (type.IsArray)
return true;

type = type.GetElementType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static bool InjectParameterChecks(MethodContext methodContext, ref Retur

// Postconditions:

if (weavingContext.CheckOutputs && (parameterType.IsPointer || (parameterType.IsByReference && !parameter.IsIn))) {
if (weavingContext.CheckOutputs && parameterType.IsByReference && !parameter.IsIn) {
bool? maybeNull = MaybeNull(parameter);

// Avoid calling MethodContext.IsParameterNullable unless we did not already do so for the getter and we are going to need the value
Expand Down
2 changes: 1 addition & 1 deletion Source/RuntimeNullables.Fody/RuntimeNullables.Fody.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FodyHelpers" Version="6.8.0" />
<PackageReference Include="FodyHelpers" Version="6.8.2" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Source/RuntimeNullables/RuntimeNullables.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fody" Version="6.8.0" PrivateAssets="none" />
<PackageReference Include="FodyPackaging" Version="6.8.0" PrivateAssets="All" />
<PackageReference Include="Fody" Version="6.8.2" PrivateAssets="none" />
<PackageReference Include="FodyPackaging" Version="6.8.2" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Source/TestAssembly/TestAssembly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<ItemGroup>
<WeaverFiles Include="$(SolutionDir)RuntimeNullables.Fody\bin\$(Configuration)\netstandard2.0\RuntimeNullables.Fody.dll" />
<PackageReference Include="Fody" Version="6.8.0">
<PackageReference Include="Fody" Version="6.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Source/TestAssemblyNonPublic/TestAssemblyNonPublic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<ItemGroup>
<WeaverFiles Include="$(SolutionDir)RuntimeNullables.Fody\bin\$(Configuration)\netstandard2.0\RuntimeNullables.Fody.dll" />
<PackageReference Include="Fody" Version="6.8.0">
<PackageReference Include="Fody" Version="6.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
11 changes: 2 additions & 9 deletions Source/TestAssemblyPointers/Pointers.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System.Diagnostics.CodeAnalysis;

#pragma warning disable CA1801 // Review unused parameters

#pragma warning disable CA1801 // Review unused parameters
namespace TestAssemblyPointers;

public static class Pointers
{
public static unsafe int* ReturnSameValueNullableParameter([AllowNull, MaybeNull] int* value) => value;

public static unsafe void AllowNullOnlyNoOp([AllowNull] int* value) { }

public static unsafe void NonNullParameter(int* value) { }
public static unsafe int* Ignored(int* value) { return value; }
}
2 changes: 1 addition & 1 deletion Source/TestAssemblyPointers/TestAssemblyPointers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<ItemGroup>
<WeaverFiles Include="$(SolutionDir)RuntimeNullables.Fody\bin\$(Configuration)\netstandard2.0\RuntimeNullables.Fody.dll" />
<PackageReference Include="Fody" Version="6.8.0">
<PackageReference Include="Fody" Version="6.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fody" Version="6.8.0">
<PackageReference Include="Fody" Version="6.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 5025ef5

Please sign in to comment.