Thread overview
[Issue 3050] New: Allow exception in CTFE (patch)
Jun 04, 2009
rsinfu@gmail.com
Jun 04, 2009
Shin Fujishiro
Jun 04, 2009
Robert Fraser
Jun 05, 2009
Shin Fujishiro
Jun 05, 2009
Shin Fujishiro
Jun 05, 2009
Denis Koroskin
Jul 14, 2009
Walter Bright
Sep 17, 2011
Kenji Hara
Nov 05, 2011
Don
June 04, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3050

           Summary: Allow exception in CTFE (patch)
           Product: D
           Version: 2.030
          Platform: x86
        OS/Version: All
            Status: NEW
          Keywords: patch
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: rsinfu@gmail.com


Created an attachment (id=389)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=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
--------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 04, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3050





--- Comment #1 from Shin Fujishiro <rsinfu@gmail.com>  2009-06-04 15:37:11 PDT ---
Created an attachment (id=391)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=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; }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 04, 2009
d-bugmail@puremagic.com wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=3050
> 
>            Summary: Allow exception in CTFE (patch)
>            Product: D
>            Version: 2.030
>           Platform: x86
>         OS/Version: All
>             Status: NEW
>           Keywords: patch
>           Severity: enhancement
>           Priority: P2
>          Component: DMD
>         AssignedTo: bugzilla@digitalmars.com
>         ReportedBy: rsinfu@gmail.com
> 
> 
> Created an attachment (id=389)
>  --> (http://d.puremagic.com/issues/attachment.cgi?id=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
> --------------------
> 

Since scope() is defined in terms of try/finally, would it be much harder to get scope(success/failure/exit) working in CTFE?
June 05, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3050





--- Comment #2 from Shin Fujishiro <rsinfu@gmail.com>  2009-06-04 20:42:46 PDT ---
Created an attachment (id=392)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=392)
Patch: scope(success/failure/exit)

Supplemental patch to get scope guard statement working in CTFE.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 05, 2009
Robert Fraser <fraserofthenight@gmail.com> wrote:
> Since scope() is defined in terms of try/finally, would it be much harder to get scope(success/failure/exit) working in CTFE?

Nope. I forgot about those. :) Thanks!
June 05, 2009
Just wow! I wonder what else can D community do since DMD has full source code available now.
July 14, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3050





--- Comment #3 from Walter Bright <bugzilla@digitalmars.com>  2009-07-14 03:02:14 PDT ---
Wow!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3050



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-09-17 14:49:45 PDT ---
I have modified his patch based on git master.

https://github.com/9rnsr/dmd/commits/ctfeException

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 05, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3050


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug@yahoo.com.au
         Resolution|                            |FIXED


--- Comment #5 from Don <clugdbug@yahoo.com.au> 2011-11-05 01:35:26 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------