Skip to content

Commit

Permalink
[Group 1] Enable nullable annotations for `Microsoft.Extensions.Depen…
Browse files Browse the repository at this point in the history
…dencyModel` (#57445)

* Annotate src

* Annotate ref

* Update ref csproj

* runtimeSignature can be null

* reader.GetString() can be null

* CodeBase is obsolete

* Update ref for HashCodeCombiner

* Annotate RuntimeAssetGroup

* Null check resolvers

* Update ReferenceAssemblyPathResolver's constructor

* DisableImplicitAssemblyReferences

* Use ProjectReference in ref

* PR feedback and fix build

* Fix unit tests -

* Don't throw for null assemblies in TryResolveAssemblyPaths
* Fix up ref source
* Fix RuntimeLibrary constructor overloads

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
  • Loading branch information
maxkoshevoi and eerhardt authored Sep 2, 2021
1 parent faafb32 commit f41b259
Show file tree
Hide file tree
Showing 35 changed files with 345 additions and 294 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<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'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\ref\System.Collections.csproj" />
</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

0 comments on commit f41b259

Please sign in to comment.