From 993d5ff1358cc1eeb20fc213096f6a2bf8be99ec Mon Sep 17 00:00:00 2001 From: VSadov Date: Thu, 11 Sep 2014 13:55:40 -0700 Subject: [PATCH] Fixed handling of parameterless struct constructors in expression tree lowering. (changeset 1333851) --- .../ExpressionLambdaRewriter.cs | 4 ++- .../CodeGen/CodeGenConstructorInitTests.cs | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Src/Compilers/CSharp/Portable/Lowering/LambdaRewriter/ExpressionLambdaRewriter.cs b/Src/Compilers/CSharp/Portable/Lowering/LambdaRewriter/ExpressionLambdaRewriter.cs index 8fe31748897..c74315514f4 100644 --- a/Src/Compilers/CSharp/Portable/Lowering/LambdaRewriter/ExpressionLambdaRewriter.cs +++ b/Src/Compilers/CSharp/Portable/Lowering/LambdaRewriter/ExpressionLambdaRewriter.cs @@ -821,7 +821,9 @@ private BoundExpression VisitObjectCreationExpressionInternal(BoundObjectCreatio return Constant(node); } - if ((object)node.Constructor == null || node.Arguments.Length == 0) + if ((object)node.Constructor == null || + (node.Arguments.Length == 0 && !node.Type.IsStructType()) || + node.Constructor.IsDefaultValueTypeConstructor()) { return ExprFactory("New", Bound.Typeof(node.Type)); } diff --git a/Src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenConstructorInitTests.cs b/Src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenConstructorInitTests.cs index c68195dd65f..473aced1095 100644 --- a/Src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenConstructorInitTests.cs +++ b/Src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenConstructorInitTests.cs @@ -810,5 +810,30 @@ static void Main() CompileAndVerifyExperimental(source, expectedOutput: "0"); } + + [Fact] + public void InstanceInitializerStructInExprTree() + { + var source = @" + +using System; +using System.Linq.Expressions; + +class C +{ + struct S1 + { + public int x = 42; + } + + static void Main() + { + Expression> testExpr = () => new S1(); + System.Console.Write(testExpr.Compile()().x); + } +} +"; + CompileAndVerifyExperimental(source, additionalRefs: new[] { ExpressionAssemblyRef }, expectedOutput: "42"); + } } } -- GitLab