未验证 提交 4378143f 编写于 作者: K kerams 提交者: GitHub

Optimize interpolated string with no holes (#11632)

上级 00397560
......@@ -6751,27 +6751,35 @@ and TcInterpolatedStringExpr cenv overallTy env m tpenv (parts: SynInterpolatedS
UnifyTypes cenv env m printerTupleTy printerTupleTyRequired
// Type check the expressions filling the holes
let flexes = argTys |> List.map (fun _ -> false)
let fillExprs, tpenv = TcExprs cenv env m tpenv flexes argTys synFillExprs
if List.isEmpty synFillExprs then
let str = mkString g m printfFormatString
let fillExprsBoxed = (argTys, fillExprs) ||> List.map2 (mkCallBox g m)
let argsExpr = mkArray (g.obj_ty, fillExprsBoxed, m)
let percentATysExpr =
if percentATys.Length = 0 then
mkNull m (mkArrayType g g.system_Type_ty)
if isString then
str, tpenv
else
let tyExprs = percentATys |> Array.map (mkCallTypeOf g m) |> Array.toList
mkArray (g.system_Type_ty, tyExprs, m)
mkCallNewFormat cenv.g m printerTy printerArgTy printerResidueTy printerResultTy printerTupleTy str, tpenv
else
// Type check the expressions filling the holes
let flexes = argTys |> List.map (fun _ -> false)
let fillExprs, tpenv = TcExprs cenv env m tpenv flexes argTys synFillExprs
let fmtExpr = MakeMethInfoCall cenv.amap m newFormatMethod [] [mkString g m printfFormatString; argsExpr; percentATysExpr]
let fillExprsBoxed = (argTys, fillExprs) ||> List.map2 (mkCallBox g m)
if isString then
// Make the call to sprintf
mkCall_sprintf g m printerTy fmtExpr [], tpenv
else
fmtExpr, tpenv
let argsExpr = mkArray (g.obj_ty, fillExprsBoxed, m)
let percentATysExpr =
if percentATys.Length = 0 then
mkNull m (mkArrayType g g.system_Type_ty)
else
let tyExprs = percentATys |> Array.map (mkCallTypeOf g m) |> Array.toList
mkArray (g.system_Type_ty, tyExprs, m)
let fmtExpr = MakeMethInfoCall cenv.amap m newFormatMethod [] [mkString g m printfFormatString; argsExpr; percentATysExpr]
if isString then
// Make the call to sprintf
mkCall_sprintf g m printerTy fmtExpr [], tpenv
else
fmtExpr, tpenv
// The case for $"..." used as type FormattableString or IFormattable
| Choice2Of2 createFormattableStringMethod ->
......
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.ComponentTests.EmittedIL
open Xunit
open FSharp.Test.Utilities.Compiler
module ``StringFormatAndInterpolation`` =
[<Fact>]
let ``Interpolated string with no holes is reduced to a string or simple format when used in printf``() =
FSharp """
module StringFormatAndInterpolation
let stringOnly () = $"no hole"
let printed () = printf $"printed no hole"
"""
|> compile
|> shouldSucceed
|> verifyIL ["""
IL_0000: ldstr "no hole"
IL_0005: ret"""
"""
IL_0000: ldstr "printed no hole"
IL_0005: newobj instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [runtime]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
IL_000a: stloc.0
IL_000b: call class [netstandard]System.IO.TextWriter [netstandard]System.Console::get_Out()
IL_0010: ldloc.0
IL_0011: call !!0 [FSharp.Core]Microsoft.FSharp.Core.PrintfModule::PrintFormatToTextWriter<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [runtime]System.IO.TextWriter,
class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0,class [runtime]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
IL_0016: pop
IL_0017: ret"""]
......@@ -23,6 +23,7 @@
<Compile Include="EmittedIL\Literals.fs" />
<Compile Include="EmittedIL\Misc.fs" />
<Compile Include="EmittedIL\SkipLocalsInit.fs" />
<Compile Include="EmittedIL\StringFormatAndInterpolation.fs" />
<Compile Include="EmittedIL\TailCalls.fs" />
<Compile Include="EmittedIL\TupleElimination.fs" />
<Compile Include="ErrorMessages\TypeEqualsMissingTests.fs" />
......
......@@ -4119,6 +4119,19 @@ module CheckEliminatedConstructs =
"""IfThenElse (Call (None, op_Equality, [ValueWithName ([||], ts), Value (<null>)]),
Value (true), Value (false))"""
module Interpolation =
let interpolatedNoHoleQuoted = <@ $"abc" @>
let actual1 = interpolatedNoHoleQuoted.ToString()
checkStrings "brewbreebrwhat1" actual1 """Value ("abc")"""
let interpolatedWithLiteralQuoted = <@ $"abc {1} def" @>
let actual2 = interpolatedWithLiteralQuoted.ToString()
checkStrings "brewbreebrwhat2" actual2
"""Call (None, PrintFormatToString,
[NewObject (PrintfFormat`5, Value ("abc %P() def"),
NewArray (Object, Call (None, Box, [Value (1)])),
Value (<null>))])"""
module TestAssemblyAttributes =
let attributes = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(false)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册