提交 31fa3ffb 编写于 作者: E Evan Hauck

Disallow invocations of local functions with dynamic arguments

上级 40a1d1ce
......@@ -259,6 +259,11 @@ private BoundExpression BindArgListOperator(InvocationExpressionSyntax node, Dia
BoundMethodGroup methodGroup = (BoundMethodGroup)expression;
BoundExpression receiver = methodGroup.ReceiverOpt;
if ((methodGroup.LookupSymbolOpt as MethodSymbol)?.MethodKind == MethodKind.LocalFunction)
{
diagnostics.Add(ErrorCode.ERR_DynamicLocalFunctionParameter, node.Location, methodGroup.Syntax);
}
// receiver is null if we are calling a static method declared on an outer class via its simple name:
if (receiver != null)
{
......
......@@ -3409,6 +3409,15 @@ internal class CSharpResources {
}
}
/// <summary>
/// Looks up a localized string similar to Cannot invoke the local function &apos;{0}&apos; with dynamic parameters..
/// </summary>
internal static string ERR_DynamicLocalFunctionParameter {
get {
return ResourceManager.GetString("ERR_DynamicLocalFunctionParameter", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?.
/// </summary>
......@@ -7559,7 +7568,7 @@ internal class CSharpResources {
}
/// <summary>
/// Looks up a localized string similar to Cannot infer the return type of {0} due to differing return types..
/// Looks up a localized string similar to Cannot infer the return type of &apos;{0}&apos; due to differing return types..
/// </summary>
internal static string ERR_ReturnTypesDontMatch {
get {
......
......@@ -4645,6 +4645,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>An expression tree may not contain a local function or a reference to a local function</value>
</data>
<data name="ERR_ReturnTypesDontMatch" xml:space="preserve">
<value>Cannot infer the return type of {0} due to differing return types.</value>
<value>Cannot infer the return type of '{0}' due to differing return types.</value>
</data>
<data name="ERR_DynamicLocalFunctionParameter" xml:space="preserve">
<value>Cannot invoke the local function '{0}' with dynamic parameters.</value>
</data>
</root>
\ No newline at end of file
......@@ -999,7 +999,7 @@ public static Cci.TypeMemberVisibility MemberVisibility(Symbol symbol)
return Cci.TypeMemberVisibility.Public;
case Accessibility.Private:
if (symbol.ContainingType.TypeKind == TypeKind.Submission)
if (symbol.ContainingType?.TypeKind == TypeKind.Submission)
{
// top-level private member:
return Cci.TypeMemberVisibility.Public;
......
......@@ -1313,5 +1313,6 @@ internal enum ErrorCode
ERR_ConstantStringTooLong = 8095,
ERR_ExpressionTreeContainsLocalFunction = 8096,
ERR_ReturnTypesDontMatch = 8097,
ERR_DynamicLocalFunctionParameter = 8098,
}
}
......@@ -2129,18 +2129,49 @@ void Local()
VerifyOutputInMain(source, "2 Program", "System");
}
[Fact(Skip = "Dynamic local function arguments not supported yet")]
[Fact]
public void DynamicArgument()
{
var source = @"
void Local(int x)
using System;
class Program
{
Console.Write(x);
static void Main()
{
void Local(int x)
{
Console.Write(x);
}
dynamic val = 2;
Local(val);
}
}
dynamic val = 2;
Local(val);
";
VerifyOutputInMain(source, "2", "System");
VerifyDiagnostics(source,
// (12,9): error CS8098: Cannot invoke the local function 'Local' with dynamic parameters.
// Local(val);
Diagnostic(ErrorCode.ERR_DynamicLocalFunctionParameter, "Local(val)").WithArguments("Local").WithLocation(12, 9)
);
}
[Fact]
public void DynamicParameter()
{
var source = @"
using System;
class Program
{
static void Main()
{
void Local(dynamic x)
{
Console.Write(x);
}
Local(2);
}
}
";
VerifyOutput(source, "2");
}
[Fact]
......@@ -2169,6 +2200,27 @@ var RetDyn()
VerifyOutputInMain(source, "2", "System");
}
[Fact]
public void DynamicDelegate()
{
var source = @"
using System;
class Program
{
static void Main()
{
dynamic Local(dynamic x)
{
return x;
}
dynamic local = (Func<dynamic, dynamic>)Local;
Console.Write(local(2));
}
}
";
VerifyOutput(source, "2");
}
[Fact]
public void Nameof()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册