JDK-8200567 : JShell: variable declaration allows reference in initializer
  • Type: Bug
  • Component: tools
  • Sub-Component: jshell
  • Affected Version: 9,10,11
  • Priority: P4
  • Status: In Progress
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2018-03-31
  • Updated: 2020-09-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbd_minorUnresolved
Description
A simpler version of the problem:

   jshell> int x = x + 3
   x ==> 3

Which should give an error.

------------

FULL PRODUCT VERSION :
java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
macOS High Sierra version 10.13.1 

A DESCRIPTION OF THE PROBLEM :

0
down vote
favorite
I was working on a problem to store reference of two classes within each other For Example:

class A {
B b;
A(B b){
this.b = b;}
}

class B {
A a;
B(A a){
this.a = a;}
}

public static void main(String...s){
A a = new A(new B(null));
a.b.a = a;
}
Now if instead of above initialisation, if I use below statement:

A a = new A(new B(a));
I got below error which is quite obvious:

Main.java:19: error: variable a might not have been initialised
        A a = new A(new B(a));

But if I try the same on JShell, it works just fine (Just to be extra sure that variable a has never been initialized, I checked for variable a just before executing the statement which confirms that it was not initialized before:

jshell> a
|  Error:
|  cannot find symbol
|    symbol:   variable a
|  a
|  ^

jshell> A a = new A(new B(a));
a ==> A@57fffcd7

jshell> a.b.a = a
$4 ==> A@57fffcd7

jshell> a.b.a
$5 ==> A@57fffcd7

jshell> a
a ==> A@57fffcd7

 There are two different behaviours of the same statement being executed in JAVA using standard compiling process and JShell





STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Step 1: Start JShell
Step 2: Execute below code:
jshell> class A { 
   ...> B b;
   ...> A(B b) {
   ...> this.b =b;
   ...> }
   ...> }
|  created class A, however, it cannot be referenced until class B is declared

jshell> class B {
   ...> A a;
   ...> B(A a){
   ...> this.a = a;
   ...> }
   ...> }
|  created class B

jshell> A a = new A(new B(a));
a ==> A@57fffcd7


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
error: variable a might not have been initialised
        A a = new A(new B(a));
ACTUAL -
Instance created

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class A {
B b;
A(B b){
this.b = b;}
}

class B {
A a;
B(A a){
this.a = a;}
}

A a = new A(new B(a));
---------- END SOURCE ----------


Comments
Executing attached test case results in compilation error in javac (As expected), Whereas jshell doesn't report the error. This issue is reproducible in all the version, and it does exist from the time jshell introduced (9 ea b89) 9 GA - Fail 10 GA - Fail 11 ea b06 - Fail
02-04-2018