On Mon, 1 Sep 2025 18:30:56 GMT, Maurizio Cimadamore <[email protected]>
wrote:
>>> ok, so I guess I still don't get whether this _must_ be an error. In
>>> principle `y` could have a local proxy, in which case the lambda could be
>>> thought of as accessing that proxy, so no need to capture `this` ?
>>
>> I wonder what is the mental model supposed to be here.
>>
>> @mcimadamore what is your opinion on whether this should compile?
>>
>> class A {
>> int y;
>> A() {
>> y = 1;
>> class B {
>> static void m() { // static context
>> System.out.println(y);
>> }
>> }
>> super();
>> }
>> }
>>
>> If your answer is "No" then aren't you then implying that `y` shouldn't be
>> available whenever `A.this` is not available? In which case doesn't that
>> answer your question?
>>
>> If your answer is "Yes", then doesn't that imply that _this_ should also
>> compile...
>>
>> class A {
>> int y;
>> A() {
>> y = 1;
>> class B {
>> static void m() { // static context
>> System.out.println(A.this.y);
>> }
>> }
>> super();
>> }
>> }
>>
>> even though this doesn't:
>>
>> class A {
>> int y;
>> A() {
>> y = 1;
>> class B {
>> static void m() { // static context
>> System.out.println(A.this);
>> }
>> }
>> super();
>> }
>> }
>
> I suppose what I'm saying is: I understand why the code doesn't compile in
> today's world. But as we relax more restrictions and we resort to more
> complex translation strategies, I do wonder if some of these rules that
> prevent reads from lambdas will feel too tight. E.g. imagine the case of a
> final field -- that is written only once. If we already saw a write for that
> field, what stops us from being able to reference it from a lambda -- through
> a local proxy?
>
> I don't buy the argument that `A.this.y` working implies `A.this`. This is
> already not the case in the code added by this PR, where reading an already
> written field in a prologue is fine, even through `A.this.y` -- but accessing
> `this` of a class from the prologue is never ok (if it was you could pass
> such a larval `this` to another method).
(in your example with static context, my answer is that no, it should _not_
compile. A static context can't access instance fields from an enclosing class)
-------------
PR Review Comment:
https://git.openjdk.org/valhalla/pull/1523#discussion_r2314448628