www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3050] New: Allow exception in CTFE (patch)

reply d-bugmail puremagic.com writes:
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: -------
Jun 03 2009
next sibling parent d-bugmail puremagic.com writes:
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: -------
Jun 04 2009
prev sibling next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
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?
Jun 04 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Jun 04 2009
prev sibling next sibling parent Shin Fujishiro <rsinfu gmail.com> writes:
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!
Jun 04 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
Just wow! I wonder what else can D community do since DMD has full source  
code available now.
Jun 05 2009
prev sibling parent d-bugmail puremagic.com writes:
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: -------
Jul 14 2009