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

Check for bad dynamic arguments to local function #19784

Merged
merged 1 commit into from May 27, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs
Expand Up @@ -641,8 +641,10 @@ private static bool HasApplicableConditionalMethod(OverloadResolutionResult<Meth
Debug.Assert(queryClause == null);

var validResult = resolution.OverloadResolutionResult.ValidResult;
var args = resolution.AnalyzedArguments.Arguments.ToImmutable();

ReportBadDynamicArguments(syntax, args, diagnostics, queryClause);

var args = resolution.AnalyzedArguments.Arguments;
var localFunction = validResult.Member;
var methodResult = validResult.Result;

Expand All @@ -659,7 +661,7 @@ private static bool HasApplicableConditionalMethod(OverloadResolutionResult<Meth

var lastParamIndex = parameters.Length - 1;

for (int i = 0; i < args.Count; ++i)
for (int i = 0; i < args.Length; ++i)
{
var arg = args[i];
if (arg.HasDynamicType() &&
Expand Down
47 changes: 47 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/LocalFunctionTests.cs
Expand Up @@ -3090,5 +3090,52 @@ class Test : System.Attribute
Assert.Equal("System.Boolean b1", b1Symbol.ToTestDisplayString());
Assert.Equal(SymbolKind.Local, b1Symbol.Kind);
}

[Fact]
[WorkItem(19778, "https://github.com/dotnet/roslyn/issues/19778")]
public void BindDynamicInvocation()
{
var source =
@"using System;
class C
{
static void M()
{
dynamic L<T>(Func<dynamic, T> t, object p) => p;
L(m => L(d => d, null), null);
L(m => L(d => d, m), null);
}
}";
var comp = CreateCompilationWithMscorlib45(source, references: new[] { SystemCoreRef, CSharpRef });
comp.VerifyEmitDiagnostics(
// (8,18): error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.
// L(m => L(d => d, m), null);
Diagnostic(ErrorCode.ERR_BadDynamicMethodArgLambda, "d => d").WithLocation(8, 18));
}

[Fact]
[WorkItem(19778, "https://github.com/dotnet/roslyn/issues/19778")]
public void BindDynamicInvocation_Async()
{
var source =
@"using System;
using System.Threading.Tasks;
class C
{
static void M()
{
async Task<dynamic> L<T>(Func<dynamic, T> t, object p)
=> await L(async m => L(async d => await d, m), p);
}
}";
var comp = CreateCompilationWithMscorlib45(source, references: new[] { SystemCoreRef, CSharpRef });
comp.VerifyEmitDiagnostics(
// (8,37): error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.
// => await L(async m => L(async d => await d, m), p);
Diagnostic(ErrorCode.ERR_BadDynamicMethodArgLambda, "async d => await d").WithLocation(8, 37),
// (8,32): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
// => await L(async m => L(async d => await d, m), p);
Diagnostic(ErrorCode.WRN_AsyncLacksAwaits, "=>").WithLocation(8, 32));
}
}
}