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

Bug 342007

Summary: [Xbase] Full Support for Cast Conversion according to Java Lang Spec
Product: [Modeling] TMF Reporter: Robert von Massow <rvonmassow>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: btickets, dennis.huebner, eclipse, Holger.Schill, jan, joerg83reichert, karsten.thoms, knut.wannheden, mail, moritz.eysholdt, sebastian.zarnekow, sven.efftinge
Version: 2.0.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 376037    
Bug Blocks: 420959, 476798    

Description Robert von Massow CLA 2011-04-06 08:20:46 EDT
Build Identifier: N201104060452

The following expression is a valid Xtend snippet (according to the validation and specification):

testBug0() {
	val it0 = newArrayList() as List<List<String>>;
}

The compiled result:
public void testBug0() {
  ArrayList<Object> _newArrayList = CollectionLiterals.<Object>newArrayList();
  final List<String> it0 = ((java.util.List<java.lang.String>) _newArrayList);
}

has the following error:
Cannot cast from ArrayList<Object> to List<String>

Reproducible: Always
Comment 1 Robert von Massow CLA 2011-04-06 08:22:36 EDT
(In reply to comment #0)
> Build Identifier: N201104060452
> 
> The following expression is a valid Xtend snippet (according to the validation
> and specification):
> 
> testBug0() {
>     val it0 = newArrayList() as List<List<String>>;
> }
> 
> The compiled result:
> public void testBug0() {
>   ArrayList<Object> _newArrayList = CollectionLiterals.<Object>newArrayList();
>   final List<String> it0 = ((java.util.List<java.lang.String>) _newArrayList);
> }
> 
> has the following error:
> Cannot cast from ArrayList<Object> to List<String>
> 
> Reproducible: Always

Sorry, wrong Xtend snippet, I actually used the following:
testBug0() {
	val it0 = newArrayList() as List<String>;
}
Comment 2 Sven Efftinge CLA 2011-04-08 03:36:42 EDT
We need to fully implement what is defined in the Java Language Specification ยง5.5:

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5

Some of the conversions are already supported by the ConformanceComputer, but we still need to have 
- the narrowing primitive  conversion http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25363
- the narrowing reference conversion
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25379
- unchecked conversion
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#190772
Comment 3 Sebastian Zarnekow CLA 2011-05-04 06:11:33 EDT
Preliminary scheduled for 2.0RC1
Comment 4 Sven Efftinge CLA 2011-05-04 09:59:06 EDT
postponed to SR1
Comment 5 Sven Efftinge CLA 2011-09-26 05:52:59 EDT
later
Comment 6 Sebastian Zarnekow CLA 2011-11-28 05:05:41 EST
see also bug 364931
Comment 7 Sven Efftinge CLA 2012-01-31 06:12:30 EST
*** Bug 362239 has been marked as a duplicate of this bug. ***
Comment 8 Sven Efftinge CLA 2012-04-26 11:46:33 EDT
*** Bug 377771 has been marked as a duplicate of this bug. ***
Comment 9 Sven Efftinge CLA 2012-11-08 08:12:59 EST
*** Bug 381152 has been marked as a duplicate of this bug. ***
Comment 10 Dennis Huebner CLA 2013-02-13 02:47:54 EST
Still valid with new TS
Comment 11 Sebastian Zarnekow CLA 2013-02-13 03:15:38 EST
We'll have to introduce the notion of 'proofable distinct types' to support full cast validation.
Comment 12 Sven Efftinge CLA 2013-03-06 04:15:37 EST
*** Bug 364931 has been marked as a duplicate of this bug. ***
Comment 13 Sven Efftinge CLA 2013-03-06 04:15:47 EST
*** Bug 389872 has been marked as a duplicate of this bug. ***
Comment 14 Sven Efftinge CLA 2013-09-13 04:26:17 EDT
*** Bug 409238 has been marked as a duplicate of this bug. ***
Comment 15 Karsten Becker CLA 2013-09-17 11:16:10 EDT
Probably the same as comment #0:
import java.util.List

class B extends A{}
class A {}
class Testcase {
	def main() {
		val List<A> a={}
		print(a as List<B>)
	}
	def print(List<B> bs){}
}
Comment 16 Joerg Reichert CLA 2014-05-04 02:38:08 EDT
Invalid cast of CharSequence to String isn't discovered as well:

class StringCastBug {
	
   def test() {
       '''Test''' as String
   }
}
Comment 17 Jan Koehnlein CLA 2015-09-09 09:58:33 EDT
Another example

  interface IBar<C extends IBar<C>> {}
	
  class Bar implements IBar<Bar> {}
	
  class Foo<T extends IBar<T>> {}
	
  class SubFoo<T extends IBar<T>> extends Foo<T> {}
	
  class Baz {
    def baz(Foo<Bar> keyRange) {
      keyRange as SubFoo<?>  
                     // Error: Cannot cast from Foo<Bar> to SubFoo<?>
    }
  }