Skip to content

Commit e7a6e27

Browse files
Fix #3464: Missing cast in string interpolation
1 parent 03b7444 commit e7a6e27

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public static void General(string[] args)
2424
Console.WriteLine($"{args.Length,5}");
2525
}
2626

27+
public static void Types()
28+
{
29+
Console.WriteLine($"{(int)Get<long>()}");
30+
}
31+
2732
public static void ArrayExpansionSpecialCases(object[] args)
2833
{
2934
Console.WriteLine($"args: {args}");
@@ -142,5 +147,10 @@ public string ConcatStringCharCSSC(string s, char c)
142147
{
143148
return c + s + s + c;
144149
}
150+
151+
public static TReturn Get<TReturn>()
152+
{
153+
return default(TReturn);
154+
}
145155
}
146156
}

ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,22 +3319,28 @@ private TranslatedExpression TranslateInterpolatedString(Block block)
33193319
for (int i = 1; i < block.Instructions.Count; i++)
33203320
{
33213321
var call = (Call)block.Instructions[i];
3322+
3323+
Interpolation BuildInterpolation(int alignment = 0, string suffix = null)
3324+
{
3325+
return new Interpolation(Translate(call.Arguments[1]).ConvertTo(call.GetParameter(1).Type, this, allowImplicitConversion: true), alignment, suffix);
3326+
}
3327+
33223328
switch (call.Method.Name)
33233329
{
33243330
case "AppendLiteral":
33253331
content.Add(new InterpolatedStringText(((LdStr)call.Arguments[1]).Value.Replace("{", "{{").Replace("}", "}}")));
33263332
break;
33273333
case "AppendFormatted" when call.Arguments.Count == 2:
3328-
content.Add(new Interpolation(Translate(call.Arguments[1])));
3334+
content.Add(BuildInterpolation());
33293335
break;
33303336
case "AppendFormatted" when call.Arguments.Count == 3 && call.Arguments[2] is LdStr ldstr:
3331-
content.Add(new Interpolation(Translate(call.Arguments[1]), suffix: ldstr.Value));
3337+
content.Add(BuildInterpolation(suffix: ldstr.Value));
33323338
break;
33333339
case "AppendFormatted" when call.Arguments.Count == 3 && call.Arguments[2] is LdcI4 ldci4:
3334-
content.Add(new Interpolation(Translate(call.Arguments[1]), alignment: ldci4.Value));
3340+
content.Add(BuildInterpolation(alignment: ldci4.Value));
33353341
break;
33363342
case "AppendFormatted" when call.Arguments.Count == 4 && call.Arguments[2] is LdcI4 ldci4 && call.Arguments[3] is LdStr ldstr:
3337-
content.Add(new Interpolation(Translate(call.Arguments[1]), ldci4.Value, ldstr.Value));
3343+
content.Add(BuildInterpolation(ldci4.Value, ldstr.Value));
33383344
break;
33393345
default:
33403346
throw new NotSupportedException();

0 commit comments

Comments
 (0)