Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge main to main-vs-deps #54351

Merged
24 commits merged into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0f61ae6
Include ExplicitInterfaceImplementation
Cosifne Jun 10, 2021
5e4fb6d
Fix formatting
Cosifne Jun 10, 2021
c350df2
Put escapse logic into helpers
Cosifne Jun 10, 2021
6e42437
Use CDATA
Cosifne Jun 10, 2021
aabf33a
Add TestIncrementInExpressionContext C# test
MaStr11 Jun 23, 2021
4edd63f
Use Postfix operator only if in statement context.
MaStr11 Jun 23, 2021
141ac4f
Merge pull request #54180 from Cosifne/dev/shech/portExplicitInterfac…
Cosifne Jun 23, 2021
d7199c5
Use IsSimpleAssignmentStatement and add some more tests
MaStr11 Jun 23, 2021
9e428fe
Use WithTriviaFrom(currentAssignment) and add tests
MaStr11 Jun 23, 2021
8b0e657
for statement increment as postfix added
MaStr11 Jun 23, 2021
e7810a7
Move trivia handling.
MaStr11 Jun 23, 2021
34d7d9b
Use TimeSpan instead of int for durations
sharwell Jun 23, 2021
1ac79a4
Simplify
CyrusNajmabadi Jun 23, 2021
ecd8a44
remove syntax facts
CyrusNajmabadi Jun 23, 2021
6e2ce53
Expedite waits in WorkCoordinatorTests
sharwell Jun 23, 2021
089ff93
Remove unnecessary configurable delays
sharwell Jun 23, 2021
df89ea0
Use IExpeditableDelaySource for delay in PreviewSolutionCrawlerRegist…
sharwell Jun 23, 2021
b4f21da
Yield instead of delay on expedited wait completion in IdleProcessor
sharwell Jun 23, 2021
ce425c8
Merge remote-tracking branch 'upstream/release/dev16.11' into merges/…
Cosifne Jun 23, 2021
047ee59
Merge pull request #54324 from MaStr11/CompoundAssignmentIncrementInE…
CyrusNajmabadi Jun 23, 2021
f814914
Add UpdatedTypes to WatchHotReloadService.Update (#54340)
tmat Jun 23, 2021
ce28cf0
Merge pull request #54333 from dotnet/merges/release/dev16.11-to-main
Jun 23, 2021
b47268b
Merge pull request #54344 from sharwell/timespan
sharwell Jun 23, 2021
4792724
Move a couple of APIs from the generator driver into extension method…
chsienki Jun 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.UseCompoundAssignment;

namespace Microsoft.CodeAnalysis.CSharp.UseCompoundAssignment
Expand All @@ -30,14 +31,32 @@ protected override AssignmentExpressionSyntax Assignment(
return SyntaxFactory.AssignmentExpression(assignmentOpKind, left, syntaxToken, right);
}

protected override ExpressionSyntax Increment(ExpressionSyntax left)
{
return SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, left);
}
protected override ExpressionSyntax Increment(ExpressionSyntax left, bool postfix)
=> postfix
? Postfix(SyntaxKind.PostIncrementExpression, left)
: Prefix(SyntaxKind.PreIncrementExpression, left);

protected override ExpressionSyntax Decrement(ExpressionSyntax left, bool postfix)
=> postfix
? Postfix(SyntaxKind.PostDecrementExpression, left)
: Prefix(SyntaxKind.PreDecrementExpression, left);

protected override ExpressionSyntax Decrement(ExpressionSyntax left)
private static ExpressionSyntax Postfix(SyntaxKind kind, ExpressionSyntax operand)
=> SyntaxFactory.PostfixUnaryExpression(kind, operand);

private static ExpressionSyntax Prefix(SyntaxKind kind, ExpressionSyntax operand)
=> SyntaxFactory.PrefixUnaryExpression(kind, operand);

protected override bool PreferPostfix(ISyntaxFactsService syntaxFacts, AssignmentExpressionSyntax currentAssignment)
{
return SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostDecrementExpression, left);
// in `for (...; x = x + 1)` we prefer to translate that idiomatically as `for (...; x++)`
if (currentAssignment.Parent is ForStatementSyntax forStatement &&
forStatement.Incrementors.Contains(currentAssignment))
{
return true;
}

return base.PreferPostfix(syntaxFacts, currentAssignment);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1093,5 +1093,109 @@ void M()
}
}");
}

[WorkItem(38054, "https://github.com/dotnet/roslyn/issues/53969")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestIncrementInExpressionContext()
{
await TestInRegularAndScript1Async(
@"public class C
{
void M(int i)
{
M(i [||]= i + 1);
}
}",
@"public class C
{
void M(int i)
{
M(++i);
}
}");
}

[WorkItem(38054, "https://github.com/dotnet/roslyn/issues/53969")]
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
[InlineData("switch($$) { }")]
[InlineData("while(($$) > 0) { }")]
[InlineData("_ = true ? $$ : 0;")]
[InlineData("_ = ($$);")]
public async Task TestPrefixIncrement1(string expressionContext)
{
var before = expressionContext.Replace("$$", "i [||]= i + 1");
var after = expressionContext.Replace("$$", "++i");
await TestInRegularAndScript1Async(
@$"public class C
{{
void M(int i)
{{
{before}
}}
}}",
@$"public class C
{{
void M(int i)
{{
{after}
}}
}}");
}

[WorkItem(38054, "https://github.com/dotnet/roslyn/issues/53969")]
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
[InlineData("return $$;")]
[InlineData("return true ? $$ : 0;")]
[InlineData("return ($$);")]
public async Task TestPrefixIncrement2(string expressionContext)
{
var before = expressionContext.Replace("$$", "i [||]= i + 1");
var after = expressionContext.Replace("$$", "++i");
await TestInRegularAndScript1Async(
@$"public class C
{{
int M(int i)
{{
{before}
}}
}}",
@$"public class C
{{
int M(int i)
{{
{after}
}}
}}");
}

[WorkItem(38054, "https://github.com/dotnet/roslyn/issues/53969")]
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
[InlineData(
"/* Before */ i [||]= i + 1; /* After */",
"/* Before */ i++; /* After */")]
[InlineData(
"M( /* Before */ i [||]= i + 1 /* After */ );",
"M( /* Before */ ++i /* After */ );")]
[InlineData(
"M( /* Before */ i [||]= i - 1 /* After */ );",
"M( /* Before */ --i /* After */ );")]
public async Task TestTriviaPreserved(string before, string after)
{
await TestInRegularAndScript1Async(
@$"public class C
{{
int M(int i)
{{
{before}
}}
}}",
@$"public class C
{{
int M(int i)
{{
{after}
}}
}}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ protected AbstractUseCompoundAssignmentCodeFixProvider(
protected abstract SyntaxToken Token(TSyntaxKind kind);
protected abstract TAssignmentSyntax Assignment(
TSyntaxKind assignmentOpKind, TExpressionSyntax left, SyntaxToken syntaxToken, TExpressionSyntax right);
protected abstract TExpressionSyntax Increment(TExpressionSyntax left);
protected abstract TExpressionSyntax Decrement(TExpressionSyntax left);
protected abstract TExpressionSyntax Increment(TExpressionSyntax left, bool postfix);
protected abstract TExpressionSyntax Decrement(TExpressionSyntax left, bool postfix);

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
Expand All @@ -68,8 +68,11 @@ protected override Task FixAllAsync(
var assignment = diagnostic.AdditionalLocations[0].FindNode(getInnermostNodeForTie: true, cancellationToken);

editor.ReplaceNode(assignment,
(currentAssignment, generator) =>
(current, generator) =>
{
if (current is not TAssignmentSyntax currentAssignment)
return current;

syntaxFacts.GetPartsOfAssignmentExpressionOrStatement(currentAssignment,
out var leftOfAssign, out var equalsToken, out var rightOfAssign);

Expand All @@ -80,14 +83,10 @@ protected override Task FixAllAsync(
out _, out var opToken, out var rightExpr);

if (diagnostic.Properties.ContainsKey(UseCompoundAssignmentUtilities.Increment))
{
return Increment((TExpressionSyntax)leftOfAssign);
}
return Increment((TExpressionSyntax)leftOfAssign, PreferPostfix(syntaxFacts, currentAssignment)).WithTriviaFrom(currentAssignment);

if (diagnostic.Properties.ContainsKey(UseCompoundAssignmentUtilities.Decrement))
{
return Decrement((TExpressionSyntax)leftOfAssign);
}
return Decrement((TExpressionSyntax)leftOfAssign, PreferPostfix(syntaxFacts, currentAssignment)).WithTriviaFrom(currentAssignment);

var assignmentOpKind = _binaryToAssignmentMap[syntaxKinds.Convert<TSyntaxKind>(rightOfAssign.RawKind)];
var compoundOperator = Token(_assignmentToTokenMap[assignmentOpKind]);
Expand All @@ -102,6 +101,17 @@ protected override Task FixAllAsync(
return Task.CompletedTask;
}

protected virtual bool PreferPostfix(ISyntaxFactsService syntaxFacts, TAssignmentSyntax currentAssignment)
{
// If we have `x = x + 1;` on it's own, then we prefer `x++` as idiomatic.
if (syntaxFacts.IsSimpleAssignmentStatement(currentAssignment.Parent))
return true;

// In any other circumstance, the value of the assignment might be read, so we need to transform to
// ++x to ensure that we preserve semantics.
return false;
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseCompoundAssignment
Return SyntaxFactory.AssignmentStatement(assignmentOpKind, left, syntaxToken, right)
End Function

Protected Overrides Function Increment(left As ExpressionSyntax) As ExpressionSyntax
Protected Overrides Function Increment(left As ExpressionSyntax, postfix As Boolean) As ExpressionSyntax
Throw ExceptionUtilities.Unreachable
End Function

Protected Overrides Function Decrement(left As ExpressionSyntax) As ExpressionSyntax
Protected Overrides Function Decrement(left As ExpressionSyntax, postfix As Boolean) As ExpressionSyntax
Throw ExceptionUtilities.Unreachable
End Function
End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static CSharpGeneratorDriver Create(params ISourceGenerator[] generators)
/// <param name="incrementalGenerators">The incremental generators to create this driver with</param>
/// <returns>A new <see cref="CSharpGeneratorDriver"/> instance.</returns>
public static CSharpGeneratorDriver Create(params IIncrementalGenerator[] incrementalGenerators)
=> Create(incrementalGenerators.Select(WrapGenerator), additionalTexts: null);
=> Create(incrementalGenerators.Select(GeneratorExtensions.AsSourceGenerator), additionalTexts: null);

/// <summary>
/// Creates a new instance of <see cref="CSharpGeneratorDriver"/> with the specified <see cref="ISourceGenerator"/>s and the provided options or default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void TestLoadGenerators()
{
AnalyzerFileReference reference = CreateAnalyzerFileReference(Assembly.GetExecutingAssembly().Location);
var generators = reference.GetGeneratorsForAllLanguages();
var typeNames = generators.Select(g => GeneratorDriver.GetGeneratorType(g).FullName);
var typeNames = generators.Select(g => g.GetGeneratorType().FullName);

AssertEx.SetEqual(new[]
{
Expand Down Expand Up @@ -291,7 +291,7 @@ public void TestLoadCSharpGenerators()
AnalyzerFileReference reference = CreateAnalyzerFileReference(Assembly.GetExecutingAssembly().Location);
var generators = reference.GetGenerators(LanguageNames.CSharp);

var typeNames = generators.Select(g => GeneratorDriver.GetGeneratorType(g).FullName);
var typeNames = generators.Select(g => g.GetGeneratorType().FullName);
AssertEx.SetEqual(new[]
{
"Microsoft.CodeAnalysis.UnitTests.AnalyzerFileReferenceTests+TestGenerator",
Expand All @@ -313,7 +313,7 @@ public void TestLoadVisualBasicGenerators()
AnalyzerFileReference reference = CreateAnalyzerFileReference(Assembly.GetExecutingAssembly().Location);
var generators = reference.GetGenerators(LanguageNames.VisualBasic);

var typeNames = generators.Select(g => GeneratorDriver.GetGeneratorType(g).FullName);
var typeNames = generators.Select(g => g.GetGeneratorType().FullName);
AssertEx.SetEqual(new[]
{
"Microsoft.CodeAnalysis.UnitTests.VisualBasicOnlyGenerator",
Expand Down Expand Up @@ -436,7 +436,7 @@ public void TestLoadedGeneratorOrderIsDeterministic()
{
AnalyzerFileReference reference = CreateAnalyzerFileReference(Assembly.GetExecutingAssembly().Location);

var csharpGenerators = reference.GetGenerators(LanguageNames.CSharp).Select(g => GeneratorDriver.GetGeneratorType(g).FullName).ToArray();
var csharpGenerators = reference.GetGenerators(LanguageNames.CSharp).Select(g => g.GetGeneratorType().FullName).ToArray();
Assert.Equal(10, csharpGenerators.Length);
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.AnalyzerFileReferenceTests+SomeType+NestedGenerator", csharpGenerators[0]);
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.AnalyzerFileReferenceTests+TestGenerator", csharpGenerators[1]);
Expand All @@ -449,7 +449,7 @@ public void TestLoadedGeneratorOrderIsDeterministic()
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.TestSourceAndIncrementalGenerator", csharpGenerators[8]);
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.VisualBasicAndCSharpGenerator", csharpGenerators[9]);

var vbGenerators = reference.GetGenerators(LanguageNames.VisualBasic).Select(g => GeneratorDriver.GetGeneratorType(g).FullName).ToArray();
var vbGenerators = reference.GetGenerators(LanguageNames.VisualBasic).Select(g => g.GetGeneratorType().FullName).ToArray();
Assert.Equal(5, vbGenerators.Length);
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.CSharpAndVisualBasicGenerator", vbGenerators[0]);
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.TestIncrementalGenerator", vbGenerators[1]);
Expand All @@ -458,7 +458,7 @@ public void TestLoadedGeneratorOrderIsDeterministic()
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.VisualBasicOnlyGenerator", vbGenerators[4]);

// generators load in language order (C#, F#, VB), and *do not* include duplicates
var allGenerators = reference.GetGeneratorsForAllLanguages().Select(g => GeneratorDriver.GetGeneratorType(g).FullName).ToArray();
var allGenerators = reference.GetGeneratorsForAllLanguages().Select(g => g.GetGeneratorType().FullName).ToArray();
Assert.Equal(12, allGenerators.Length);
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.AnalyzerFileReferenceTests+SomeType+NestedGenerator", allGenerators[0]);
Assert.Equal("Microsoft.CodeAnalysis.UnitTests.AnalyzerFileReferenceTests+TestGenerator", allGenerators[1]);
Expand Down
5 changes: 3 additions & 2 deletions src/Compilers/Core/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Microsoft.CodeAnalysis.Emit.EmitDifferenceResult.UpdatedMethods.get -> System.Co
Microsoft.CodeAnalysis.Emit.EmitDifferenceResult.UpdatedTypes.get -> System.Collections.Immutable.ImmutableArray<System.Reflection.Metadata.TypeDefinitionHandle>
Microsoft.CodeAnalysis.GeneratorAttribute.GeneratorAttribute(string! firstLanguage, params string![]! additionalLanguages) -> void
Microsoft.CodeAnalysis.GeneratorAttribute.Languages.get -> string![]!
Microsoft.CodeAnalysis.GeneratorExtensions
Microsoft.CodeAnalysis.IFieldSymbol.IsExplicitlyNamedTupleElement.get -> bool
Microsoft.CodeAnalysis.GeneratorExecutionContext.SyntaxContextReceiver.get -> Microsoft.CodeAnalysis.ISyntaxContextReceiver?
Microsoft.CodeAnalysis.GeneratorInitializationContext.RegisterForSyntaxNotifications(Microsoft.CodeAnalysis.SyntaxContextReceiverCreator! receiverCreator) -> void
Expand Down Expand Up @@ -86,10 +87,10 @@ override Microsoft.CodeAnalysis.Operations.OperationWalker<TArgument>.DefaultVis
override Microsoft.CodeAnalysis.Operations.OperationWalker<TArgument>.Visit(Microsoft.CodeAnalysis.IOperation? operation, TArgument argument) -> object?
static Microsoft.CodeAnalysis.FileLinePositionSpan.operator !=(Microsoft.CodeAnalysis.FileLinePositionSpan left, Microsoft.CodeAnalysis.FileLinePositionSpan right) -> bool
static Microsoft.CodeAnalysis.FileLinePositionSpan.operator ==(Microsoft.CodeAnalysis.FileLinePositionSpan left, Microsoft.CodeAnalysis.FileLinePositionSpan right) -> bool
static Microsoft.CodeAnalysis.GeneratorExtensions.AsSourceGenerator(this Microsoft.CodeAnalysis.IIncrementalGenerator! incrementalGenerator) -> Microsoft.CodeAnalysis.ISourceGenerator!
static Microsoft.CodeAnalysis.GeneratorExtensions.GetGeneratorType(this Microsoft.CodeAnalysis.ISourceGenerator! generator) -> System.Type!
static Microsoft.CodeAnalysis.LineMapping.operator !=(Microsoft.CodeAnalysis.LineMapping left, Microsoft.CodeAnalysis.LineMapping right) -> bool
static Microsoft.CodeAnalysis.LineMapping.operator ==(Microsoft.CodeAnalysis.LineMapping left, Microsoft.CodeAnalysis.LineMapping right) -> bool
static Microsoft.CodeAnalysis.GeneratorDriver.GetGeneratorType(Microsoft.CodeAnalysis.ISourceGenerator! generator) -> System.Type!
static Microsoft.CodeAnalysis.GeneratorDriver.WrapGenerator(Microsoft.CodeAnalysis.IIncrementalGenerator! incrementalGenerator) -> Microsoft.CodeAnalysis.ISourceGenerator!
static Microsoft.CodeAnalysis.IncrementalValueProviderExtensions.Collect<TSource>(this Microsoft.CodeAnalysis.IncrementalValuesProvider<TSource> source) -> Microsoft.CodeAnalysis.IncrementalValueProvider<System.Collections.Immutable.ImmutableArray<TSource>>
static Microsoft.CodeAnalysis.IncrementalValueProviderExtensions.Combine<TLeft, TRight>(this Microsoft.CodeAnalysis.IncrementalValueProvider<TLeft> provider1, Microsoft.CodeAnalysis.IncrementalValueProvider<TRight> provider2) -> Microsoft.CodeAnalysis.IncrementalValueProvider<(TLeft Left, TRight Right)>
static Microsoft.CodeAnalysis.IncrementalValueProviderExtensions.Combine<TLeft, TRight>(this Microsoft.CodeAnalysis.IncrementalValuesProvider<TLeft> provider1, Microsoft.CodeAnalysis.IncrementalValueProvider<TRight> provider2) -> Microsoft.CodeAnalysis.IncrementalValuesProvider<(TLeft Left, TRight Right)>
Expand Down
Loading