Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Group 1] Enable nullable annotations for Microsoft.Extensions.DependencyModel #57445

Merged
merged 23 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;net461</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Microsoft.Extensions.DependencyModel.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttribute.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Reference Include="System.Linq" />
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
<Reference Include="System.Collections" />
<Reference Include="System.Memory" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace System.Collections.Generic
{
public static class CollectionExtensions
{
public static RuntimeAssetGroup GetDefaultGroup(this IEnumerable<RuntimeAssetGroup> self) => GetGroup(self, string.Empty);
public static RuntimeAssetGroup? GetDefaultGroup(this IEnumerable<RuntimeAssetGroup> self) => GetGroup(self, string.Empty);

public static RuntimeAssetGroup GetRuntimeGroup(this IEnumerable<RuntimeAssetGroup> self, string runtime)
public static RuntimeAssetGroup? GetRuntimeGroup(this IEnumerable<RuntimeAssetGroup> self, string runtime)
{
if (string.IsNullOrEmpty(runtime))
{
Expand All @@ -19,7 +19,7 @@ public static RuntimeAssetGroup GetRuntimeGroup(this IEnumerable<RuntimeAssetGro
return GetGroup(self, runtime);
}

private static RuntimeAssetGroup GetGroup(IEnumerable<RuntimeAssetGroup> groups, string runtime)
private static RuntimeAssetGroup? GetGroup(IEnumerable<RuntimeAssetGroup> groups, string runtime)
{
return groups.FirstOrDefault(g => g.Runtime == runtime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CompilationLibrary : Library
public CompilationLibrary(string type,
string name,
string version,
string hash,
string? hash,
IEnumerable<string> assemblies,
IEnumerable<Dependency> dependencies,
bool serviceable)
Expand All @@ -24,12 +24,12 @@ public CompilationLibrary(string type,
public CompilationLibrary(string type,
string name,
string version,
string hash,
string? hash,
IEnumerable<string> assemblies,
IEnumerable<Dependency> dependencies,
bool serviceable,
string path,
string hashPath)
string? path,
string? hashPath)
: base(type, name, version, hash, dependencies, serviceable, path, hashPath)
{
if (assemblies == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ namespace Microsoft.Extensions.DependencyModel
{
public class CompilationOptions
{
public IReadOnlyList<string> Defines { get; }
public IReadOnlyList<string?> Defines { get; }

public string LanguageVersion { get; }
public string? LanguageVersion { get; }

public string Platform { get; }
public string? Platform { get; }

public bool? AllowUnsafe { get; }

public bool? WarningsAsErrors { get; }

public bool? Optimize { get; }

public string KeyFile { get; }
public string? KeyFile { get; }

public bool? DelaySign { get; }

public bool? PublicSign { get; }

public string DebugType { get; }
public string? DebugType { get; }

public bool? EmitEntryPoint { get; }

public bool? GenerateXmlDocumentation { get; }

public static CompilationOptions Default { get; } = new CompilationOptions(
defines: Enumerable.Empty<string>(),
defines: Enumerable.Empty<string?>(),
languageVersion: null,
platform: null,
allowUnsafe: null,
Expand All @@ -47,16 +47,16 @@ public class CompilationOptions
emitEntryPoint: null,
generateXmlDocumentation: null);

public CompilationOptions(IEnumerable<string> defines,
string languageVersion,
string platform,
public CompilationOptions(IEnumerable<string?> defines,
string? languageVersion,
string? platform,
bool? allowUnsafe,
bool? warningsAsErrors,
bool? optimize,
string keyFile,
string? keyFile,
bool? delaySign,
bool? publicSign,
string debugType,
string? debugType,
bool? emitEntryPoint,
bool? generateXmlDocumentation)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics.Hashing;

namespace Microsoft.Extensions.DependencyModel
Expand Down Expand Up @@ -30,7 +31,7 @@ public bool Equals(Dependency other)
return string.Equals(Name, other.Name) && string.Equals(Version, other.Version);
}

public override bool Equals(object obj) => obj is Dependency dependency && Equals(dependency);
public override bool Equals([NotNullWhen(true)] object? obj) => obj is Dependency dependency && Equals(dependency);

public override int GetHashCode() =>
HashHelpers.Combine(Name.GetHashCode(), Version.GetHashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DependencyContext

[UnconditionalSuppressMessage("SingleFile", "IL3002:Avoid calling members marked with 'RequiresAssemblyFilesAttribute' when publishing as a single-file",
Justification = "The annotation should be on the static constructor but is Compiler Generated, annotating the caller Default method instead")]
private static readonly Lazy<DependencyContext> _defaultContext = new Lazy<DependencyContext>(LoadDefault);
private static readonly Lazy<DependencyContext?> _defaultContext = new(LoadDefault);

public DependencyContext(TargetInfo target,
CompilationOptions compilationOptions,
Expand Down Expand Up @@ -51,7 +51,7 @@ public DependencyContext(TargetInfo target,
}

[RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")]
public static DependencyContext Default => _defaultContext.Value;
public static DependencyContext? Default => _defaultContext.Value;

public TargetInfo Target { get; }

Expand Down Expand Up @@ -80,7 +80,7 @@ public DependencyContext Merge(DependencyContext other)
}

[RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")]
private static DependencyContext LoadDefault()
private static DependencyContext? LoadDefault()
{
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly == null)
Expand All @@ -92,16 +92,16 @@ private static DependencyContext LoadDefault()
}

[RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")]
public static DependencyContext Load(Assembly assembly)
public static DependencyContext? Load(Assembly assembly)
{
return DependencyContextLoader.Default.Load(assembly);
}

private sealed class LibraryMergeEqualityComparer<T> : IEqualityComparer<T> where T : Library
{
public bool Equals(T x, T y)
public bool Equals(T? x, T? y)
{
return StringComparer.OrdinalIgnoreCase.Equals(x.Name, y.Name);
return StringComparer.OrdinalIgnoreCase.Equals(x?.Name, y?.Name);
}

public int GetHashCode(T obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ private static IEnumerable<string> ResolveAssets(
string runtimeIdentifier,
IEnumerable<RuntimeAssetGroup> assets)
{
RuntimeFallbacks fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier);
IEnumerable<string> rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty<string>());
RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier);
IEnumerable<string?> rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty<string?>());
return SelectAssets(rids, assets);
}

Expand All @@ -192,16 +192,16 @@ private static IEnumerable<RuntimeFile> ResolveRuntimeFiles(
string runtimeIdentifier,
IEnumerable<RuntimeAssetGroup> assets)
{
RuntimeFallbacks fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier);
IEnumerable<string> rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty<string>());
RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier);
IEnumerable<string?> rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty<string?>());
return SelectRuntimeFiles(rids, assets);
}

private static IEnumerable<string> SelectAssets(IEnumerable<string> rids, IEnumerable<RuntimeAssetGroup> groups)
private static IEnumerable<string> SelectAssets(IEnumerable<string?> rids, IEnumerable<RuntimeAssetGroup> groups)
{
foreach (string rid in rids)
foreach (string? rid in rids)
{
RuntimeAssetGroup group = groups.FirstOrDefault(g => g.Runtime == rid);
RuntimeAssetGroup? group = groups.FirstOrDefault(g => g.Runtime == rid);
if (group != null)
{
return group.AssetPaths;
Expand All @@ -212,11 +212,11 @@ private static IEnumerable<string> SelectAssets(IEnumerable<string> rids, IEnume
return groups.GetDefaultAssets();
}

private static IEnumerable<RuntimeFile> SelectRuntimeFiles(IEnumerable<string> rids, IEnumerable<RuntimeAssetGroup> groups)
private static IEnumerable<RuntimeFile> SelectRuntimeFiles(IEnumerable<string?> rids, IEnumerable<RuntimeAssetGroup> groups)
{
foreach (string rid in rids)
foreach (string? rid in rids)
{
RuntimeAssetGroup group = groups.FirstOrDefault(g => g.Runtime == rid);
RuntimeAssetGroup? group = groups.FirstOrDefault(g => g.Runtime == rid);
if (group != null)
{
return group.RuntimeFiles;
Expand Down
Loading