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

Fix reference finding for tuple fields and anonymous type properties #69804

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
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 @@ -1791,8 +1791,8 @@ class C
{
C M(C y, C z)
{
{{fix}} = M2();
C x;
{{fix}} = M2();
(x, y) = (y ?? z, z);
return x;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Imports Microsoft.CodeAnalysis.Remote.Testing
Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
<Trait(Traits.Feature, Traits.Features.FindReferences)>
Partial Public Class FindReferencesTests
#Region "Visual Basic"
<WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/542553")>
<WpfTheory, CombinatorialData>
Public Async Function TestAnonymousType1(kind As TestKind, host As TestHost) As Task
Expand Down Expand Up @@ -165,5 +166,133 @@ End Class
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
#End Region

#Region "C#"
<WorkItem("https://github.com/dotnet/roslyn/issues/20115")>
<WpfTheory, CombinatorialData>
Public Async Function TestAnonymousTypesCSharp_SimpleExpressionHoverNameImplication(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
<![CDATA[
class C
{
void M(string {|Definition:a|})
{
int length = [|a|].Length;
var r = new { [|{|Definition:a|}|], Length = 1 };
r = new { $$[|a|], [|a|].Length };
Copy link
Member

Choose a reason for hiding this comment

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

add an example where there is a named-arg for 'a'. like new { a = "", ...}

Copy link
Member

Choose a reason for hiding this comment

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

add test where the $$ is on the defintion. add test where $$ is on the a in new { a = "", ...

r = new { [|a|] = string.Empty, Length = [|a|].Length };
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/20115")>
<WpfTheory, CombinatorialData>
Public Async Function TestAnonymousTypesCSharp_SimpleExpressionHoverParameterDeclaration(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
<![CDATA[
class C
{
void M(string $${|Definition:a|})
{
int length = [|a|].Length;
var r = new { [|{|Definition:a|}|], Length = 1 };
r = new { [|a|], [|a|].Length };
r = new { [|a|] = string.Empty, Length = [|a|].Length };
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/20115")>
<WpfTheory, CombinatorialData>
Public Async Function TestAnonymousTypesCSharp_SimpleExpressionHoverAnonymousTypeProperty(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
<![CDATA[
class C
{
void M(string a)
{
int length = a.Length;
var r = new { a, Length = 1 };
r = new { a, a.Length };
r = new { $$[|{|Definition:a|}|] = string.Empty, Length = a.Length };
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/20115")>
<WpfTheory, CombinatorialData>
Public Async Function TestAnonymousTypesCSharp_PropertyAccessHoverAccessedProperty(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
<![CDATA[
class C
{
void M(string a)
{
int length = a.[|Length|];
var r = new { a, [|Length|] = 1 };
r = new { a, a.$$[|{|Definition:Length|}|] };
r = new { a, [|Length|] = a.[|Length|] };
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/20115")>
<WpfTheory, CombinatorialData>
Public Async Function TestAnonymousTypesCSharp_PropertyAccessHoverAnonymousTypeProperty(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
<![CDATA[
class C
{
void M(string a)
{
int length = a.Length;
var r = new { a, [|Length|] = 1 };
r = new { a, a.Length };
r = new { a, $$[|{|Definition:Length|}|] = a.Length };
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
#End Region
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,227 @@ partial class Program
]]>
</DocumentFromSourceGenerator>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestImplicitlyNamedTuples(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class Program
{
static void Main()
{
int {|Definition:x|} = 4, y = 5;
var z = ($$[|{|Definition:x|}|], y);
z = ([|x|]: 0, y: 1);
z = ([|x|]: y, y: [|x|]);
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestImplicitTupleSwitchStatement(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class Program
{
static void Main()
{
int {|Definition:x|} = 4, y = 5;
int z = 3;
switch ($$[|{|Definition:x|}|], y)
{
case (1, 0):
z += [|x|];
break;
case (1, 1):
z += [|x|];
break;
default:
break;
}
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestTupleDeconstruction(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class C
{
(int, int) M()
{
return (1, 1);
}

void M2()
{
int {|Definition:x|};
int y;

($$[|{|Definition:x|}|], y) = M();

[|x|] = 0;
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestTupleSwappedFields(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class C
{
void M(int {|Definition:left|}, int right)
{
var r = ($$[|{|Definition:left|}|], right);
r = (right, [|{|Definition:left|}|]);
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestTupleNonLocal(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class C
{
int {|Definition:Property|} { get; set; }

void M(string a)
{
var r = (a.Length, $$[|{|Definition:Property|}|]);
r = ([|Property|], [|Property|]: [|Property|]);
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestTupleExplicitNames(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class C
{
void M(int a, int b)
{
var t = ($$[|{|Definition:x|}|]: a, y: b);
t = ([|x|]: b, y: a);
b = t.[|x|];
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestTupleExplicitNamesSameAsLocals01(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class C
{
void M(int x, int y)
{
var t = ($$[|{|Definition:x|}|]: x, y: y);
t = ([|x|]: y, y: x);
Copy link
Member

Choose a reason for hiding this comment

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

test of x:x as well please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

x: x is included above, it's the definition, do you also want a test that doesn't involve the definition itself?

Copy link
Member

Choose a reason for hiding this comment

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

yes.

Copy link
Member

Choose a reason for hiding this comment

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

add test where there is also an implicit name. i.e. (x, ...)

t = ([|x|]: x, y: y);
b = t.[|x|];
}
}
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WorkItem("https://github.com/dotnet/roslyn/issues/52621")>
<WpfTheory, CombinatorialData>
Public Async Function TestTupleExplicitNamesSameAsLocals02(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferencesNetCoreApp="true">
<Document><![CDATA[
using System;

class C
{
void M(int {|Definition:x|}, int y)
{
var t = (x: $$[|x|], y: y);
t = (x: y, y: [|x|]);
t = (x: [|x|], y: y);
b = t.x;
}
}
]]>
</Document>
</Project>
</Workspace>
Copy link
Member

Choose a reason for hiding this comment

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

i'm finding the tests hard to totally understand because they don't show both implicit and explicit named-props in anonymous-types, and implicit and explicit arguments for tuples.

Await TestAPIAndFeature(input, kind, host)
End Function
Expand Down
Loading
Loading