Bug 4647 - Recursive constructor does not give a compile error
Summary: Recursive constructor does not give a compile error
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-04-25 13:51 UTC by Eric Samsel
Modified: 2012-04-25 14:43 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Eric Samsel 2012-04-25 13:51:17 UTC
mcs does not give a compile time error if a constructor is called recursively. At runtime this results in a stack overflow. Example code is below. The Microsoft compiler results in the following compile error:

error CS0516: Constructor 'RecursiveConstructor.A.A(int)' cannot call itself


namespace RecursiveConstructor
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A(1);
        }
    }

    public class A
    {
        public A(int i)
            : this(i)
        {
        }
    }
}
Comment 1 Eric Samsel 2012-04-25 14:06:26 UTC
Apparently my test case above was incorrect and this only fails to give an error with generics. Updated example below:

namespace RecursiveConstructor
{
    class Program
    {
        static void Main(string[] args)
        {
            A<int> a = new A<int>(1);
        }
    }

    public class A<T>
    {
        public A(T i)
            : this(i)
        {
        }
    }
}

Microsoft error is the same:
error CS0516: Constructor 'RecursiveConstructor.A<T>.A(T)' cannot call itself
Comment 2 Marek Safar 2012-04-25 14:43:16 UTC
Fixed in master