diff --git a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_StringInterpolation.cs b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_StringInterpolation.cs index 0dd84820bcf7a..b3400bc60f2b0 100644 --- a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_StringInterpolation.cs +++ b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_StringInterpolation.cs @@ -64,7 +64,13 @@ private void MakeInterpolatedStringFormat(BoundInterpolatedString node, out Boun formatString.Builder.Append(":").Append(fillin.Format.ConstantValue.StringValue); } formatString.Builder.Append("}"); - expressions.Add(fillin.Value); // NOTE: must still be lowered + var value = fillin.Value; + if (value.Type?.TypeKind == TypeKind.Dynamic) + { + value = MakeConversion(value, _compilation.ObjectType, @checked: false); + } + + expressions.Add(value); // NOTE: must still be lowered } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/InterpolationTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/InterpolationTests.cs index 2b39efdc87c46..30f5cb2724ad1 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/InterpolationTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/InterpolationTests.cs @@ -248,6 +248,30 @@ static void Main(string[] args) CompileAndVerify(source, expectedOutput: expectedOutput); } + [Fact, WorkItem(306), WorkItem(308)] + public void DynamicInterpolation() + { + string source = +@"using System; +using System.Linq.Expressions; +class Program +{ + static void Main(string[] args) + { + dynamic nil = null; + dynamic a = new string[] {""Hello"", ""world""}; + Console.WriteLine($""<{nil}>""); + Console.WriteLine($""<{a}>""); + } + Expression> M(dynamic d) { + return () => $""Dynamic: {d}""; + } +}"; + string expectedOutput = @"<> +"; + var verifier = CompileAndVerify(source, new[] { SystemCoreRef, CSharpRef }, expectedOutput: expectedOutput).VerifyDiagnostics(); + } + [Fact] public void UnclosedInterpolation01() {