After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 546123 - vala could verify the validity of regex literals at compile time
vala could verify the validity of regex literals at compile time
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: general
0.3.x
Other All
: Normal enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on: 607702
Blocks:
 
 
Reported: 2008-08-03 18:12 UTC by Martin Olsson
Modified: 2010-03-25 10:03 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Martin Olsson 2008-08-03 18:12:50 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'
Comment 1 Michael 'Mickey' Lauer 2009-10-07 13:33:03 UTC
I agree, that would be slick.
Comment 2 Jürg Billeter 2010-03-22 20:31:41 UTC
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.
Comment 3 Jürg Billeter 2010-03-25 10:03:00 UTC
Regular expression literals are now compile-time checked.