Browse Source

Fix #3464: Missing cast in string interpolation

pull/3472/head
Siegfried Pammer 2 months ago
parent
commit
e7a6e27820
  1. 10
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringInterpolation.cs
  2. 14
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

10
ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringInterpolation.cs

@ -24,6 +24,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -24,6 +24,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine($"{args.Length,5}");
}
public static void Types()
{
Console.WriteLine($"{(int)Get<long>()}");
}
public static void ArrayExpansionSpecialCases(object[] args)
{
Console.WriteLine($"args: {args}");
@ -142,5 +147,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -142,5 +147,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
return c + s + s + c;
}
public static TReturn Get<TReturn>()
{
return default(TReturn);
}
}
}

14
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -3319,22 +3319,28 @@ namespace ICSharpCode.Decompiler.CSharp @@ -3319,22 +3319,28 @@ namespace ICSharpCode.Decompiler.CSharp
for (int i = 1; i < block.Instructions.Count; i++)
{
var call = (Call)block.Instructions[i];
Interpolation BuildInterpolation(int alignment = 0, string suffix = null)
{
return new Interpolation(Translate(call.Arguments[1]).ConvertTo(call.GetParameter(1).Type, this, allowImplicitConversion: true), alignment, suffix);
}
switch (call.Method.Name)
{
case "AppendLiteral":
content.Add(new InterpolatedStringText(((LdStr)call.Arguments[1]).Value.Replace("{", "{{").Replace("}", "}}")));
break;
case "AppendFormatted" when call.Arguments.Count == 2:
content.Add(new Interpolation(Translate(call.Arguments[1])));
content.Add(BuildInterpolation());
break;
case "AppendFormatted" when call.Arguments.Count == 3 && call.Arguments[2] is LdStr ldstr:
content.Add(new Interpolation(Translate(call.Arguments[1]), suffix: ldstr.Value));
content.Add(BuildInterpolation(suffix: ldstr.Value));
break;
case "AppendFormatted" when call.Arguments.Count == 3 && call.Arguments[2] is LdcI4 ldci4:
content.Add(new Interpolation(Translate(call.Arguments[1]), alignment: ldci4.Value));
content.Add(BuildInterpolation(alignment: ldci4.Value));
break;
case "AppendFormatted" when call.Arguments.Count == 4 && call.Arguments[2] is LdcI4 ldci4 && call.Arguments[3] is LdStr ldstr:
content.Add(new Interpolation(Translate(call.Arguments[1]), ldci4.Value, ldstr.Value));
content.Add(BuildInterpolation(ldci4.Value, ldstr.Value));
break;
default:
throw new NotSupportedException();

Loading…
Cancel
Save