Skip to content

Conversation

estebank
Copy link
Contributor

Point at format string position inside the formatting string:

error: invalid format string: unmatched `}` found
  --> $DIR/format-string-error.rs:21:22
   |
LL |     let _ = format!("}");
   |                      ^ unmatched `}` in format string

Explain that argument names can't start with an underscore:

error: invalid format string: invalid argument name `_foo`
  --> $DIR/format-string-error.rs:15:23
   |
LL |     let _ = format!("{_foo}", _foo = 6usize);
   |                       ^^^^ invalid argument name in format string
   |
   = note: argument names cannot start with an underscore

Fix #23476.

The more accurate spans will only be seen when using format! directly, when using println! the diagnostics machinery makes the span be the entire statement.

 - Point at format string position inside the formatting string
 - Explain that argument names can't start with an underscore
@rust-highfive
Copy link
Contributor

r? @Kimundi

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 10, 2018
@pietroalbini
Copy link
Member

Ping from triage @Kimundi! This PR needs your review.

@Kimundi
Copy link
Contributor

Kimundi commented May 14, 2018

@bors: r+

@bors
Copy link
Collaborator

bors commented May 14, 2018

📌 Commit 3f6b3bb has been approved by Kimundi

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 14, 2018
@bors
Copy link
Collaborator

bors commented May 17, 2018

⌛ Testing commit 3f6b3bb with merge c6ec5f4...

bors added a commit that referenced this pull request May 17, 2018
Improve format string errors

Point at format string position inside the formatting string:
```
error: invalid format string: unmatched `}` found
  --> $DIR/format-string-error.rs:21:22
   |
LL |     let _ = format!("}");
   |                      ^ unmatched `}` in format string
```

Explain that argument names can't start with an underscore:
```
error: invalid format string: invalid argument name `_foo`
  --> $DIR/format-string-error.rs:15:23
   |
LL |     let _ = format!("{_foo}", _foo = 6usize);
   |                       ^^^^ invalid argument name in format string
   |
   = note: argument names cannot start with an underscore
```

Fix #23476.

The more accurate spans will only be seen when using `format!` directly, when using `println!` the diagnostics machinery makes the span be the entire statement.
@bors
Copy link
Collaborator

bors commented May 17, 2018

💔 Test failed - status-travis

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 17, 2018
@rust-highfive

This comment has been minimized.

@kennytm
Copy link
Member

kennytm commented May 17, 2018

@bors retry #43283

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 17, 2018
@rust-lang rust-lang deleted a comment from rust-highfive May 17, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 17, 2018
Improve format string errors

Point at format string position inside the formatting string:
```
error: invalid format string: unmatched `}` found
  --> $DIR/format-string-error.rs:21:22
   |
LL |     let _ = format!("}");
   |                      ^ unmatched `}` in format string
```

Explain that argument names can't start with an underscore:
```
error: invalid format string: invalid argument name `_foo`
  --> $DIR/format-string-error.rs:15:23
   |
LL |     let _ = format!("{_foo}", _foo = 6usize);
   |                       ^^^^ invalid argument name in format string
   |
   = note: argument names cannot start with an underscore
```

Fix rust-lang#23476.

The more accurate spans will only be seen when using `format!` directly, when using `println!` the diagnostics machinery makes the span be the entire statement.
bors added a commit that referenced this pull request May 18, 2018
Rollup of 10 pull requests

Successful merges:

 - #50387 (Remove leftover tab in libtest outputs)
 - #50553 (Add Option::xor method)
 - #50610 (Improve format string errors)
 - #50649 (Tweak `nearest_common_ancestor()`.)
 - #50790 (Fix grammar documentation wrt Unicode identifiers)
 - #50791 (Fix null exclusions in grammar docs)
 - #50806 (Add `bless` x.py subcommand for easy ui test replacement)
 - #50818 (Speed up `opt_normalize_projection_type`)
 - #50837 (Revert #49767)
 - #50839 (Make sure people know the book is free oline)

Failed merges:
@bors bors merged commit 3f6b3bb into rust-lang:master May 18, 2018
dtolnay added a commit to dtolnay/rust that referenced this pull request Nov 28, 2019
Previously:

    error: invalid format string: invalid argument name `_x`
     --> src/main.rs:2:16
      |
    2 |     println!("{_x}", a=0);
      |                ^^ invalid argument name in format string
      |
      = note: argument names cannot start with an underscore

Not supporting identifiers starting with underscore appears to have been
an arbitrary limitation from 2013 in code that was most likely never
reviewed:
https://github.com/rust-lang/rust/pull/8245/files#diff-0347868ef389c805e97636623e4a4ea6R277

The error message was dutifully improved in rust-lang#50610 but is there any
reason that leading underscore would be a special case?

This commit updates the format_args parser to accept identifiers with
leading underscores.
dtolnay added a commit to dtolnay/rust that referenced this pull request Nov 30, 2019
Allow any identifier as format arg name

Previously:

```console
error: invalid format string: invalid argument name `_x`
 --> src/main.rs:2:16
  |
2 |     println!("{_x}", _x=0);
  |                ^^ invalid argument name in format string
  |
  = note: argument names cannot start with an underscore
```

Not supporting identifiers starting with underscore appears to have been an arbitrary limitation from 2013 in code that was most likely never reviewed: https://github.com/rust-lang/rust/pull/8245/files#diff-0347868ef389c805e97636623e4a4ea6R277

The error message was dutifully improved in rust-lang#50610 but is there any reason that leading underscore would be a special case?

This commit updates the format_args parser to accept identifiers with leading underscores.
@estebank estebank deleted the fmt-str branch November 9, 2023 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants