diff --git a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Call.cs b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Call.cs index 0223b8ad2b9eef6f5163bab97813e101514499f3..4396d9c22a6a01a6ac8e4bd7514deb3ae3815078 100644 --- a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Call.cs +++ b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Call.cs @@ -615,7 +615,11 @@ private static ImmutableArray GetRefKindsOrNull(ArrayBuilder r var paramArrayType = parameters[paramsParam].Type; var arrayArgs = paramArray.ToImmutableAndFree(); - if (arrayArgs.Length == 0) // if this is a zero-length array, rather than using "new T[0]", optimize with "Array.Empty()" if it's available + // If this is a zero-length array, rather than using "new T[0]", optimize with "Array.Empty()" + // if it's available. However, we also disable the optimization if we're in an expression lambda, the + // point of which is just to represent the semantics of an operation, and we don't know that all consumers + // of expression lambdas will appropriately understand Array.Empty(). + if (arrayArgs.Length == 0 && !_inExpressionLambda) { ArrayTypeSymbol ats = paramArrayType as ArrayTypeSymbol; if (ats != null) // could be null if there's a semantic error, e.g. the params parameter type isn't an array diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenExprLambdaTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenExprLambdaTests.cs index d431df6d53a1d0d086553ff26a92a61a4a0212f5..1594c3a214699ae8344d4e0dfb049d2907db3d10 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenExprLambdaTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenExprLambdaTests.cs @@ -2273,6 +2273,19 @@ static void Main() CompileAndVerify( text, new[] { ExpressionAssemblyRef }, expectedOutput: TrimExpectedOutput(expectedOutput)); + + // Also verify with the assemblies on which the tests are running, as there's a higher + // likelihood that they have Array.Empty, and we want to verify that Array.Empty is not used + // in expression lambdas. This can be changed to use the mscorlib 4.6 metadata once it's + // available in the Roslyn tests. + CompileAndVerify(CreateCompilation( + text, + references: new[] { + MetadataReference.CreateFromAssembly(typeof(object).Assembly), + MetadataReference.CreateFromAssembly(typeof(System.Linq.Enumerable).Assembly) + }, + options: TestOptions.ReleaseExe), + expectedOutput: TrimExpectedOutput(expectedOutput)); } [WorkItem(544270, "DevDiv")]