提交 72a1cfe0 编写于 作者: S Stephen Toub

Disable Array.Empty params array optimization in expression lambdas

上级 dd9c889d
......@@ -615,7 +615,11 @@ private static ImmutableArray<RefKind> GetRefKindsOrNull(ArrayBuilder<RefKind> 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<T>()" if it's available
// If this is a zero-length array, rather than using "new T[0]", optimize with "Array.Empty<T>()"
// 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<T>().
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
......
......@@ -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
// liklihood 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")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册