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 212bfc8
Show file tree
Hide file tree
Showing 28 changed files with 117 additions and 182 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
11 changes: 11 additions & 0 deletions Source/RuntimeNullables.Fody.Tests/NullableDisabledTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestAssembly;

namespace RuntimeNullables.Fody.Tests;

[TestClass]
public class NullableDisabledTests
{
[TestMethod]
public void NonNull() => NullableDisabled.NonNull(null);
}
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,7 +2,7 @@
extends [System.Runtime]System.Object
{
string) = ( 01 00 15 52 75 6E 74 69 6D 65 4E 75 6C 6C 61 62 // ...RuntimeNullab
6C 65 73 2E 46 6F 64 79 07 31 2E 30 2E 36 2E 30 // les.Fody.1.0.6.0
6C 65 73 2E 46 6F 64 79 07 32 2E 30 2E 30 2E 30 // les.Fody.2.0.0.0
00 00 )
.method assembly hidebysig static void
ThrowArgumentNull(string paramName) cil managed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extends [System.Runtime]System.Object
{
string) = ( 01 00 15 52 75 6E 74 69 6D 65 4E 75 6C 6C 61 62 // ...RuntimeNullab
6C 65 73 2E 46 6F 64 79 07 31 2E 30 2E 36 2E 30 // les.Fody.1.0.6.0
6C 65 73 2E 46 6F 64 79 07 32 2E 30 2E 30 2E 30 // les.Fody.2.0.0.0
00 00 )
.method assembly hidebysig static void
ThrowArgumentNull(string paramName) cil managed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extends [System.Runtime]System.Object
{
string) = ( 01 00 15 52 75 6E 74 69 6D 65 4E 75 6C 6C 61 62 // ...RuntimeNullab
6C 65 73 2E 46 6F 64 79 07 31 2E 30 2E 36 2E 30 // les.Fody.1.0.6.0
6C 65 73 2E 46 6F 64 79 07 32 2E 30 2E 30 2E 30 // les.Fody.2.0.0.0
00 00 )
.method assembly hidebysig static void
ThrowArgumentNull(string paramName) cil managed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extends [System.Runtime]System.Object
{
string) = ( 01 00 15 52 75 6E 74 69 6D 65 4E 75 6C 6C 61 62 // ...RuntimeNullab
6C 65 73 2E 46 6F 64 79 07 31 2E 30 2E 36 2E 30 // les.Fody.1.0.6.0
6C 65 73 2E 46 6F 64 79 07 32 2E 30 2E 30 2E 30 // les.Fody.2.0.0.0
00 00 )
.method assembly hidebysig static void
ThrowArgumentNull(string paramName) cil managed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.class public abstract auto ansi sealed beforefieldinit TestAssembly.NullableDisabled
extends [System.Runtime]System.Object
{
.method public hidebysig static string
NonNull(string x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: ret
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.class public abstract auto ansi sealed beforefieldinit TestAssembly.NullableDisabled
extends [System.Runtime]System.Object
{
.method public hidebysig static string
NonNull(string x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: ret
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.class public abstract auto ansi sealed beforefieldinit TestAssembly.NullableDisabled
extends [System.Runtime]System.Object
{
.method public hidebysig static string
NonNull(string x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: ret
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.class public abstract auto ansi sealed beforefieldinit TestAssembly.NullableDisabled
extends [System.Runtime]System.Object
{
.method public hidebysig static string
NonNull(string x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: 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,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
}
}
6 changes: 6 additions & 0 deletions Source/RuntimeNullables.Fody.Tests/VerifyILTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public Task NestedContainer()
return Verify(Decompile(typeof(NestedContainer)), _verifySettings);
}

[TestMethod]
public Task NullableDisabled()
{
return Verify(Decompile(typeof(NullableDisabled)), _verifySettings);
}

[TestMethod]
public Task OutputParameters()
{
Expand Down
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>
Loading

0 comments on commit 212bfc8

Please sign in to comment.