-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Make format_args!("literal") const. #87005
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![crate_type = "lib"] | ||
|
||
const A: std::fmt::Arguments = format_args!("literal"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is insta-stable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think it's just not gated in this WIP form of the PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. That's why I added |
||
|
||
const B: std::fmt::Arguments = format_args!("{}", 123); | ||
//~^ ERROR calls in constants are limited to | ||
//~| ERROR calls in constants are limited to | ||
//~| ERROR temporary value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are there 3 errors here and not just the one we'd expect? I guess that comes from the extra code for the argument There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It expands to ::core::fmt::Arguments::new_v1(
&[""],
&match (&123,) {
(arg0,) => [::core::fmt::ArgumentV1::new(
arg0,
::core::fmt::Display::fmt,
)],
},
); The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah right that would not be promoted. const B: () = {
let _ = format_args!(...);
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants | ||
--> $DIR/format_args.rs:5:32 | ||
| | ||
LL | const B: std::fmt::Arguments = format_args!("{}", 123); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants | ||
--> $DIR/format_args.rs:5:32 | ||
| | ||
LL | const B: std::fmt::Arguments = format_args!("{}", 123); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/format_args.rs:5:32 | ||
| | ||
LL | const B: std::fmt::Arguments = format_args!("{}", 123); | ||
| ^^^^^^^^^^^^^^^^^^^^^^- | ||
| | | | ||
| | temporary value is freed at the end of this statement | ||
| creates a temporary which is freed while still in use | ||
| using this value as a constant requires that borrow lasts for `'static` | ||
| | ||
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0015, E0716. | ||
For more information about an error, try `rustc --explain E0015`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to make it const-unstable at the same time under a different feature gate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is const-unstable (implicitly). But the
format_args!
macro expanded code has some kind of span that still lets stable code call this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the
fmt_internals
feature is allowed:rust/library/core/src/macros/mod.rs
Line 826 in a84d1b2
Using a different feature for const checking would require this feature to be enabled by the user of
format_args!
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason i want this is so
panic!()
can use this for const panic. (See #86998) Putting anallow_internal_unstable(const_fmt_internals)
on thepanic
macro won't work through the nested format_args call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would at least prevent it from being insta-stable, right? Maybe use
const_panic
as feature gate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should just go for the
const_format_args!()
macro for now (#86998) and at some point stabilizeformat_args!()
in const entirely. (And then removeconst_format_args!()
again.)