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

Confusing error message with invalid format specifier #23476

Closed
mrmonday opened this issue Mar 18, 2015 · 1 comment
Closed

Confusing error message with invalid format specifier #23476

mrmonday opened this issue Mar 18, 2015 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@mrmonday
Copy link
Contributor

The following code:

fn main() {
    let _ = format!("{_foo}", _foo = 6u);
}

(Playpen: http://is.gd/me8pNE)

Gives the error:

<anon>:2:21: 2:29 error: invalid format string: expected `'}'`, found `'_'`
<anon>:2     let _ = format!("{_foo}", _foo = 6u);
                             ^~~~~~~~
error: aborting due to previous error

It is not too confusing in this short example, but in a longer string it is not obvious that named identifiers cannot start with an underscore.

@kmcallister kmcallister added the A-syntaxext Area: Syntax extensions label Mar 18, 2015
@steveklabnik
Copy link
Member

Updated code:

fn main() {
    let _ = format!("{_foo}", _foo = 6usize);
}

this still happens though.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label May 1, 2018
bors added a commit that referenced this issue 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.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants