Skip to content

Commit 5ace589

Browse files
committed
Add nullable attributes for downlevel platforms.
1 parent a99dd27 commit 5ace589

File tree

2 files changed

+152
-4
lines changed

2 files changed

+152
-4
lines changed

Rx.NET/Source/Directory.build.targets

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
</PropertyGroup>
88

99
<PropertyGroup Condition="'$(TargetFramework)' == 'net472'">
10-
<DefineConstants>$(DefineConstants);HAS_WINFORMS;HAS_WPF;HAS_WINRT;HAS_DISPATCHER;HAS_REMOTING;DESKTOPCLR</DefineConstants>
10+
<DefineConstants>$(DefineConstants);HAS_WINFORMS;HAS_WPF;HAS_WINRT;HAS_DISPATCHER;HAS_REMOTING;DESKTOPCLR;NO_NULLABLE_ATTRIBUTES</DefineConstants>
1111
</PropertyGroup>
1212
<PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0'">
1313
<TargetPlatformVersion>10.0.19041.0</TargetPlatformVersion>
1414
<TargetPlatformMinVersion>10.0.15063.0</TargetPlatformMinVersion>
15-
<DefineConstants>$(DefineConstants);HAS_WINRT;NO_CODE_COVERAGE_ATTRIBUTE;NO_SERIALIZABLE;CRIPPLED_REFLECTION;NO_THREAD;NO_TRACE;WINDOWS</DefineConstants>
15+
<DefineConstants>$(DefineConstants);HAS_WINRT;NO_CODE_COVERAGE_ATTRIBUTE;NO_SERIALIZABLE;CRIPPLED_REFLECTION;NO_THREAD;NO_TRACE;WINDOWS;NO_NULLABLE_ATTRIBUTES</DefineConstants>
1616
</PropertyGroup>
1717
<PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0.16299'">
18-
<DefineConstants>$(DefineConstants);HAS_WINRT;WINDOWS;HAS_OS_XAML;LEGACY_WINRT</DefineConstants>
18+
<DefineConstants>$(DefineConstants);HAS_WINRT;WINDOWS;HAS_OS_XAML;LEGACY_WINRT;NO_NULLABLE_ATTRIBUTES</DefineConstants>
1919
<TargetPlatformVersion>10.0.19041.0</TargetPlatformVersion>
2020
</PropertyGroup>
2121
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.1'">
22-
<DefineConstants>$(DefineConstants);HAS_WINRT</DefineConstants>
22+
<DefineConstants>$(DefineConstants);HAS_WINRT;NO_NULLABLE_ATTRIBUTES</DefineConstants>
2323
</PropertyGroup>
2424
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
2525
<DefineConstants>$(DefineConstants);HAS_WINRT;HAS_WINFORMS;HAS_WPF;HAS_DISPATCHER;DESKTOPCLR</DefineConstants>
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#if NO_NULLABLE_ATTRIBUTES
5+
6+
namespace System.Diagnostics.CodeAnalysis
7+
{
8+
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
9+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
10+
internal sealed class AllowNullAttribute : Attribute
11+
{ }
12+
13+
/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
14+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
15+
internal sealed class DisallowNullAttribute : Attribute
16+
{ }
17+
18+
/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
19+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
20+
internal sealed class MaybeNullAttribute : Attribute
21+
{ }
22+
23+
/// <summary>Specifies that an output will not be null even if the corresponding type allows it. Specifies that an input argument was not null when the call returns.</summary>
24+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
25+
internal sealed class NotNullAttribute : Attribute
26+
{ }
27+
28+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
29+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
30+
internal sealed class MaybeNullWhenAttribute : Attribute
31+
{
32+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
33+
/// <param name="returnValue">
34+
/// The return value condition. If the method returns this value, the associated parameter may be null.
35+
/// </param>
36+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
37+
38+
/// <summary>Gets the return value condition.</summary>
39+
public bool ReturnValue { get; }
40+
}
41+
42+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
43+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
44+
internal sealed class NotNullWhenAttribute : Attribute
45+
{
46+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
47+
/// <param name="returnValue">
48+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
49+
/// </param>
50+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
51+
52+
/// <summary>Gets the return value condition.</summary>
53+
public bool ReturnValue { get; }
54+
}
55+
56+
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
57+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
58+
internal sealed class NotNullIfNotNullAttribute : Attribute
59+
{
60+
/// <summary>Initializes the attribute with the associated parameter name.</summary>
61+
/// <param name="parameterName">
62+
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
63+
/// </param>
64+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
65+
66+
/// <summary>Gets the associated parameter name.</summary>
67+
public string ParameterName { get; }
68+
}
69+
70+
/// <summary>Applied to a method that will never return under any circumstance.</summary>
71+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
72+
internal sealed class DoesNotReturnAttribute : Attribute
73+
{ }
74+
75+
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
76+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
77+
internal sealed class DoesNotReturnIfAttribute : Attribute
78+
{
79+
/// <summary>Initializes the attribute with the specified parameter value.</summary>
80+
/// <param name="parameterValue">
81+
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
82+
/// the associated parameter matches this value.
83+
/// </param>
84+
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
85+
86+
/// <summary>Gets the condition parameter value.</summary>
87+
public bool ParameterValue { get; }
88+
}
89+
90+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
91+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
92+
internal sealed class MemberNotNullAttribute : Attribute
93+
{
94+
/// <summary>Initializes the attribute with a field or property member.</summary>
95+
/// <param name="member">
96+
/// The field or property member that is promised to be not-null.
97+
/// </param>
98+
public MemberNotNullAttribute(string member) => Members = new[] { member };
99+
100+
/// <summary>Initializes the attribute with the list of field and property members.</summary>
101+
/// <param name="members">
102+
/// The list of field and property members that are promised to be not-null.
103+
/// </param>
104+
public MemberNotNullAttribute(params string[] members) => Members = members;
105+
106+
/// <summary>Gets field or property member names.</summary>
107+
public string[] Members { get; }
108+
}
109+
110+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.</summary>
111+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
112+
internal sealed class MemberNotNullWhenAttribute : Attribute
113+
{
114+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
115+
/// <param name="returnValue">
116+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
117+
/// </param>
118+
/// <param name="member">
119+
/// The field or property member that is promised to be not-null.
120+
/// </param>
121+
public MemberNotNullWhenAttribute(bool returnValue, string member)
122+
{
123+
ReturnValue = returnValue;
124+
Members = new[] { member };
125+
}
126+
127+
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
128+
/// <param name="returnValue">
129+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
130+
/// </param>
131+
/// <param name="members">
132+
/// The list of field and property members that are promised to be not-null.
133+
/// </param>
134+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
135+
{
136+
ReturnValue = returnValue;
137+
Members = members;
138+
}
139+
140+
/// <summary>Gets the return value condition.</summary>
141+
public bool ReturnValue { get; }
142+
143+
/// <summary>Gets field or property member names.</summary>
144+
public string[] Members { get; }
145+
}
146+
}
147+
148+
#endif

0 commit comments

Comments
 (0)