Skip to content

Commit 98c2967

Browse files
authored
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: Adam Boniecki <[email protected]>
1 parent b38116a commit 98c2967

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Compiler/Checking/CheckExpressions.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7165,13 +7165,14 @@ and TcInterpolatedStringExpr cenv (overallTy: OverallTy) env m tpenv (parts: Syn
71657165
// Type check the expressions filling the holes
71667166

71677167
if List.isEmpty synFillExprs then
7168-
let str = mkString g m printfFormatString
7169-
71707168
if isString then
7169+
let sb = System.Text.StringBuilder(printfFormatString).Replace("%%", "%")
7170+
let str = mkString g m (sb.ToString())
71717171
TcPropagatingExprLeafThenConvert cenv overallTy g.string_ty env (* true *) m (fun () ->
71727172
str, tpenv
71737173
)
71747174
else
7175+
let str = mkString g m printfFormatString
71757176
mkCallNewFormat g m printerTy printerArgTy printerResidueTy printerResultTy printerTupleTy str, tpenv
71767177
else
71777178
// Type check the expressions filling the holes

tests/FSharp.Compiler.ComponentTests/Language/InterpolatedStringsTests.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ let b: System.IComparable = $"string"
3030
let c: System.IFormattable = $"string"
3131
"""
3232
|> compile
33-
|> shouldSucceed
33+
|> shouldSucceed
34+
35+
[<Fact>]
36+
let ``Percent sign characters in interpolated strings`` () =
37+
Assert.Equal("%", $"%%")
38+
Assert.Equal("42%", $"{42}%%")
39+
Assert.Equal("% 42", $"%%%3d{42}")

0 commit comments

Comments
 (0)