diff --git a/src/core/Microsoft.Scripting/ErrorSink.cs b/src/core/Microsoft.Scripting/ErrorSink.cs
index 78a71215..71c442b7 100644
--- a/src/core/Microsoft.Scripting/ErrorSink.cs
+++ b/src/core/Microsoft.Scripting/ErrorSink.cs
@@ -2,20 +2,26 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
+#nullable enable
+
using System.Threading;
using Microsoft.Scripting.Utils;
namespace Microsoft.Scripting {
public class ErrorSink {
- public static readonly ErrorSink/*!*/ Default = new();
+ public static readonly ErrorSink Default = new();
- public static readonly ErrorSink/*!*/ Null = new NullErrorSink();
+ public static readonly ErrorSink Null = new NullErrorSink();
protected ErrorSink() {
}
- public virtual void Add(SourceUnit source, string/*!*/ message, SourceSpan span, int errorCode, Severity severity) {
+ ///
+ /// Reports an error against a source unit. Use the
+ /// overload when no is available (e.g. remote execution).
+ ///
+ public virtual void Add(SourceUnit source, string message, SourceSpan span, int errorCode, Severity severity) {
if (severity == Severity.FatalError || severity == Severity.Error) {
throw new SyntaxErrorException(message, source, span, errorCode, severity);
}
@@ -25,7 +31,7 @@ public virtual void Add(SourceUnit source, string/*!*/ message, SourceSpan span,
/// This overload will be called when a SourceUnit is not available. This can happen if the code is being executed remotely,
/// since SourceUnit cannot be marshaled across AppDomains.
///
- public virtual void Add(string message, string path, string code, string line, SourceSpan span, int errorCode, Severity severity) {
+ public virtual void Add(string message, string? path, string? code, string? line, SourceSpan span, int errorCode, Severity severity) {
if (severity == Severity.FatalError || severity == Severity.Error) {
throw new SyntaxErrorException(message, path, code, line, span, errorCode, severity);
}
@@ -34,12 +40,12 @@ public virtual void Add(string message, string path, string code, string line, S
internal sealed class NullErrorSink : ErrorSink {
- public override void Add(SourceUnit source, string/*!*/ message, SourceSpan span, int errorCode, Severity severity) {
+ public override void Add(SourceUnit source, string message, SourceSpan span, int errorCode, Severity severity) {
}
}
public class ErrorCounter : ErrorSink {
- private readonly ErrorSink/*!*/ _sink;
+ private readonly ErrorSink _sink;
private int _fatalErrorCount;
private int _errorCount;
@@ -63,15 +69,15 @@ public bool AnyError {
}
}
- public ErrorCounter()
+ public ErrorCounter()
: this(ErrorSink.Null) {
}
- public ErrorCounter(ErrorSink/*!*/ sink) {
+ public ErrorCounter(ErrorSink sink) {
ContractUtils.RequiresNotNull(sink, nameof(sink));
_sink = sink;
}
-
+
protected virtual void CountError(Severity severity) {
if (severity == Severity.FatalError) Interlocked.Increment(ref _fatalErrorCount);
else if (severity == Severity.Error) Interlocked.Increment(ref _errorCount);
@@ -82,7 +88,7 @@ public void ClearCounters() {
_warningCount = _errorCount = _fatalErrorCount = 0;
}
- public override void Add(SourceUnit source, string/*!*/ message, SourceSpan span, int errorCode, Severity severity) {
+ public override void Add(SourceUnit source, string message, SourceSpan span, int errorCode, Severity severity) {
CountError(severity);
_sink.Add(source, message, span, errorCode, severity);
}
diff --git a/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs b/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs
index 9e185b27..a983adef 100644
--- a/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs
+++ b/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs
@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
+#nullable enable
+
using System;
using System.Collections.Generic;
@@ -46,7 +48,7 @@ public string FormatException(ObjectHandle exception) {
var exceptionObj = exception.Unwrap() as Exception;
ContractUtils.Requires(exceptionObj is not null, nameof(exception), "ObjectHandle must be to Exception object");
- return _context.FormatException(exceptionObj);
+ return _context.FormatException(exceptionObj!);
}
public void GetExceptionMessage(ObjectHandle exception, out string message, out string errorTypeName) {
@@ -54,7 +56,7 @@ public void GetExceptionMessage(ObjectHandle exception, out string message, out
var exceptionObj = exception.Unwrap() as Exception;
ContractUtils.Requires(exceptionObj is not null, nameof(exception), "ObjectHandle must be to Exception object");
- _context.GetExceptionMessage(exceptionObj, out message, out errorTypeName);
+ _context.GetExceptionMessage(exceptionObj!, out message, out errorTypeName);
}
public bool HandleException(ObjectHandle exception) {
@@ -69,12 +71,11 @@ public IList GetStackFrames(ObjectHandle exception) {
var exceptionObj = exception.Unwrap() as Exception;
ContractUtils.Requires(exceptionObj is not null, nameof(exception), "ObjectHandle must be to Exception object");
- return _context.GetStackFrames(exceptionObj);
+ return _context.GetStackFrames(exceptionObj!);
}
- // TODO: Figure out what is the right lifetime
public override object InitializeLifetimeService() {
- return null;
+ return base.InitializeLifetimeService();
}
#endif
}
diff --git a/src/core/Microsoft.Scripting/Microsoft.Scripting.csproj b/src/core/Microsoft.Scripting/Microsoft.Scripting.csproj
index a3b59efc..79cca8e1 100644
--- a/src/core/Microsoft.Scripting/Microsoft.Scripting.csproj
+++ b/src/core/Microsoft.Scripting/Microsoft.Scripting.csproj
@@ -7,6 +7,7 @@
True
T:System.Diagnostics.CodeAnalysis.MaybeNullAttribute;
+ T:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute;
T:System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute;
T:System.Diagnostics.CodeAnalysis.NotNullAttribute;
T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute;
diff --git a/src/core/Microsoft.Scripting/Runtime/DlrConfiguration.cs b/src/core/Microsoft.Scripting/Runtime/DlrConfiguration.cs
index 93c81a83..7bf102c1 100644
--- a/src/core/Microsoft.Scripting/Runtime/DlrConfiguration.cs
+++ b/src/core/Microsoft.Scripting/Runtime/DlrConfiguration.cs
@@ -2,9 +2,12 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
+#nullable enable
+
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading;
@@ -17,9 +20,9 @@ namespace Microsoft.Scripting.Runtime {
///
internal sealed class LanguageConfiguration {
private readonly IDictionary _options;
- private LanguageContext _context;
+ private LanguageContext? _context;
- public LanguageContext LanguageContext => _context;
+ public LanguageContext? LanguageContext => _context;
public AssemblyQualifiedTypeName ProviderName { get; }
@@ -41,32 +44,32 @@ internal LanguageContext LoadLanguageContext(ScriptDomainManager domainManager,
// Let assembly load errors bubble out
var assembly = domainManager.Platform.LoadAssembly(ProviderName.AssemblyName.FullName);
- Type type = assembly.GetType(ProviderName.TypeName);
+ Type? type = assembly.GetType(ProviderName.TypeName);
if (type is null) {
throw new InvalidOperationException(
String.Format(
"Failed to load language '{0}': assembly '{1}' does not contain type '{2}'",
- DisplayName,
+ DisplayName,
#if FEATURE_FILESYSTEM
assembly.Location,
#else
assembly.FullName,
#endif
- ProviderName.TypeName
+ ProviderName.TypeName
));
}
if (!type.IsSubclassOf(typeof(LanguageContext))) {
throw new InvalidOperationException(
- $"Failed to load language '{DisplayName}': type '{type}' is not a valid language provider because it does not inherit from LanguageContext");
+ $"Failed to load language '{DisplayName}': type '{type}' is not a valid language provider because it does not inherit from LanguageContext");
}
LanguageContext context;
try {
- context = (LanguageContext)Activator.CreateInstance(type, new object[] { domainManager, _options });
+ context = (LanguageContext)Activator.CreateInstance(type, new object[] { domainManager, _options })!;
} catch (TargetInvocationException e) {
throw new TargetInvocationException(
- $"Failed to load language '{DisplayName}': {e.InnerException.Message}",
+ $"Failed to load language '{DisplayName}': {e.InnerException?.Message}",
e.InnerException
);
} catch (Exception e) {
@@ -131,8 +134,8 @@ public void AddLanguage(string languageTypeName, string displayName, IList names, IList fileExtensions,
- IDictionary options, string paramName) {
+ internal void AddLanguage(string languageTypeName, string displayName, IList names, IList fileExtensions,
+ IDictionary options, string? paramName) {
ContractUtils.Requires(!_frozen, "Configuration cannot be modified once the runtime is initialized");
ContractUtils.Requires(
names.All(id => !String.IsNullOrEmpty(id) && !_languageNames.ContainsKey(id)),
@@ -187,10 +190,10 @@ internal void Freeze() {
_frozen = true;
}
- internal bool TryLoadLanguage(ScriptDomainManager manager, in AssemblyQualifiedTypeName providerName, out LanguageContext language) {
+ internal bool TryLoadLanguage(ScriptDomainManager manager, in AssemblyQualifiedTypeName providerName, [NotNullWhen(true)] out LanguageContext? language) {
Assert.NotNull(manager);
- if (_languageConfigurations.TryGetValue(providerName, out LanguageConfiguration config)) {
+ if (_languageConfigurations.TryGetValue(providerName, out LanguageConfiguration? config)) {
language = LoadLanguageContext(manager, config);
return true;
}
@@ -199,12 +202,12 @@ internal bool TryLoadLanguage(ScriptDomainManager manager, in AssemblyQualifiedT
return false;
}
- internal bool TryLoadLanguage(ScriptDomainManager manager, string str, bool isExtension, out LanguageContext language) {
+ internal bool TryLoadLanguage(ScriptDomainManager manager, string str, bool isExtension, [NotNullWhen(true)] out LanguageContext? language) {
Assert.NotNull(manager, str);
var dict = isExtension ? _languageExtensions : _languageNames;
- if (dict.TryGetValue(str, out LanguageConfiguration config)) {
+ if (dict.TryGetValue(str, out LanguageConfiguration? config)) {
language = LoadLanguageContext(manager, config);
return true;
}
@@ -222,7 +225,7 @@ private LanguageContext LoadLanguageContext(ScriptDomainManager manager, Languag
// The check takes place after config.LoadLanguageContext is called to avoid calling user code while holding a lock.
lock (_loadedProviderTypes) {
Type type = language.GetType();
- if (_loadedProviderTypes.TryGetValue(type, out LanguageConfiguration existingConfig)) {
+ if (_loadedProviderTypes.TryGetValue(type, out LanguageConfiguration? existingConfig)) {
throw new InvalidOperationException(
$"Language implemented by type '{config.ProviderName}' has already been loaded using name '{existingConfig.ProviderName}'");
}
@@ -289,7 +292,7 @@ public string[] GetFileExtensions() {
return ArrayUtils.MakeArray(_languageExtensions.Keys);
}
- internal LanguageConfiguration GetLanguageConfig(LanguageContext context) {
+ internal LanguageConfiguration? GetLanguageConfig(LanguageContext context) {
foreach (var config in _languageConfigurations.Values) {
if (config.LanguageContext == context) {
return config;
diff --git a/src/core/Microsoft.Scripting/Runtime/DynamicOperations.Generated.cs b/src/core/Microsoft.Scripting/Runtime/DynamicOperations.Generated.cs
index 499f23e6..dfc279a5 100644
--- a/src/core/Microsoft.Scripting/Runtime/DynamicOperations.Generated.cs
+++ b/src/core/Microsoft.Scripting/Runtime/DynamicOperations.Generated.cs
@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
+#nullable enable
+
//
// To regenerate code in this file run:
// ir Languages/Ruby/Scripts/CodeGenerator.rb DynamicOperations.Generated.cs
@@ -17,8 +19,8 @@ namespace Microsoft.Scripting.Runtime {
public sealed partial class DynamicOperations {
private const int /*$$*/PregeneratedInvokerCount = 14;
- private Func GetInvoker(int paramCount) {
- Func invoker;
+ private Func GetInvoker(int paramCount) {
+ Func? invoker;
lock (_invokers) {
if (!_invokers.TryGetValue(paramCount, out invoker)) {
_invokers[paramCount] = invoker = GetPregeneratedInvoker(paramCount) ?? EmitInvoker(paramCount);
@@ -28,7 +30,7 @@ private Func GetInv
}
[MethodImpl(MethodImplOptions.NoInlining)]
- private Func EmitInvoker(int paramCount) {
+ private Func EmitInvoker(int paramCount) {
#if !FEATURE_REFEMIT
throw new NotSupportedException();
#else
@@ -46,7 +48,7 @@ private Func EmitIn
}
var getOrCreateSiteFunc = new Func>>(GetOrCreateSite>).GetMethodInfo().GetGenericMethodDefinition();
- return Expression.Lambda>(
+ return Expression.Lambda>(
Expression.Block(
new[] { site },
Expression.Assign(
@@ -56,7 +58,7 @@ private Func EmitIn
Expression.Invoke(
Expression.Field(
site,
- site.Type.GetField("Target")
+ site.Type.GetField("Target")!
),
siteArgs
)
@@ -66,89 +68,89 @@ private Func EmitIn
#endif
}
- private static Func GetPregeneratedInvoker(int paramCount) {
+ private static Func? GetPregeneratedInvoker(int paramCount) {
switch (paramCount) {
#if GENERATOR
def generate; $PregeneratedInvokerCount.times { |n| @n = n + 1; super }; end
def n; @n; end
- def objects; "object, " * @n; end
+ def objects; "object?, " * @n; end
def args; (0..@n-1).map { |i| ", args[#{i}]" }.join; end
#else
case /*$n{*/0/*}*/:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target/*$args*/);
};
#endif
#region Generated
case 1:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0]);
};
case 2:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1]);
};
case 3:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2]);
};
case 4:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3]);
};
case 5:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4]);
};
case 6:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5]);
};
case 7:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
};
case 8:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
};
case 9:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
};
case 10:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
};
case 11:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
};
case 12:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]);
};
case 13:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12]);
};
case 14:
return (ops, binder, target, args) => {
- var site = ops.GetOrCreateSite>(binder);
+ var site = ops.GetOrCreateSite>(binder);
return site.Target(site, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13]);
};
#endregion
diff --git a/src/core/Microsoft.Scripting/Runtime/DynamicOperations.cs b/src/core/Microsoft.Scripting/Runtime/DynamicOperations.cs
index 82abc9a3..cc4f119e 100644
--- a/src/core/Microsoft.Scripting/Runtime/DynamicOperations.cs
+++ b/src/core/Microsoft.Scripting/Runtime/DynamicOperations.cs
@@ -2,9 +2,12 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
+#nullable enable
+
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
@@ -54,43 +57,43 @@ public DynamicOperations(LanguageContext lc) {
#region Basic Operations
- private readonly Dictionary> _invokers = new();
+ private readonly Dictionary> _invokers = new();
///
/// Calls the provided object with the given parameters and returns the result.
- ///
- /// The prefered way of calling objects is to convert the object to a strongly typed delegate
+ ///
+ /// The prefered way of calling objects is to convert the object to a strongly typed delegate
/// using the ConvertTo methods and then invoking that delegate.
///
- public object Invoke(object obj, params object[] parameters) {
+ public object? Invoke(object? obj, params object?[] parameters) {
return GetInvoker(parameters.Length)(this, _lc.CreateInvokeBinder(new CallInfo(parameters.Length)), obj, parameters);
}
-
+
///
/// Invokes a member on the provided object with the given parameters and returns the result.
///
- public object InvokeMember(object obj, string memberName, params object[] parameters) {
+ public object? InvokeMember(object? obj, string memberName, params object?[] parameters) {
return InvokeMember(obj, memberName, false, parameters);
}
///
/// Invokes a member on the provided object with the given parameters and returns the result.
///
- public object InvokeMember(object obj, string memberName, bool ignoreCase, params object[] parameters) {
+ public object? InvokeMember(object? obj, string memberName, bool ignoreCase, params object?[] parameters) {
return GetInvoker(parameters.Length)(this, _lc.CreateCallBinder(memberName, ignoreCase, new CallInfo(parameters.Length)), obj, parameters);
}
///
/// Creates a new instance from the provided object using the given parameters, and returns the result.
///
- public object CreateInstance(object obj, params object[] parameters) {
+ public object? CreateInstance(object? obj, params object?[] parameters) {
return GetInvoker(parameters.Length)(this, _lc.CreateCreateBinder(new CallInfo(parameters.Length)), obj, parameters);
}
///
/// Gets the member name from the object obj. Throws an exception if the member does not exist or is write-only.
///
- public object GetMember(object obj, string name) {
+ public object? GetMember(object? obj, string name) {
return GetMember(obj, name, false);
}
@@ -98,36 +101,37 @@ public object GetMember(object obj, string name) {
/// Gets the member name from the object obj and converts it to the type T. Throws an exception if the
/// member does not exist, is write-only, or cannot be converted.
///
- public T GetMember(object obj, string name) {
+ [return: MaybeNull]
+ public T GetMember(object? obj, string name) {
return GetMember(obj, name, false);
}
///
- /// Gets the member name from the object obj. Returns true if the member is successfully retrieved and
+ /// Gets the member name from the object obj. Returns true if the member is successfully retrieved and
/// stores the value in the value out param.
///
- public bool TryGetMember(object obj, string name, out object value) {
+ public bool TryGetMember(object? obj, string name, out object? value) {
return TryGetMember(obj, name, false, out value);
}
///
/// Returns true if the object has a member named name, false if the member does not exist.
///
- public bool ContainsMember(object obj, string name) {
+ public bool ContainsMember(object? obj, string name) {
return ContainsMember(obj, name, false);
}
///
/// Removes the member name from the object obj.
///
- public void RemoveMember(object obj, string name) {
+ public void RemoveMember(object? obj, string name) {
RemoveMember(obj, name, false);
}
///
/// Sets the member name on object obj to value.
///
- public void SetMember(object obj, string name, object value) {
+ public void SetMember(object? obj, string name, object? value) {
SetMember(obj, name, value, false);
}
@@ -135,15 +139,15 @@ public void SetMember(object obj, string name, object value) {
/// Sets the member name on object obj to value. This overload can be used to avoid
/// boxing and casting of strongly typed members.
///
- public void SetMember(object obj, string name, T value) {
+ public void SetMember(object? obj, string name, T value) {
SetMember(obj, name, value, false);
}
///
/// Gets the member name from the object obj. Throws an exception if the member does not exist or is write-only.
///
- public object GetMember(object obj, string name, bool ignoreCase) {
- CallSite> site = GetOrCreateSite