-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add an error return method to format
#3206
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
Comments
This sounds like something that could be added to calm_io before being added to the standard library. |
@shepmaster |
Actually, it's not clear what you mean by failing. Formatting to a string cannot fail at all beyond memory allocation. You also say "catch the compiler error", but then you show runtime code like |
@shepmaster You're right, I'm just stating my intent, there's nothing wrong with the sample code. |
|
technically it can panic if the use std::fmt;
struct Y;
impl fmt::Debug for Y {
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Err(fmt::Error)
}
}
fn main() {
dbg!(format!("{:?}", Y));
// panic: "a formatting trait implementation returned an error: Error"
} but for built-in types like str and integers they won't fail. |
@cheeroncode I'm afraid your suggestion doesn't really make sense. For your specific example, as long as your The reason your suggestion of If you are unable to generate the correct spans, then you should validate the format string yourself within your procedural macro, and report the error directly using |
Thanks, but what I really want is an error message returned by the compiler when formatting placeholders fail. |
Maybe you don't quite understand what I need, my |
sounds like what you really want is a |
As far as I can see the 'const_format' crate has a macro that produces a const function that can return an error at compile-time if the arguments do not match. But it requires the 'const_mut_refs' feature which is only available on unstable https://docs.rs/const_format/latest/const_format/macro.try_.html |
It's similar in terms of ease of use. |
@nielsle Thank you, they don't apply to me, my requirement is that I get the compiler's error in the Proc macro and decide how to deal with it, rather than having the compiler report it directly. |
This may be a good idea :> |
Uh oh!
There was an error while loading. Please reload this page.
Add an error return method to
format!
The
format!
macro never fails and the compiler reports an error when it does.Sometimes, however, I may need to reorganize the formatted string in a custom program macro based on user input.
When the user typed something wrong, I wanted to catch the compiler error and re-report the error in the right place.
I wish there was a way to return the wrong or a way to do this, thanks a lot.
Here's a scenario that could go wrong to make my intentions more obvious.
I provide my users with a macro called
comment!
, which takes a format argument likeformat!
:comment!
inserts a new line of code into methoddo
:If the user accidentally enters formatting parameters incorrectly:
Then, the newly generated code will be wrong.
The compiler will give a vague error on
#[comment(...)]
and cannot indicate the correct location:Now, I want to check that the formatting parameters are correct when I generate 'println'.
I think the best way is to catch any errors the compiler gives you,
And display the error in the corresponding position of
comment!
:So, I wish there was a
try_format!
.When the parameter is not correct, can return an error, let me control.
The text was updated successfully, but these errors were encountered: