Skip to content

Ensure local function invocations and method references always have null receivers #56395

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -103,7 +103,14 @@ private IVariableDeclaratorOperation CreateVariableDeclaratorInternal(BoundLocal
}

// Static members cannot have an implicit this receiver
if (symbol != null && symbol.IsStatic && instance.WasCompilerGenerated && instance.Kind == BoundKind.ThisReference)
if (symbol?.IsStatic == true && instance.WasCompilerGenerated && instance.Kind == BoundKind.ThisReference)
{
return null;
}

// Local functions are not invoked on receivers from a language point of view, and thus we do not expose
// a receiver for them.
if (symbol is MethodSymbol { MethodKind: MethodKind.LocalFunction })
Copy link
Contributor

@AlekseyTs AlekseyTs Sep 16, 2021

Choose a reason for hiding this comment

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

if (symbol is MethodSymbol { MethodKind: MethodKind.LocalFunction })

Would it be better to adjust binding so that it wouldn't add a receiver? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we are doing this for regular static methods too

{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
Expand Down Expand Up @@ -1312,5 +1313,81 @@ static void M1(object o1, object o2, object o3) { }
";
VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>(source, expectedFlowGraph, expectedDiagnostics);
}

[Theory, WorkItem(51715, "https://github.com/dotnet/roslyn/issues/51715")]
[InlineData("static")]
[InlineData("")]
public void Invocation_LocalFunction_01(string modifier)
Copy link
Contributor

Choose a reason for hiding this comment

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

Invocation_LocalFunction_01

It looks like all the tests are testing scenarios within an implicit static method. Consider testing inside a regular instance method and, perhaps, a regular static method as well.

{
var code = @"
using System;

/*<bind>*/localFunction()/*</bind>*/;

" + modifier + @" void localFunction() {}
";

var expectedDiagnostics = DiagnosticDescription.None;
var expectedTree = @"
IInvocationOperation (void localFunction()) (OperationKind.Invocation, Type: System.Void) (Syntax: 'localFunction()')
Instance Receiver:
null
Arguments(0)
";

VerifyOperationTreeAndDiagnosticsForTest<InvocationExpressionSyntax>(code, expectedTree, expectedDiagnostics);
}

[Fact, WorkItem(51715, "https://github.com/dotnet/roslyn/issues/51715")]
public void Invocation_LocalFunction_02()
{
var code = @"
using System;

int localVariable = 1;
/*<bind>*/localFunction()/*</bind>*/;

int localFunction() => localVariable;
";

var expectedDiagnostics = DiagnosticDescription.None;
var expectedTree = @"
IInvocationOperation (System.Int32 localFunction()) (OperationKind.Invocation, Type: System.Int32) (Syntax: 'localFunction()')
Instance Receiver:
null
Arguments(0)
";

VerifyOperationTreeAndDiagnosticsForTest<InvocationExpressionSyntax>(code, expectedTree, expectedDiagnostics);
}

[Fact, WorkItem(51715, "https://github.com/dotnet/roslyn/issues/51715")]
public void Invocation_LocalFunction_03()
{
var code = @"
using System;

int localVariable = 1;
/*<bind>*/localFunction()/*</bind>*/;

static int localFunction() => localVariable;
";

var expectedDiagnostics = new DiagnosticDescription[]
{
// (7,31): error CS8421: A static local function cannot contain a reference to 'localVariable'.
// static int localFunction() => localVariable;
Diagnostic(ErrorCode.ERR_StaticLocalFunctionCannotCaptureVariable, "localVariable").WithArguments("localVariable").WithLocation(7, 31)
};

var expectedTree = @"
IInvocationOperation (System.Int32 localFunction()) (OperationKind.Invocation, Type: System.Int32) (Syntax: 'localFunction()')
Instance Receiver:
null
Arguments(0)
";

VerifyOperationTreeAndDiagnosticsForTest<InvocationExpressionSyntax>(code, expectedTree, expectedDiagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,89 @@ void M(C c1, C c2, System.Func<int> m1, System.Func<int> m2)

VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>(source, expectedFlowGraph, expectedDiagnostics);
}

[Fact, WorkItem(51715, "https://github.com/dotnet/roslyn/issues/51715")]
public void MethodReference_LocalFunction_01()
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as for the invocation tests.

{
var code = @"
using System;
Action a = /*<bind>*/localFunction/*</bind>*/;

void localFunction() {}
";

var expectedDiagnostics = DiagnosticDescription.None;
var expectedTree = @"
IMethodReferenceOperation: void localFunction() (OperationKind.MethodReference, Type: null) (Syntax: 'localFunction')
Instance Receiver:
null";

VerifyOperationTreeAndDiagnosticsForTest<IdentifierNameSyntax>(code, expectedTree, expectedDiagnostics);
}

[Fact, WorkItem(51715, "https://github.com/dotnet/roslyn/issues/51715")]
public void MethodReference_LocalFunction_02()
{
var code = @"
using System;
Action a = /*<bind>*/localFunction/*</bind>*/;

static void localFunction() {}
";

var expectedDiagnostics = DiagnosticDescription.None;
var expectedTree = @"
IMethodReferenceOperation: void localFunction() (Static) (OperationKind.MethodReference, Type: null) (Syntax: 'localFunction')
Instance Receiver:
null";

VerifyOperationTreeAndDiagnosticsForTest<IdentifierNameSyntax>(code, expectedTree, expectedDiagnostics);
}

[Fact, WorkItem(51715, "https://github.com/dotnet/roslyn/issues/51715")]
public void MethodReference_LocalFunction_03()
{
var code = @"
using System;
int local = 1;
Func<int> a = /*<bind>*/localFunction/*</bind>*/;

int localFunction() => local;
";

var expectedDiagnostics = DiagnosticDescription.None;
var expectedTree = @"
IMethodReferenceOperation: System.Int32 localFunction() (OperationKind.MethodReference, Type: null) (Syntax: 'localFunction')
Instance Receiver:
null";

VerifyOperationTreeAndDiagnosticsForTest<IdentifierNameSyntax>(code, expectedTree, expectedDiagnostics);
}

[Fact, WorkItem(51715, "https://github.com/dotnet/roslyn/issues/51715")]
public void MethodReference_LocalFunction_04()
{
var code = @"
using System;
int local = 1;
Func<int> a = /*<bind>*/localFunction/*</bind>*/;

static int localFunction() => local;
";

var expectedDiagnostics = new DiagnosticDescription[]
{
// (6,31): error CS8421: A static local function cannot contain a reference to 'local'.
// static int localFunction() => local;
Diagnostic(ErrorCode.ERR_StaticLocalFunctionCannotCaptureVariable, "local").WithArguments("local").WithLocation(6, 31)
};

var expectedTree = @"
IMethodReferenceOperation: System.Int32 localFunction() (Static) (OperationKind.MethodReference, Type: null) (Syntax: 'localFunction')
Instance Receiver:
null";

VerifyOperationTreeAndDiagnosticsForTest<IdentifierNameSyntax>(code, expectedTree, expectedDiagnostics);
}
}
}