]> git.proxmox.com Git - rustc.git/blob - src/test/ui/fmt/format-args-capture.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / src / test / ui / fmt / format-args-capture.rs
1 // run-pass
2 #![feature(format_args_capture)]
3 #![feature(cfg_panic)]
4
5 fn main() {
6 named_argument_takes_precedence_to_captured();
7 formatting_parameters_can_be_captured();
8
9 #[cfg(panic = "unwind")]
10 {
11 panic_with_single_argument_does_not_get_formatted();
12 panic_with_multiple_arguments_is_formatted();
13 }
14 }
15
16 fn named_argument_takes_precedence_to_captured() {
17 let foo = "captured";
18 let s = format!("{foo}", foo="named");
19 assert_eq!(&s, "named");
20
21 let s = format!("{foo}-{foo}-{foo}", foo="named");
22 assert_eq!(&s, "named-named-named");
23
24 let s = format!("{}-{bar}-{foo}", "positional", bar="named");
25 assert_eq!(&s, "positional-named-captured");
26 }
27
28 #[cfg(panic = "unwind")]
29 fn panic_with_single_argument_does_not_get_formatted() {
30 // panic! with a single argument does not perform string formatting.
31 // RFC #2795 suggests that this may need to change so that captured arguments are formatted.
32 // For stability reasons this will need to part of an edition change.
33
34 #[allow(non_fmt_panic)]
35 let msg = std::panic::catch_unwind(|| {
36 panic!("{foo}");
37 }).unwrap_err();
38
39 assert_eq!(msg.downcast_ref::<&str>(), Some(&"{foo}"))
40 }
41
42 #[cfg(panic = "unwind")]
43 fn panic_with_multiple_arguments_is_formatted() {
44 let foo = "captured";
45
46 let msg = std::panic::catch_unwind(|| {
47 panic!("{}-{bar}-{foo}", "positional", bar="named");
48 }).unwrap_err();
49
50 assert_eq!(msg.downcast_ref::<String>(), Some(&"positional-named-captured".to_string()))
51 }
52
53 fn formatting_parameters_can_be_captured() {
54 let width = 9;
55 let precision = 3;
56
57 let x = 7.0;
58
59 let s = format!("{x:width$}");
60 assert_eq!(&s, " 7");
61
62 let s = format!("{x:<width$}");
63 assert_eq!(&s, "7 ");
64
65 let s = format!("{x:-^width$}");
66 assert_eq!(&s, "----7----");
67
68 let s = format!("{x:-^width$.precision$}");
69 assert_eq!(&s, "--7.000--");
70 }