Skip to content

Commit 32017d1

Browse files
author
dotnet-automerge-bot
authored
Merge pull request #36531 from dotnet/merges/master-to-features/param-nullchecking
Merge master to features/param-nullchecking
2 parents e74f602 + d4afaea commit 32017d1

File tree

17 files changed

+199
-39
lines changed

17 files changed

+199
-39
lines changed

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.symbols = non
6666
dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.style = non_private_static_field_style
6767

6868
dotnet_naming_symbols.non_private_static_fields.applicable_kinds = field
69-
dotnet_naming_symbols.non_private_static_fields.applicable_accessibilities = public, protected, internal, protected internal, private protected
69+
dotnet_naming_symbols.non_private_static_fields.applicable_accessibilities = public, protected, internal, protected_internal, private_protected
7070
dotnet_naming_symbols.non_private_static_fields.required_modifiers = static
7171

7272
dotnet_naming_style.non_private_static_field_style.capitalization = pascal_case
@@ -77,7 +77,7 @@ dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.symbols = n
7777
dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.style = non_private_readonly_field_style
7878

7979
dotnet_naming_symbols.non_private_readonly_fields.applicable_kinds = field
80-
dotnet_naming_symbols.non_private_readonly_fields.applicable_accessibilities = public, protected, internal, protected internal, private protected
80+
dotnet_naming_symbols.non_private_readonly_fields.applicable_accessibilities = public, protected, internal, protected_internal, private_protected
8181
dotnet_naming_symbols.non_private_readonly_fields.required_modifiers = readonly
8282

8383
dotnet_naming_style.non_private_readonly_field_style.capitalization = pascal_case

src/Compilers/CSharp/Portable/Parser/LanguageParser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8294,6 +8294,7 @@ private StatementSyntax ParseLocalDeclarationStatement(SyntaxToken awaitKeywordO
82948294
{
82958295
usingKeyword = CheckFeatureAvailability(usingKeyword, MessageID.IDS_FeatureUsingDeclarations);
82968296
}
8297+
bool canParseAsLocalFunction = usingKeyword == default;
82978298

82988299
var mods = _pool.Allocate();
82998300
this.ParseDeclarationModifiers(mods);
@@ -8304,7 +8305,7 @@ private StatementSyntax ParseLocalDeclarationStatement(SyntaxToken awaitKeywordO
83048305
TypeSyntax type;
83058306
LocalFunctionStatementSyntax localFunction;
83068307
this.ParseLocalDeclaration(variables,
8307-
allowLocalFunctions: usingKeyword == default,
8308+
allowLocalFunctions: canParseAsLocalFunction,
83088309
mods: mods.ToList(),
83098310
type: out type,
83108311
localFunction: out localFunction);
@@ -8317,7 +8318,8 @@ private StatementSyntax ParseLocalDeclarationStatement(SyntaxToken awaitKeywordO
83178318

83188319
// If we find an accessibility modifier but no local function it's likely
83198320
// the user forgot a closing brace. Let's back out of statement parsing.
8320-
if (mods.Count > 0 &&
8321+
if (canParseAsLocalFunction &&
8322+
mods.Count > 0 &&
83218323
IsAccessibilityModifier(((SyntaxToken)mods[0]).ContextualKind))
83228324
{
83238325
return null;

src/Compilers/CSharp/Test/Semantic/Semantics/UsingDeclarationTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
44
using Microsoft.CodeAnalysis.CSharp.UnitTests;
5+
using Roslyn.Test.Utilities;
56
using Xunit;
67

78
namespace Microsoft.CodeAnalysis.CSharp.Semantic.UnitTests.Semantics
@@ -769,5 +770,29 @@ static void Main()
769770
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "using S3 S3 = new S3();").WithArguments("S3.Dispose()").WithLocation(40, 9)
770771
);
771772
}
773+
774+
[Fact]
775+
[WorkItem(36413, "https://github.com/dotnet/roslyn/issues/36413")]
776+
public void UsingDeclarationsWithInvalidModifiers()
777+
{
778+
var source = @"
779+
using System;
780+
class C
781+
{
782+
static void Main()
783+
{
784+
using public readonly var x = (IDisposable)null;
785+
}
786+
}
787+
";
788+
CreateCompilation(source, parseOptions: TestOptions.RegularPreview).VerifyDiagnostics(
789+
// (7,15): error CS0106: The modifier 'public' is not valid for this item
790+
// using public readonly var x = (IDisposable)null;
791+
Diagnostic(ErrorCode.ERR_BadMemberFlag, "public").WithArguments("public").WithLocation(7, 15),
792+
// (7,22): error CS0106: The modifier 'readonly' is not valid for this item
793+
// using public readonly var x = (IDisposable)null;
794+
Diagnostic(ErrorCode.ERR_BadMemberFlag, "readonly").WithArguments("readonly").WithLocation(7, 22)
795+
);
796+
}
772797
}
773798
}

src/Compilers/CSharp/Test/Syntax/Parsing/StatementParsingTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,44 @@ public void TestUsingVarWithVarDeclaration()
25082508
Assert.Equal("b", us.Declaration.Variables[0].Initializer.Value.ToString());
25092509
}
25102510

2511+
[Fact]
2512+
[WorkItem(36413, "https://github.com/dotnet/roslyn/issues/36413")]
2513+
public void TestUsingVarWithInvalidDeclaration()
2514+
{
2515+
var text = "using public readonly var a = b;";
2516+
var statement = this.ParseStatement(text, options: TestOptions.Regular8);
2517+
2518+
Assert.NotNull(statement);
2519+
Assert.Equal(SyntaxKind.LocalDeclarationStatement, statement.Kind());
2520+
Assert.Equal(text, statement.ToString());
2521+
Assert.Equal(2, statement.Errors().Length);
2522+
Assert.Equal((int)ErrorCode.ERR_BadMemberFlag, statement.Errors()[0].Code);
2523+
Assert.Equal("public", statement.Errors()[0].Arguments[0]);
2524+
Assert.Equal((int)ErrorCode.ERR_BadMemberFlag, statement.Errors()[1].Code);
2525+
Assert.Equal("readonly", statement.Errors()[1].Arguments[0]);
2526+
2527+
var us = (LocalDeclarationStatementSyntax)statement;
2528+
Assert.NotNull(us.UsingKeyword);
2529+
Assert.Equal(SyntaxKind.UsingKeyword, us.UsingKeyword.Kind());
2530+
2531+
Assert.NotNull(us.Declaration);
2532+
Assert.NotNull(us.Declaration.Type);
2533+
Assert.Equal("var", us.Declaration.Type.ToString());
2534+
Assert.Equal(SyntaxKind.IdentifierName, us.Declaration.Type.Kind());
2535+
Assert.Equal(SyntaxKind.IdentifierToken, ((IdentifierNameSyntax)us.Declaration.Type).Identifier.Kind());
2536+
Assert.Equal(2, us.Modifiers.Count);
2537+
Assert.Equal("public", us.Modifiers[0].ToString());
2538+
Assert.Equal("readonly", us.Modifiers[1].ToString());
2539+
Assert.Equal(1, us.Declaration.Variables.Count);
2540+
Assert.NotNull(us.Declaration.Variables[0].Identifier);
2541+
Assert.Equal("a", us.Declaration.Variables[0].Identifier.ToString());
2542+
Assert.Null(us.Declaration.Variables[0].ArgumentList);
2543+
Assert.NotNull(us.Declaration.Variables[0].Initializer);
2544+
Assert.NotNull(us.Declaration.Variables[0].Initializer.EqualsToken);
2545+
Assert.NotNull(us.Declaration.Variables[0].Initializer.Value);
2546+
Assert.Equal("b", us.Declaration.Variables[0].Initializer.Value.ToString());
2547+
}
2548+
25112549
[Fact]
25122550
public void TestUsingVarWithVarDeclarationTree()
25132551
{

src/EditorFeatures/CSharpTest/MoveToNamespace/MoveToNamespaceTests.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// Copyright(c) Microsoft.All Rights Reserved.Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System.Collections.Generic;
4-
using System.Collections.Immutable;
4+
using System.Linq;
5+
using System.Threading;
56
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis.CodeRefactorings;
8+
using Microsoft.CodeAnalysis.CSharp.MoveToNamespace;
69
using Microsoft.CodeAnalysis.Editor.UnitTests;
710
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
11+
using Microsoft.CodeAnalysis.MoveToNamespace;
812
using Microsoft.CodeAnalysis.Test.Utilities;
913
using Microsoft.CodeAnalysis.Test.Utilities.MoveToNamespace;
1014
using Microsoft.VisualStudio.Composition;
@@ -16,14 +20,15 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.MoveToNamespace
1620
[UseExportProvider]
1721
public class MoveToNamespaceTests : AbstractMoveToNamespaceTests
1822
{
19-
private static readonly IExportProviderFactory CSharpExportProviderFactory =
23+
private static readonly IExportProviderFactory ExportProviderFactory =
2024
ExportProviderCache.GetOrCreateExportProviderFactory(
21-
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic
22-
.WithPart(typeof(TestMoveToNamespaceOptionsService))
23-
.WithPart(typeof(TestSymbolRenamedCodeActionOperationFactoryWorkspaceService)));
25+
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithPart(typeof(TestMoveToNamespaceOptionsService)));
2426

2527
protected override TestWorkspace CreateWorkspaceFromFile(string initialMarkup, TestParameters parameters)
26-
=> TestWorkspace.CreateCSharp(initialMarkup, parameters.parseOptions, parameters.compilationOptions, exportProvider: CSharpExportProviderFactory.CreateExportProvider());
28+
=> CreateWorkspaceFromFile(initialMarkup, parameters, ExportProviderFactory);
29+
30+
protected TestWorkspace CreateWorkspaceFromFile(string initialMarkup, TestParameters parameters, IExportProviderFactory exportProviderFactory)
31+
=> TestWorkspace.CreateCSharp(initialMarkup, parameters.parseOptions, parameters.compilationOptions, exportProvider: exportProviderFactory.CreateExportProvider());
2732

2833
protected override ParseOptions GetScriptOptions() => Options.Script;
2934

@@ -1080,5 +1085,34 @@ class C2
10801085
{
10811086
{"Two.C2", "Three.C2" }
10821087
});
1088+
1089+
[WpfFact, Trait(Traits.Feature, Traits.Features.MoveToNamespace)]
1090+
[WorkItem(35577, "https://github.com/dotnet/roslyn/issues/35577")]
1091+
public async Task MoveToNamespace_WithoutOptionsService()
1092+
{
1093+
var code = @"namespace A[||]
1094+
{
1095+
class MyClass
1096+
{
1097+
void Method() { }
1098+
}
1099+
}";
1100+
1101+
var exportProviderWithoutOptionsService = ExportProviderCache.GetOrCreateExportProviderFactory(
1102+
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithoutPartsOfType(typeof(IMoveToNamespaceOptionsService)));
1103+
1104+
using (var workspace = CreateWorkspaceFromFile(code, new TestParameters(), exportProviderWithoutOptionsService))
1105+
using (var testState = new TestState(workspace))
1106+
{
1107+
Assert.Null(testState.TestMoveToNamespaceOptionsService);
1108+
1109+
var actions = await testState.MoveToNamespaceService.GetCodeActionsAsync(
1110+
testState.InvocationDocument,
1111+
testState.TestInvocationDocument.SelectedSpans.Single(),
1112+
CancellationToken.None);
1113+
1114+
Assert.Empty(actions);
1115+
}
1116+
}
10831117
}
10841118
}

src/EditorFeatures/CSharpTest/UseCompoundAssignment/UseCompoundAssignmentTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ void M(int? a)
251251
new TestParameters(parseOptions: new CSharpParseOptions(LanguageVersion.CSharp7_3)));
252252
}
253253

254+
[Fact]
255+
[WorkItem(36467, "https://github.com/dotnet/roslyn/issues/36467")]
256+
[Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
257+
public async Task TestNotSuggestedWhenRightHandIsThrowExpression()
258+
{
259+
await TestMissingAsync(
260+
@"using System;
261+
public class C
262+
{
263+
void M(int? a)
264+
{
265+
a [||]= a ?? throw new Exception();
266+
}
267+
}",
268+
new TestParameters(parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8)));
269+
}
270+
254271
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
255272
public async Task TestField()
256273
{

src/EditorFeatures/CSharpTest/UseLocalFunction/UseLocalFunctionTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using Microsoft.CodeAnalysis.CodeFixes;
55
using Microsoft.CodeAnalysis.CSharp;
6+
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
67
using Microsoft.CodeAnalysis.CSharp.UseLocalFunction;
78
using Microsoft.CodeAnalysis.Diagnostics;
89
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
@@ -3660,5 +3661,33 @@ void M()
36603661
}
36613662
}", parseOptions: CSharp8ParseOptions);
36623663
}
3664+
3665+
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
3666+
public async Task TestWithNullableParameterAndReturn()
3667+
{
3668+
await TestInRegularAndScriptAsync(
3669+
@"#nullable enable
3670+
3671+
using System;
3672+
3673+
class Program
3674+
{
3675+
static void Main(string[] args)
3676+
{
3677+
Func<string?, string?> [||]f = s => s;
3678+
}
3679+
}",
3680+
@"#nullable enable
3681+
3682+
using System;
3683+
3684+
class Program
3685+
{
3686+
static void Main(string[] args)
3687+
{
3688+
static string? f(string? s) => s;
3689+
}
3690+
}", parseOptions: TestOptions.Regular8WithNullableAnalysis);
3691+
}
36633692
}
36643693
}

src/EditorFeatures/TestUtilities/MoveToNamespace/AbstractMoveToNamespaceTests.TestState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void Dispose()
2727
public Document InvocationDocument => Workspace.CurrentSolution.GetDocument(TestInvocationDocument.Id);
2828

2929
public TestMoveToNamespaceOptionsService TestMoveToNamespaceOptionsService
30-
=> (TestMoveToNamespaceOptionsService)Workspace.Services.GetService<IMoveToNamespaceOptionsService>();
30+
=> (TestMoveToNamespaceOptionsService)MoveToNamespaceService.OptionsService;
3131

3232
public IMoveToNamespaceService MoveToNamespaceService
3333
=> InvocationDocument.GetLanguageService<IMoveToNamespaceService>();

src/EditorFeatures/TestUtilities/MoveToNamespace/TestMoveToNamespaceOptionsService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2-
using System;
32
using System.Collections.Immutable;
43
using Microsoft.CodeAnalysis.LanguageServices;
54
using Microsoft.CodeAnalysis.MoveToNamespace;
6-
using Microsoft.CodeAnalysis.Host.Mef;
75
using System.Composition;
86

97
namespace Microsoft.CodeAnalysis.Test.Utilities.MoveToNamespace
108
{
11-
[ExportWorkspaceService(typeof(IMoveToNamespaceOptionsService)), Shared]
9+
[Export(typeof(IMoveToNamespaceOptionsService)), Shared]
10+
[PartNotDiscoverable]
1211
class TestMoveToNamespaceOptionsService : IMoveToNamespaceOptionsService
1312
{
1413
internal static readonly MoveToNamespaceOptionsResult DefaultOptions = new MoveToNamespaceOptionsResult("TestNewNamespaceValue");

src/Features/CSharp/Portable/MoveToNamespace/CSharpMoveToNamespaceService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ internal class CSharpMoveToNamespaceService :
1313
AbstractMoveToNamespaceService<NamespaceDeclarationSyntax, TypeDeclarationSyntax>
1414
{
1515
[ImportingConstructor]
16-
public CSharpMoveToNamespaceService()
16+
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
17+
public CSharpMoveToNamespaceService(
18+
[Import(AllowDefault = true)] IMoveToNamespaceOptionsService optionsService)
19+
: base(optionsService)
1720
{
1821
}
1922

0 commit comments

Comments
 (0)