Skip to content

Invalid parsing of interpolated strings with curly braces followed after interpolation with format specifier #57750

@bernd5

Description

@bernd5

Version Used: commit 973b3aa

Steps to Reproduce:
Run the following code with net 6.0:

using System;

Console.WriteLine($"{{{12:X}}}");

See here

Expected Behavior:
It should print "{C}" like it did in net5.0

Actual Behavior:
It prints: "{X}}"

That there is parsing error is very good visible in Visual studios highlighting:
grafik

See csharplang discussion: here


Details

With net5.0 it was lowered to:

Console.WriteLine(string.Format("{{{0:X}}}", 12)); //prints "{C}"

but with net6.0 it is lowered to

DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(1, 1);
defaultInterpolatedStringHandler.AppendLiteral("{");
defaultInterpolatedStringHandler.AppendFormatted(12, "X}}");
Console.WriteLine(defaultInterpolatedStringHandler.ToStringAndClear()); //prints "{X}}"

instead of:

DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(2, 1);
defaultInterpolatedStringHandler.AppendLiteral("{");
defaultInterpolatedStringHandler.AppendFormatted(12, "X");
defaultInterpolatedStringHandler.AppendLiteral("}");
Console.WriteLine(defaultInterpolatedStringHandler.ToStringAndClear()); //prints "{C}"

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions