Skip to content

format! should be able to parse arbitrary inline expressions and provide a targeted error #96999

Open
@estebank

Description

@estebank

format! currently doesn't support inline expressions, like format!("{binding.field}");, but the error it emits while serviceable isn't great:

error: invalid format string: expected `'}'`, found `'.'`
  --> src/main.rs:10:23
   |
10 |     println!("{binding.field}");
   |               -       ^ expected `}` in format string
   |               |
   |               because of this opening brace
   |
   = note: if you intended to print `{`, you can escape it using `{{`

It would be much better experience if we started parsing first field access expressions, potentially method calls and later arbitrary expressions and explicitly told the user "you can't do that, move the expression to its own binding". If we were to ever attempt to support this, prototyping the machinery for diagnostics first would also be informative about its limits or potential problems.

error: invalid format string: field access isn't supported
  --> src/main.rs:10:23
   |
10 |     println!("{binding.field}");
   |                ^^^^^^^^^^^^^ not supported
help: consider using a positional formatting argument instead
   |
10 |     println!("{}", binding.field);
   |               --   +++++++++++++

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions