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

Don't allow {} to refer to implicit captures in format_args. #93394

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum ArgumentType {

enum Position {
Exact(usize),
Capture(usize),
Named(Symbol),
}

Expand All @@ -47,6 +48,8 @@ struct Context<'a, 'b> {
/// * `arg_unique_types` (in simplified JSON): `[["o", "x"], ["o", "x"], ["o", "x"]]`
/// * `names` (in JSON): `{"foo": 2}`
args: Vec<P<ast::Expr>>,
/// The number of arguments that were added by implicit capturing.
num_captured_args: usize,
/// Placeholder slot numbers indexed by argument.
arg_types: Vec<Vec<usize>>,
/// Unique format specs seen for each argument.
Expand Down Expand Up @@ -229,6 +232,11 @@ fn parse_args<'a>(
}

impl<'a, 'b> Context<'a, 'b> {
/// The number of arguments that were explicitly given.
fn num_args(&self) -> usize {
self.args.len() - self.num_captured_args
}

fn resolve_name_inplace(&self, p: &mut parse::Piece<'_>) {
// NOTE: the `unwrap_or` branch is needed in case of invalid format
// arguments, e.g., `format_args!("{foo}")`.
Expand Down Expand Up @@ -343,7 +351,7 @@ impl<'a, 'b> Context<'a, 'b> {
}

fn describe_num_args(&self) -> Cow<'_, str> {
match self.args.len() {
match self.num_args() {
0 => "no arguments were given".into(),
1 => "there is 1 argument".into(),
x => format!("there are {} arguments", x).into(),
Expand All @@ -369,7 +377,7 @@ impl<'a, 'b> Context<'a, 'b> {

let count = self.pieces.len()
+ self.arg_with_formatting.iter().filter(|fmt| fmt.precision_span.is_some()).count();
if self.names.is_empty() && !numbered_position_args && count != self.args.len() {
if self.names.is_empty() && !numbered_position_args && count != self.num_args() {
Copy link
Contributor

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 gets invalid 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 be self.names.len() == self.num_captured_args && !numbered_position_args && count != self.num_args()?

Copy link
Member Author

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.

e = self.ecx.struct_span_err(
sp,
&format!(
Expand Down Expand Up @@ -417,7 +425,7 @@ impl<'a, 'b> Context<'a, 'b> {
if let Some(span) = fmt.precision_span {
let span = self.fmtsp.from_inner(span);
match fmt.precision {
parse::CountIsParam(pos) if pos > self.args.len() => {
parse::CountIsParam(pos) if pos > self.num_args() => {
e.span_label(
span,
&format!(
Expand Down Expand Up @@ -460,7 +468,7 @@ impl<'a, 'b> Context<'a, 'b> {
if let Some(span) = fmt.width_span {
let span = self.fmtsp.from_inner(span);
match fmt.width {
parse::CountIsParam(pos) if pos > self.args.len() => {
parse::CountIsParam(pos) if pos > self.num_args() => {
e.span_label(
span,
&format!(
Expand Down Expand Up @@ -492,12 +500,15 @@ impl<'a, 'b> Context<'a, 'b> {
/// Actually verifies and tracks a given format placeholder
/// (a.k.a. argument).
fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
if let Exact(arg) = arg {
if arg >= self.num_args() {
self.invalid_refs.push((arg, self.curpiece));
return;
}
}

match arg {
Exact(arg) => {
if self.args.len() <= arg {
self.invalid_refs.push((arg, self.curpiece));
return;
}
Exact(arg) | Capture(arg) => {
match ty {
Placeholder(_) => {
// record every (position, type) combination only once
Expand All @@ -524,7 +535,7 @@ impl<'a, 'b> Context<'a, 'b> {
match self.names.get(&name) {
Some(&idx) => {
// Treat as positional arg.
self.verify_arg_type(Exact(idx), ty)
self.verify_arg_type(Capture(idx), ty)
}
None => {
// For the moment capturing variables from format strings expanded from macros is
Expand All @@ -539,9 +550,10 @@ impl<'a, 'b> Context<'a, 'b> {
} else {
self.fmtsp
};
self.num_captured_args += 1;
self.args.push(self.ecx.expr_ident(span, Ident::new(name, span)));
self.names.insert(name, idx);
self.verify_arg_type(Exact(idx), ty)
self.verify_arg_type(Capture(idx), ty)
} else {
let msg = format!("there is no argument named `{}`", name);
let sp = if self.is_literal {
Expand Down Expand Up @@ -1010,6 +1022,7 @@ pub fn expand_preparsed_format_args(
let mut cx = Context {
ecx,
args,
num_captured_args: 0,
arg_types,
arg_unique_types,
names,
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/fmt/format-args-capture-issue-93378.rs
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)
}
22 changes: 22 additions & 0 deletions src/test/ui/fmt/format-args-capture-issue-93378.stderr
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
Copy link
Contributor

Choose a reason for hiding this comment

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

This output needs some work to make it clearer :(

Copy link
Member Author

Choose a reason for hiding this comment

The 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 fmt::Arguments is implemented/represented, which would require a lot of changes to the format_args builtin macro. That would probably be the best time to just rewrite the macro and the way it produces diagnostics.

| 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

8 changes: 8 additions & 0 deletions src/test/ui/fmt/format-args-capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fn main() {
named_argument_takes_precedence_to_captured();
formatting_parameters_can_be_captured();
capture_raw_strings_and_idents();
repeated_capture();

#[cfg(panic = "unwind")]
{
Expand Down Expand Up @@ -80,3 +81,10 @@ fn formatting_parameters_can_be_captured() {
let s = format!("{x:-^width$.precision$}");
assert_eq!(&s, "--7.000--");
}

fn repeated_capture() {
let a = 1;
let b = 2;
let s = format!("{a} {b} {a}");
assert_eq!(&s, "1 2 1");
}