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

Make positional argument error in format! clearer #45807

Merged
merged 4 commits into from
Nov 11, 2017

Conversation

tommyip
Copy link
Contributor

@tommyip tommyip commented Nov 6, 2017

r? @estebank

Fixes #44954


format!("{} {value} {} {}", 1, value=2);
//~^ ERROR: invalid reference to positional argument 2 (there are only 2 arguments)
format!("{name} {value} {} {} {} {} {} {}", 0, name=1, value=2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a case with multiple named arguments that are not used, mixed with positional arguments? I'm curious about the new output.

.map(|r| r.to_string())
.collect();

let msg = if self.names.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add another check for self.args.iter().filter_map(|arg| /* Some(arg) if numbered positional argument */).count() == 0 here, so that if there are arguments like {1}, it goes to the else branch and it is explicit about the arguments that are wrong.

Copy link
Contributor Author

@tommyip tommyip Nov 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the position of numbered and implicit placeholder is already resolved in the parser, should we change it so we still have that information here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you prefer, that's also a reasonable change.

self.describe_num_args())
};

self.ecx.span_err(self.fmtsp, &msg[..]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add a note making it explicit that positional arguments are 0 indexed, probably only when it is needed (I think only for the else branch).

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 7, 2017
@tommyip
Copy link
Contributor Author

tommyip commented Nov 9, 2017

@estebank this should work now.

Could you add a case with multiple named arguments that are not used, mixed with positional arguments? I'm curious about the new output.

Yeah the output is incorrect, fixed that in the latest commit (test case: https://github.com/tommyip/rust/blob/1be9cc76daa1a5047fadb0bb0e826b47a63d8f1b/src/test/compile-fail/ifmt-bad-arg.rs#L37)

@estebank
Copy link
Contributor

estebank commented Nov 9, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 9, 2017

📌 Commit b577b9a has been approved by estebank

@kennytm kennytm 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 Nov 10, 2017
@bors
Copy link
Contributor

bors commented Nov 11, 2017

⌛ Testing commit b577b9a with merge bd7ce71...

bors added a commit that referenced this pull request Nov 11, 2017
Make positional argument error in format! clearer

r? @estebank

Fixes #44954
@bors
Copy link
Contributor

bors commented Nov 11, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: estebank
Pushing bd7ce71 to master...

@bors bors merged commit b577b9a into rust-lang:master Nov 11, 2017
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.

4 participants