-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
ParseIntError and ParseFloatError does not allow to introspect the cause #22639
Comments
Note that this was purposefully done to avoid adding extra |
Triage: no change here. @rust-lang/libs is the intention still to change this to an enum of some kind in the future? |
Any reason to postpone this further? |
@Seeker14491 I don't think it's postponed. I think someone just needs to propose an API? |
Hey, we already have an enum Lines 2769 to 2774 in fc02736
|
I just brushed up against this issue while demoing the guessing game example from the Rust Book - seemed like an easy thing to show off matching on the error kind, but turned out not so much! |
I also wanted to match |
@frontsideair you can just check if the string's empty before parsing, right? |
I guess, but it was a fun exercise in pattern matching. |
I see that checking for empty has come up in the context of the guessing game tutorial. Could somebody make a list of use cases from real code that needs to be able to distinguish between the following other ParseIntError cases?
In the event that empty is the only one we ever need to introspect for, I would prefer to close this and instead recommend to check |
It can be convenient to distinguish too large/too small numbers when accepting configuration parameters that the code will later sanitize by clamping to a valid range. Specifically, they make it possible to perform saturation of the parsed value before it is passed on. Example: the valid range of offsets is -10..+10, therefore the application stores it in an Without the ability to discriminate if the number is too big/small, it is not possible to determine what is the nearest legitimate value. |
@ranma42 do you think that use case would be solved by rust-lang/rfcs#928? use std::num::Saturating;
fn main() {
let s = "-999";
let n = match s.parse::<Saturating<i8>>()?.0 {
std::i8::MIN ... -11 => { println!("saturating"); -10 }
11 ... std::i8::MAX => { println!("saturating"); 10 }
n => { println!("in range"); n }
};
println!("n={}", n);
} |
Yes, that use case would be covered if Rust had the ability to parse a |
The only way I found around this problem was to do something ugly like this: match my_str.parse::<i32>() {
Ok(x) => {my_int = x;},
Err(e) => {
if e.to_string() == "invalid digit found in string" {
println!("Only positive integers are allowed!");
}
else if e.to_string() == "number too large to fit in target type" {
println!("Number is too large!");
}
else if e.to_string() == "number too small to fit in target type" {
println!("Number is too small!");
}
else if e.to_string() == "cannot parse integer from empty string" {
println!("Number is empty!");
}
}
} What is so annoying is that |
I'm doing custom type parsing (something like this: My map would be something like this:
....except I can't do that at the moment without resorting to @amosbatto 's trick. It also makes testing very difficult, because I can't tell what error I got back (except by checking the string). |
Hello everyone. My PR fixes this issue and has been merged. Use the feature flag |
If this is to be stabilized, I think that the terminology has to be improved: |
Maybe |
This will also solve my use case described in #39381. |
What is missing to make it stable? |
I would find this useful for |
Before stabilizing, would it be very complicated to additionally add the byte index where parsing failed? I can imagine that information being useful to an introspector (for example, to print a verbose rust-like arrow pointing to the exact location of the parse failure). |
Should be easy to do. I will probably open a PR with that soon so people discuss it. |
It has been over half a year since #77640 (review), and the indexing question is rejected in #79728 (review), so I guess we can submit another stabilization attempt? 😉 |
Summary: Here we improve the following: * The test case for long integers (big integers) * We have to use an unusual workaround to detect integer overflow of the `parse::<isize>` function as Meta is on a version of rust < 2022. * The workaround is documented and there is a todo as follows: ``` // TODO: use ParseIntError.kind() to detect integer overflow of // parse of const_value when Meta is on rust 2022. // In rust 2021 ParseIntError.kind is private // For now, store an overflow Err from parsing a large integer // Adapted from rust-lang/rust#22639 // and uutils/coreutils#2882 ``` This would seem to be the recommended workaround for detecting integer overflow. Reviewed By: stroxler Differential Revision: D42407915 Privacy Context Container: L1152058 fbshipit-source-id: 049dd6c74305fe193131fd1ea2ad72a0965d253a
It seems |
There's currently no |
The only way to introspect reason of
.parse
failure is to compare to error messages returned by description or use the following code snippet:The text was updated successfully, but these errors were encountered: