Skip to content

Commit cf05314

Browse files
authored
Fix how much is trimmed from an interp string part (#18123)
* Fix how much is trimmed from an interp string part Only trim last 2 characters if they are "%s" and the '%' is not escaped * Add release note --------- Co-authored-by: Adam Boniecki <[email protected]>
1 parent 7b1eb07 commit cf05314

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.200.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Fix lowering of computed array expressions when the expression consists of a simple mapping from a `uint64` or `unativeint` array. [PR #18081](https://github.com/dotnet/fsharp/pull/18081)
1717
* Add missing nullable-metadata for C# consumers of records,exceptions and DU subtypes generated from F# code. [PR #18079](https://github.com/dotnet/fsharp/pull/18079)
1818
* Fix a race condition in file book keeping in the compiler service ([#18008](https://github.com/dotnet/fsharp/pull/18008))
19-
19+
* Fix trimming '%' characters when lowering interpolated string to a concat call [PR #18123](https://github.com/dotnet/fsharp/pull/18123)
2020

2121
### Added
2222

src/Compiler/Checking/Expressions/CheckExpressions.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ let (|WithTrailingStringSpecifierRemoved|) (s: string) =
174174
let i = s.AsSpan(0, s.Length - 2).LastIndexOfAnyExcept '%'
175175
let diff = s.Length - 2 - i
176176
if diff &&& 1 <> 0 then
177-
s[..i]
177+
s[..s.Length - 3]
178178
else
179179
s
180180
else

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

+11
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ type Foo () =
130130
|> compile
131131
|> shouldSucceed
132132

133+
[<Fact>]
134+
let ``Percent signs and format specifiers with string expression`` () =
135+
Fsx """
136+
let x = "abc"
137+
let s = $"%%%s{x}%%"
138+
printfn "%s" s
139+
"""
140+
|> compileExeAndRun
141+
|> shouldSucceed
142+
|> withStdOutContains "%abc%"
143+
133144
[<Theory>]
134145
// Test different number of interpolated string parts
135146
[<InlineData("$\"\"\"abc{\"d\"}e\"\"\"")>]

0 commit comments

Comments
 (0)