-
Notifications
You must be signed in to change notification settings - Fork 466
Regex::must(...) to replace Regex::new(...).unwrap()? #450
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
It seems like this may be a case where |
@sfackler That's an interesting thought. I guess the problem there is that it's a pretty significant (albeit trivial to fix) breaking change. My plan is to introduce the regex-syntax rewrite without doing a semver bump on regex. |
Oh yeah, I didn't see that this was talking about regex-syntax breakage rather than regex. |
Having a panicking constructor is a good idea I think. Some thoughts on that:
|
Longer-term I think the best solution is having some form of compile-time checking as the default and making a runtime-specific version more obvious. I wouldn't mind if initially, the compile-time version just checked the syntax and did a |
To clarify: I think that having a |
Compile time checking is definitively out of scope for this issue. The I like |
The standard library tends to go in the opposite direction I think, where fallible methods are prefixed with |
Great ideas, I especially like the A new thought: Show the fancy parse error when |
I'll just add my voice to the chorus in favor of option number 2. My bikeshedding 2 cents on the name is that length matters the most because I expect that people will end up writing this a lot, but |
OK, so it looks like I can actually sidestep this issue for the most part. One thing I forgot about was that the error type in the regex crate doesn't actually use the error type from
Apologies for the distraction folks! (I still do kind of like the aesthetics of |
This commit adds an explicit Debug impl for regex's main Error type. The purpose of this impl is to format parse errors in normal panic messages more nicely. This is slightly idiosyncratic, but the default Debug impl prints the full string anyway, we might as well format it nicely. See also: rust-lang#450
This commit adds an explicit Debug impl for regex's main Error type. The purpose of this impl is to format parse errors in normal panic messages more nicely. This is slightly idiosyncratic, but the default Debug impl prints the full string anyway, we might as well format it nicely. See also: rust-lang#450
This commit adds an explicit Debug impl for regex's main Error type. The purpose of this impl is to format parse errors in normal panic messages more nicely. This is slightly idiosyncratic, but the default Debug impl prints the full string anyway, we might as well format it nicely. See also: #450
@NickHackman wrote: This issue is a continuation of #450, as it seems like this would be a nice feature to add. To slightly diverge from #450, instead of replacing This seems beneficial because in a large number of cases regular expressions are constants that are just unwrapped anyway, it should just panic instead for these cases. ExampleBefore: let REGEX = Regex::new(r"regular-expression").unwrap(); After: let REGEX = Regex::must_compile(r"regular-expression"); I'm not particularly taken by the Original link: #688 (comment) |
That's exactly what this issue proposes though. It proposes adding a new The main reason why I closed this issue was because I found another way to solve the problem at hand, which was producing nicer error messages. With that said, it does seem like there was positive reception to the idea.
I think a name like |
I think, if going the must_compile path, it's better to provide a proc-macro (again) rather than a new panicking method, so that any errors could be detected at compile-time. It would expand to the same |
As I said above, compile time checks are definitively out of scope for this issue. It isn't going to happen any time soon. (Years, if at all.) |
@BurntSushi Why? There are already 3rd-party crates that implement it, so my suggestion is merely either documenting them or incorporating similar approach. It doesn't have much complexity, since all it does is calls |
Can you show me such an example? I don't see how calling |
Yeah that's whey they usually do. I believe Clippy you mentioned is a good example of that actually. I'll try to find a standalone one I've seen somewhere, but yeah, that's true that it will require an additional crate (that can be reexported though). |
Okay, well at least I now understand what you meant. But yeah, I think that's more trouble than it's worth, particularly with the existence of the Clippy lint. Adding a |
Uh oh!
There was an error while loading. Please reload this page.
This issue is intended to brainstorm some ideas on how to report regex parse errors to Rust programmers.
One of my long running projects has been a rewrite of the
regex-syntax
crate. There are several goals I'm achieving via this rewrite, and one of them in particular is better error messages for regex parse errors.Here's an example:
Here's another example:
The above error messages correspond to the
fmt::Display
implementation on the error returned by the parser. What I'd like to have happen is for these error messages to appear to programmers when they create malformed regexes in their source code. Specifically, today, the code pattern for building a regex that is known at compile time is:The issue here is that if the regex contains a parse error, then the
unwrap
implementation will show theDebug
message for the error instead of theDisplay
implementation. That's definitely the right behavior in most cases, but in this case, I'd really like to show a nicer error message. My question is: how can I achieve that?I can think of two ways:
Debug
implementation defer to theDisplay
implementation. This causesunwrap
to show the right error message, but now we don't have a "normal"Debug
implementation, which feels kind of wrong to me.must
(name subject to bikeshedding) that is basically the same asRegex::new(...).unwrap()
, except it will emit theDisplay
impl for the error instead of theDebug
impl. The downside here is that users of theregex
crate need to usemust
, and this particular style of handling panics isn't really idiomatic in the Rust ecosystem, where we instead prefer an explicitunwrap
. The key difference here is that regexes in the source code with parse errors are inherently programmer errors, and programmers should get nice regex error messages, because theDebug
impl isn't going to be particularly helpful.Are there other ways of approaching this problem? What do other people think? My inclination right now is hovering around (2), particularly since
Regex::new(...).unwrap()
is not only extraordinarily common, but is also correct in the vast majority of cases (with it being incorrect only when the regex isn't known until runtime). That is, normalizing that code pattern with an explicit constructor feels like a good thing to me on its own, but it's not clear how the balances with established idioms.cc @rust-lang/libs @ethanpailes @robinst @killercup @steveklabnik @bluss
The text was updated successfully, but these errors were encountered: