3 ms·
See the update at the end. The halting aspect comes from checking if a value is defined: when a return statement is executed, check that the expression being r
by hyperman1 3y ago
See the update at the end. The halting aspect comes from checking if a value is defined: when a return statement is executed, check that the expression being returned is filled in. In fact, it goes much wider, it has to check that every variable used has been defined and nothing final gets redefined.
See for some interesting cases the examples at the start of https://docs.oracle.com/javase/specs/jls/se17/html/jls-16.html https://docs.oracle.com/javase/specs/jls/se17/html/jls-16.ht...
Update: come to think of it, it probably is the halting problem simply for returning:
int somefunc(){
if(this_returns_true()){
throw Something();
}else{
// No throw here
}
// Do I need return 1 here?
}
The famous Sufficiently Smart Compiler can decide the function always throws, and does not need a return statement. Let this_returns_true depend on deciding the halting problem, and there you are. Clearly, this level of analysis is out of reach for javac. The standard in this case just claims the then and else side can both be executed, so the return is required. If the else had a second throw, the compiler would not need the return.