safe navigation in while condition clause not fully evaluated

Open
#2,303 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java
Domain
compilers

Research direction

Start with the generated Java while-loop shown in the issue and trace how the safe-navigation expression is evaluated across loop iterations. Done means the full a?.b?.c condition is reevaluated each time, so the loop stops when getB() returns null; add or run a regression test covering this example.

Written by the indexing model from the issue text.

Description

bug confirmed
class A {
   B b;
   Integer limit = 3
   new (){
       b = new B()
       b.c = new C()
   }
   def getB(){
       limit -- 
       if(limit == 0){
           return null
       }else{
           return b
       }
   }
   
   def getLimit(){
       return limit; 
   }
}
class B {
   C c;
   def setC(C c){
       this.c = c
   }
   def getC(){
       return c;
   }
}
class C {
   
}

class Test {
    def static void main(String[] args) {
        val A a = new A(); 
        while(a?.b?.c != null){
            println(a.limit)
        }
    }
}

The code runs in infinite loop.

Generated Java code:

public class Test {
  public static void main(final String[] args) {
    final A a = new A();
    B _b = null;
    if (a!=null) {
      _b=a.getB();
    }
    C _c = null;
    if (_b!=null) {
      _c=_b.getC();
    }
    boolean _notEquals = (!Objects.equal(_c, null));
    boolean _while = _notEquals;
    while (_while) {
      Integer _limit = a.getLimit();
      InputOutput.<Integer>println(_limit);
      C _c_1 = null;
      if (_b!=null) {
        _c_1=_b.getC();
      }
      boolean _notEquals_1 = (!Objects.equal(_c_1, null));
      _while = _notEquals_1;
    }
  }
}

In the end of the while loop, the 'a?.b' of 'a?.b?.c' is not evaluated again, causing the condition not updated.

Dominant language
Java
Stars
831
Forks
330
Avg merge
3d 7h
Merged PRs (30d)
12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from eclipse-xtext/xtext

All issues in eclipse-xtext/xtext

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.