-
Notifications
You must be signed in to change notification settings - Fork 453
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
Idea: compile-time verification of the regular expressions #607
Comments
Does the Aside from that, it's a nice idea, but I don't think it needs to be maintained in this repo? It would need to be a separate crate anyway I think? There are some complexities to this too. Namely, whether building the regex succeeds or not doesn't just depend on the concrete syntax, but also on the options given to regex construction (such as whether the pattern is permitted to match invalid UTF-8). Clippy doesn't get this quite right---I think they are just using default options and making sure the regex is a valid parse. This is probably good enough to cover most cases though. Technically, it goes even further than this: even if the regex parses correctly, it's still not guaranteed to compile, since it may exceed size limits or other errors enforced only in the compiler. |
Oh, I didn't know about the clippy lint. It never even crossed my mind a generic linter would know about specific crates, but I guess it makes some sense.
It wouldn't necessarily need to live here, no. But I asked here first for several reasons:
Thanks for the pointers about the complexities, something to watch out for. As somewhat related question ‒ how far is Rust support of |
Yeah those are good points. In principle, I'm not opposed to bringing a hypothetical crate for this into this repo and maintaining it, but I just don't think I have the bandwidth for it right now. I could in principle maintain a minimal first version of it, but it wouldn't do much more than what Clippy gives you now. My current focus is on improving the implementation of
I don't know. :-( I haven't been following |
I can't promise the bandwidth for writing it right now either, but if I found the motivation and wrote it, I guess I would feel morally obliged to take some care of it myself (like solving bugs, etc), no matter if it lived in a separate repo or here. But then again, how much maintainership could be in the minimal version anyway? I wonder if it could, eventually, evolve into the full-featured version of doing the work in compile time ‒ but that's maybe not worth the trouble.
I see. In that case this is definitely not any time soon. This is something C++ is definitely ahead of Rust right now :-(. |
Sorry, it looks like your comment either didn't make it to my inbox, or it fell through the cracks of my email triage. :-)
It used to be a lot more maintenance when I would definitely see if you could leverage Clippy for this, honestly. If not, yeah, I'd start it as a different repo. I'd be open to having it rolled into this repo at some point in the future. |
Now that control-flow is available in |
Not even close unfortunately. I addressed this above:
|
Apologies for not noticing that sentence before posting! |
It seems like someone has already done it. |
I'm going to close this out primarily because I think the Clippy lint is probably sufficient for most cases here. In particular, this issue is "merely" asking for verification, and Clippy can just about almost give you that. It is possible for the Clippy lint to pass but for the regex to fail. Notably, if your regex blows the default compilation limits. For example, There is a separate issue of "compile time regexes," but that's an entirely different can of worms. |
Does clippy only check the |
I don't know. That's a question for the Clippy project... |
I checked manually (it didn't occur to me earlier, sorry for the question spam). The answer for everyone else is: No, it's not checked. I opened an issue against clippy to explore what it might take to get that functionality. |
Notably, Go has compile time validated regexes. While clippy is helpful, not all Rust coders use clippy. Would love to support for compile time regex validation in the Rust regex crate, in order to catch mistakes earlier in the SDLC. |
@mcandre Can you show me a Go program with "compile time validated regexes"?
If you want to catch mistakes earlier in the SDLC, then Clippy sounds like a great way to do it! |
Oh, maybe I misread the Go docs. https://pkg.go.dev/regexp#MustCompile Looks like Go may not validate at compile time, but rather panics. So Rust is at feature parity with Go there. Can we do better? |
Indeed, that's what I thought. If you want compile time verification, use Clippy. Otherwise, I don't think the benefit is worth the development, maintenance and API cost for this. Compile time verification would require a whole separate API, probably proc-macro based, for building a regex. I don't see it happening. Especially when Clippy exists and does this for you already. Now, some day, compile time regexes might happen, and a side effect of that is compile time verification. But that's a lot more work, however, it has some potentially very interesting benefits. The design space for it is quite large though, so it isn't happening any time soon. |
Yes, rust can do compile-time verification of regular expressions! https://docs.rs/lazy-regex/latest/lazy_regex/ This crate uses a macro that wraps the regex in a once-cell, so it is only compiled once at runtime, but also compiles the the macro at build-time just to check for errors. The actual regex compilation (both runtime and compile-time) is still done by the This is still not "real" compile-time compilation of the regexes (it still links with the entire regex crate even if only a very simple regex is used). But the compile-time checks, both of the actual regex syntax and that the correct number of capture groups is used, is very useful. |
Hello
I know the compile-time regular expressions that were provided by regex-macros are no longer supported. I found them quite an interesting thing.
I was wondering how much work it would be to provide a subset of that functionality. I'm not really worried about runtime allocation or the performance of building the regex at runtime. But being able to validate the regular expression syntax at compile time was a nice thing.
So I was wondering, with proc-macros stabilized, how much work would it be to have just the verification? Basically, the macro would take the string, try to compile it and then spit out the very ordinary
Regex::new(string)
code in its place.Does that idea make some sense?
The text was updated successfully, but these errors were encountered: