Skip to content

Commit

Permalink
Add netcoreapp3.1 target for the features layer
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Mar 30, 2020
1 parent 7c63d34 commit c5f8516
Show file tree
Hide file tree
Showing 24 changed files with 63 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
// actually looking at something Nullable (and not some type that uses a similar
// syntactic pattern).
var semanticModel = context.SemanticModel;
var nullableType = semanticModel.Compilation.GetTypeByMetadataName(typeof(Nullable<>).FullName);
var nullableType = semanticModel.Compilation.GetTypeByMetadataName(typeof(Nullable<>).FullName!);
if (nullableType == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override void InitializeWorker(AnalysisContext context)

private void OnCompilationStart(CompilationStartAnalysisContext context)
{
var ienumerableType = context.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
var ienumerableType = context.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName!);
if (ienumerableType != null)
{
var syntaxKinds = GetSyntaxFacts().SyntaxKinds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.AddExplicitCast
///
/// 'Derived1' is less specific than 'Derived2' compared to 'Base'
/// </summary>
internal sealed class InheritanceDistanceComparer : IComparer<ITypeSymbol>
internal sealed class InheritanceDistanceComparer : IComparer<ITypeSymbol?>
{
private readonly ITypeSymbol _baseType;
private readonly SemanticModel _semanticModel;
Expand All @@ -41,8 +41,13 @@ public InheritanceDistanceComparer(SemanticModel semanticModel, ITypeSymbol base
_baseType = baseType;
}

public int Compare(ITypeSymbol x, ITypeSymbol y)
public int Compare(ITypeSymbol? x, ITypeSymbol? y)
{
if (x is null)
return y is null ? 0 : -1;
else if (y is null)
return 1;

var xDist = GetInheritanceDistance(x);
var yDist = GetInheritanceDistance(y);
return xDist.CompareTo(yDist);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ private bool TryGetMethodReturnType(

private bool IsCorrectTypeForYieldReturn(ITypeSymbol typeArgument, ITypeSymbol returnExpressionType, ITypeSymbol methodReturnType, SemanticModel model)
{
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator).FullName);
var ienumerableGenericSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable<>).FullName);
var ienumeratorGenericSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator<>).FullName);
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName!);
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator).FullName!);
var ienumerableGenericSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable<>).FullName!);
var ienumeratorGenericSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator<>).FullName!);

if (ienumerableGenericSymbol == null ||
ienumerableSymbol == null ||
Expand Down Expand Up @@ -190,8 +190,8 @@ private bool CanConvertTypes(ITypeSymbol typeArgument, ITypeSymbol returnExpress

private bool IsCorrectTypeForYieldReturn(ITypeSymbol methodReturnType, SemanticModel model)
{
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator).FullName);
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName!);
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator).FullName!);

if (ienumerableSymbol == null ||
ienumeratorSymbol == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.CSharp</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<ApplyNgenOptimization>partial</ApplyNgenOptimization>

<!-- NuGet -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private async Task<Solution> ConfigureAsync()

// Otherwise, add analyzer config document to all applicable projects for the current project's solution.
AnalyzerConfigDocument? analyzerConfigDocument = null;
var analyzerConfigDirectory = PathUtilities.GetDirectoryName(analyzerConfigPath);
var analyzerConfigDirectory = PathUtilities.GetDirectoryName(analyzerConfigPath) ?? throw ExceptionUtilities.Unreachable;
var currentSolution = _project.Solution;
foreach (var projectId in _project.Solution.ProjectIds)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private ImmutableArray<CodeRefactoringProvider> CreateRefactorings(string langua
attribute.Languages.Length == 0 ||
attribute.Languages.Contains(language))
{
builder.Add((CodeRefactoringProvider)Activator.CreateInstance(typeInfo.AsType()));
builder.Add((CodeRefactoringProvider)Activator.CreateInstance(typeInfo.AsType())!);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
return new ImportCompletionCacheService(_peItemsCache, _projectItemsCache);
}

private void OnCacheFlushRequested(object sender, EventArgs e)
private void OnCacheFlushRequested(object? sender, EventArgs e)
{
_peItemsCache.Clear();
_projectItemsCache.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ private bool TryGetCacheForPEReference(
bool forceCacheCreation,
IDictionary<TKey, CacheEntry> cache,
CancellationToken cancellationToken)
where TKey : notnull
{
var language = syntaxContext.SemanticModel.Language;

Expand Down
2 changes: 1 addition & 1 deletion src/Features/Core/Portable/Diagnostics/AnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static (string analyzerId, VersionStamp version) GetAnalyzerIdAndVersion(
}

public static string GetAnalyzerAssemblyName(this DiagnosticAnalyzer analyzer)
=> analyzer.GetType().Assembly.GetName().Name;
=> analyzer.GetType().Assembly.GetName().Name ?? throw ExceptionUtilities.Unreachable;

public static OptionSet GetAnalyzerOptionSet(this AnalyzerOptions analyzerOptions, SyntaxTree syntaxTree, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -125,10 +126,10 @@ public bool IsActiveFile(DocumentId documentId)
public bool FromBuild(ProjectId projectId)
=> _projectStates.TryGetValue(projectId, out var projectState) && projectState.FromBuild;

public bool TryGetActiveFileState(DocumentId documentId, out ActiveFileState state)
public bool TryGetActiveFileState(DocumentId documentId, [NotNullWhen(true)] out ActiveFileState? state)
=> _activeFileStates.TryGetValue(documentId, out state);

public bool TryGetProjectState(ProjectId projectId, out ProjectState state)
public bool TryGetProjectState(ProjectId projectId, [NotNullWhen(true)] out ProjectState? state)
=> _projectStates.TryGetValue(projectId, out state);

public ActiveFileState GetOrCreateActiveFileState(DocumentId documentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public bool NeedsReanalysisOnOptionChanged(object sender, OptionChangedEventArgs
#pragma warning restore CS0618 // Type or member is obsolete
}

private void OnProjectAnalyzerReferenceChanged(object sender, ProjectAnalyzerReferenceChangedEventArgs e)
private void OnProjectAnalyzerReferenceChanged(object? sender, ProjectAnalyzerReferenceChangedEventArgs e)
{
if (e.Removed.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,27 @@ internal static bool AreEquivalent(Diagnostic[] diagnosticsA, Diagnostic[] diagn
return set.SetEquals(diagnosticsB);
}

private sealed class DiagnosticComparer : IEqualityComparer<Diagnostic>
private sealed class DiagnosticComparer : IEqualityComparer<Diagnostic?>
{
internal static readonly DiagnosticComparer Instance = new DiagnosticComparer();

public bool Equals(Diagnostic x, Diagnostic y)
=> x.Id == y.Id && x.Location == y.Location;
public bool Equals(Diagnostic? x, Diagnostic? y)
{
if (x is null)
return y is null;
else if (y is null)
return false;

return x.Id == y.Id && x.Location == y.Location;
}

public int GetHashCode(Diagnostic obj)
=> Hash.Combine(obj.Id.GetHashCode(), obj.Location.GetHashCode());
public int GetHashCode(Diagnostic? obj)
{
if (obj is null)
return 0;

return Hash.Combine(obj.Id.GetHashCode(), obj.Location.GetHashCode());
}
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public HostAnalyzerReferenceDiagnosticReporter(AbstractHostDiagnosticUpdateSourc
_primaryWorkspace = primaryWorkspace;
}

public void OnAnalyzerLoadFailed(object sender, AnalyzerLoadFailureEventArgs e)
public void OnAnalyzerLoadFailed(object? sender, AnalyzerLoadFailureEventArgs e)
{
if (!(sender is AnalyzerFileReference reference))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2108,11 +2108,11 @@ private static int CompareLineChanges(LineChange x, LineChange y)

#region Semantic Analysis

private sealed class AssemblyEqualityComparer : IEqualityComparer<IAssemblySymbol>
private sealed class AssemblyEqualityComparer : IEqualityComparer<IAssemblySymbol?>
{
public static readonly IEqualityComparer<IAssemblySymbol> Instance = new AssemblyEqualityComparer();
public static readonly IEqualityComparer<IAssemblySymbol?> Instance = new AssemblyEqualityComparer();

public bool Equals(IAssemblySymbol x, IAssemblySymbol y)
public bool Equals(IAssemblySymbol? x, IAssemblySymbol? y)
{
// Types defined in old source assembly need to be treated as equivalent to types in the new source assembly,
// provided that they only differ in their containing assemblies.
Expand All @@ -2123,11 +2123,11 @@ public bool Equals(IAssemblySymbol x, IAssemblySymbol y)
// a single PE symbol. Thus comparing assemblies by identity partitions them so that each partition
// contains assemblies that originated from the same Gen0 assembly.

return x.Identity.Equals(y.Identity);
return Equals(x?.Identity, y?.Identity);
}

public int GetHashCode(IAssemblySymbol obj)
=> obj.Identity.GetHashCode();
public int GetHashCode(IAssemblySymbol? obj)
=> obj?.Identity.GetHashCode() ?? 0;
}

protected static readonly SymbolEquivalenceComparer s_assemblyEqualityComparer = new SymbolEquivalenceComparer(
Expand Down Expand Up @@ -2727,7 +2727,7 @@ private static bool HasExplicitOrSequentialLayout(
return false;
}

lazyLayoutAttribute ??= model.Compilation.GetTypeByMetadataName(typeof(StructLayoutAttribute).FullName);
lazyLayoutAttribute ??= model.Compilation.GetTypeByMetadataName(typeof(StructLayoutAttribute).FullName!);
if (lazyLayoutAttribute == null)
{
return false;
Expand Down Expand Up @@ -3397,6 +3397,7 @@ private static void MarkVariables(ref BitVector mask, ImmutableArray<ISymbol> va
}

private static void BuildIndex<TKey>(Dictionary<TKey, int> index, ImmutableArray<TKey> array)
where TKey : notnull
{
for (var i = 0; i < array.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ from region in delta.NonRemappableRegions
{
Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA, "SymReader requires MTA");

EmitBaseline baseline;
EmitBaseline? baseline;
lock (_projectEmitBaselinesGuard)
{
if (_projectEmitBaselines.TryGetValue(projectId, out baseline))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ private bool WellKnownFrameworkValueType(Compilation compilation, ITypeSymbol ty
return false;
}

var cancellationTokenType = compilation.GetTypeByMetadataName(typeof(CancellationToken).FullName);
var cancellationTokenType = compilation.GetTypeByMetadataName(typeof(CancellationToken).FullName!);
if (cancellationTokenType != null && cancellationTokenType.Equals(type))
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
return null;

var semanticModel = await _document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var equatableType = semanticModel.Compilation.GetTypeByMetadataName(typeof(IEquatable<>).FullName);
var equatableType = semanticModel.Compilation.GetTypeByMetadataName(typeof(IEquatable<>).FullName!);
if (equatableType == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private IMethodSymbol CreateDisposeInterfaceMethod(
statements.Add(g.ExpressionStatement(
g.InvocationExpression(
g.MemberAccessExpression(
g.TypeExpression(compilation.GetTypeByMetadataName(typeof(GC).FullName)),
g.TypeExpression(compilation.GetTypeByMetadataName(typeof(GC).FullName!)),
nameof(GC.SuppressFinalize)),
g.ThisExpression())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<ApplyNgenOptimization>partial</ApplyNgenOptimization>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static void UnwrapFormatString(
{
if (invocation.Arguments.Length == 1 &&
invocation.Arguments[0].Value is ILiteralOperation { ConstantValue: { HasValue: true, Value: string value } } literal &&
invocation.SemanticModel.Compilation.GetTypeByMetadataName(typeof(System.IFormattable).FullName) is { } systemIFormattable &&
invocation.SemanticModel.Compilation.GetTypeByMetadataName(typeof(System.IFormattable).FullName!) is { } systemIFormattable &&
invocation.Instance.Type.Implements(systemIFormattable))
{
unwrapped = invocation.Instance;
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Core/Portable/UseSystemHashCode/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static bool TryGetAnalyzer(Compilation compilation, [NotNullWhen(true)] o
// This may not find anything. However, CanAnalyze checks for this. So
// we represent the value as non-nullable for all future code.
var objectGetHashCodeMethod = objectType?.GetMembers(nameof(GetHashCode)).FirstOrDefault() as IMethodSymbol;
var equalityComparerType = compilation.GetTypeByMetadataName(typeof(EqualityComparer<>).FullName);
var equalityComparerType = compilation.GetTypeByMetadataName(typeof(EqualityComparer<>).FullName!);
var systemHashCodeType = compilation.GetTypeByMetadataName("System.HashCode");

if (systemHashCodeType == null || objectGetHashCodeMethod == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(startContext =>
{
var formatProviderType = startContext.Compilation.GetTypeByMetadataName(typeof(System.IFormatProvider).FullName);
var formatProviderType = startContext.Compilation.GetTypeByMetadataName(typeof(System.IFormatProvider).FullName!);
if (formatProviderType == null)
{
return;
Expand Down Expand Up @@ -343,8 +343,10 @@ protected void ValidateAndReportDiagnostic(
var formatStringWithEscapedBracketsChangedToSpaces = RemoveEscapedBrackets(formatString);

var matches = s_extractPlaceholdersRegex.Matches(formatStringWithEscapedBracketsChangedToSpaces);
foreach (Match match in matches)
foreach (Match? match in matches)
{
RoslynDebug.AssertNotNull(match);

var textInsideBrackets = match.Groups[1].Value;

if (!PlaceholderIndexIsValid(textInsideBrackets, numberOfPlaceholderArguments))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<RootNamespace></RootNamespace>
<ApplyNgenOptimization>partial</ApplyNgenOptimization>

Expand Down

0 comments on commit c5f8516

Please sign in to comment.