Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 363296 - Incorrect code generated when using the '?.' operator in the context of a closure or for loop
Summary: Incorrect code generated when using the '?.' operator in the context of a clo...
Status: CLOSED FIXED
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.2.0   Edit
Hardware: PC Windows 7
: P3 major (vote)
Target Milestone: M6   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-09 07:43 EST by Lieven Lemiengre CLA
Modified: 2017-09-19 17:30 EDT (History)
2 users (show)

See Also:
sven.efftinge: juno+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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