-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
SpillSequenceSpiller.Spill - ensure sequences under ref assignments are spilled. #58657
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3582,5 +3582,210 @@ class C | |
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c").WithLocation(10, 5) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void PatternIndexArrayAndAwait_01() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1(int[] arr) | ||
{ | ||
arr[^1] = await System.Threading.Tasks.Task.FromResult(0); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new[] { 123 }; | ||
System.Console.WriteLine(arr[0]); | ||
await M1(arr); | ||
System.Console.WriteLine(arr[0]); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: | ||
@" | ||
123 | ||
0 | ||
").VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PatternIndexArrayAndAwait_02() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1(int[] arr) | ||
{ | ||
(arr[^1], arr[0]) = (123, await System.Threading.Tasks.Task.FromResult(124)); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new int[2]; | ||
await M1(arr); | ||
System.Console.WriteLine(arr[0]); | ||
System.Console.WriteLine(arr[1]); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: | ||
@" | ||
124 | ||
123 | ||
").VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PatternIndexArrayAndAwait_03() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1((int x, int y)[] arr) | ||
{ | ||
arr[^1].x = await System.Threading.Tasks.Task.FromResult(124); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new (int x, int y)[1]; | ||
await M1(arr); | ||
System.Console.WriteLine(arr[0].x); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: | ||
@" | ||
124 | ||
").VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
[WorkItem(58569, "https://github.com/dotnet/roslyn/issues/58569")] | ||
public void PatternIndexArrayAndAwait_04() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1((int x, int y)[] arr) | ||
{ | ||
(arr[^1].x, arr[0].y) = (123, await System.Threading.Tasks.Task.FromResult(124)); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new (int x, int y)[2]; | ||
await M1(arr); | ||
System.Console.WriteLine(arr[0].y); | ||
System.Console.WriteLine(arr[1].x); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: | ||
@" | ||
124 | ||
123 | ||
").VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
[WorkItem(58569, "https://github.com/dotnet/roslyn/issues/58569")] | ||
public void PatternIndexArrayAndAwait_05() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1((int x, int y)[] arr) | ||
{ | ||
arr[^1].x += await System.Threading.Tasks.Task.FromResult(124); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new (int x, int y)[] { (1, 2) }; | ||
await M1(arr); | ||
System.Console.WriteLine(arr[0].x); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: | ||
@" | ||
125 | ||
").VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PatternIndexArrayAndAwait_06() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1(int[] arr) | ||
{ | ||
arr[^1] += await System.Threading.Tasks.Task.FromResult(124); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new int[] { 1 }; | ||
await M1(arr); | ||
System.Console.WriteLine(arr[0]); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: | ||
@" | ||
125 | ||
").VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PatternIndexArrayAndAwait_07() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1(int[][] arr) | ||
{ | ||
arr[^1][0] += await System.Threading.Tasks.Task.FromResult(124); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new[] { new[] { 1 } }; | ||
await M1(arr); | ||
System.Console.WriteLine(arr[0][0]); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: | ||
@" | ||
125 | ||
").VerifyDiagnostics(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth testing with some slicing as well: Something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'll add a test. However, a scenario like that isn't interesting for the specific problem I am trying to address in this PR. It doesn't matter how array came to the existence. It is a reference type and it is going to be captured by value. It is the array element (the actual target of the assignment) captured by reference. That is what needs the right spilling. |
||
|
||
[Fact] | ||
public void PatternIndexArrayAndAwait_08() | ||
{ | ||
var src = @" | ||
class C | ||
{ | ||
static async System.Threading.Tasks.Task M1((int x, int y)[] arr) | ||
{ | ||
(arr[1..][^1].x, arr[1..][0].y) = (123, await System.Threading.Tasks.Task.FromResult(124)); | ||
} | ||
|
||
static async System.Threading.Tasks.Task Main() | ||
{ | ||
var arr = new (int x, int y)[5]; | ||
await M1(arr); | ||
System.Console.WriteLine(""Done""); | ||
} | ||
} | ||
"; | ||
CompileAndVerifyWithIndexAndRange(src, expectedOutput: "Done").VerifyDiagnostics(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What drives the need to optimize? Do we think this is especially common? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.