about summary refs log tree commit diff
path: root/src/test/ui/fmt
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/ui/fmt')
-rw-r--r--src/test/ui/fmt/ifmt-bad-arg.rs94
-rw-r--r--src/test/ui/fmt/ifmt-bad-arg.stderr327
-rw-r--r--src/test/ui/fmt/ifmt-bad-format-args.rs4
-rw-r--r--src/test/ui/fmt/ifmt-bad-format-args.stderr21
-rw-r--r--src/test/ui/fmt/ifmt-unimpl.rs4
-rw-r--r--src/test/ui/fmt/ifmt-unimpl.stderr13
-rw-r--r--src/test/ui/fmt/ifmt-unknown-trait.rs4
-rw-r--r--src/test/ui/fmt/ifmt-unknown-trait.stderr19
8 files changed, 486 insertions, 0 deletions
diff --git a/src/test/ui/fmt/ifmt-bad-arg.rs b/src/test/ui/fmt/ifmt-bad-arg.rs
new file mode 100644
index 00000000000..a0b0a8fb985
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-bad-arg.rs
@@ -0,0 +1,94 @@
+fn main() {
+    // bad arguments to the format! call
+
+    // bad number of arguments, see #44954 (originally #15780)
+
+    format!("{}");
+    //~^ ERROR: 1 positional argument in format string, but no arguments were given
+
+    format!("{1}", 1);
+    //~^ ERROR: invalid reference to positional argument 1 (there is 1 argument)
+    //~^^ ERROR: argument never used
+
+    format!("{} {}");
+    //~^ ERROR: 2 positional arguments in format string, but no arguments were given
+
+    format!("{0} {1}", 1);
+    //~^ ERROR: invalid reference to positional argument 1 (there is 1 argument)
+
+    format!("{0} {1} {2}", 1, 2);
+    //~^ ERROR: invalid reference to positional argument 2 (there are 2 arguments)
+
+    format!("{} {value} {} {}", 1, value=2);
+    //~^ ERROR: invalid reference to positional argument 2 (there are 2 arguments)
+    format!("{name} {value} {} {} {} {} {} {}", 0, name=1, value=2);
+    //~^ ERROR: invalid reference to positional arguments 3, 4 and 5 (there are 3 arguments)
+
+    format!("{} {foo} {} {bar} {}", 1, 2, 3);
+    //~^ ERROR: there is no argument named `foo`
+    //~^^ ERROR: there is no argument named `bar`
+
+    format!("{foo}");                //~ ERROR: no argument named `foo`
+    format!("", 1, 2);               //~ ERROR: multiple unused formatting arguments
+    format!("{}", 1, 2);             //~ ERROR: argument never used
+    format!("{1}", 1, 2);            //~ ERROR: argument never used
+    format!("{}", 1, foo=2);         //~ ERROR: named argument never used
+    format!("{foo}", 1, foo=2);      //~ ERROR: argument never used
+    format!("", foo=2);              //~ ERROR: named argument never used
+    format!("{} {}", 1, 2, foo=1, bar=2);  //~ ERROR: multiple unused formatting arguments
+
+    format!("{foo}", foo=1, foo=2);  //~ ERROR: duplicate argument
+    format!("{foo} {} {}", foo=1, 2);   //~ ERROR: positional arguments cannot follow
+
+    // bad named arguments, #35082
+
+    format!("{valuea} {valueb}", valuea=5, valuec=7);
+    //~^ ERROR there is no argument named `valueb`
+    //~^^ ERROR named argument never used
+
+    // bad syntax of the format string
+
+    format!("{"); //~ ERROR: expected `'}'` but string was terminated
+
+    format!("foo } bar"); //~ ERROR: unmatched `}` found
+    format!("foo }"); //~ ERROR: unmatched `}` found
+
+    format!("foo %s baz", "bar"); //~ ERROR: argument never used
+
+    format!(r##"
+
+        {foo}
+
+    "##);
+    //~^^^ ERROR: there is no argument named `foo`
+
+    // bad syntax in format string with multiple newlines, #53836
+    format!("first number: {}
+second number: {}
+third number: {}
+fourth number: {}
+fifth number: {}
+sixth number: {}
+seventh number: {}
+eighth number: {}
+ninth number: {
+tenth number: {}",
+        1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+    //~^^ ERROR: invalid format string
+    println!("{} {:.*} {}", 1, 3.2, 4);
+    //~^ ERROR 4 positional arguments in format string, but there are 3 arguments
+    //~| ERROR mismatched types
+    println!("{} {:07$.*} {}", 1, 3.2, 4);
+    //~^ ERROR 4 positional arguments in format string, but there are 3 arguments
+    //~| ERROR mismatched types
+    println!("{} {:07$} {}", 1, 3.2, 4);
+    //~^ ERROR invalid reference to positional argument 7 (there are 3 arguments)
+    println!("{:foo}", 1); //~ ERROR unknown format trait `foo`
+    println!("{5} {:4$} {6:7$}", 1);
+    //~^ ERROR invalid reference to positional arguments 4, 5, 6 and 7 (there is 1 argument)
+
+    // We used to ICE here because we tried to unconditionally access the first argument, which
+    // doesn't exist.
+    println!("{:.*}");
+    //~^ ERROR 2 positional arguments in format string, but no arguments were given
+}
diff --git a/src/test/ui/fmt/ifmt-bad-arg.stderr b/src/test/ui/fmt/ifmt-bad-arg.stderr
new file mode 100644
index 00000000000..0ff478826f7
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-bad-arg.stderr
@@ -0,0 +1,327 @@
+error: 1 positional argument in format string, but no arguments were given
+  --> $DIR/ifmt-bad-arg.rs:6:14
+   |
+LL |     format!("{}");
+   |              ^^
+
+error: invalid reference to positional argument 1 (there is 1 argument)
+  --> $DIR/ifmt-bad-arg.rs:9:14
+   |
+LL |     format!("{1}", 1);
+   |              ^^^
+   |
+   = note: positional arguments are zero-based
+
+error: argument never used
+  --> $DIR/ifmt-bad-arg.rs:9:20
+   |
+LL |     format!("{1}", 1);
+   |             -----  ^ argument never used
+   |             |
+   |             formatting specifier missing
+
+error: 2 positional arguments in format string, but no arguments were given
+  --> $DIR/ifmt-bad-arg.rs:13:14
+   |
+LL |     format!("{} {}");
+   |              ^^ ^^
+
+error: invalid reference to positional argument 1 (there is 1 argument)
+  --> $DIR/ifmt-bad-arg.rs:16:18
+   |
+LL |     format!("{0} {1}", 1);
+   |                  ^^^
+   |
+   = note: positional arguments are zero-based
+
+error: invalid reference to positional argument 2 (there are 2 arguments)
+  --> $DIR/ifmt-bad-arg.rs:19:22
+   |
+LL |     format!("{0} {1} {2}", 1, 2);
+   |                      ^^^
+   |
+   = note: positional arguments are zero-based
+
+error: invalid reference to positional argument 2 (there are 2 arguments)
+  --> $DIR/ifmt-bad-arg.rs:22:28
+   |
+LL |     format!("{} {value} {} {}", 1, value=2);
+   |                            ^^
+   |
+   = note: positional arguments are zero-based
+
+error: invalid reference to positional arguments 3, 4 and 5 (there are 3 arguments)
+  --> $DIR/ifmt-bad-arg.rs:24:38
+   |
+LL |     format!("{name} {value} {} {} {} {} {} {}", 0, name=1, value=2);
+   |                                      ^^ ^^ ^^
+   |
+   = note: positional arguments are zero-based
+
+error: there is no argument named `foo`
+  --> $DIR/ifmt-bad-arg.rs:27:17
+   |
+LL |     format!("{} {foo} {} {bar} {}", 1, 2, 3);
+   |                 ^^^^^
+   |
+   = help: if you intended to capture `foo` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
+
+error: there is no argument named `bar`
+  --> $DIR/ifmt-bad-arg.rs:27:26
+   |
+LL |     format!("{} {foo} {} {bar} {}", 1, 2, 3);
+   |                          ^^^^^
+   |
+   = help: if you intended to capture `bar` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
+
+error: there is no argument named `foo`
+  --> $DIR/ifmt-bad-arg.rs:31:14
+   |
+LL |     format!("{foo}");
+   |              ^^^^^
+   |
+   = help: if you intended to capture `foo` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
+
+error: multiple unused formatting arguments
+  --> $DIR/ifmt-bad-arg.rs:32:17
+   |
+LL |     format!("", 1, 2);
+   |             --  ^  ^ argument never used
+   |             |   |
+   |             |   argument never used
+   |             multiple missing formatting specifiers
+
+error: argument never used
+  --> $DIR/ifmt-bad-arg.rs:33:22
+   |
+LL |     format!("{}", 1, 2);
+   |             ----     ^ argument never used
+   |             |
+   |             formatting specifier missing
+
+error: argument never used
+  --> $DIR/ifmt-bad-arg.rs:34:20
+   |
+LL |     format!("{1}", 1, 2);
+   |             -----  ^ argument never used
+   |             |
+   |             formatting specifier missing
+
+error: named argument never used
+  --> $DIR/ifmt-bad-arg.rs:35:26
+   |
+LL |     format!("{}", 1, foo=2);
+   |             ----         ^ named argument never used
+   |             |
+   |             formatting specifier missing
+
+error: argument never used
+  --> $DIR/ifmt-bad-arg.rs:36:22
+   |
+LL |     format!("{foo}", 1, foo=2);
+   |             -------  ^ argument never used
+   |             |
+   |             formatting specifier missing
+
+error: named argument never used
+  --> $DIR/ifmt-bad-arg.rs:37:21
+   |
+LL |     format!("", foo=2);
+   |             --      ^ named argument never used
+   |             |
+   |             formatting specifier missing
+
+error: multiple unused formatting arguments
+  --> $DIR/ifmt-bad-arg.rs:38:32
+   |
+LL |     format!("{} {}", 1, 2, foo=1, bar=2);
+   |             -------            ^      ^ named argument never used
+   |             |                  |
+   |             |                  named argument never used
+   |             multiple missing formatting specifiers
+
+error: duplicate argument named `foo`
+  --> $DIR/ifmt-bad-arg.rs:40:33
+   |
+LL |     format!("{foo}", foo=1, foo=2);
+   |                          -      ^ duplicate argument
+   |                          |
+   |                          previously here
+
+error: positional arguments cannot follow named arguments
+  --> $DIR/ifmt-bad-arg.rs:41:35
+   |
+LL |     format!("{foo} {} {}", foo=1, 2);
+   |                                -  ^ positional arguments must be before named arguments
+   |                                |
+   |                                named argument
+
+error: there is no argument named `valueb`
+  --> $DIR/ifmt-bad-arg.rs:45:23
+   |
+LL |     format!("{valuea} {valueb}", valuea=5, valuec=7);
+   |                       ^^^^^^^^
+   |
+   = help: if you intended to capture `valueb` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
+
+error: named argument never used
+  --> $DIR/ifmt-bad-arg.rs:45:51
+   |
+LL |     format!("{valuea} {valueb}", valuea=5, valuec=7);
+   |             -------------------                   ^ named argument never used
+   |             |
+   |             formatting specifier missing
+
+error: invalid format string: expected `'}'` but string was terminated
+  --> $DIR/ifmt-bad-arg.rs:51:15
+   |
+LL |     format!("{");
+   |              -^ expected `'}'` in format string
+   |              |
+   |              because of this opening brace
+   |
+   = note: if you intended to print `{`, you can escape it using `{{`
+
+error: invalid format string: unmatched `}` found
+  --> $DIR/ifmt-bad-arg.rs:53:18
+   |
+LL |     format!("foo } bar");
+   |                  ^ unmatched `}` in format string
+   |
+   = note: if you intended to print `}`, you can escape it using `}}`
+
+error: invalid format string: unmatched `}` found
+  --> $DIR/ifmt-bad-arg.rs:54:18
+   |
+LL |     format!("foo }");
+   |                  ^ unmatched `}` in format string
+   |
+   = note: if you intended to print `}`, you can escape it using `}}`
+
+error: argument never used
+  --> $DIR/ifmt-bad-arg.rs:56:27
+   |
+LL |     format!("foo %s baz", "bar");
+   |                  --       ^^^^^ argument never used
+   |                  |
+   |                  help: format specifiers use curly braces: `{}`
+   |
+   = note: printf formatting not supported; see the documentation for `std::fmt`
+
+error: there is no argument named `foo`
+  --> $DIR/ifmt-bad-arg.rs:60:9
+   |
+LL |         {foo}
+   |         ^^^^^
+   |
+   = help: if you intended to capture `foo` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
+
+error: invalid format string: expected `'}'`, found `'t'`
+  --> $DIR/ifmt-bad-arg.rs:75:1
+   |
+LL | ninth number: {
+   |               - because of this opening brace
+LL | tenth number: {}",
+   | ^ expected `}` in format string
+   |
+   = note: if you intended to print `{`, you can escape it using `{{`
+
+error: 4 positional arguments in format string, but there are 3 arguments
+  --> $DIR/ifmt-bad-arg.rs:78:15
+   |
+LL |     println!("{} {:.*} {}", 1, 3.2, 4);
+   |               ^^ ^^--^ ^^   -  ---  -
+   |                    |           |
+   |                    |           this parameter corresponds to the precision flag
+   |                    this precision flag adds an extra required argument at position 1, which is why there are 4 arguments expected
+   |
+   = note: positional arguments are zero-based
+   = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
+
+error: 4 positional arguments in format string, but there are 3 arguments
+  --> $DIR/ifmt-bad-arg.rs:81:15
+   |
+LL |     println!("{} {:07$.*} {}", 1, 3.2, 4);
+   |               ^^ ^^^----^ ^^   -  ---  -
+   |                     | |           |
+   |                     | |           this parameter corresponds to the precision flag
+   |                     | this precision flag adds an extra required argument at position 1, which is why there are 4 arguments expected
+   |                     this width flag expects an `usize` argument at position 7, but there are 3 arguments
+   |
+   = note: positional arguments are zero-based
+   = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
+
+error: invalid reference to positional argument 7 (there are 3 arguments)
+  --> $DIR/ifmt-bad-arg.rs:84:18
+   |
+LL |     println!("{} {:07$} {}", 1, 3.2, 4);
+   |                  ^^^--^
+   |                     |
+   |                     this width flag expects an `usize` argument at position 7, but there are 3 arguments
+   |
+   = note: positional arguments are zero-based
+   = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
+
+error: unknown format trait `foo`
+  --> $DIR/ifmt-bad-arg.rs:86:17
+   |
+LL |     println!("{:foo}", 1);
+   |                 ^^^
+   |
+   = note: the only appropriate formatting traits are:
+           - ``, which uses the `Display` trait
+           - `?`, which uses the `Debug` trait
+           - `e`, which uses the `LowerExp` trait
+           - `E`, which uses the `UpperExp` trait
+           - `o`, which uses the `Octal` trait
+           - `p`, which uses the `Pointer` trait
+           - `b`, which uses the `Binary` trait
+           - `x`, which uses the `LowerHex` trait
+           - `X`, which uses the `UpperHex` trait
+
+error: invalid reference to positional arguments 4, 5, 6 and 7 (there is 1 argument)
+  --> $DIR/ifmt-bad-arg.rs:87:15
+   |
+LL |     println!("{5} {:4$} {6:7$}", 1);
+   |               ^^^ ^^--^ ^^^--^
+   |                     |      |
+   |                     |      this width flag expects an `usize` argument at position 7, but there is 1 argument
+   |                     this width flag expects an `usize` argument at position 4, but there is 1 argument
+   |
+   = note: positional arguments are zero-based
+   = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
+
+error: 2 positional arguments in format string, but no arguments were given
+  --> $DIR/ifmt-bad-arg.rs:92:15
+   |
+LL |     println!("{:.*}");
+   |               ^^--^
+   |                 |
+   |                 this precision flag adds an extra required argument at position 0, which is why there are 2 arguments expected
+   |
+   = note: positional arguments are zero-based
+   = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
+
+error[E0308]: mismatched types
+  --> $DIR/ifmt-bad-arg.rs:78:32
+   |
+LL |     println!("{} {:.*} {}", 1, 3.2, 4);
+   |                                ^^^ expected `usize`, found floating-point number
+   |
+   = note: expected reference `&usize`
+              found reference `&{float}`
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error[E0308]: mismatched types
+  --> $DIR/ifmt-bad-arg.rs:81:35
+   |
+LL |     println!("{} {:07$.*} {}", 1, 3.2, 4);
+   |                                   ^^^ expected `usize`, found floating-point number
+   |
+   = note: expected reference `&usize`
+              found reference `&{float}`
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 36 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/src/test/ui/fmt/ifmt-bad-format-args.rs b/src/test/ui/fmt/ifmt-bad-format-args.rs
new file mode 100644
index 00000000000..ba7301561bd
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-bad-format-args.rs
@@ -0,0 +1,4 @@
+fn main() {
+    format_args!(); //~ ERROR: requires at least a format string argument
+    format_args!(|| {}); //~ ERROR: must be a string literal
+}
diff --git a/src/test/ui/fmt/ifmt-bad-format-args.stderr b/src/test/ui/fmt/ifmt-bad-format-args.stderr
new file mode 100644
index 00000000000..8bb0d40629f
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-bad-format-args.stderr
@@ -0,0 +1,21 @@
+error: requires at least a format string argument
+  --> $DIR/ifmt-bad-format-args.rs:2:5
+   |
+LL |     format_args!();
+   |     ^^^^^^^^^^^^^^^
+   |
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: format argument must be a string literal
+  --> $DIR/ifmt-bad-format-args.rs:3:18
+   |
+LL |     format_args!(|| {});
+   |                  ^^^^^
+   |
+help: you might be missing a string literal to format with
+   |
+LL |     format_args!("{}", || {});
+   |                  ^^^^^
+
+error: aborting due to 2 previous errors
+
diff --git a/src/test/ui/fmt/ifmt-unimpl.rs b/src/test/ui/fmt/ifmt-unimpl.rs
new file mode 100644
index 00000000000..258f4eea509
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-unimpl.rs
@@ -0,0 +1,4 @@
+fn main() {
+    format!("{:X}", "3");
+    //~^ ERROR: `str: UpperHex` is not satisfied
+}
diff --git a/src/test/ui/fmt/ifmt-unimpl.stderr b/src/test/ui/fmt/ifmt-unimpl.stderr
new file mode 100644
index 00000000000..65b0f4a09b3
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-unimpl.stderr
@@ -0,0 +1,13 @@
+error[E0277]: the trait bound `str: UpperHex` is not satisfied
+  --> $DIR/ifmt-unimpl.rs:2:21
+   |
+LL |     format!("{:X}", "3");
+   |                     ^^^ the trait `UpperHex` is not implemented for `str`
+   |
+   = note: required because of the requirements on the impl of `UpperHex` for `&str`
+   = note: required by `std::fmt::UpperHex::fmt`
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/fmt/ifmt-unknown-trait.rs b/src/test/ui/fmt/ifmt-unknown-trait.rs
new file mode 100644
index 00000000000..158152c89a4
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-unknown-trait.rs
@@ -0,0 +1,4 @@
+fn main() {
+    format!("{:notimplemented}", "3");
+    //~^ ERROR: unknown format trait `notimplemented`
+}
diff --git a/src/test/ui/fmt/ifmt-unknown-trait.stderr b/src/test/ui/fmt/ifmt-unknown-trait.stderr
new file mode 100644
index 00000000000..459432bf4e4
--- /dev/null
+++ b/src/test/ui/fmt/ifmt-unknown-trait.stderr
@@ -0,0 +1,19 @@
+error: unknown format trait `notimplemented`
+  --> $DIR/ifmt-unknown-trait.rs:2:16
+   |
+LL |     format!("{:notimplemented}", "3");
+   |                ^^^^^^^^^^^^^^
+   |
+   = note: the only appropriate formatting traits are:
+           - ``, which uses the `Display` trait
+           - `?`, which uses the `Debug` trait
+           - `e`, which uses the `LowerExp` trait
+           - `E`, which uses the `UpperExp` trait
+           - `o`, which uses the `Octal` trait
+           - `p`, which uses the `Pointer` trait
+           - `b`, which uses the `Binary` trait
+           - `x`, which uses the `LowerHex` trait
+           - `X`, which uses the `UpperHex` trait
+
+error: aborting due to previous error
+