Skip to content

Commit b4426e9

Browse files
committed
Add compatibility nullable attributes
Nullaibility attributes are not defined in .NET Standard 2.0, so we add them here to make the compiler aware of them.
1 parent 69c0153 commit b4426e9

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

YarnSpinner/NullableAttributes.cs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
// https://github.com/dotnet/runtime/blob/527f9ae88a0ee216b44d556f9bdc84037fe0ebda/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
2+
3+
#pragma warning disable
4+
#define INTERNAL_NULLABLE_ATTRIBUTES
5+
6+
// Licensed to the .NET Foundation under one or more agreements.
7+
// The .NET Foundation licenses this file to you under the MIT license.
8+
9+
namespace System.Diagnostics.CodeAnalysis
10+
{
11+
#if NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48
12+
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
13+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
14+
#if SYSTEM_PRIVATE_CORELIB
15+
public
16+
#else
17+
internal
18+
#endif
19+
sealed class AllowNullAttribute : Attribute
20+
{ }
21+
22+
/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
23+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
24+
#if SYSTEM_PRIVATE_CORELIB
25+
public
26+
#else
27+
internal
28+
#endif
29+
sealed class DisallowNullAttribute : Attribute
30+
{ }
31+
32+
/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
33+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
34+
#if SYSTEM_PRIVATE_CORELIB
35+
public
36+
#else
37+
internal
38+
#endif
39+
sealed class MaybeNullAttribute : Attribute
40+
{ }
41+
42+
/// <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>
43+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
44+
#if SYSTEM_PRIVATE_CORELIB
45+
public
46+
#else
47+
internal
48+
#endif
49+
sealed class NotNullAttribute : Attribute
50+
{ }
51+
52+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
53+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
54+
#if SYSTEM_PRIVATE_CORELIB
55+
public
56+
#else
57+
internal
58+
#endif
59+
sealed class MaybeNullWhenAttribute : Attribute
60+
{
61+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
62+
/// <param name="returnValue">
63+
/// The return value condition. If the method returns this value, the associated parameter may be null.
64+
/// </param>
65+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
66+
67+
/// <summary>Gets the return value condition.</summary>
68+
public bool ReturnValue { get; }
69+
}
70+
71+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
72+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
73+
#if SYSTEM_PRIVATE_CORELIB
74+
public
75+
#else
76+
internal
77+
#endif
78+
sealed class NotNullWhenAttribute : Attribute
79+
{
80+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
81+
/// <param name="returnValue">
82+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
83+
/// </param>
84+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
85+
86+
/// <summary>Gets the return value condition.</summary>
87+
public bool ReturnValue { get; }
88+
}
89+
90+
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
91+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
92+
#if SYSTEM_PRIVATE_CORELIB
93+
public
94+
#else
95+
internal
96+
#endif
97+
sealed class NotNullIfNotNullAttribute : Attribute
98+
{
99+
/// <summary>Initializes the attribute with the associated parameter name.</summary>
100+
/// <param name="parameterName">
101+
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
102+
/// </param>
103+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
104+
105+
/// <summary>Gets the associated parameter name.</summary>
106+
public string ParameterName { get; }
107+
}
108+
109+
/// <summary>Applied to a method that will never return under any circumstance.</summary>
110+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
111+
#if SYSTEM_PRIVATE_CORELIB
112+
public
113+
#else
114+
internal
115+
#endif
116+
sealed class DoesNotReturnAttribute : Attribute
117+
{ }
118+
119+
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
120+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
121+
#if SYSTEM_PRIVATE_CORELIB
122+
public
123+
#else
124+
internal
125+
#endif
126+
sealed class DoesNotReturnIfAttribute : Attribute
127+
{
128+
/// <summary>Initializes the attribute with the specified parameter value.</summary>
129+
/// <param name="parameterValue">
130+
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
131+
/// the associated parameter matches this value.
132+
/// </param>
133+
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
134+
135+
/// <summary>Gets the condition parameter value.</summary>
136+
public bool ParameterValue { get; }
137+
}
138+
#endif
139+
140+
#if NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 || NETCOREAPP3_1 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48
141+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
142+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
143+
#if SYSTEM_PRIVATE_CORELIB
144+
public
145+
#else
146+
internal
147+
#endif
148+
sealed class MemberNotNullAttribute : Attribute
149+
{
150+
/// <summary>Initializes the attribute with a field or property member.</summary>
151+
/// <param name="member">
152+
/// The field or property member that is promised to be not-null.
153+
/// </param>
154+
public MemberNotNullAttribute(string member) => Members = new[] { member };
155+
156+
/// <summary>Initializes the attribute with the list of field and property members.</summary>
157+
/// <param name="members">
158+
/// The list of field and property members that are promised to be not-null.
159+
/// </param>
160+
public MemberNotNullAttribute(params string[] members) => Members = members;
161+
162+
/// <summary>Gets field or property member names.</summary>
163+
public string[] Members { get; }
164+
}
165+
166+
/// <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>
167+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
168+
#if SYSTEM_PRIVATE_CORELIB
169+
public
170+
#else
171+
internal
172+
#endif
173+
sealed class MemberNotNullWhenAttribute : Attribute
174+
{
175+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
176+
/// <param name="returnValue">
177+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
178+
/// </param>
179+
/// <param name="member">
180+
/// The field or property member that is promised to be not-null.
181+
/// </param>
182+
public MemberNotNullWhenAttribute(bool returnValue, string member)
183+
{
184+
ReturnValue = returnValue;
185+
Members = new[] { member };
186+
}
187+
188+
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
189+
/// <param name="returnValue">
190+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
191+
/// </param>
192+
/// <param name="members">
193+
/// The list of field and property members that are promised to be not-null.
194+
/// </param>
195+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
196+
{
197+
ReturnValue = returnValue;
198+
Members = members;
199+
}
200+
201+
/// <summary>Gets the return value condition.</summary>
202+
public bool ReturnValue { get; }
203+
204+
/// <summary>Gets field or property member names.</summary>
205+
public string[] Members { get; }
206+
}
207+
#endif
208+
}

0 commit comments

Comments
 (0)