diff --git a/src/Compiler/Checking/Expressions/CheckExpressions.fs b/src/Compiler/Checking/Expressions/CheckExpressions.fs index 98ae7580eba..a3cee51a48d 100644 --- a/src/Compiler/Checking/Expressions/CheckExpressions.fs +++ b/src/Compiler/Checking/Expressions/CheckExpressions.fs @@ -7530,9 +7530,9 @@ and TcInterpolatedStringExpr cenv (overallTy: OverallTy) env m tpenv (parts: Syn let concatenableExprs = if canLower then concatenable [] fillExprs parts else [] match concatenableExprs with - | [p1; p2; p3; p4] -> mkStaticCall_String_Concat4 g m p1 p2 p3 p4, tpenv - | [p1; p2; p3] -> mkStaticCall_String_Concat3 g m p1 p2 p3, tpenv - | [p1; p2] -> mkStaticCall_String_Concat2 g m p1 p2, tpenv + | [p1; p2; p3; p4] -> TcPropagatingExprLeafThenConvert cenv overallTy g.string_ty env m (fun () -> mkStaticCall_String_Concat4 g m p1 p2 p3 p4, tpenv) + | [p1; p2; p3] -> TcPropagatingExprLeafThenConvert cenv overallTy g.string_ty env m (fun () -> mkStaticCall_String_Concat3 g m p1 p2 p3, tpenv) + | [p1; p2] -> TcPropagatingExprLeafThenConvert cenv overallTy g.string_ty env m (fun () -> mkStaticCall_String_Concat2 g m p1 p2, tpenv) | [p1] -> p1, tpenv | _ -> diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs index c6fde4ad8af..22d58ad8b27 100644 --- a/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs +++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs @@ -88,4 +88,46 @@ IL_0014: call string [runtime]System.String::Concat(string, string, string, string) -IL_0019: ret"""] \ No newline at end of file +IL_0019: ret"""] + + [] + let ``Interpolated string with concat converts to span implicitly`` () = + let compilation = + FSharp $""" + module InterpolatedStringByefLikes + type Foo() = + + let sb = System.Text.StringBuilder() + + member _.Bar(s: System.ReadOnlySpan) = sb.Append(s) |> ignore + + let [] main _ = + let foo = Foo() + let foos = "foo" + foo.Bar($"{{foos}} is bar") + 0 + """ + + compilation |> compile |> shouldSucceed |> ignore + + compilation |> asExe |> compileAndRun |> shouldSucceed |> ignore + + compilation |> compile |> shouldSucceed |> verifyIL [""" +.locals init (class InterpolatedStringByefLikes/Foo V_0, + valuetype [runtime]System.ReadOnlySpan`1 V_1, + class [runtime]System.Text.StringBuilder V_2) +IL_0000: newobj instance void InterpolatedStringByefLikes/Foo::.ctor() +IL_0005: stloc.0 +IL_0006: ldstr "foo" +IL_000b: ldstr " is bar" +IL_0010: call string [runtime]System.String::Concat(string, + string) +IL_0015: call valuetype [runtime]System.ReadOnlySpan`1 [runtime]System.String::op_Implicit(string) +IL_001a: stloc.1 +IL_001b: ldloc.0 +IL_001c: ldfld class [runtime]System.Text.StringBuilder InterpolatedStringByefLikes/Foo::sb +IL_0021: ldloc.1 +IL_0022: callvirt instance class [runtime]System.Text.StringBuilder [runtime]System.Text.StringBuilder::Append(valuetype [runtime]System.ReadOnlySpan`1) +IL_0027: stloc.2 +IL_0028: ldc.i4.0 +IL_0029: ret"""] \ No newline at end of file