Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaped % char in interpolated string literals and TextWriterFormat accepts and ignores format flags, width and precision specifiers #14681

Closed
abonie opened this issue Feb 1, 2023 · 1 comment · Fixed by #14685
Assignees
Labels
Area-Compiler Compiler-related issues which don't belong to other categories Bug Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code.
Milestone

Comments

@abonie
Copy link
Member

abonie commented Feb 1, 2023

Repro steps

Create a string literal containing a:

  1. substring consisting of % char, followed by a sequence that could be interpreted as format specifier's flag/width/precision, followed by another % char.
  2. any interpolation

Expected behavior

Some sort of FS3376 error probably:

> $"abc%-13%def{0}";;
error FS3376: Invalid interpolated string. Interpolated strings may not use '%' format specifiers unless each is given an expression, e.g. '%d{1+1}'.

Actual behavior

Whatever is between the two % chars is ignored (as long as it looks like a valid format specifier, i.e. a flag/width/precision info), and the whole sequence is treated as an escaped %:

> $"abc%-13%def{0}";;
val it: string = "abc%def0"

it also happens with regular (non-interpolated) string literals with printf:

> printfn "abc%-13%qwe";;
abc%qwe
val it: unit = ()

Known workarounds

Workaround is probably not needed, as this should be an error.

@abonie
Copy link
Member Author

abonie commented Feb 1, 2023

This happens in CheckFormatStrings.fs and I will self-assign, since I am about to make changes to this particular part of this file anyway

@abonie abonie modified the milestones: Backlog, February-2023 Feb 1, 2023
@abonie abonie changed the title Confused parsing of interpolated string literals with certain substrings containing '%' Escaped % char in interpolated string literals and TextWriterFormat accepts and ignores format flags, width and precision specifiers Feb 6, 2023
@abonie abonie modified the milestones: Backlog, February-2023 Feb 6, 2023
@T-Gro T-Gro moved this from Not Planned to In Progress in F# Compiler and Tooling Feb 6, 2023
@0101 0101 added Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code. Area-Compiler Compiler-related issues which don't belong to other categories and removed Needs-Triage labels Feb 13, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in F# Compiler and Tooling Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler Compiler-related issues which don't belong to other categories Bug Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants