Skip to content

Commit

Permalink
'with' on anonymous type needs to lower values (#54086)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Jun 15, 2021
1 parent 0e720ce commit e2d605e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ ImmutableArray<BoundExpression> getAnonymousTypeValues(BoundWithExpression withE
Debug.Assert(left.MemberSymbol is not null);

// We evaluate the values provided in source first
BoundLocal valueTemp = _factory.StoreToTemp(assignment.Right, out BoundAssignmentOperator boundAssignmentToTemp);
var rewrittenRight = VisitExpression(assignment.Right);
BoundLocal valueTemp = _factory.StoreToTemp(rewrittenRight, out BoundAssignmentOperator boundAssignmentToTemp);
temps.Add(valueTemp.LocalSymbol);
sideEffects.Add(boundAssignmentToTemp);

Expand Down
29 changes: 29 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/RecordStructTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9993,6 +9993,35 @@ public static void M()
VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>(src, expectedFlowGraph, expectedDiagnostics, parseOptions: TestOptions.RegularPreview);
}

[Fact, WorkItem(53849, "https://github.com/dotnet/roslyn/issues/53849")]
public void WithExpr_AnonymousType_ValueIsLoweredToo()
{
var src = @"
var x = new { Property = 42 };
var adjusted = x with { Property = x.Property + 2 };
System.Console.WriteLine(adjusted);
";
var comp = CreateCompilation(src, parseOptions: TestOptions.RegularPreview);
var verifier = CompileAndVerify(comp, expectedOutput: "{ Property = 44 }");
verifier.VerifyDiagnostics();
}

[Fact, WorkItem(53849, "https://github.com/dotnet/roslyn/issues/53849")]
public void WithExpr_AnonymousType_ValueIsLoweredToo_NestedWith()
{
var src = @"
var x = new { Property = 42 };
var container = new { Item = x };
var adjusted = container with { Item = x with { Property = x.Property + 2 } };
System.Console.WriteLine(adjusted);
";
var comp = CreateCompilation(src, parseOptions: TestOptions.RegularPreview);
var verifier = CompileAndVerify(comp, expectedOutput: "{ Item = { Property = 44 } }");
verifier.VerifyDiagnostics();
}

[Fact]
public void AttributesOnPrimaryConstructorParameters_01()
{
Expand Down

0 comments on commit e2d605e

Please sign in to comment.