Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b2097cb

Browse files
committed
Handle nullable attributes on platforms that lack them
1 parent 23cd3cf commit b2097cb

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

src/Common/src/CoreLib/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,48 @@ namespace System.Diagnostics.CodeAnalysis
66
{
77
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
88
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
9-
public sealed class AllowNullAttribute : Attribute { }
9+
#if INTERNAL_NULLABLE_ATTRIBUTES
10+
internal
11+
#else
12+
public
13+
#endif
14+
sealed class AllowNullAttribute : Attribute { }
1015

1116
/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
1217
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
13-
public sealed class DisallowNullAttribute : Attribute { }
18+
#if INTERNAL_NULLABLE_ATTRIBUTES
19+
internal
20+
#else
21+
public
22+
#endif
23+
sealed class DisallowNullAttribute : Attribute { }
1424

1525
/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
1626
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
17-
public sealed class MaybeNullAttribute : Attribute { }
27+
#if INTERNAL_NULLABLE_ATTRIBUTES
28+
internal
29+
#else
30+
public
31+
#endif
32+
sealed class MaybeNullAttribute : Attribute { }
1833

1934
/// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
2035
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
21-
public sealed class NotNullAttribute : Attribute { }
36+
#if INTERNAL_NULLABLE_ATTRIBUTES
37+
internal
38+
#else
39+
public
40+
#endif
41+
sealed class NotNullAttribute : Attribute { }
2242

2343
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
2444
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
25-
public sealed class MaybeNullWhenAttribute : Attribute
45+
#if INTERNAL_NULLABLE_ATTRIBUTES
46+
internal
47+
#else
48+
public
49+
#endif
50+
sealed class MaybeNullWhenAttribute : Attribute
2651
{
2752
/// <summary>Initializes the attribute with the specified return value condition.</summary>
2853
/// <param name="returnValue">
@@ -36,7 +61,12 @@ public sealed class MaybeNullWhenAttribute : Attribute
3661

3762
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
3863
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
39-
public sealed class NotNullWhenAttribute : Attribute
64+
#if INTERNAL_NULLABLE_ATTRIBUTES
65+
internal
66+
#else
67+
public
68+
#endif
69+
sealed class NotNullWhenAttribute : Attribute
4070
{
4171
/// <summary>Initializes the attribute with the specified return value condition.</summary>
4272
/// <param name="returnValue">
@@ -50,7 +80,12 @@ public sealed class NotNullWhenAttribute : Attribute
5080

5181
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
5282
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
53-
public sealed class NotNullIfNotNullAttribute : Attribute
83+
#if INTERNAL_NULLABLE_ATTRIBUTES
84+
internal
85+
#else
86+
public
87+
#endif
88+
sealed class NotNullIfNotNullAttribute : Attribute
5489
{
5590
/// <summary>Initializes the attribute with the associated parameter name.</summary>
5691
/// <param name="parameterName">
@@ -64,11 +99,21 @@ public sealed class NotNullIfNotNullAttribute : Attribute
6499

65100
/// <summary>Applied to a method that will never return under any circumstance.</summary>
66101
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
67-
public sealed class DoesNotReturnAttribute : Attribute { }
102+
#if INTERNAL_NULLABLE_ATTRIBUTES
103+
internal
104+
#else
105+
public
106+
#endif
107+
sealed class DoesNotReturnAttribute : Attribute { }
68108

69109
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
70110
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
71-
public sealed class DoesNotReturnIfAttribute : Attribute
111+
#if INTERNAL_NULLABLE_ATTRIBUTES
112+
internal
113+
#else
114+
public
115+
#endif
116+
sealed class DoesNotReturnIfAttribute : Attribute
72117
{
73118
/// <summary>Initializes the attribute with the specified parameter value.</summary>
74119
/// <param name="parameterValue">

src/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<AssemblyName>Microsoft.Diagnostics.Tracing.EventSource</AssemblyName>
44
<ProjectGuid>{0CAF38F5-C7E7-46F2-8F39-C5D57492FF7F}</ProjectGuid>
55
<NoWarn>$(NoWarn);CS1573</NoWarn>
6-
<DefineConstants>$(DefineConstants);NO_EVENTCOMMANDEXECUTED_SUPPORT;ES_BUILD_STANDALONE;FEATURE_MANAGED_ETW;PLATFORM_WINDOWS</DefineConstants>
6+
<DefineConstants>$(DefineConstants);NO_EVENTCOMMANDEXECUTED_SUPPORT;ES_BUILD_STANDALONE;FEATURE_MANAGED_ETW;PLATFORM_WINDOWS;INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
77
<Configurations>net461-Windows_NT-Debug;net461-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release</Configurations>
88
<Nullable>enable</Nullable>
99
</PropertyGroup>
@@ -25,6 +25,7 @@
2525
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Advapi32\Interop.EventUnregister.cs" />
2626
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Advapi32\Interop.EventWriteString.cs" />
2727
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Advapi32\Interop.EventWriteTransfer.cs" />
28+
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs" />
2829
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\Tracing\*.cs" />
2930
<Compile Remove="$(CommonPath)\CoreLib\System\Diagnostics\Tracing\FrameworkEventSource.cs" />
3031
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\Tracing\TraceLogging\*.cs" />

src/Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<AssemblyName>Microsoft.IO.Redist</AssemblyName>
44
<ProjectGuid>{FEF5811F-ED50-4407-A6B9-885EBD3206FB}</ProjectGuid>
55
<Configurations>netfx-Debug;netfx-Release</Configurations>
6-
<DefineConstants>$(DefineConstants);MS_IO_REDIST</DefineConstants>
6+
<DefineConstants>$(DefineConstants);MS_IO_REDIST;INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<NoWarn>$(NoWarn);CS1573</NoWarn>
99
<ClsCompliant>false</ClsCompliant>
@@ -34,6 +34,9 @@
3434
<Compile Include="..\..\System.IO.FileSystem\src\System\IO\ReadLinesIterator.cs" Link="Microsoft\IO\ReadLinesIterator.cs" />
3535
<Compile Include="..\..\System.IO.FileSystem\src\System\IO\SearchOption.cs" Link="Microsoft\IO\SearchOption.cs" />
3636
<Compile Include="..\..\System.IO.FileSystem\src\System\IO\SearchTarget.cs" Link="Microsoft\IO\SearchTarget.cs" />
37+
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs">
38+
<Link>Common\System\Diagnostics\CodeAnalysis\NullableAttributes.cs</Link>
39+
</Compile>
3740
<Compile Include="$(CommonPath)\CoreLib\System\IO\DriveInfoInternal.Windows.cs">
3841
<Link>Common\System\IO\DriveInfoInternal.Windows.cs</Link>
3942
</Compile>

src/System.Data.SqlClient/src/System.Data.SqlClient.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@
1212
<DefineConstants Condition="'$(TargetsNetCoreApp)' != 'true'">$(DefineConstants);netstandard</DefineConstants>
1313
<DefineConstants Condition="'$(TargetsNetCoreApp)' == 'true'">$(DefineConstants);netcoreapp</DefineConstants>
1414
<DefineConstants Condition="'$(TargetGroup)' == 'netcoreapp'">$(DefineConstants);FEATURE_TCPKEEPALIVE</DefineConstants>
15+
<DefineConstants Condition="'$(TargetGroup)' != 'netcoreapp'">$(DefineConstants);INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
1516
<Configurations>net461-Windows_NT-Debug;net461-Windows_NT-Release;netcoreapp-Debug;netcoreapp-Release;netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;netcoreapp2.1-Debug;netcoreapp2.1-Release;netcoreapp2.1-Unix-Debug;netcoreapp2.1-Unix-Release;netcoreapp2.1-Windows_NT-Debug;netcoreapp2.1-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release;netstandard-Debug;netstandard-Release;netstandard-Unix-Debug;netstandard-Unix-Release;netstandard-Windows_NT-Debug;netstandard-Windows_NT-Release;netstandard1.2-Debug;netstandard1.2-Release;netstandard1.3-Debug;netstandard1.3-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release;uap10.0.16299-Windows_NT-Debug;uap10.0.16299-Windows_NT-Release</Configurations>
1617
</PropertyGroup>
18+
<ItemGroup Condition="'$(TargetGroup)' != 'netcoreapp'">
19+
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs">
20+
<Link>Common\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs</Link>
21+
</Compile>
22+
</ItemGroup>
1723
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard' OR '$(TargetsNetCoreApp)' == 'true' OR '$(IsUAPAssembly)' == 'true' ">
1824
<Compile Include="System.Data.SqlClient.TypeForwards.cs" />
1925
</ItemGroup>

src/System.Resources.Extensions/src/System.Resources.Extensions.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
44
<Configurations>netstandard-Debug;netstandard-Release</Configurations>
5-
<DefineConstants>$(DefineConstants);RESOURCES_EXTENSIONS</DefineConstants>
5+
<DefineConstants>$(DefineConstants);RESOURCES_EXTENSIONS;INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
66
</PropertyGroup>
77
<PropertyGroup>
88
<ProjectGuid>{6773D354-A573-4D9C-9A67-C7FAE579DA50}</ProjectGuid>
@@ -14,6 +14,7 @@
1414
<Compile Include="$(CommonPath)\CoreLib\System\Resources\ResourceReader.cs" Link="System\Resources\Extensions\ResourceReader.cs" />
1515
<Compile Include="$(CommonPath)\CoreLib\System\Resources\ResourceTypeCode.cs" Link="System\Resources\ResourceTypeCode.cs" />
1616
<Compile Include="$(CommonPath)\CoreLib\System\Resources\RuntimeResourceSet.cs" Link="System\Resources\Extensions\RuntimeResourceSet.cs" />
17+
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs" Link="System\Diagnostics\CodeAnalysis\NullableAttributes.cs" />
1718
<Compile Include="BinaryReaderExtensions.cs" />
1819
<Compile Include="System\Resources\Extensions\DeserializingResourceReader.cs" />
1920
<Compile Include="System\Resources\Extensions\PreserializedResourceWriter.cs" />
@@ -22,4 +23,4 @@
2223
<ItemGroup>
2324
<Reference Include="System.Memory" />
2425
</ItemGroup>
25-
</Project>
26+
</Project>

src/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<GenFacadesIgnoreMissingTypes Condition="'$(TargetsAOT)'=='true' OR '$(TargetGroup)' == 'uap'">true</GenFacadesIgnoreMissingTypes>
1010
<Configurations>netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release;uapaot-Windows_NT-Debug;uapaot-Windows_NT-Release</Configurations>
1111
<Nullable>enable</Nullable>
12+
<DefineConstants Condition="'$(TargetGroup)' == 'uapaot'">$(DefineConstants);INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants> <!-- TODO-NULLABLE: Remove when uapaot Corelib defines these attributes -->
1213
</PropertyGroup>
1314
<ItemGroup>
1415
<Compile Include="System\AppDomainUnloadedException.cs" />
@@ -179,6 +180,11 @@
179180
<Link>Common\System\IO\PersistedFiles.Unix.cs</Link>
180181
</Compile>
181182
</ItemGroup>
183+
<ItemGroup Condition="'$(TargetGroup)' == 'uapaot'"> <!-- TODO-NULLABLE: Remove when uapaot Corelib defines these attributes -->
184+
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs">
185+
<Link>Common\System\Diagnostics\CodeAnalysis\NullableAttributes.cs</Link>
186+
</Compile>
187+
</ItemGroup>
182188
<ItemGroup>
183189
<ProjectReference Include="..\..\System.Private.Uri\src\System.Private.Uri.csproj" />
184190
<ProjectReference Include="..\..\System.Diagnostics.Debug\src\System.Diagnostics.Debug.csproj" />

0 commit comments

Comments
 (0)