Skip to content

Commit 4ff0763

Browse files
[automated] Merge branch 'main' => 'main-vs-deps' (#79433)
I detected changes in the main branch which have not been merged yet to main-vs-deps. I'm a robot and am configured to help you automatically keep main-vs-deps up to date, so I've opened this PR. This PR merges commits made on main by the following committers: * CyrusNajmabadi * jcouv * AlekseyTs * 333fred ## Instructions for merging from UI This PR will not be auto-merged. When pull request checks pass, complete this PR by creating a merge commit, *not* a squash or rebase commit. <img alt="merge button instructions" src="https://i.imgur.com/GepcNJV.png" width="300" /> If this repo does not allow creating merge commits from the GitHub UI, use command line instructions. ## Instructions for merging via command line Run these commands to merge this pull request from the command line. ``` sh git fetch git checkout main git pull --ff-only git checkout main-vs-deps git pull --ff-only git merge --no-ff main # If there are merge conflicts, resolve them and then run git merge --continue to complete the merge # Pushing the changes to the PR branch will re-trigger PR validation. git push https://github.com/dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` <details> <summary>or if you are using SSH</summary> ``` git push [email protected]:dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` </details> After PR checks are complete push the branch ``` git push ``` ## Instructions for resolving conflicts :warning: If there are merge conflicts, you will need to resolve them manually before merging. You can do this [using GitHub][resolve-github] or using the [command line][resolve-cli]. [resolve-github]: https://help.github.com/articles/resolving-a-merge-conflict-on-github/ [resolve-cli]: https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/ ## Instructions for updating this pull request Contributors to this repo have permission update this pull request by pushing to the branch 'merge/main-to-main-vs-deps'. This can be done to resolve conflicts or make other changes to this pull request before it is merged. The provided examples assume that the remote is named 'origin'. If you have a different remote name, please replace 'origin' with the name of your remote. ``` git fetch git checkout -b merge/main-to-main-vs-deps origin/main-vs-deps git pull https://github.com/dotnet/roslyn merge/main-to-main-vs-deps (make changes) git commit -m "Updated PR with my changes" git push https://github.com/dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` <details> <summary>or if you are using SSH</summary> ``` git fetch git checkout -b merge/main-to-main-vs-deps origin/main-vs-deps git pull [email protected]:dotnet/roslyn merge/main-to-main-vs-deps (make changes) git commit -m "Updated PR with my changes" git push [email protected]:dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` </details> Contact .NET Core Engineering (dotnet/dnceng) if you have questions or issues. Also, if this PR was generated incorrectly, help us fix it. See https://github.com/dotnet/arcade/blob/main/.github/workflows/scripts/inter-branch-merge.ps1.
2 parents 7e00ea2 + 24ab192 commit 4ff0763

File tree

105 files changed

+19301
-8382
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+19301
-8382
lines changed

docs/contributing/Compiler Test Plan.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ This document provides guidance for thinking about language interactions and tes
5555
- Compiler-recognized attributes should not have any effect in earlier LangVersions,
5656
except a LangVersion error should be reported when functionality depending on the attribute is used
5757
(for example, InlineArray conversion to Span).
58-
- Generics (type arguments, variance, constraints including `class`, `struct`, `new()`, `unmanaged`, `notnull`, types and interfaces with nullability)
58+
- Generics (type arguments, variance, constraints including `class`, `struct`, `new()`, `unmanaged`, `notnull`, `allows ref struct`, types and interfaces with nullability)
5959
- Default and constant values
6060
- Partial classes
6161
- Literals

src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.CodeAnalysis.Operations;
1515
using Microsoft.CodeAnalysis.PooledObjects;
1616
using Microsoft.CodeAnalysis.Shared.Extensions;
17-
using Roslyn.Utilities;
1817

1918
namespace Microsoft.CodeAnalysis.CSharp.MakeStructMemberReadOnly;
2019

@@ -331,6 +330,10 @@ private static bool OperationPotentiallyMutatesThis(
331330

332331
if (operation is IInlineArrayAccessOperation)
333332
{
333+
// to determine if this is safe to make into a ReadOnlySpan or not.
334+
if (operation.Type.IsSpan())
335+
return true;
336+
334337
// If we're writing into an inline-array off of 'this'. Then we can't make this `readonly`.
335338
if (CSharpSemanticFacts.Instance.IsWrittenTo(semanticModel, operation.Syntax, cancellationToken))
336339
return true;

src/Analyzers/CSharp/Analyzers/UseExpressionBody/Helpers/UseExpressionBodyForPropertiesHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
#nullable disable
66

77
using System;
8+
using System.Linq;
89
using System.Threading;
910
using Microsoft.CodeAnalysis.CodeStyle;
1011
using Microsoft.CodeAnalysis.CSharp.CodeGeneration;
1112
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
13+
using Microsoft.CodeAnalysis.CSharp.Extensions;
1214
using Microsoft.CodeAnalysis.CSharp.Syntax;
1315
using Microsoft.CodeAnalysis.Diagnostics;
16+
using Microsoft.CodeAnalysis.Shared.Extensions;
1417

1518
namespace Microsoft.CodeAnalysis.CSharp.UseExpressionBody;
1619

src/Analyzers/CSharp/Analyzers/UseExpressionBody/Helpers/UseExpressionBodyHelper`1.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,48 @@ public TDeclaration Update(SemanticModel semanticModel, TDeclaration declaration
242242
.Concat(declaration.GetTrailingTrivia());
243243
semicolonToken = semicolonToken.WithTrailingTrivia(trailingTrivia);
244244

245-
return WithSemicolonToken(
246-
WithExpressionBody(
247-
WithBody(declaration, body: null),
248-
expressionBody),
249-
semicolonToken);
245+
var updateDeclaration = WithSemicolonToken(
246+
WithExpressionBody(
247+
WithBody(declaration, body: null),
248+
expressionBody),
249+
semicolonToken);
250+
251+
return TransferTrailingCommentsToAfterExpressionBody(updateDeclaration);
250252
}
251253
else
252254
{
253255
return WithSemicolonToken(
254-
WithExpressionBody(
255-
WithGenerateBody(semanticModel, declaration),
256-
expressionBody: null),
257-
default);
256+
WithExpressionBody(
257+
WithGenerateBody(semanticModel, declaration),
258+
expressionBody: null),
259+
default);
258260
}
259261
}
260262

263+
private TDeclaration TransferTrailingCommentsToAfterExpressionBody(TDeclaration declaration)
264+
{
265+
var expressionBody = GetExpressionBody(declaration);
266+
267+
// Don't need to transfer if we don't have an expression body, or it already has leading trivia (like comments).
268+
// Those will already be formatted and placed properly. We only want to transfer comments that were conceptually
269+
// at the end of the property/method/etc. header before and should stay that way after becoming single line.
270+
if (expressionBody == null)
271+
return declaration;
272+
273+
if (expressionBody.GetLeadingTrivia().Any(t => t.IsRegularComment()))
274+
return declaration;
275+
276+
var previousToken = expressionBody.GetFirstToken().GetPreviousToken();
277+
var trailingTrivia = previousToken.TrailingTrivia;
278+
var lastComment = trailingTrivia.LastOrDefault(t => t.IsRegularComment());
279+
if (lastComment == default)
280+
return declaration;
281+
282+
return declaration
283+
.ReplaceToken(previousToken, previousToken.WithTrailingTrivia(Space))
284+
.WithTrailingTrivia(trailingTrivia.Take(trailingTrivia.IndexOf(lastComment) + 1).Concat(declaration.GetTrailingTrivia()));
285+
}
286+
261287
protected abstract BlockSyntax? GetBody(TDeclaration declaration);
262288

263289
protected abstract ArrowExpressionClauseSyntax? GetExpressionBody(TDeclaration declaration);

src/Analyzers/CSharp/Tests/GenerateMethod/GenerateConversionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class C
125125
public static implicit operator C(int v) => throw new NotImplementedException();
126126
}
127127
""",
128-
options: Option(CSharpCodeStyleOptions.PreferExpressionBodiedOperators, CSharpCodeStyleOptions.WhenPossibleWithSilentEnforcement));
128+
options: Option(CSharpCodeStyleOptions.PreferExpressionBodiedOperators, CSharpCodeStyleOptions.WhenPossibleWithSilentEnforcement));
129129

130130
[Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/774321")]
131131
public Task TestGenerateImplicitConversionAwaitExpression()

src/Analyzers/CSharp/Tests/MakeLocalFunctionStatic/MakeLocalFunctionStaticTests.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -290,25 +290,25 @@ bool otherFunction()
290290
}
291291
}
292292
""",
293-
$$"""
294-
using System;
293+
$$"""
294+
using System;
295295
296-
class C
297-
{
298-
void M()
299-
{
300-
bool otherFunction()
301-
{
302-
return true;
303-
}{{leadingTrivia}}
304-
static int fibonacci(int n)
305-
{
306-
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
307-
}
308-
}
309-
}
310-
""",
311-
parseOptions: CSharp8ParseOptions);
296+
class C
297+
{
298+
void M()
299+
{
300+
bool otherFunction()
301+
{
302+
return true;
303+
}{{leadingTrivia}}
304+
static int fibonacci(int n)
305+
{
306+
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
307+
}
308+
}
309+
}
310+
""",
311+
parseOptions: CSharp8ParseOptions);
312312

313313
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsMakeLocalFunctionStatic)]
314314
[InlineData("")]
@@ -328,19 +328,19 @@ void M()
328328
}
329329
}
330330
""",
331-
$$"""
332-
using System;
331+
$$"""
332+
using System;
333333
334-
class C
335-
{
336-
void M()
337-
{
338-
bool otherFunction() => true;{{leadingTrivia}}
339-
static int fibonacci(int n) => n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
340-
}
341-
}
342-
""",
343-
parseOptions: CSharp8ParseOptions);
334+
class C
335+
{
336+
void M()
337+
{
338+
bool otherFunction() => true;{{leadingTrivia}}
339+
static int fibonacci(int n) => n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
340+
}
341+
}
342+
""",
343+
parseOptions: CSharp8ParseOptions);
344344

345345
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsMakeLocalFunctionStatic)]
346346
[InlineData("")]
@@ -378,7 +378,7 @@ static int fibonacci(int n)
378378
}
379379
}
380380
""",
381-
parseOptions: CSharp8ParseOptions);
381+
parseOptions: CSharp8ParseOptions);
382382

383383
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsMakeLocalFunctionStatic)]
384384
[InlineData("\r\n")]
@@ -415,7 +415,7 @@ static int fibonacci(int n)
415415
}
416416
}
417417
""",
418-
parseOptions: CSharp8ParseOptions);
418+
parseOptions: CSharp8ParseOptions);
419419

420420
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeLocalFunctionStatic)]
421421
[WorkItem("https://github.com/dotnet/roslyn/issues/46858")]

src/Analyzers/CSharp/Tests/MakeStructMemberReadOnly/MakeStructMemberReadOnlyTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,4 +2345,33 @@ public int Z
23452345
LanguageVersion = LanguageVersion.CSharp12,
23462346
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
23472347
}.RunAsync();
2348+
2349+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79384")]
2350+
public Task TestSpanFromInlineArray()
2351+
=> new VerifyCS.Test
2352+
{
2353+
TestCode = """
2354+
using System.Runtime.CompilerServices;
2355+
2356+
public struct Example
2357+
{
2358+
private Buffer _buffer;
2359+
2360+
public void Set(int index, int value)
2361+
{
2362+
var bufferSpan = _buffer[..0];
2363+
2364+
bufferSpan[0] = value;
2365+
}
2366+
}
2367+
2368+
[InlineArray(1)]
2369+
public struct Buffer
2370+
{
2371+
private int _element0;
2372+
}
2373+
""",
2374+
LanguageVersion = LanguageVersion.CSharp13,
2375+
ReferenceAssemblies = ReferenceAssemblies.Net.Net90,
2376+
}.RunAsync();
23482377
}

src/Analyzers/CSharp/Tests/RemoveUnusedParametersAndValues/RemoveUnusedValueAssignmentTests.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5819,26 +5819,26 @@ int M(bool flag)
58195819
int M3() => 0;
58205820
}
58215821
""",
5822-
$$"""
5823-
class C
5824-
{
5825-
int M(bool flag)
5826-
{
5827-
int x;
5828-
if ({{condition}})
5829-
{
5830-
}
5831-
else
5832-
{
5833-
}
5822+
$$"""
5823+
class C
5824+
{
5825+
int M(bool flag)
5826+
{
5827+
int x;
5828+
if ({{condition}})
5829+
{
5830+
}
5831+
else
5832+
{
5833+
}
58345834
5835-
return x;
5836-
}
5835+
return x;
5836+
}
58375837
5838-
bool M2(out int x) { x = 0; return true; }
5839-
int M3() => 0;
5840-
}
5841-
""");
5838+
bool M2(out int x) { x = 0; return true; }
5839+
int M3() => 0;
5840+
}
5841+
""");
58425842

58435843
[Theory]
58445844
[InlineData(nameof(PreferDiscard))]

src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,32 @@ record Class
102102
}
103103
""", new TestParameters(TestOptions.RegularPreview));
104104

105+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76813")]
106+
public Task TestSingleGetterFromField_CommentBeforeField()
107+
=> TestInRegularAndScript1Async(
108+
"""
109+
class Class
110+
{
111+
// Comment to preserve
112+
[|int i|];
113+
114+
int P
115+
{
116+
get
117+
{
118+
return i;
119+
}
120+
}
121+
}
122+
""",
123+
"""
124+
class Class
125+
{
126+
// Comment to preserve
127+
int P { get; }
128+
}
129+
""");
130+
105131
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/28511")]
106132
public Task TestNullable1()
107133
=> TestMissingInRegularAndScriptAsync(
@@ -215,6 +241,39 @@ class Class
215241
struct MutableInt { public int Value; }
216242
""");
217243

244+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/76815")]
245+
[InlineData("DateTime")]
246+
[InlineData("ArraySegment<int>")]
247+
[InlineData("DateTimeOffset")]
248+
[InlineData("Guid")]
249+
[InlineData("Index")]
250+
[InlineData("Range")]
251+
[InlineData("ReadOnlyMemory<int>")]
252+
[InlineData("ReadOnlySpan<int>")]
253+
[InlineData("TimeSpan")]
254+
public Task TestWellKnownImmutableValueType1(string typeName)
255+
=> TestInRegularAndScript1Async(
256+
$$"""
257+
class Class
258+
{
259+
[|System.{{typeName}} i|];
260+
261+
System.{{typeName}} P
262+
{
263+
get
264+
{
265+
return i;
266+
}
267+
}
268+
}
269+
""",
270+
$$"""
271+
class Class
272+
{
273+
System.{{typeName}} P { get; }
274+
}
275+
""");
276+
218277
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/28511")]
219278
public Task TestMutableValueType1()
220279
=> TestMissingInRegularAndScriptAsync(

0 commit comments

Comments
 (0)