Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363296

Summary: Incorrect code generated when using the '?.' operator in the context of a closure or for loop
Product: [Tools] Xtend Reporter: Lieven Lemiengre <lieven.lemiengre>
Component: CoreAssignee: Project Inbox <xtend-inbox>
Status: CLOSED FIXED QA Contact:
Severity: major    
Priority: P3 CC: sebastian.zarnekow, sven.efftinge
Version: 2.2.0Flags: sven.efftinge: juno+
Target Milestone: M6   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Lieven Lemiengre CLA 2011-11-09 07:43:02 EST
package xtend.bugs
import java.util.List

class Bugs {
	def bug(List<Object> xs) {
		for(x : xs) {
			x?.foo
		}
		
		xs.forEach [
			it?.foo
		]
	}
	def void foo(Object o) {
	}
}

The generated java code is wrong:

package xtend.bugs;

import java.util.List;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@SuppressWarnings("all")
public class Bugs {
  public void bug(final List<Object> xs) {
      for (final Object x : xs) {
        x==null?(void)null:this.foo(x);
      }
      final Procedure1<Object> _function = new Procedure1<Object>() {
          public void apply(final Object it) {
            it==null?(void)null:Bugs.this.foo(it);
          }
        };
      IterableExtensions.<Object>forEach(xs, _function);
  }
  
  public void foo(final Object o) {
  }
}

2 problems:
- casting to 'void' is impossible
- in this case the ternary operator can't be used, it should be replaced with an if statement
Comment 1 Sebastian Zarnekow CLA 2012-04-04 03:29:54 EDT
Already fixed in M6
Comment 2 Karsten Thoms CLA 2017-09-19 17:18:47 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 3 Karsten Thoms CLA 2017-09-19 17:30:08 EDT
Closing all bugs that were set to RESOLVED before Neon.0