提交 916060f4 编写于 作者: A Andy Gocke 提交者: GitHub

Change CallerMemberName behavior in local functions to use member name (#14551)

Local functions previously used the local function name but, as local functions
are not members, this was inappropriate.

Fixes #13268
上级 efb92297
...@@ -64,7 +64,9 @@ public MethodSymbol CurrentMethod ...@@ -64,7 +64,9 @@ public MethodSymbol CurrentMethod
set set
{ {
_currentMethod = value; _currentMethod = value;
if ((object)value != null && value.MethodKind != MethodKind.AnonymousFunction) if ((object)value != null &&
value.MethodKind != MethodKind.AnonymousFunction &&
value.MethodKind != MethodKind.LocalFunction)
{ {
_topLevelMethod = value; _topLevelMethod = value;
_currentType = value.ContainingType; _currentType = value.ContainingType;
......
...@@ -1005,6 +1005,49 @@ public static void Main() ...@@ -1005,6 +1005,49 @@ public static void Main()
CompileAndVerify(compilation, expectedOutput: expected); CompileAndVerify(compilation, expectedOutput: expected);
} }
[Fact]
public void TestCallerMemberName_LocalFunction()
{
string source = @"
using System.Runtime.CompilerServices;
using System;
class D
{
public void LocalFunctionCaller()
{
void Local()
{
void LocalNested() => Test.Log();
LocalNested();
}
Local();
}
}
class Test
{
public static int Log([CallerMemberName] string callerName = """")
{
Console.WriteLine(""name: "" + callerName);
return 1;
}
public static void Main()
{
var d = new D();
d.LocalFunctionCaller();
}
}";
var expected = @"
name: LocalFunctionCaller
";
var compilation = CreateCompilationWithMscorlib45(source, references: new MetadataReference[] { SystemRef }, options: TestOptions.ReleaseExe);
CompileAndVerify(compilation, expectedOutput: expected);
}
[Fact] [Fact]
public void TestCallerMemberName_Operator() public void TestCallerMemberName_Operator()
{ {
......
...@@ -168,7 +168,7 @@ void LocalFuncName() ...@@ -168,7 +168,7 @@ void LocalFuncName()
Console.Write(' '); Console.Write(' ');
CallerMemberName(); CallerMemberName();
"; ";
VerifyOutputInMain(source, "LocalFuncName Main", "System", "System.Runtime.CompilerServices"); VerifyOutputInMain(source, "Main Main", "System", "System.Runtime.CompilerServices");
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册