withfield semantics

Srikanth srikanth.adayapalam at oracle.com
Mon Feb 19 13:22:47 UTC 2018


Hello,

I am working on https://bugs.openjdk.java.net/browse/JDK-8197792 (Allow 
updates to instance fields of value types via withfield by any method in 
the same nest) and have run into some interesting situations/questions.

As a part of this exercise, I am getting rid of the static value factory 
mechanism that is presently in place. Once this  highly restricted 
environment/semantics for updates to instance fields of value classes is 
removed, some new cases pop up. (also because the present work considers 
some scenarios which may have been overlooked by javac in the earlier 
prototype)

May I request that the code fragment below be checked for correctness of 
the compiler behavior ?
Basically, when a final instance field of a value class is updated, the 
code generator will emit
a withfield operation, but this operation needs a mutable owner of the 
field in order to be able to write back the copy that was produced by 
withfield.

In a more complex scenario the owner of the field that is being updated 
can itself be an instance field that is a value type which would have to 
be updated via withfield again - which would require successive outer 
owners to be mutable etc.

class P {
     A[] ca = new A[1];
     A foo() {
         return __MakeDefault A();
     }
}
__ByValue final class A {
     final int y = i();
     final int x = 10;

     int i() {
         return 10;
     }
     void foo() {
         x = 10; // Error, can't write back copy to `this`
         this.x = 20; // Error, can't write back copy to `this`
         A.this.x = 30; // Error, can't write back copy to `this`
     }

     __ByValue final class B {
         final A a = __MakeDefault A();
     }

     __ByValue final class C {
         final B b = __MakeDefault B();
     }

     __ByValue final class D {
         final C c = __MakeDefault C();

         void withfield(D d, final D fd) {
             P p = new P();
             p.ca[0].x = 40; // OK, array element can always be written to.
             p.foo().x = 50; // Error: can't write back to constant 
expression.
             d.c.b.a.x = 11; // OK.
             fd.c.b.a.x = 11; // oops, can't write back to final parameter.
         }
     }

So is my understanding correct ??

I am also assuming that there is no distinction between final and blank 
final fields when it comes to withfield - Is this a valid assumption ?

Thanks!
Srikanth


More information about the valhalla-dev mailing list