未验证 提交 98c29678 编写于 作者: A Adam Boniecki 提交者: GitHub

Fix handling % chars in interpolated strings (#14440)

Interpolated strings require escaping '%' chars by doubling them,
however if an interpolated string literal had no interpolation
expressions, it would not drop extra '%' chars in the content.
Co-authored-by: NAdam Boniecki <adboniec@microsoft.com>
上级 b38116a7
......@@ -7165,13 +7165,14 @@ and TcInterpolatedStringExpr cenv (overallTy: OverallTy) env m tpenv (parts: Syn
// Type check the expressions filling the holes
if List.isEmpty synFillExprs then
let str = mkString g m printfFormatString
if isString then
let sb = System.Text.StringBuilder(printfFormatString).Replace("%%", "%")
let str = mkString g m (sb.ToString())
TcPropagatingExprLeafThenConvert cenv overallTy g.string_ty env (* true *) m (fun () ->
str, tpenv
)
else
let str = mkString g m printfFormatString
mkCallNewFormat g m printerTy printerArgTy printerResidueTy printerResultTy printerTupleTy str, tpenv
else
// Type check the expressions filling the holes
......
......@@ -30,4 +30,10 @@ let b: System.IComparable = $"string"
let c: System.IFormattable = $"string"
"""
|> compile
|> shouldSucceed
\ No newline at end of file
|> shouldSucceed
[<Fact>]
let ``Percent sign characters in interpolated strings`` () =
Assert.Equal("%", $"%%")
Assert.Equal("42%", $"{42}%%")
Assert.Equal("% 42", $"%%%3d{42}")
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册