Skip to content

System.FormatException for FormattableString with {{ }} content #12285

@mrange

Description

@mrange

Apologies if this is already reported but didn't find a matching report.

Repro steps

This program below crashes for me with System.FormatException

open System

let s   : string            = $"{{ {1} }}"
let fs  : FormattableString = $"{{ {1} }}"

// Works
printfn "s: %s" <| s
// Crashes with `System.FormatException`
printfn "fs: %s" <| fs.ToString ()

Expected behavior

I expect the program to print

{ 1 }
{ 1 }

Actual behavior

s: { 1 }
Unhandled exception. System.FormatException: Input string was not in a correct format.
   at System.Text.ValueStringBuilder.ThrowFormatError()
   at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at System.Runtime.CompilerServices.FormattableStringFactory.ConcreteFormattableString.ToString(IFormatProvider formatProvider)
   at System.FormattableString.ToString()

Known workarounds

None known

Related information

I tried writing a similar program in C# and it produced the expected results

string              s   = $"{{ {1} }}";
FormattableString   fs  = $"{{ {1} }}";

Console.WriteLine(s);
Console.WriteLine(fs.ToString());

When printing the F# FormattableString.Format it is : { {0} }
When printing the C# FormattableString.Format it is : {{ {0} }}

  • Windows 10
  • net6.0
  • Visual Studio 2022 Preview 5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions