Bug 3050 – Allow exception in CTFE (patch)

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2009-06-03T21:05:00Z
Last change time
2015-06-09T01:27:56Z
Keywords
patch
Assigned to
bugzilla
Creator
rsinfu

Attachments

IDFilenameSummaryContent-TypeSize
389enh_ctfeexception.patchPatch (DMD 2.030)text/plain29499
391enh_ctfeexception_2.patchAdditional patchtext/plain5106
392enh_ctfeexception_scope.patchPatch: scope(success/failure/exit)text/plain992

Comments

Comment #0 by rsinfu — 2009-06-03T21:05:10Z
Created attachment 389 Patch (DMD 2.030) The proposed patch implements support for throw/try/catch/finally in CTFE. Throw statement is, however, somewhat limited; new expression is not allowed, except for new Exception("msg"). Example and output: -------------------- int thrower(string s) { // The interpretor emulates throw new Exception("msg") throw new Exception("exception " ~ s); return 0; } int catcher() { try { return thrower("abc"); } catch (Exception e) { throw e; } return 0; } enum a = catcher("abc"); -------------------- test.d(21): Error: uncaught exception from catcher(): "exception abc" test.d(21): Error: cannot evaluate catcher() at compile time --------------------
Comment #1 by rsinfu — 2009-06-04T15:37:11Z
Created attachment 391 Additional patch Please apply this patch in addition to the first one. The first one allowed this problematic code: -------------------- Exception foo() { Exception r; try { throw new Exception("error"); } catch (Exception e) { r = e; } return r; } enum Exception e = foo(); -------------------- This patch fix the first patch so that a CTFE exception object cannot be used as a usual object. The only allowed use of a CTFE exception object is this: catch (Exception e) { throw e; }
Comment #2 by rsinfu — 2009-06-04T20:42:46Z
Created attachment 392 Patch: scope(success/failure/exit) Supplemental patch to get scope guard statement working in CTFE.
Comment #3 by bugzilla — 2009-07-14T03:02:14Z
Wow!
Comment #4 by k.hara.pg — 2011-09-17T14:49:45Z
I have modified his patch based on git master. https://github.com/9rnsr/dmd/commits/ctfeException
Comment #5 by clugdbug — 2011-11-05T01:35:26Z
More than two years later, CTFE exceptions are fully implemented. I did use some ideas from the patch. https://github.com/D-Programming-Language/dmd/commit/c0b26683e4e668581424e611105599ce8ab52592