related to compile time error in record constructor

Alex Buckley alex.buckley at oracle.com
Wed Oct 7 16:24:44 UTC 2020


On 10/7/2020 4:18 AM, Brian Goetz wrote:
>> This error seems to be unnecessary, could have been a warning instead.
> 
> The error is correct, because the variable this.low really is not 
> initialized yet!   You say the call to m() "works", but when you call 
> it, it will see this.low == 0.

I think the totality of Pravin's experience is understandable. As I 
noted in 
https://mail.openjdk.java.net/pipermail/compiler-dev/2020-September/015080.html, 
in an ordinary class, the constructor body would anchor the reader's 
understanding of what a new `TestRecord` object looks like by assigning 
`this.low` somewhere in the body (let's set aside alternate constructor 
invocations) ... but in a record class, the reader is untethered because 
they never see `this.low` being assigned and they do see `low` being 
used and it's like they're in a method rather than a constructor and so 
they perceive `this.low` as OK. Grokking the compact constructor syntax 
in a record class is one thing; grokking the invisible field 
initialization is quite another.

> Arguably, `this` should be considered DU for the entirety of the compact 
> constructor.

JLS ch.16 already "looks through" the field access expression `this.XYZ` 
to check that XYZ (if a blank final field) is DA here. This limited 
sanity check avoids the expansive question of how `this` (a term which 
is not a variable but rather a primary expression that evaluates to a 
value everywhere but a static context) could be subject to a definite 
assignment analysis intended for local variables and blank final fields.

Alex


More information about the amber-dev mailing list