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

Fix reversed output in interpolation concatenation #55494

Merged
merged 5 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
Expand Down Expand Up @@ -157,15 +158,15 @@ private static ImmutableArray<BoundExpression> CollectBinaryOperatorInterpolated
var partsBuilder = ArrayBuilder<BoundExpression>.GetInstance();
while (true)
{
partsBuilder.AddRange(((BoundInterpolatedString)node.Right).Parts);
addReversedParts((BoundInterpolatedString)node.Right);

if (node.Left is BoundBinaryOperator next)
{
node = next;
}
else
{
partsBuilder.AddRange(((BoundInterpolatedString)node.Left).Parts);
addReversedParts((BoundInterpolatedString)node.Left);
break;
}
}
Expand All @@ -174,6 +175,14 @@ private static ImmutableArray<BoundExpression> CollectBinaryOperatorInterpolated

ImmutableArray<BoundExpression> parts = partsBuilder.ToImmutableAndFree();
return parts;

void addReversedParts(BoundInterpolatedString boundInterpolated)
{
for (int i = boundInterpolated.Parts.Length - 1; i >= 0; i--)
{
partsBuilder.Add(boundInterpolated.Parts[i]);
}
}
}

private BoundExpression MakeBinaryOperator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12182,6 +12182,93 @@ .locals init (int V_0, //i1
");
}

[Theory]
[InlineData(@"$""({i1}),"" + $""[{i2}],"" + $""{{{i3}}}""")]
[InlineData(@"($""({i1}),"" + $""[{i2}],"") + $""{{{i3}}}""")]
[InlineData(@"$""({i1}),"" + ($""[{i2}],"" + $""{{{i3}}}"")")]
public void InterpolatedStringsAddedUnderObjectAddition2(string expression)
{
var code = $@"
int i1 = 1;
int i2 = 2;
int i3 = 3;
System.Console.WriteLine({expression});";

var comp = CreateCompilation(new[] { code, GetInterpolatedStringHandlerDefinition(includeSpanOverloads: false, useDefaultParameters: false, useBoolReturns: false) });

CompileAndVerify(comp, expectedOutput: @"
(
value:1
),
[
value:2
],
{
value:3
}
");
}

[Fact]
public void InterpolatedStringsAddedUnderObjectAddition3()
{
var code = @"
using System;

try
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
{
string s = string.Empty;
Console.WriteLine($""{s = null}{s.Length}"" + $"""");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
";

var comp = CreateCompilation(new[] { code, GetInterpolatedStringHandlerDefinition(includeSpanOverloads: false, useDefaultParameters: false, useBoolReturns: false) });

CompileAndVerify(comp, expectedOutput: @"System.NullReferenceException: Object reference not set to an instance of an object.
at Program.<Main>$(String[] args)
").VerifyIL("<top-level-statements-entry-point>", @"
{
// Code size 64 (0x40)
.maxstack 3
.locals init (string V_0, //s
System.Runtime.CompilerServices.DefaultInterpolatedStringHandler V_1)
.try
{
IL_0000: ldsfld ""string string.Empty""
IL_0005: stloc.0
IL_0006: ldc.i4.0
IL_0007: ldc.i4.2
IL_0008: newobj ""System.Runtime.CompilerServices.DefaultInterpolatedStringHandler..ctor(int, int)""
IL_000d: stloc.1
IL_000e: ldloca.s V_1
IL_0010: ldnull
IL_0011: dup
IL_0012: stloc.0
IL_0013: call ""void System.Runtime.CompilerServices.DefaultInterpolatedStringHandler.AppendFormatted(string)""
IL_0018: ldloca.s V_1
IL_001a: ldloc.0
IL_001b: callvirt ""int string.Length.get""
IL_0020: call ""void System.Runtime.CompilerServices.DefaultInterpolatedStringHandler.AppendFormatted<int>(int)""
IL_0025: ldloca.s V_1
IL_0027: call ""string System.Runtime.CompilerServices.DefaultInterpolatedStringHandler.ToStringAndClear()""
IL_002c: call ""void System.Console.WriteLine(string)""
IL_0031: leave.s IL_003f
}
catch System.Exception
{
IL_0033: callvirt ""string object.ToString()""
IL_0038: call ""void System.Console.WriteLine(string)""
IL_003d: leave.s IL_003f
}
IL_003f: ret
}
");
}

[Fact]
public void InterpolatedStringsAddedUnderObjectAddition_DefiniteAssignment()
{
Expand Down