Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 347175 - wrong generated java class in Xtend2 (=>cannot cast from null to int)
Summary: wrong generated java class in Xtend2 (=>cannot cast from null to int)
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 minor (vote)
Target Milestone: SR1   Edit
Assignee: Jan Koehnlein CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-25 11:19 EDT by Serano Colameo CLA
Modified: 2017-09-19 17:19 EDT (History)
4 users (show)

See Also:
sebastian.zarnekow: indigo+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Serano Colameo CLA 2011-05-25 11:19:42 EDT
Build Identifier: Xtext 2.0.0.v201105241033

xtend2:
	def String getExpression(ConstExp exp) {
		switch exp.expr { /* value field is a primitice 'int' type*/
			IntLiteral : (exp?.expr as IntLiteral)?.value?.toString 
                        ...
		}
	}

generated java:
 public String getExpression(final ConstExp exp) {
    String _switchResult = null;
    ConstExp _expr = exp.getExpr();
    final ConstExp __valOfSwitchOver = _expr;
    boolean matched = false;
    if (!matched) {
      if (__valOfSwitchOver instanceof IntLiteral) {
        matched=true;
        ConstExp _expr_1 = exp==null?(ConstExp)null:exp.getExpr();
        int _value = ((IntLiteral) _expr_1)==null?(int)null:((IntLiteral) 
        ....                                  //  ^^^^ cannot cast from null to int)
}

Reproducible: Always
Comment 1 Jos Warmer CLA 2011-06-12 09:35:03 EDT
Xtend code:

      var String empty = null;
      var int i = empty?.length

Generated Java code:

      String empty = null;
      int _length = empty==null?(int)null:empty.length();
      int i = _length;
Comment 2 Jan Koehnlein CLA 2011-07-07 09:24:19 EDT
Added default values for primitives to compiler and interpreter and a test.

Two notes:

1) We don't have special treatment for extensions: if the syntactic receiver is null, we don't evaluate the function, e.g.

  def foo() {
     (null as String)?.bar()
  }

  def bar(String x) {}

will not execute bar()

2) I've added a validation rule such that ?. is invalid on primitive receivers
Comment 3 Karsten Thoms CLA 2017-09-19 17:08:04 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 4 Karsten Thoms CLA 2017-09-19 17:19:39 EDT
Closing all bugs that were set to RESOLVED before Neon.0