Skip to content

Commit d19b64f

Browse files
committed
Auto merge of rust-lang#109999 - m-ou-se:flatten-format-args, r=oli-obk
Enable flatten-format-args by default. Part of rust-lang#99012. This enables the `flatten-format-args` feature that was added by rust-lang#106824: > This change inlines string literals, integer literals and nested format_args!() into format_args!() during ast lowering, making all of the following pairs result in equivalent hir: > > ```rust > println!("Hello, {}!", "World"); > println!("Hello, World!"); > ``` > > ```rust > println!("[info] {}", format_args!("error")); > println!("[info] error"); > ``` > > ```rust > println!("[{}] {}", status, format_args!("error: {}", msg)); > println!("[{}] error: {}", status, msg); > ``` > > ```rust > println!("{} + {} = {}", 1, 2, 1 + 2); > println!("1 + 2 = {}", 1 + 2); > ``` > > And so on. > > This is useful for macros. E.g. a `log::info!()` macro could just pass the tokens from the user directly into a `format_args!()` that gets efficiently flattened/inlined into a `format_args!("info: {}")`. > > It also means that `dbg!(x)` will have its file, line, and expression name inlined: > > ```rust > eprintln!("[{}:{}] {} = {:#?}", file!(), line!(), stringify!(x), x); // before > eprintln!("[example.rs:1] x = {:#?}", x); // after > ``` > > Which can be nice in some cases, but also means a lot more unique static strings than before if dbg!() is used a lot. This is mostly an optimization, except that it will be visible through [`fmt::Arguments::as_str()`](https://doc.rust-lang.org/nightly/std/fmt/struct.Arguments.html#method.as_str). In rust-lang#106823, there was already a libs-api FCP about the documentation of `fmt::Arguments::as_str()` to allow it to give `Some` rather than `None` depending on optimizations like this. That was just a documentation update though. This PR is the one that actually makes the user visible change: ```rust assert_eq!(format_args!("abc").as_str(), Some("abc")); // Unchanged. assert_eq!(format_args!("ab{}", "c").as_str(), Some("abc")); // Was `None` before! ```
2 parents 8bdcc62 + 2cd5ce0 commit d19b64f

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ fn test_unstable_options_tracking_hash() {
747747
tracked!(emit_thin_lto, false);
748748
tracked!(export_executable_symbols, true);
749749
tracked!(fewer_names, Some(true));
750-
tracked!(flatten_format_args, true);
750+
tracked!(flatten_format_args, false);
751751
tracked!(force_unstable_if_unmarked, true);
752752
tracked!(fuel, Some(("abc".to_string(), 99)));
753753
tracked!(function_sections, Some(false));

compiler/rustc_session/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1452,9 +1452,9 @@ options! {
14521452
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
14531453
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
14541454
(default: no)"),
1455-
flatten_format_args: bool = (false, parse_bool, [TRACKED],
1455+
flatten_format_args: bool = (true, parse_bool, [TRACKED],
14561456
"flatten nested format_args!() and literals into a simplified format_args!() call \
1457-
(default: no)"),
1457+
(default: yes)"),
14581458
force_unstable_if_unmarked: bool = (false, parse_bool, [TRACKED],
14591459
"force all crates to be `rustc_private` unstable (default: no)"),
14601460
fuel: Option<(String, u64)> = (None, parse_optimization_fuel, [TRACKED],

src/tools/clippy/src/driver.rs

+3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
160160
// MIR passes can be enabled / disabled separately, we should figure out, what passes to
161161
// use for Clippy.
162162
config.opts.unstable_opts.mir_opt_level = Some(0);
163+
164+
// Disable flattening and inlining of format_args!(), so the HIR matches with the AST.
165+
config.opts.unstable_opts.flatten_format_args = false;
163166
}
164167
}
165168

0 commit comments

Comments
 (0)