-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Don't allow {} to refer to implicit captures in format_args. #93394
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fn main() { | ||
let a = "a"; | ||
let b = "b"; | ||
|
||
println!("{a} {b} {} {} {c} {}", c = "c"); | ||
//~^ ERROR: invalid reference to positional arguments 1 and 2 (there is 1 argument) | ||
|
||
let n = 1; | ||
println!("{a:.n$} {b:.*}"); | ||
//~^ ERROR: invalid reference to positional argument 0 (no arguments were given) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: invalid reference to positional arguments 1 and 2 (there is 1 argument) | ||
--> $DIR/format-args-capture-issue-93378.rs:5:26 | ||
| | ||
LL | println!("{a} {b} {} {} {c} {}", c = "c"); | ||
| ^^ ^^ | ||
| | ||
= note: positional arguments are zero-based | ||
|
||
error: invalid reference to positional argument 0 (no arguments were given) | ||
--> $DIR/format-args-capture-issue-93378.rs:9:23 | ||
| | ||
LL | println!("{a:.n$} {b:.*}"); | ||
| ------- ^^^--^ | ||
| | | | ||
| | this precision flag adds an extra required argument at position 0, which is why there are 3 arguments expected | ||
Comment on lines
+9
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This output needs some work to make it clearer :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there's quite a few cases right now where this macro produces suboptimal diagnostics. We have plans to change how |
||
| this parameter corresponds to the precision flag | ||
| | ||
= note: positional arguments are zero-based | ||
= note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html | ||
|
||
error: aborting due to 2 previous errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally this (positive) branch would result in a message
1 positional argument in format string, but no arguments were given
, whereas now adding an implicit capture getsinvalid reference to positional argument 0 (no arguments were given)
. Ideally this wording would be kept, even with implicit args; I think the proper check would then beself.names.len() == self.num_captured_args && !numbered_position_args && count != self.num_args()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the
let count =
above would also have to be adjusted to exclude all placeholders that refer to captures, which is a bit more complicated.I agree the diagnostics here can be improved when there's captured arguments involved, but I rather leave that to a separate change that can fix all those cases properly.