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

Make 'generate field' work better with implicit object creation #68325

Merged
merged 2 commits into from
May 25, 2023
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 @@ -11065,15 +11065,77 @@ public async Task M(out int x, int y, System.TimeSpan time, out int z, params do
public async Task TestMissingWhenGeneratingFunctionPointer()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
"""
using System;

public unsafe class Bar
{
public static ZZZ()
{
delegate*<void> i = &[|Goo|];
}
}");
public unsafe class Bar
{
public static ZZZ()
{
delegate*<void> i = &[|Goo|];
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/68322")]
public async Task TestImplicitObjectCreationExpression()
{
await TestInRegularAndScriptAsync(
"""
class Example
{
public Example(int argument) { }

void M()
{
Example e = new([|_field|]);
}
}
""",
"""
class Example
{
private int _field;

public Example(int argument) { }

void M()
{
Example e = new(_field);
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/68322")]
public async Task TestImplicitCollectionCreationExpression()
{
await TestInRegularAndScriptAsync(
"""
using System.Collections.Generic;

class Example
{
void M()
{
List<int> list = new() { [|_field|] };
}
}
""",
"""
using System.Collections.Generic;

class Example
{
private int _field;

void M()
{
List<int> list = new() { [|_field|] };
}
}
""");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private IEnumerable<TypeInferenceInfo> InferTypeInArgument(
return InferTypeInInvocationExpression(invocation, index, argument);
}

if (argument.Parent?.Parent is ObjectCreationExpressionSyntax creation)
if (argument.Parent?.Parent is BaseObjectCreationExpressionSyntax creation)
{
// new Outer(Goo());
//
Expand Down Expand Up @@ -323,7 +323,7 @@ private IEnumerable<TypeInferenceInfo> InferTypeInArgument(
if (argument.Parent.IsParentKind(SyntaxKind.ImplicitElementAccess) &&
argument.Parent.Parent.IsParentKind(SyntaxKind.SimpleAssignmentExpression) &&
argument.Parent.Parent.Parent.IsParentKind(SyntaxKind.ObjectInitializerExpression) &&
argument.Parent.Parent.Parent.Parent?.Parent is ObjectCreationExpressionSyntax objectCreation)
argument.Parent.Parent.Parent.Parent?.Parent is BaseObjectCreationExpressionSyntax objectCreation)
{
var types = GetTypes(objectCreation).Select(t => t.InferredType);

Expand Down