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

Bug 361061

Summary: [Xtend] Array Conversion fails
Product: [Modeling] TMF Reporter: Jason Barkanic <jbarkanic>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: christian.dietrich.opensource, sebastian.zarnekow, sven.efftinge
Version: unspecifiedFlags: sebastian.zarnekow: indigo+
Target Milestone: SR2   
Hardware: All   
OS: All   
Whiteboard:

Description Jason Barkanic CLA 2011-10-15 10:37:32 EDT
Build Identifier: 

var a = 'a,b,c'.split(',')
a = a.tail

Validates and compiles but causes a cast exception on the second line.  In this case the call to IterableExtensions.tail wraps the already wrapped array, so it doesn't know how to unwrap it afterward.

The compiled code:

String[] _split = "a,b,c".split(",");
String[] a = _split;
final String[] typeConverted_a = (String[])a;
Iterable<String> _tail = IterableExtensions.<String>tail(((Iterable<String>)Conversions.doWrapArray(typeConverted_a)));
a = ((String[])Conversions.unwrapArray(_tail));

Reproducible: Always
Comment 1 Jason Barkanic CLA 2011-10-15 10:53:51 EDT
In my case, the following was the actual behavior I desired.

var Iterable<String> a = 'a,b,c'.split(',')
a = a.tail

which avoids conversion back to array:

String[] _split = "a,b,c".split(",");
Iterable<String> a = ((Iterable<String>)Conversions.doWrapArray(_split));
Iterable<String> _tail = IterableExtensions.<String>tail(a);
Iterable<String> _a = a = _tail;

I'm not sure of the general solution, but currently there seems to be no way to tell when array conversion should or shouldn't work.
Comment 2 Sebastian Zarnekow CLA 2011-10-15 13:22:16 EDT
Which build do you use?
Comment 3 Christian Dietrich CLA 2011-10-15 13:47:33 EDT
Hello Sebastian,

the problem even occurs with build 2.1.0.v201110141320 thus i asken Jason to file a bug.

Regards
Christian
Comment 4 Sebastian Zarnekow CLA 2011-10-15 14:15:43 EDT
It's related to bug 345955.
Comment 5 Sven Efftinge CLA 2011-10-16 18:14:30 EDT
I tried with two different builds, one from Oct. 12 and the latest stable and it generated correctly working code in both cases. @Christian could you please double check and reopen if you really get it with the latest builds?
Comment 6 Christian Dietrich CLA 2011-10-17 00:30:23 EDT
I created following Test class:

class BugTest {

	@org.junit.Test
	def void test() {
		var a = 'a,b,c'.split(',')
		a = a.tail
	}
	
}

it fails with 2.1.0.v201110141320. somehow i cannot reopen this bug. buzilla doesnt let me ;-)

@SuppressWarnings("all")
public class BugTest {
  @Test
  public void test() {
      String[] _split = "a,b,c".split(",");
      String[] a = _split;
      final String[] typeConverted_a = (String[])a;
      Iterable<String> _tail = IterableExtensions.<String>tail(((Iterable<String>)Conversions.doWrapArray(typeConverted_a)));
      a = ((String[])Conversions.unwrapArray(_tail));
  }
}


java.lang.ClassCastException: org.eclipse.xtext.xbase.lib.IterableExtensions$2 cannot be cast to [Ljava.lang.String;
	at test.mm.BugTest.test(BugTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Comment 7 Sebastian Zarnekow CLA 2011-10-17 01:49:44 EDT
See last comment
Comment 8 Sebastian Zarnekow CLA 2011-10-17 01:50:41 EDT
We have to pass the String[].class into the conversion routine since this information is not available at runtime but only statically at compile time. See also the ticket that's mentioned in comment #4
Comment 9 Sebastian Zarnekow CLA 2011-10-17 05:59:04 EDT
Pushed to master.
Comment 10 Karsten Thoms CLA 2017-09-19 17:30:58 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 11 Karsten Thoms CLA 2017-09-19 17:42:09 EDT
Closing all bugs that were set to RESOLVED before Neon.0