From 00a8ee984cc412c1f5ea875e236f52ca84245a8c Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Fri, 13 Sep 2024 13:23:39 +0200 Subject: [PATCH 1/2] Fix #17719 --- .../Checking/Expressions/CheckExpressions.fs | 6 +-- .../EmittedIL/StringFormatAndInterpolation.fs | 44 ++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) 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 From f551033d601cf0c60d59949be617d9f423c3d391 Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Fri, 13 Sep 2024 14:18:49 +0200 Subject: [PATCH 2/2] Only run test in netcoreapp --- .../EmittedIL/StringFormatAndInterpolation.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs index 22d58ad8b27..38729fc70be 100644 --- a/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs +++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/StringFormatAndInterpolation.fs @@ -90,7 +90,7 @@ IL_0014: call string [runtime]System.String::Concat(string, string) IL_0019: ret"""] - [] + [] let ``Interpolated string with concat converts to span implicitly`` () = let compilation = FSharp $"""