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 for methods in formatting. #11724

Closed
ckarmann opened this issue Jan 22, 2014 · 6 comments
Closed

Confusing error message for methods in formatting. #11724

ckarmann opened this issue Jan 22, 2014 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ckarmann
Copy link

Someone asked this question on StackOverflow: http://stackoverflow.com/questions/21273648/how-do-you-use-the-plural-method-in-rust/21274149#21274149

I'm just learning Rust, so I tried to research the subject and I was confused by the error message from rustc:

error: argument used to format with `d` was attempted to not be used for formatting

While I'm not a native English speaker, I can usually understand any technical english text. However I find this error message very difficult to parse. What does "attempted to not be used" mean ?

So, this issue is mainly about the error message: can it be made clearer? or am I just lacking more context to understand it?

As for the StackOverflow problem itself, shouldn't an argument be usable for both format tokens {:d} and {0, format, ... } ?

@huonw
Copy link
Member

huonw commented Aug 4, 2014

Still an issue:

fn main() {
    println!("This page has been visited {} {:0$}.", 1, 2);
}
<anon>:2:54: 2:55 error: argument used to format with `` was attempted to not be used for formatting
<anon>:2     println!("This page has been visited {} {:0$}.", 1, 2);
                                                              ^

(NB. the {plural} and {select} forms have now been removed from the formatter, so this requires using a dynamic width parameter or some such.)

Explanation:

At the moment a formatting parameter can only be used for one style of printing, e.g. argument 0 can be formatted with {0} as many times as wanted or with {0:d} arbitrarily many times, but cannot be used in two contexts, e.g. you can't have "{0} {0:d}" (a somewhat artificial restriction, I guess). This error message is saying "you used it for some style of printing, and now are trying to use it for not-formatting" (using it in {plural} or as a dynamic width 0$ is using it for something other than actual printing).

I guess it could be rephrased to something like

argumented used for format with `...` was attempted to be used in a non-formatting role.

@steveklabnik
Copy link
Member

Triage: same message today.

@steveklabnik
Copy link
Member

Triage: no change

@doppioslash
Copy link

Still there...

@sophiajt
Copy link
Contributor

I don't get an error with this:

fn main() {
    println!("This page has been visited {} {:0$}.", 1, 2);
}

And for the original from so, I get:

error: invalid format string: expected `'}'`, found `','`
 --> /Users/jturner/Source/bad_error.rs:9:22
  |
9 |     let s1 = format!("This page has been visited {:d} {0, plural, one{time} other{times}}.", count);
  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

I guess this has been fixed now, or at least I can't reproduce. I'm going to go ahead and close this issue.

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 2, 2023
Fix missing parenthesis in suboptimal floating point help

This fixes rust-lang#11559 by adding a branch in the `Neg` implementation for `Sugg` that adds parentheses to keep precedence in order, then using that in the suggestion. I also removed some needless `.to_string()`s while I was at it.

---

changelog: none
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants