Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 365109 - [Xbase] fix caching of AbstractTypeProvider.computeEarlyExits
Summary: [Xbase] fix caching of AbstractTypeProvider.computeEarlyExits
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: unspecified   Edit
Hardware: Macintosh Mac OS X
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 365320 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-11-29 12:22 EST by Moritz Eysholdt CLA
Modified: 2017-10-31 11:16 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Moritz Eysholdt CLA 2011-11-29 12:22:03 EST
Example

----
class foo  {
	def bar() {
		try { 
			newArrayList("file1.ext").map(f| new File(f).canonicalFile)
		} catch(IOException o) {  
			Collections::<File>emptyList
		} 
	}
}
-----

this compiles to

----
public class foo {
  public List<File> bar() {
    List<File> _xtrycatchfinallyexpression = null;
    try {
      ArrayList<String> _newArrayList = CollectionLiterals.<String>newArrayList("file1.ext");
      final Function1<String,File> _function = new Function1<String,File>() {
          public File apply(final String f) {
            File _file = new File(f);
            File _canonicalFile = _file.getCanonicalFile();
            return _canonicalFile;
          }
        };
      List<File> _map = ListExtensions.<String, File>map(_newArrayList, _function);
      _xtrycatchfinallyexpression = _map;
    } catch (final IOException o) {
      List<File> _emptyList = Collections.<File>emptyList();
      _xtrycatchfinallyexpression = _emptyList;
    }
    return _xtrycatchfinallyexpression;
  }
}
-----

the Java code doesn't compile because of two errors
- "Unhandled exception type IOException" under "File _canonicalFile = _file.getCanonicalFile();"
- "Unreachable catch block for IOException. This exception is never thrown from the try statement body" under "catch (final IOException o) ".

Valid Java code could look like this:
-----
public List<File> bar() {
	List<File> _xtrycatchfinallyexpression = null;
	try {
		final Function1<String, File> _function = new Function1<String, File>() {
			public File apply(final String f) {
				File _canonicalFile;
				try {
					_canonicalFile = new File(f).getCanonicalFile();
				} catch (IOException e) {
					Exceptions.sneakyThrow(e);
					return null;
				}
				return _canonicalFile;
			}
		};
		_xtrycatchfinallyexpression = ListExtensions.<String, File> map(
				CollectionLiterals.<String> newArrayList("file1.ext"),
				_function);
	} catch (final Throwable o) {
		if (o instanceof IOException) {
			List<File> _emptyList = Collections.<File> emptyList();
			_xtrycatchfinallyexpression = _emptyList;
		}
	}
	return _xtrycatchfinallyexpression;
}
---

This code catches the IOException and sneaks it out and later it fishes for THwables and checks via instancoef if it's an IOException.

This code however has the conceptual problem that there is no guarantee that the closure is executed within the declared try-catch-block. It could be stored in a variable and executed later - after the control flow has left the try-catch-block.
Comment 1 Sven Efftinge CLA 2011-11-30 10:32:32 EST
The main problem is solved.
However, the outlined scenario still doesn't work, because the caching mechanism remembers wrong results during linking. We'll have to fix/improve the caching strategy. 

I've added an uncommented test case to Xtend2CompilerTest
Comment 2 Sven Efftinge CLA 2011-12-01 10:02:50 EST
*** Bug 365320 has been marked as a duplicate of this bug. ***
Comment 3 Sven Efftinge CLA 2013-09-16 12:51:59 EDT
fixed in the meantime.
Comment 4 Eclipse Webmaster CLA 2017-10-31 11:05:34 EDT
Requested via bug 522520.

-M.
Comment 5 Eclipse Webmaster CLA 2017-10-31 11:16:51 EDT
Requested via bug 522520.

-M.