Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -465,6 +465,201 @@ void M()
}

(string name, int age) GetPerson() => default;
}");
}

[WorkItem(25260, "https://github.com/dotnet/roslyn/issues/25260")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestNotWithDefaultLiteralInitializer()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
void M()
{
(string name, int age) [|person|] = default;
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.

Filed #25559

Console.WriteLine(person.name + "" "" + person.age);
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestWithDefaultExpressionInitializer()
{
await TestInRegularAndScriptAsync(
@"class C
{
void M()
{
(string name, int age) [|person|] = default((string, int));
Console.WriteLine(person.name + "" "" + person.age);
}
}",
@"class C
{
void M()
{
(string name, int age) = default((string, int));
Console.WriteLine(name + "" "" + age);
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestNotWithImplicitConversionFromNonTuple()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
class Person
{
public static implicit operator (string, int)(Person person) => default;
}

void M()
{
(string name, int age) [|person|] = new Person();
Console.WriteLine(person.name + "" "" + person.age);
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestWithExplicitImplicitConversionFromNonTuple()
{
await TestInRegularAndScriptAsync(
@"class C
{
class Person
{
public static implicit operator (string, int)(Person person) => default;
}

void M()
{
(string name, int age) [|person|] = ((string, int))new Person();
Console.WriteLine(person.name + "" "" + person.age);
}
}",
@"class C
{
class Person
{
public static implicit operator (string, int)(Person person) => default;
}

void M()
{
(string name, int age) = ((string, int))new Person();
Console.WriteLine(name + "" "" + age);
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestNotWithImplicitConversionFromNonTupleInForEach()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
class Person
{
public static implicit operator (string, int)(Person person) => default;
}

void M()
{
foreach ((string name, int age) [|person|] in new Person[] { })
Console.WriteLine(person.name + "" "" + person.age);
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestWithExplicitImplicitConversionFromNonTupleInForEach()
{
await TestInRegularAndScriptAsync(
@"using System.Linq;
class C
{
class Person
{
public static implicit operator (string, int)(Person person) => default;
}

void M()
{
foreach ((string name, int age) [|person|] in new Person[] { }.Cast<(string, int)>())
Console.WriteLine(person.name + "" "" + person.age);
}
}",
@"using System.Linq;
class C
{
class Person
{
public static implicit operator (string, int)(Person person) => default;
}

void M()
{
foreach ((string name, int age) in new Person[] { }.Cast<(string, int)>())
Console.WriteLine(name + "" "" + age);
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestWithImplicitTupleConversion()
{
await TestInRegularAndScriptAsync(
@"class C
{
void M()
{
(object name, double age) [|person|] = GetPerson();
Console.WriteLine(person.name + "" "" + person.age);
}

(string name, int age) GetPerson() => default;
}",
@"class C
{
void M()
{
(object name, double age) = GetPerson();
Console.WriteLine(name + "" "" + age);
}

(string name, int age) GetPerson() => default;
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestWithImplicitTupleConversionInForEach()
{
await TestInRegularAndScriptAsync(
@"using System.Collections.Generic;
class C
{
void M()
{
foreach ((object name, double age) [|person|] in GetPeople())
Console.WriteLine(person.name + "" "" + person.age);
}

IEnumerable<(string name, int age)> GetPeople() => default;
}",
@"using System.Collections.Generic;
class C
{
void M()
{
foreach ((object name, double age) in GetPeople())
Console.WriteLine(name + "" "" + age);
}

IEnumerable<(string name, int age)> GetPeople() => default;
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ public static bool TryAnalyzeVariableDeclaration(
}

var local = (ILocalSymbol)semanticModel.GetDeclaredSymbol(declarator, cancellationToken);
var expressionType = semanticModel.GetTypeInfo(declarator.Initializer.Value, cancellationToken).Type;

return TryAnalyze(
semanticModel, local, variableDeclaration.Type,
declarator.Identifier, variableDeclaration.Parent.Parent,
out tupleType, out memberAccessExpressions, cancellationToken);
semanticModel, local, variableDeclaration.Type, declarator.Identifier, expressionType,
variableDeclaration.Parent.Parent, out tupleType, out memberAccessExpressions, cancellationToken);
}

public static bool TryAnalyzeForEachStatement(
Expand All @@ -140,9 +140,10 @@ public static bool TryAnalyzeForEachStatement(
CancellationToken cancellationToken)
{
var local = semanticModel.GetDeclaredSymbol(forEachStatement, cancellationToken);
var elementType = semanticModel.GetForEachStatementInfo(forEachStatement).ElementType;

return TryAnalyze(
semanticModel, local, forEachStatement.Type, forEachStatement.Identifier,
semanticModel, local, forEachStatement.Type, forEachStatement.Identifier, elementType,
forEachStatement, out tupleType, out memberAccessExpressions, cancellationToken);
}

Expand All @@ -151,6 +152,7 @@ private static bool TryAnalyze(
ILocalSymbol local,
TypeSyntax typeNode,
SyntaxToken identifier,
ITypeSymbol initializerType,
SyntaxNode searchScope,
out INamedTypeSymbol tupleType,
out ImmutableArray<MemberAccessExpressionSyntax> memberAccessExpressions,
Expand All @@ -170,7 +172,8 @@ private static bool TryAnalyze(
}

var type = semanticModel.GetTypeInfo(typeNode, cancellationToken).Type;
if (type == null || !type.IsTupleType)
if (type == null || !type.IsTupleType ||
initializerType == null || !initializerType.IsTupleType)
{
return false;
}
Expand Down