Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into merge-main-to-featu…
Browse files Browse the repository at this point in the history
…res/required-members

* upstream/main: (66 commits)
  Fix #55183: Add SymbolVisitor<TArgument, TResult> (#56530)
  Simplifier options (#60174)
  Remove duplicated asset
  Do not try to refcount solution syncing when communicating with OOP
  Delay symbol-search index updating until solution is fully loaded.
  add more miscellaneous tests for checked operators (#60727)
  Support checked operators in explicit interface implementation (#60715)
  Avoid formatting diagnostics with raw strings (#60655)
  Make heading levels for warning waves documentation consistent (#60721)
  Clean up IDiagnosticService extension methods
  Remove #nullable enable
  Add integration test to flag MEF composition breaks
  Generate static abstract interface members correctly (#60618)
  Merge release/dev17.2 to main (#60682)
  Fix FAR on checked operators (#60698)
  Add implement interface support for checked operators and cast operators (#60719)
  Update csc.dll path in launch.json (#60663)
  Grab bag of UTF8 string support in IDE features (#60599)
  Allow code actions to retrieve options for any language (#60697)
  Fix flaky VSTypeScriptHandlerTests  (#60706)
  ...
  • Loading branch information
333fred committed Apr 14, 2022
2 parents b0798f4 + ee9e2e6 commit 16a02f8
Show file tree
Hide file tree
Showing 470 changed files with 5,849 additions and 1,916 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/Compilers/CSharp/csc/bin/Debug/netcoreapp2.1/csc.dll",
"program": "${workspaceFolder}/artifacts/bin/csc/Debug/net6.0/csc.dll",
"args": [],
"cwd": "${workspaceFolder}/src/Compilers/CSharp/csc",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
2 changes: 1 addition & 1 deletion docs/compilers/CSharp/Warnversion Warning Waves.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The compiler shipped with .NET 7 (the C# 11 compiler) contains the following war
|------------|-------------|
| CS8981 | [Type names only containing lower-cased ascii characters may become reserved for the language](https://github.com/dotnet/roslyn/issues/56653) |

# Warning level 6
## Warning level 6

The compiler shipped with .NET 6 (the C# 10 compiler) contains the following warnings which are reported only under `/warn:6` or higher.

Expand Down
2 changes: 1 addition & 1 deletion dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"isRoot": true,
"tools": {
"dotnet-format": {
"version": "6.2.315104",
"version": "6.3.317301",
"commands": [
"dotnet-format"
]
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<MicrosoftCodeAnalysisTestingVersion>1.1.2-beta1.22122.4</MicrosoftCodeAnalysisTestingVersion>
<MicrosoftVisualStudioExtensibilityTestingVersion>0.1.132-beta</MicrosoftVisualStudioExtensibilityTestingVersion>
<!-- CodeStyleAnalyzerVersion should we updated together with version of dotnet-format in dotnet-tools.json -->
<CodeStyleAnalyzerVersion>4.1.0</CodeStyleAnalyzerVersion>
<CodeStyleAnalyzerVersion>4.2.0-2.final</CodeStyleAnalyzerVersion>
<VisualStudioEditorPackagesVersion>17.2.178-preview</VisualStudioEditorPackagesVersion>
<VisualStudioEditorNewPackagesVersion>17.2.140-preview-ga1e1777dca</VisualStudioEditorNewPackagesVersion>
<ILAsmPackageVersion>5.0.0-alpha1.19409.1</ILAsmPackageVersion>
Expand Down
4 changes: 4 additions & 0 deletions eng/config/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ M:Microsoft.CodeAnalysis.Formatting.Formatter.Format(Microsoft.CodeAnalysis.Synt
M:Microsoft.CodeAnalysis.Formatting.Formatter.GetFormattedTextChanges(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.Workspace,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload with SyntaxFormattingOptions instead
M:Microsoft.CodeAnalysis.Formatting.Formatter.GetFormattedTextChanges(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.Text.TextSpan,Microsoft.CodeAnalysis.Workspace,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload with SyntaxFormattingOptions instead
M:Microsoft.CodeAnalysis.Formatting.Formatter.GetFormattedTextChanges(Microsoft.CodeAnalysis.SyntaxNode,System.Collections.Generic.IEnumerable{Microsoft.CodeAnalysis.Text.TextSpan},Microsoft.CodeAnalysis.Workspace,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload with SyntaxFormattingOptions instead
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.SyntaxAnnotation,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.Text.TextSpan,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,System.Collections.Generic.IEnumerable{Microsoft.CodeAnalysis.Text.TextSpan},Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="$(MSBuildThisFileDirectory)PopulateSwitch\CSharpPopulateSwitchStatementDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnreachableCode\CSharpRemoveUnreachableCodeDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnreachableCode\RemoveUnreachableCodeHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Simplification\CSharpSimplifierOptionsFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SimplifyBooleanExpression\CSharpSimplifyConditionalDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SimplifyInterpolation\CSharpSimplifyInterpolationHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SimplifyInterpolation\CSharpSimplifyInterpolationDiagnosticAnalyzer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression
{
Expand All @@ -23,14 +21,14 @@ private sealed class Analyzer : CSharpSyntaxVisitor<SyntaxKind>
{
private readonly bool _supportsOrPatterns;

private ExpressionSyntax _assignmentTargetOpt;
private ExpressionSyntax? _assignmentTargetOpt;

private Analyzer(bool supportsOrPatterns)
{
_supportsOrPatterns = supportsOrPatterns;
}

public static (SyntaxKind nodeToGenerate, VariableDeclaratorSyntax declaratorToRemoveOpt) Analyze(
public static (SyntaxKind nodeToGenerate, VariableDeclaratorSyntax? declaratorToRemoveOpt) Analyze(
SwitchStatementSyntax node,
SemanticModel semanticModel,
out bool shouldRemoveNextStatement)
Expand All @@ -41,29 +39,35 @@ public static (SyntaxKind nodeToGenerate, VariableDeclaratorSyntax declaratorToR
if (nodeToGenerate == SyntaxKind.SimpleAssignmentExpression &&
analyzer.TryGetVariableDeclaratorAndSymbol(semanticModel) is var (declarator, symbol))
{
if (shouldRemoveNextStatement &&
semanticModel.AnalyzeDataFlow(node.GetNextStatement()).DataFlowsIn.Contains(symbol))
if (shouldRemoveNextStatement && node.GetNextStatement() is StatementSyntax nextStatement)
{
// Bail out if data flows into the next statement that we want to move
// For example:
//
// string name = "";
// switch (index)
// {
// case 0: name = "0"; break;
// case 1: name = "1"; break;
// }
// throw new Exception(name);
//
return default;
var dataFlow = semanticModel.AnalyzeDataFlow(nextStatement);
Contract.ThrowIfNull(dataFlow);
if (dataFlow.DataFlowsIn.Contains(symbol))
{
// Bail out if data flows into the next statement that we want to move
// For example:
//
// string name = "";
// switch (index)
// {
// case 0: name = "0"; break;
// case 1: name = "1"; break;
// }
// throw new Exception(name);
//
return default;
}
}

var declaration = declarator.GetAncestor<StatementSyntax>();
Contract.ThrowIfNull(declaration);
if (declaration.Parent == node.Parent && declarator.Initializer is null)
{
var beforeSwitch = node.GetPreviousStatement() is StatementSyntax previousStatement
? semanticModel.AnalyzeDataFlow(declaration, previousStatement)
: semanticModel.AnalyzeDataFlow(declaration);
Contract.ThrowIfNull(beforeSwitch);
if (!beforeSwitch.WrittenInside.Contains(symbol))
{
// Move declarator only if it has no initializer and it's not used before switch
Expand Down Expand Up @@ -204,7 +208,7 @@ private static SyntaxKind Intersect(SyntaxKind left, SyntaxKind right)
return default;
}

private SyntaxKind AnalyzeNextStatement(StatementSyntax nextStatement)
private SyntaxKind AnalyzeNextStatement(StatementSyntax? nextStatement)
{
// Only the following "throw" and "return" can be moved into the switch expression.
return nextStatement.IsKind(SyntaxKind.ThrowStatement, SyntaxKind.ReturnStatement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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.

#nullable disable

using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -14,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.InlineDeclaration
{
Expand Down Expand Up @@ -52,13 +51,13 @@ protected override void InitializeWorker(AnalysisContext context)
context.RegisterCompilationStartAction(compilationContext =>
{
var compilation = compilationContext.Compilation;
var expressionTypeOpt = compilation.GetTypeByMetadataName(typeof(Expression<>).FullName);
var expressionType = compilation.GetTypeByMetadataName(typeof(Expression<>).FullName!);
compilationContext.RegisterSyntaxNodeAction(
syntaxContext => AnalyzeSyntaxNode(syntaxContext, expressionTypeOpt), SyntaxKind.Argument);
syntaxContext => AnalyzeSyntaxNode(syntaxContext, expressionType), SyntaxKind.Argument);
});
}

private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionTypeOpt)
private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol? expressionType)
{
var argumentNode = (ArgumentSyntax)context.Node;
var csOptions = (CSharpParseOptions)context.Node.SyntaxTree.Options;
Expand Down Expand Up @@ -87,7 +86,7 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymb
}

var argumentExpression = argumentNode.Expression;
if (!argumentExpression.IsKind(SyntaxKind.IdentifierName, out IdentifierNameSyntax identifierName))
if (!argumentExpression.IsKind(SyntaxKind.IdentifierName, out IdentifierNameSyntax? identifierName))
{
// has to be exactly the form "out i". i.e. "out this.i" or "out v[i]" are legal
// cases for out-arguments, but could not be converted to an out-variable-declaration.
Expand Down Expand Up @@ -176,7 +175,7 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymb
return;
}

if (argumentExpression.IsInExpressionTree(semanticModel, expressionTypeOpt, cancellationToken))
if (argumentExpression.IsInExpressionTree(semanticModel, expressionType, cancellationToken))
{
// out-vars are not allowed inside expression-trees. So don't offer to
// fix if we're inside one.
Expand All @@ -186,6 +185,8 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymb
// Find the scope that the out-declaration variable will live in after we
// rewrite things.
var outArgumentScope = GetOutArgumentScope(argumentExpression);
if (outArgumentScope == null)
return;

if (!outLocalSymbol.CanSafelyMoveLocalToBlock(enclosingBlockOfLocalStatement, outArgumentScope))
{
Expand Down Expand Up @@ -256,14 +257,17 @@ private static bool WouldCauseDefiniteAssignmentErrors(
//
// In this case, inlining the 'i' would cause it to longer be definitely
// assigned in the WriteLine invocation.
var nextStatement = localStatement.GetNextStatement();
Contract.ThrowIfNull(nextStatement);

var dataFlow = semanticModel.AnalyzeDataFlow(
localStatement.GetNextStatement(),
nextStatement,
enclosingBlock.Statements.Last());
Contract.ThrowIfNull(dataFlow);
return dataFlow.DataFlowsIn.Contains(outLocalSymbol);
}

private static SyntaxNode GetOutArgumentScope(SyntaxNode argumentExpression)
private static SyntaxNode? GetOutArgumentScope(SyntaxNode argumentExpression)
{
for (var current = argumentExpression; current != null; current = current.Parent)
{
Expand Down Expand Up @@ -346,7 +350,7 @@ private static bool IsAccessed(
break;
}

if (descendentNode.IsKind(SyntaxKind.IdentifierName, out IdentifierNameSyntax identifierName))
if (descendentNode.IsKind(SyntaxKind.IdentifierName, out IdentifierNameSyntax? identifierName))
{
// See if this looks like an accessor to the local variable syntactically.
if (identifierName.Identifier.ValueText == variableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Simplification;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.QualifyMemberAccess;
Expand All @@ -13,11 +14,14 @@ namespace Microsoft.CodeAnalysis.CSharp.QualifyMemberAccess
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpQualifyMemberAccessDiagnosticAnalyzer
: AbstractQualifyMemberAccessDiagnosticAnalyzer<SyntaxKind, ExpressionSyntax, SimpleNameSyntax>
: AbstractQualifyMemberAccessDiagnosticAnalyzer<SyntaxKind, ExpressionSyntax, SimpleNameSyntax, CSharpSimplifierOptions>
{
protected override string GetLanguageName()
=> LanguageNames.CSharp;

protected override CSharpSimplifierOptions GetSimplifierOptions(AnalyzerOptions options, SyntaxTree syntaxTree)
=> options.GetCSharpSimplifierOptions(syntaxTree);

protected override bool IsAlreadyQualifiedMemberAccess(ExpressionSyntax node)
=> node.IsKind(SyntaxKind.ThisExpression);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Diagnostics;

namespace Microsoft.CodeAnalysis.CSharp.Simplification;

internal static class CSharpSimplifierOptionsFactory
{
internal static CSharpSimplifierOptions GetCSharpSimplifierOptions(this AnalyzerOptions options, SyntaxTree syntaxTree)
{
var configOptions = options.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree);
var ideOptions = options.GetIdeOptions();

return CSharpSimplifierOptions.Create(configOptions, (CSharpSimplifierOptions?)ideOptions.SimplifierOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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.

#nullable disable

using System.Collections.Immutable;
using Roslyn.Utilities;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Simplification;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Simplification;

namespace Microsoft.CodeAnalysis.CSharp.UseImplicitObjectCreation
{
Expand Down Expand Up @@ -44,7 +46,6 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
if (syntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp9)
return;

var optionSet = options.GetAnalyzerOptionSet(syntaxTree, cancellationToken);
var styleOption = options.GetOption(CSharpCodeStyleOptions.ImplicitObjectCreationWhenTypeIsApparent, syntaxTree, cancellationToken);
if (!styleOption.Value)
{
Expand Down Expand Up @@ -73,11 +74,15 @@ objectCreation.Parent.Parent.Parent is VariableDeclarationSyntax variableDeclara
typeNode = variableDeclaration.Type;

var helper = CSharpUseImplicitTypeHelper.Instance;
if (helper.ShouldAnalyzeVariableDeclaration(variableDeclaration, cancellationToken) &&
helper.AnalyzeTypeName(typeNode, semanticModel, optionSet, cancellationToken).IsStylePreferred)
if (helper.ShouldAnalyzeVariableDeclaration(variableDeclaration, cancellationToken))
{
// this is a case where the user would prefer 'var'. don't offer to use an implicit object here.
return;
var simplifierOptions = context.Options.GetCSharpSimplifierOptions(syntaxTree);

if (helper.AnalyzeTypeName(typeNode, semanticModel, simplifierOptions, cancellationToken).IsStylePreferred)
{
// this is a case where the user would prefer 'var'. don't offer to use an implicit object here.
return;
}
}
}
else if (objectCreation.Parent.IsKind(SyntaxKind.ArrowExpressionClause))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Simplification;
using Microsoft.CodeAnalysis.CSharp.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
Expand Down Expand Up @@ -52,10 +53,8 @@ protected override void InitializeWorker(AnalysisContext context)
private void HandleVariableDeclaration(SyntaxNodeAnalysisContext context)
{
var declarationStatement = context.Node;
var options = context.Options;
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = options.GetAnalyzerOptionSet(syntaxTree, cancellationToken);

var semanticModel = context.SemanticModel;
var declaredType = Helper.FindAnalyzableType(declarationStatement, semanticModel, cancellationToken);
Expand All @@ -64,8 +63,10 @@ private void HandleVariableDeclaration(SyntaxNodeAnalysisContext context)
return;
}

var simplifierOptions = context.Options.GetCSharpSimplifierOptions(syntaxTree);

var typeStyle = Helper.AnalyzeTypeName(
declaredType, semanticModel, optionSet, cancellationToken);
declaredType, semanticModel, simplifierOptions, cancellationToken);
if (!typeStyle.IsStylePreferred || !typeStyle.CanConvert())
{
return;
Expand Down
Loading

0 comments on commit 16a02f8

Please sign in to comment.