From e7a6e27820aef83471173ddc60421ffa93cc10f1 Mon Sep 17 00:00:00 2001
From: Siegfried Pammer <siegfriedpammer@gmail.com>
Date: Tue, 29 Apr 2025 00:51:18 +0200
Subject: [PATCH] Fix #3464: Missing cast in string interpolation

---
 .../TestCases/Pretty/StringInterpolation.cs        | 10 ++++++++++
 ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs | 14 ++++++++++----
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringInterpolation.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringInterpolation.cs
index 0dbbc9064..b3903295e 100644
--- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringInterpolation.cs
+++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringInterpolation.cs
@@ -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
 		{
 			return c + s + s + c;
 		}
+
+		public static TReturn Get<TReturn>()
+		{
+			return default(TReturn);
+		}
 	}
 }
diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
index 19c21fd34..13ec88dcc 100644
--- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
+++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
@@ -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();