Skip to content

Commit

Permalink
Targeting IDE projects to .NET 6 (#66655)
Browse files Browse the repository at this point in the history
* Targeting IDE projects to .NET 6
  • Loading branch information
genlu authored Feb 9, 2023
1 parent ba58075 commit 65866e2
Show file tree
Hide file tree
Showing 68 changed files with 147 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace
Expand All @@ -24,7 +22,7 @@ public static (string title, string equivalenceKey) GetInfo(NamespaceDeclaration
_ => throw ExceptionUtilities.UnexpectedValue(preference),
};

public static bool CanOfferUseBlockScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, BaseNamespaceDeclarationSyntax declaration, bool forAnalyzer)
public static bool CanOfferUseBlockScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, [NotNullWhen(true)] BaseNamespaceDeclarationSyntax? declaration, bool forAnalyzer)
{
if (declaration is not FileScopedNamespaceDeclarationSyntax)
return false;
Expand All @@ -40,13 +38,13 @@ public static bool CanOfferUseBlockScoped(CodeStyleOption2<NamespaceDeclarationP
return canOffer;
}

internal static bool CanOfferUseFileScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, CompilationUnitSyntax root, BaseNamespaceDeclarationSyntax declaration, bool forAnalyzer)
internal static bool CanOfferUseFileScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, CompilationUnitSyntax root, [NotNullWhen(true)] BaseNamespaceDeclarationSyntax? declaration, bool forAnalyzer)
=> CanOfferUseFileScoped(option, root, declaration, forAnalyzer, root.SyntaxTree.Options.LanguageVersion());

internal static bool CanOfferUseFileScoped(
CodeStyleOption2<NamespaceDeclarationPreference> option,
CompilationUnitSyntax root,
BaseNamespaceDeclarationSyntax declaration,
BaseNamespaceDeclarationSyntax? declaration,
bool forAnalyzer,
LanguageVersion version)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private InfoCache(INamedTypeSymbol rangeType, INamedTypeSymbol stringType, IName
.OfType<IMethodSymbol>()
.FirstOrDefault(m => IsTwoArgumentSliceLikeMethod(m));

_methodToMemberInfo[substringMethod] = ComputeMemberInfo(substringMethod, requireRangeMember: false);
if (substringMethod is not null)
_methodToMemberInfo[substringMethod] = ComputeMemberInfo(substringMethod, requireRangeMember: false);
}
}

Expand All @@ -65,7 +66,7 @@ public static bool TryCreate(Compilation compilation, [NotNullWhen(true)] out In
return true;
}

private static IMethodSymbol GetSliceLikeMethod(INamedTypeSymbol namedType)
private static IMethodSymbol? GetSliceLikeMethod(INamedTypeSymbol namedType)
=> namedType.GetMembers()
.OfType<IMethodSymbol>()
.Where(m => IsTwoArgumentSliceLikeMethod(m))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ internal static class Helpers
/// <summary>
/// Find an `int MyType.Count` or `int MyType.Length` property.
/// </summary>
public static IPropertySymbol TryGetLengthOrCountProperty(ITypeSymbol namedType)
public static IPropertySymbol? TryGetLengthOrCountProperty(ITypeSymbol namedType)
=> TryGetNoArgInt32Property(namedType, nameof(string.Length)) ??
TryGetNoArgInt32Property(namedType, nameof(ICollection.Count));

/// <summary>
/// Tried to find a public, non-static, int-returning property in the given type with the
/// specified <paramref name="name"/>.
/// </summary>
public static IPropertySymbol TryGetNoArgInt32Property(ITypeSymbol type, string name)
public static IPropertySymbol? TryGetNoArgInt32Property(ITypeSymbol type, string name)
=> type.GetMembers(name)
.OfType<IPropertySymbol>()
.Where(p => IsPublicInstance(p) &&
Expand Down Expand Up @@ -124,7 +124,7 @@ private static bool IsSliceSecondParameter(IParameterSymbol parameter)
/// provided <paramref name="parameterType"/> and must return the provided <paramref
/// name="returnType"/>.
/// </summary>
public static IPropertySymbol GetIndexer(ITypeSymbol type, ITypeSymbol parameterType, ITypeSymbol returnType)
public static IPropertySymbol? GetIndexer(ITypeSymbol type, ITypeSymbol parameterType, ITypeSymbol returnType)
=> type.GetMembers(WellKnownMemberNames.Indexer)
.OfType<IPropertySymbol>()
.Where(p => p.IsIndexer &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var serializedNamingStyle = diagnostic.Properties[nameof(NamingStyle)];
Contract.ThrowIfNull(serializedNamingStyle);

var style = NamingStyle.FromXElement(XElement.Parse(serializedNamingStyle));

var document = context.Document;
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/Core/MSBuildTask/RCWForCurrentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ private void CleanupComObject()
_shouldReleaseRCW &&
Marshal.IsComObject(_rcwForCurrentCtx))
{
#if NETCOREAPP
Debug.Assert(OperatingSystem.IsWindows());
#endif
Marshal.ReleaseComObject(_rcwForCurrentCtx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copied from:
// https://github.com/dotnet/runtime/blob/fdd104ec5e1d0d2aa24a6723995a98d0124f724b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CompilerFeatureRequiredAttribute.cs

#if !NET6_0_OR_GREATER
#if !NET7_0_OR_GREATER

namespace System.Runtime.CompilerServices
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.IO;
using System.Runtime.Versioning;

namespace Roslyn.Utilities
{
Expand All @@ -14,7 +15,11 @@ namespace Roslyn.Utilities
/// </summary>
internal static class PlatformInformation
{
#if NET5_0_OR_GREATER
[SupportedOSPlatformGuard("windows")]
#endif
public static bool IsWindows => Path.DirectorySeparatorChar == '\\';

public static bool IsUnix => Path.DirectorySeparatorChar == '/';
public static bool IsRunningOnMono
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copied from:
// https://github.com/dotnet/runtime/blob/fdd104ec5e1d0d2aa24a6723995a98d0124f724b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RequiredMemberAttribute.cs

#if !NET6_0_OR_GREATER
#if !NET7_0_OR_GREATER

namespace System.Runtime.CompilerServices
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copied from:
// https://github.com/dotnet/runtime/blob/fdd104ec5e1d0d2aa24a6723995a98d0124f724b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/SetsRequiredMembersAttribute.cs

#if !NET6_0_OR_GREATER
#if !NET7_0_OR_GREATER

namespace System.Diagnostics.CodeAnalysis
{
Expand Down
13 changes: 11 additions & 2 deletions src/Compilers/Shared/GlobalAssemblyCacheHelpers/GacFileResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ internal sealed class GacFileResolver : IEquatable<GacFileResolver>
/// Returns true if GAC is available on the current platform.
/// </summary>
public static bool IsAvailable
// Since mscorlib may not be loaded from the GAC on Mono, also check if the platform is Mono which supports a GAC.
=> typeof(object).Assembly.GlobalAssemblyCache || PlatformInformation.IsRunningOnMono;
{
get
{
// Since mscorlib may not be loaded from the GAC on Mono, also check if the platform is Mono which supports a GAC.
return
#if !NETCOREAPP
typeof(object).Assembly.GlobalAssemblyCache ||
#endif
PlatformInformation.IsRunningOnMono;
}
}

/// <summary>
/// Architecture filter used when resolving assembly references.
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.Editor.CSharp</RootNamespace>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>
<!-- NuGet -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Updater;
using Microsoft.CodeAnalysis.EditorConfig;
using Microsoft.CodeAnalysis.Shared.Extensions;
using RoslynEnumerableExtensions = Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions.EnumerableExtensions;

namespace Microsoft.CodeAnalysis.Editor.EditorConfigSettings.DataProvider.Analyzer
{
Expand All @@ -28,7 +29,7 @@ public AnalyzerSettingsProvider(string fileName, AnalyzerSettingsUpdater setting

protected override void UpdateOptions(TieredAnalyzerConfigOptions options, ImmutableArray<Project> projectsInScope)
{
var analyzerReferences = projectsInScope.SelectMany(p => p.AnalyzerReferences).DistinctBy(a => a.Id).ToImmutableArray();
var analyzerReferences = RoslynEnumerableExtensions.DistinctBy(projectsInScope.SelectMany(p => p.AnalyzerReferences), a => a.Id).ToImmutableArray();
foreach (var analyzerReference in analyzerReferences)
{
var configSettings = GetSettings(analyzerReference, options.EditorConfigOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions;
using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Updater;
using Microsoft.CodeAnalysis.Options;
using RoslynEnumerableExtensions = Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions.EnumerableExtensions;

namespace Microsoft.CodeAnalysis.Editor.EditorConfigSettings.DataProvider.NamingStyles
{
Expand All @@ -29,7 +30,7 @@ protected override void UpdateOptions(TieredAnalyzerConfigOptions options, Immut

var fileName = (location.LocationKind != LocationKind.VisualStudio) ? options.EditorConfigFileName : null;
var namingRules = namingPreferences.NamingRules.Select(r => r.GetRule(namingPreferences));
var allStyles = namingPreferences.NamingStyles.DistinctBy(s => s.Name).ToArray();
var allStyles = RoslynEnumerableExtensions.DistinctBy(namingPreferences.NamingStyles, s => s.Name).ToArray();
var namingStyles = namingRules.Select(namingRule => new NamingStyleSetting(namingRule, allStyles, SettingsUpdater, fileName));

AddRange(namingStyles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ protected SettingsProviderBase(string fileName, TOptionsUpdater settingsUpdater,
protected void Update()
{
var givenFolder = new DirectoryInfo(FileName).Parent;
if (givenFolder is null)
{
return;
}

var solution = Workspace.CurrentSolution;
var projects = solution.GetProjectsUnderEditorConfigFile(FileName);
var project = projects.FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using static Microsoft.CodeAnalysis.EditorConfig.Parsing.NamingStyles.EditorConfigNamingStylesParser;
using RoslynEnumerableExtensions = Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions.EnumerableExtensions;

namespace Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Updater
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public NamingStyleSettingsUpdater(Workspace workspace, IGlobalOptionService glob
{
var allCurrentStyles = result.Rules.Select(x => x.NamingScheme).Distinct().Select(x => (x, style: x.AsNamingStyle()));
var styleParseResult = TryGetStyleParseResult(prevStyle, allCurrentStyles);
var allDistinctStyles = allCurrentStyles.Select(x => x.style).DistinctBy(x => x.Name).ToArray();
var allDistinctStyles = RoslynEnumerableExtensions.DistinctBy(allCurrentStyles.Select(x => x.style), x => x.Name).ToArray();
if (styleParseResult is (NamingScheme namingScheme, NamingStyle style))
{
var newLine = $"dotnet_naming_rule.{parseResult.RuleName.Value}.style = {namingScheme.OptionName.Value}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async Task QueueUpdateAsync()
}
}

public async Task<SourceText?> GetChangedEditorConfigAsync(AnalyzerConfigDocument analyzerConfigDocument, CancellationToken token)
public async Task<SourceText?> GetChangedEditorConfigAsync(AnalyzerConfigDocument? analyzerConfigDocument, CancellationToken token)
{
if (analyzerConfigDocument is null)
return null;
Expand Down Expand Up @@ -81,7 +81,7 @@ async Task QueueUpdateAsync()
return null;
}

var originalText = await analyzerConfigDocument.GetTextAsync(token).ConfigureAwait(false);
var originalText = await analyzerConfigDocument!.GetTextAsync(token).ConfigureAwait(false);
return newText.GetTextChanges(originalText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ServiceWrapper(IVSTypeScriptGoToDefinitionService service)
public async Task<IEnumerable<INavigableItem>?> FindDefinitionsAsync(Document document, int position, CancellationToken cancellationToken)
{
var items = await _service.FindDefinitionsAsync(document, position, cancellationToken).ConfigureAwait(false);
return items.Select(item => new VSTypeScriptNavigableItemWrapper(item));
return items?.Select(item => new VSTypeScriptNavigableItemWrapper(item));
}

public bool TryGoToDefinition(Document document, int position, CancellationToken cancellationToken)
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>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);EDITOR_FEATURES</DefineConstants>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.VisualStudio.Text.Classification;
using Roslyn.Utilities;
using ReferenceEqualityComparer = Roslyn.Utilities.ReferenceEqualityComparer;

namespace Microsoft.CodeAnalysis.Editor.Shared.Utilities
{
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.Text</RootNamespace>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>

<!-- NuGet -->
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>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<RootNamespace></RootNamespace>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>
<!-- NuGet -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -15,8 +14,6 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
Expand Down Expand Up @@ -63,7 +60,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}

private static bool CanOfferRefactoring(
BaseNamespaceDeclarationSyntax namespaceDecl,
[NotNullWhen(true)] BaseNamespaceDeclarationSyntax? namespaceDecl,
CompilationUnitSyntax root,
CSharpCodeFixOptionsProvider options,
[NotNullWhen(true)] out (string title, string equivalenceKey)? info)
Expand All @@ -72,6 +69,7 @@ private static bool CanOfferRefactoring(
CanOfferUseBlockScoped(options.NamespaceDeclarations, namespaceDecl, forAnalyzer: false) ? GetInfo(NamespaceDeclarationPreference.BlockScoped) :
CanOfferUseFileScoped(options.NamespaceDeclarations, root, namespaceDecl, forAnalyzer: false) ? GetInfo(NamespaceDeclarationPreference.FileScoped) :
null;

return info != 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>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>

<!-- NuGet -->
Expand Down
Loading

0 comments on commit 65866e2

Please sign in to comment.