Compatibility between baseLanguage and Java: return statements
Hi,
I always think about baseLanguage as an equivalent to Java. However, there are some cases in which their semantics differ a little bit. I wonder if this is are bugs of the implementation or features ;-) (i.e., the baseLanguage was designed to be that way). Note that I am comparing with JDT's java compiler, which may differ from other implementations.
One of the differences is regarding return statements. First, baseLanguage allows me to sometimes ignore the "return" keywork and assumes that the last statement should be returned. For example, this could would never work in Java (I would have to use "return 1;"):
public Integer m() {
1;
}
Second, the editor complains if I don't return anything in a "finally" block. For example, this is correct Java:
public Integer m() {
try {
return 1;
} finally {
System.out.println();
}
}
But baseLanguage complains that the finally block should return something. I think that this could be a bug in the DataFlowUtil.checkReturns method of baseLanguage, because it only verifies for ancestors of the block (and the try block is not an ancestor of the finally block), or it could be a bug in the DataFlow.getExpectedReturns method, because it should not return the finally block. I could not play with these methods because I was not yet able to find the time to set up the environment and compile MPS myself.
Third, baseLanguage does not detect infinite loops when dealing with return statements. For example, in Java, you can do this and the compiler will know that the only way to get out of the loop is through a return statement.
public Integer m() {
for (int i = 0; ; i++) {
if (/* some check */) { return i; }
}
}
I found some other differences (not supported by baseLanguage), like adding chars to Strings and coercing ints into chars, but that's for another post...
Cheers,
Thiago
Please sign in to leave a comment.
Hi, Thiago!
I'll try answering your questions below:
Right. This is a "feature" of MPS. Kind of extension to java always switched on.
I cannot reproduce this problem with MPS 1.5 RC5. If you can reproduce this problem with latest MPS build please file youtrack issue for it.
Looks like a problem in DataFlow analysis. Please, file youtrack issue for it.
Ok. It's up to you, but it looks like most of problems you are reporting are valuable and concerning some clear bug in base language you can skip discussing step and simply file a request. Of course, in case you find some tricky inconsistencies between MPs Base Language and java it's better discussing these cases here. ;-)
Hi Alex,
thanks for the comments!
I reproduced the "finally" bug in MPS 1.5RC5. The code that I sent you was really not showing the bug, I needed to add a declaration first.
So, for history, here are the bugs I filed:
Java vs baseLanguage: Return statement on infinite loop
Java vs baseLanguage: Return statement on finally block
Java vs baseLanguage: coercing ints into chars
Java vs baseLanguage: adding chars to strings
Bests,
Thiago