You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #131663 - veera-sivarajan:fix-128709-final, r=<try>
Evaluate `std::fmt::Arguments::new_const()` during Compile Time
Fixes#128709
This PR aims to optimize calls to string formating macros without any arguments by evaluating `std::fmt::Arguments::new_const()` in a const context.
Currently,
`println!("hola")` compiles to `std::io::_print(std::fmt::Arguments::new_const(&["hola\n"]))`.
With this PR,
`println!("hola")` compiles to `std::io::_print(const { std::fmt::Arguments::new_const(&["hola\n"]) })`.
This is accomplished in two steps:
1. Const stabilize `std::fmt::Arguments::new_const()`.
2. Wrap calls to `std::fmt::Arguments::new_const()` in an inline const block when lowering the AST to HIR.
This reduces the generated code to a `memcpy` instead of multiple `getelementptr` and `store` instructions even with `-C no-prepopulate-passes -C opt-level=0`. Godbolt for code comparison: https://rust.godbolt.org/z/P7Px7de6c
This is a safe and sound transformation because `std::fmt::Arguments::new_const()` is a trivial constructor function taking a slice containing a `'static` string literal as input.
CC #99012
error: `Arguments::<'a>::new_const` is not yet stable as a const fn
2
-
--> $DIR/issue-64453.rs:4:31
3
-
|
4
-
LL | static settings_dir: String = format!("");
5
-
| ^^^^^^^^^^^
6
-
|
7
-
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
8
-
= note: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
9
-
10
1
error[E0015]: cannot call non-const fn `format` in statics
0 commit comments