1 /*
  2  * @test /nodynamiccopyright/
  3  * @bug 8205910
  4  * @summary Complain when `this' of a value class is leaked from constructor before all instance fields are definitely assigned.
  5  * @compile/fail/ref=CheckThisLeak.out -XDrawDiagnostics -XDdev -XDenablePrimitiveClasses CheckThisLeak.java
  6  */
  7 
  8 primitive class V {
  9 
 10 	private final int x, ymx;
 11 
 12 	V(int x, int y) {
 13 
 14 		OK();                       // OK to call static methods.
 15 		this.OK();                  // OK to call static methods.
 16 
 17 		validate();                 // BAD to call instance method DU = {x, ymx}
 18 		this.validate();            // BAD to call instance method DU = {x, ymx}
 19 		V.this.validate();          // BAD to call instance method DU = {x, ymx}
 20 
 21 		System.out.println(this);   // BAD to pass `this' as argument DU = {x, ymx}
 22 		System.out.println(V.this); // BAD to pass `this' as argument DU = {x, ymx}
 23 
 24 		V v = this;                 // BAD to create alias  DU = {x, ymx}
 25 		v = V.this;                 // BAD to create alias  DU = {x, ymx}
 26 
 27 		ymx = y - x;                // OK, implicit this for field write.
 28 		int l = this.ymx;           // OK, explicit this for DA field read.
 29 
 30 		OK();                       // OK to call static methods.
 31 		this.OK();                  // OK to call static methods.
 32 
 33 		validate();                 // BAD to call instance method DU = {x}
 34 		this.validate();            // BAD to call instance method DU = {x}
 35 		V.this.validate();          // BAD to call instance method DU = {x}
 36 
 37 		System.out.println(this);   // BAD to pass `this' as argument DU = {x}
 38 		System.out.println(V.this); // BAD to pass `this' as argument DU = {x}
 39 
 40 		v = this;                   // BAD to create alias  DU = {x}
 41 		v = V.this;                 // BAD to create alias  DU = {x}
 42 
 43 		this.x = x;
 44 
 45         // ALL fields are assigned now.
 46 
 47 		OK();                       // OK to call static methods.
 48 		this.OK();                  // OK to call static methods.
 49 
 50 		validate();                 // OK: DU = {}
 51 		this.validate();            // OK: DU = {}
 52 		V.this.validate();          // OK: DU = {}
 53 
 54 		System.out.println(this);   // OK: DU = {}
 55 		System.out.println(V.this); // OK: DU = {}
 56 
 57 		v = this;                   // OK: DU = {}
 58 		v = V.this;                 // OK: DU = {}
 59 		assert (this.x > 0);        // OK: DU = {}
 60 		assert (this.y() > 0);      // OK: DU = {}
 61 	}
 62 
 63 	V() { // non-initial constructor. All, statements below are OK.
 64         this(10, 20);
 65 		OK();                       
 66 		this.OK();                  
 67 
 68 		validate();                 
 69 		this.validate();            
 70 		V.this.validate();          
 71 
 72 		System.out.println(this);   
 73 		System.out.println(V.this); 
 74 
 75 		V v = this;                 
 76 		v = V.this;                 
 77 
 78 		int l = this.ymx;           
 79 
 80 		assert (this.x > 0);        
 81 		assert (this.y() > 0);      
 82 	}
 83 
 84 	static void OK() {
 85 	}
 86 
 87 	int x() {
 88 		return x;
 89 	}
 90 
 91 	int y() {
 92 		return ymx + x;
 93 	}
 94 
 95 	void validate() {
 96 		assert (x() > 0 && y() > 0);
 97 	}
 98 
 99 	public static void main(String... av) {
100 		V z = new V(1, 10);
101 		assert (z.x() == 1);
102 		assert (z.y() == 10);
103 	}
104 }