GNOME Bugzilla – Bug 546123
vala could verify the validity of regex literals at compile time
Last modified: 2010-03-25 10:03:00 UTC
Compiling following program gives me a warning that I'm not handling the runtime error "RegexError" with a try clause. I could silence this compiler warning by wrapping this regex literal in a try-catch (RegexError e) { // do nothing } but this makes my program more fragile (i.e. what if someone fixes a bug in this code later so that the regex literal needs to be changed, and that also a typo inserts an extra "\" in the regex at some random place so that it because a invalid regex literal. Now the "error handling" I add to silence the compiler warning will actually suppress a real bug. Not good. I propose that valac instead prints a compiler warning if a hardcoded string literal is passed into "new Regex()" that is NOT a valid regular expression. If the hardcoded string literal is in fact a valid regex then the code should compile just fine without warnings. using GLib; public class Main { public static int main (string[] args) { Regex regex = new Regex ("FIRST|SECOND", RegexCompileFlags.CASELESS); return 0; } } main.vala:6.19-6.72: warning: unhandled error `GLib.RegexError'
I agree, that would be slick.
I think this would only make sense with native regular expression support, otherwise it would be quite strange to sometimes require error handling and sometimes not. See bug 607702 for native regular expression support.
Regular expression literals are now compile-time checked.