Thread overview
[Issue 8788] New: The super constructor call can be prevented by mentioning "return".
Oct 09, 2012
David Eckardt
Oct 09, 2012
David Eckardt
Oct 10, 2012
Don
Oct 20, 2012
Walter Bright
October 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8788

           Summary: The super constructor call can be prevented by
                    mentioning "return".
           Product: D
           Version: D1 & D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: david.eckardt@sociomantic.com


--- Comment #0 from David Eckardt <david.eckardt@sociomantic.com> 2012-10-09 07:20:46 PDT ---
Mentioning a "return" prevents DMD from rejecting code where a constructor has
an explicit super() call which is done conditionally or not at all. Also it is
possible to return or throw before doing the super() call.
This example code compiles although it should be rejected:
---
class A { this ( ) { } }

class B : A
{
    this ( )
    {
        return;
        super();
    }

    this(int x)
    {
        try
        {
            throw new Exception;
            super();
        }
        catch ( ) { }
    }

    this(float f)
    {
        if (x == 3)
            return;
        else
            super();
    }


    this(char c)
    {
        if (c == 'x')
            super();
        else
            return;
    }
}
---
However, if the "return" statement in the second and third constructor is omitted, the compile error is triggered correctly.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8788



--- Comment #1 from David Eckardt <david.eckardt@sociomantic.com> 2012-10-09 07:27:39 PDT ---
Of course the "if" condition in the third constructor should be f >= 3, not x == 3.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8788


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #2 from Don <clugdbug@yahoo.com.au> 2012-10-10 02:58:34 PDT ---
statement.c, ReturnStatement::semantic() contains this comment:

   /* BUG: need to issue an error on:
     *  this
     *  {   if (x) return;
     *      super();
     *  }
     */
Funny that this never got entered in Bugzilla before. The bug however is in the
treatment of CSXreturn, mainly in scope.c.

For each branch of code there are 5 cases:
1. neither constructor calls nor returns have been made.
2. at least one subbranch has returned without calling a constructor.
3. all subbranches have called a constructor. Some may have returned
afterwards, but at least one has not returned yet.
4. all subbranches have exited after calling a constructor
5. some subbranches have returned after calling a constructor, the others have
not yet made constructor calls.


The case with the 'throw' is more difficult than the others. Ideally the first example below would be OK but the second would be an error:

  try {
    thow SomeFunkyException;
  }
  catch (SomeFunkyException) {
      super();
  }

  try {
    thow SomeFunkyException;
  }
  catch (DifferentFunkyException) {
      super();
  }
It might be worth forbidding 'super()' inside catch clauses.

I don't think it's worth the effort. But the 'return' cases should be detected.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8788



--- Comment #3 from github-bugzilla@puremagic.com 2012-10-20 15:51:36 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/ecfb19e7d602594db174d9670416916b14b38466 Fix bug 8788 The super constructor call can be prevented by mentioning "return"

Fixes the flow analysis by clearly distinguishing "ALL branches have called a
constructor" from "ANY branches have called a ctor".
There are a large number of special cases.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8788


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


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