Skip to content

Commit ff61e59

Browse files
authored
Add nullable annotations (#1962)
* Add nullable annotations * Fix generate
1 parent 4d07fc1 commit ff61e59

File tree

90 files changed

+492
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+492
-568
lines changed

eng/scripts/generate_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def get_clr_name(e):
265265
return e.replace('Error', '') + 'Exception'
266266

267267
FACTORY = """
268-
public static Exception %(name)s(string format, params object[] args) {
268+
public static Exception %(name)s(string format, params object?[] args) {
269269
return new %(clrname)s(string.Format(format, args));
270270
}"""
271271

eng/scripts/generate_typecache.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,15 @@ def gen_typecache_storage(cw):
7474
types[x.typeType] = [x]
7575
for type in types:
7676
for a_type in types[type]:
77-
cw.write('private static %s %s;' % (type, a_type.name))
77+
cw.write('private static %s? %s;' % (type, a_type.name))
7878

7979
# outputs the public getters for each cached type
8080
def gen_typecache(cw):
8181
for x in data:
82-
cw.enter_block("public static %s %s" % (x.typeType, x.entryName))
83-
cw.enter_block("get")
84-
85-
if x.typeType != 'PythonType': cast = '(%s)' % x.typeType
86-
else: cast = ""
87-
88-
cw.write("if (%s == null) %s = %sDynamicHelpers.GetPythonTypeFromType(typeof(%s));" % (x.name, x.name, cast, x.type))
89-
cw.write("return %s;" % x.name)
90-
cw.exit_block()
91-
cw.exit_block()
82+
cw.write("public static %s %s" % (x.typeType, x.entryName))
83+
cw.indent()
84+
cw.write("=> %s ??= DynamicHelpers.GetPythonTypeFromType(typeof(%s));" % (x.name, x.type))
85+
cw.dedent()
9286
cw.write("")
9387

9488
def main():

src/core/IronPython/Runtime/Binding/BindingWarnings.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
8+
using System.Diagnostics.CodeAnalysis;
69
using System.Linq.Expressions;
7-
using System.Reflection;
810
using System.Threading;
911

1012
using IronPython.Runtime.Exceptions;
@@ -20,7 +22,7 @@ namespace IronPython.Runtime.Binding {
2022
/// Provides support for emitting warnings when built in methods are invoked at runtime.
2123
/// </summary>
2224
internal static class BindingWarnings {
23-
public static bool ShouldWarn(PythonContext/*!*/ context, OverloadInfo/*!*/ method, out WarningInfo info) {
25+
public static bool ShouldWarn(PythonContext/*!*/ context, OverloadInfo/*!*/ method, [NotNullWhen(true)] out WarningInfo? info) {
2426
Assert.NotNull(method);
2527

2628
ObsoleteAttribute[] os = (ObsoleteAttribute[])method.ReflectionInfo.GetCustomAttributes(typeof(ObsoleteAttribute), true);
@@ -39,25 +41,23 @@ public static bool ShouldWarn(PythonContext/*!*/ context, OverloadInfo/*!*/ meth
3941

4042
#if FEATURE_APARTMENTSTATE
4143
// no apartment states on Silverlight
42-
if (method.DeclaringType == typeof(Thread)) {
43-
if (method.Name == "Sleep") {
44-
info = new WarningInfo(
45-
PythonExceptions.RuntimeWarning,
46-
"Calling Thread.Sleep on an STA thread doesn't pump messages. Use Thread.CurrentThread.Join instead.",
47-
Expression.Equal(
48-
Expression.Call(
49-
Expression.Property(
50-
null,
51-
typeof(Thread).GetProperty("CurrentThread")
52-
),
53-
typeof(Thread).GetMethod("GetApartmentState")
44+
if (method.DeclaringType == typeof(Thread) && method.Name == nameof(Thread.Sleep)) {
45+
info = new WarningInfo(
46+
PythonExceptions.RuntimeWarning,
47+
"Calling Thread.Sleep on an STA thread doesn't pump messages. Use Thread.CurrentThread.Join instead.",
48+
Expression.Equal(
49+
Expression.Call(
50+
Expression.Property(
51+
null,
52+
typeof(Thread).GetProperty(nameof(Thread.CurrentThread))!
5453
),
55-
AstUtils.Constant(ApartmentState.STA)
56-
)
57-
);
54+
typeof(Thread).GetMethod(nameof(Thread.GetApartmentState))!
55+
),
56+
AstUtils.Constant(ApartmentState.STA)
57+
)
58+
);
5859

59-
return true;
60-
}
60+
return true;
6161
}
6262
#endif
6363

src/core/IronPython/Runtime/Binding/CompatibilityInvokeBinder.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Linq.Expressions;
5+
#nullable enable
66

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Diagnostics;
107
using System.Dynamic;
118

12-
using Microsoft.Scripting;
139
using Microsoft.Scripting.Actions;
14-
using Microsoft.Scripting.Actions.Calls;
15-
using Microsoft.Scripting.Utils;
16-
17-
using IronPython.Runtime.Operations;
1810

1911
using AstUtils = Microsoft.Scripting.Ast.Utils;
2012

@@ -32,7 +24,7 @@ public CompatibilityInvokeBinder(PythonContext/*!*/ context, CallInfo /*!*/ call
3224
_context = context;
3325
}
3426

35-
public override DynamicMetaObject/*!*/ FallbackInvoke(DynamicMetaObject target, DynamicMetaObject/*!*/[]/*!*/ args, DynamicMetaObject errorSuggestion) {
27+
public override DynamicMetaObject/*!*/ FallbackInvoke(DynamicMetaObject target, DynamicMetaObject/*!*/[]/*!*/ args, DynamicMetaObject? errorSuggestion) {
3628
if (target.Value is IDynamicMetaObjectProvider && errorSuggestion == null) {
3729
// try creating an instance...
3830
return target.BindCreateInstance(
@@ -51,7 +43,7 @@ public CompatibilityInvokeBinder(PythonContext/*!*/ context, CallInfo /*!*/ call
5143
return InvokeFallback(target, args, BindingHelpers.CallInfoToSignature(CallInfo), errorSuggestion);
5244
}
5345

54-
internal DynamicMetaObject/*!*/ InvokeFallback(DynamicMetaObject/*!*/ target, DynamicMetaObject/*!*/[]/*!*/ args, CallSignature sig, DynamicMetaObject errorSuggestion) {
46+
internal DynamicMetaObject/*!*/ InvokeFallback(DynamicMetaObject/*!*/ target, DynamicMetaObject/*!*/[]/*!*/ args, CallSignature sig, DynamicMetaObject? errorSuggestion) {
5547
return
5648
PythonProtocol.Call(this, target, args) ??
5749
Context.Binder.Create(sig, target, args, AstUtils.Constant(_context.SharedContext)) ??
@@ -62,7 +54,7 @@ public override int GetHashCode() {
6254
return base.GetHashCode() ^ _context.Binder.GetHashCode();
6355
}
6456

65-
public override bool Equals(object obj) {
57+
public override bool Equals(object? obj) {
6658
if (!(obj is CompatibilityInvokeBinder ob)) {
6759
return false;
6860
}

src/core/IronPython/Runtime/Binding/ContextArgBuilder.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Linq.Expressions;
5+
#nullable enable
66

7-
using System.Collections.Generic;
8-
using System.Diagnostics;
7+
using System.Linq.Expressions;
98
using System.Reflection;
10-
using System;
11-
using System.Dynamic;
9+
1210
using Microsoft.Scripting.Actions.Calls;
13-
using Microsoft.Scripting.Utils;
1411

1512
namespace IronPython.Runtime.Binding {
1613

src/core/IronPython/Runtime/Binding/CreateFallbackBinder.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Linq.Expressions;
5+
#nullable enable
66

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Diagnostics;
107
using System.Dynamic;
11-
using IronPython.Runtime.Operations;
12-
using Microsoft.Scripting;
13-
using Microsoft.Scripting.Actions;
14-
using Microsoft.Scripting.Utils;
15-
using AstUtils = Microsoft.Scripting.Ast.Utils;
168

179
namespace IronPython.Runtime.Binding {
1810
/// <summary>
@@ -27,7 +19,7 @@ public CreateFallback(CompatibilityInvokeBinder/*!*/ realFallback, CallInfo /*!*
2719
_fallback = realFallback;
2820
}
2921

30-
public override DynamicMetaObject/*!*/ FallbackCreateInstance(DynamicMetaObject/*!*/ target, DynamicMetaObject/*!*/[]/*!*/ args, DynamicMetaObject errorSuggestion) {
22+
public override DynamicMetaObject/*!*/ FallbackCreateInstance(DynamicMetaObject/*!*/ target, DynamicMetaObject/*!*/[]/*!*/ args, DynamicMetaObject? errorSuggestion) {
3123
return _fallback.InvokeFallback(target, args, BindingHelpers.GetCallSignature(this), errorSuggestion);
3224
}
3325

src/core/IronPython/Runtime/Binding/FastBindResult.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Linq.Expressions;
6-
7-
using System;
8-
using System.Dynamic;
9-
using System.Runtime.CompilerServices;
10-
11-
using Microsoft.Scripting.Actions;
5+
#nullable enable
126

137
namespace IronPython.Runtime.Binding {
14-
using Ast = Expression;
15-
168
internal readonly struct FastBindResult<T> where T : class {
179
public readonly T Target;
1810
public readonly bool ShouldCache;

src/core/IronPython/Runtime/Binding/FastGetBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ protected static object Update(CallSite site, object self, CodeContext context)
3939
return ((CallSite<Func<CallSite, object, CodeContext, object>>)site).Update(site, self, context);
4040
}
4141
}
42-
}
42+
}

src/core/IronPython/Runtime/Binding/IComConvertible.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Collections.Generic;
5+
#nullable enable
6+
77
using System.Dynamic;
8-
using System.Text;
98

109
namespace IronPython.Runtime.Binding {
1110
/// <summary>

src/core/IronPython/Runtime/Binding/IFastGettable.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Linq.Expressions;
6-
7-
using System;
8-
using System.Dynamic;
9-
10-
using Microsoft.Scripting.Actions;
5+
#nullable enable
116

127
using System.Runtime.CompilerServices;
138

0 commit comments

Comments
 (0)