[nem-bug] [Nemerle 0000664]: Usage of 'this' reference should be controlled before call to base ctor

feedback at nemerle.org feedback at nemerle.org
Tue Apr 10 21:28:40 CEST 2007


A NOTE has been added to this issue.
======================================================================
<http://nemerle.org/bugs/view.php?id=664> 
======================================================================
Reported By:                nazgul
Assigned To:                
======================================================================
Project:                    Nemerle
Issue ID:                   664
Category:                   Compiler
Reproducibility:            always
Severity:                   minor
Priority:                   normal
Status:                     new
======================================================================
Date Submitted:             05-07-2006 13:53 CEST
Last Modified:              04-10-2007 21:28 CEST
======================================================================
Summary:                    Usage of 'this' reference should be controlled
before call to base ctor
Description: 
class Base {
  public this (_ : Base) { 
  } 
}

class Inh : Base {
 
  public this () {
    base (this)
  }
  
}

()

causes verification error:
[IL]: Error: [C:\cygwin\home\nazgul\nemerle\ncc\testsuite\out.exe :
Inh::.ctor][offset 0x00000002][found <uninitialized> ref ('this' ptr)
'Inh'][expected ref 'Base'] Unexpected type on the stack.

======================================================================

----------------------------------------------------------------------
 akhropov - 10-06-06 18:38 
----------------------------------------------------------------------
I believe it should also be prohibited to access base and its members
before its constructor is called because logically speaking base object
doesn't exist before its constructor is called.

However,  the following code compiles just fine at the moment:

class A
{
	internal x : int;
	public this( x : int = 10 )
	{
		this.x = x;
	}
}

class B : A
{
	public this()
	{
		System.Console.WriteLine($"base.x = $(base.x)");
		base( 1 );
	}
}

def b = B();
System.Console.WriteLine(b);

----------------------------------------------------------------------
 nazgul - 01-05-07 20:44 
----------------------------------------------------------------------
One more example we now handle incorrectly:

abstract class A {
   protected this (x : string) {
     assert (x != null);   
   }
}

class B : A {
   mutable a : string = "asa";
   public this () {
     base (a);    
   }
}


Actually, the initializer is executed AFTER base ctor call, so NPE is
thrown in base. In C# equivalent code is not allowed, because it does now
allow passing instance field values to base call (this is actually
consequence of disallowing usage of 'this' reference)

----------------------------------------------------------------------
 divan - 04-10-07 21:28 
----------------------------------------------------------------------
Last one example works on r7588, fields are initialized before base() call
now.

Issue History
Date Modified  Username       Field                    Change              
======================================================================
05-07-06 13:53 nazgul         New Issue                                    
10-06-06 18:38 akhropov       Note Added: 0001468                          
01-05-07 20:44 nazgul         Note Added: 0001600                          
04-10-07 21:28 divan          Note Added: 0001841                          
======================================================================




More information about the bugs mailing list