Skip to content

Catch deconstruction ref escape errors in invocation escape analysis - #64470

Merged
cston merged 9 commits into
dotnet:mainfrom
cston:64448
Oct 10, 2022
Merged

cston merged 9 commits into
dotnet:mainfrom
cston:64448

Conversation

@cston

@cston cston commented Oct 4, 2022 •

Copy link
Copy Markdown
Contributor

Fixes #64000

The first commit contains tests only, no compiler changes.

@ghost ghost added the Area-Compilers label Oct 4, 2022
@cston cston changed the title Catch deconstruction assignment errors in invocation escape analysis Catch deconstruction ref escape errors in invocation escape analysis Oct 4, 2022
@cston
cston force-pushed the 64448 branch 4 times, most recently from 5cef066 to 3d637b3 Compare October 5, 2022 22:53
@cston
cston marked this pull request as ready for review October 6, 2022 03:04
@cston
cston requested a review from a team as a code owner October 6, 2022 03:04
[Theory]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
public void Deconstruct_11(LanguageVersion languageVersion)

@cston cston Oct 6, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deconstruct_09, _10, _11 test var deconstruction declarations which should be addressed by #64414.

@cston

cston commented Oct 7, 2022

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-compiler, please review, thanks.

var source =
@"ref struct R1
{
public R1(ref int i) { }

@jaredpar jaredpar Oct 7, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider having the constructor look like this

public R1(Span<int> span) { }

#Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed test. The Span<int> case is covered in 4A.

static void F1()
{
int i = 1;
var r = new R1(ref i);

@jaredpar jaredpar Oct 7, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then here do the following:

var r = new R1(stackalloc int[42]);

Together those two changes will make the errors uniform through the language versions. #Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new tests include a number of Span<int> / stackalloc tests to test that the errors are uniform across language versions. For these tests, I was interested in the differences between language versions as well.

@cston cston Oct 7, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 2A and 2B (the equivalent Span<int> cases were 4A and 4B).

[Theory]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
public void Deconstruct_02B(LanguageVersion languageVersion)

@jaredpar jaredpar Oct 7, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback here. #Resolved

[Theory]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
public void Deconstruct_05(LanguageVersion languageVersion)

@jaredpar jaredpar Oct 7, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback here. #Resolved

@cston cston Oct 7, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 5 (the equivalent Span<int> case was 6).

}
ref struct R2
{
public R2(ref int i) { }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback here.

[Theory]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
public void Deconstruct_02B(LanguageVersion languageVersion)

@jaredpar jaredpar Oct 7, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could almost just use [InlineData(LanguageVersion.CSharp10, "Span<int>")] + a few more entries and just combine with the test above. #Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd considered combining with the previous test - either with a Span<int> parameter to the test method, or having two methods in the test, one for Span<int> and one for ReadOnlySpan<int> - but two separate test methods seemed easy to read.

int x2;
int i = 2;
R y2 = new R(ref i);
(x2, y2) = s;

@jaredpar jaredpar Oct 7, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(x2, y2) = s;
(x2, y2) = s; // 3

and update tests accordingly.

Again though, feel like this is just making it look like there are big differences for deconstruct between lang versions. Every one of these tests I have to approach with

Wait, why did language rules around deconstruct change between 10 and 11?

In reality they're no different at all. #Resolved

static void Deconstruct(this Span<int> s, out R2 x, out R1 y) => throw null;
}";
var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.Regular.WithLanguageVersion(languageVersion));
if (languageVersion == LanguageVersion.CSharp10)

@jaredpar jaredpar Oct 7, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren'd the diagnostics in the if and else the same here. #Resolved

@cston cston Oct 7, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter in the ERR_CallArgMixing diagnostic is different: x vs. this.

@RikkiGibson RikkiGibson self-assigned this Oct 7, 2022

Conversion conversion;
// Among other things, MakeDeconstructionConversion() will handle
// value escape analysis for the Deconstruct() method group.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this escape analysis eventually occurs through the already existing call to BindMethodGroupInvocation on the synthesized Deconstruct call.

@cston

cston commented Oct 10, 2022

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code that compiles on dotnet 6 gives compile errors on dotnet 7 preview

3 participants