Improve backend error handling (#11136)

- Fix debug logging for #11088 case--attempt to create an exception that is its own cause fails.
- In case the parser is used after closing, throw an `IllegalStateException` instead of UB. (This case is not known to occur and doesn't seem to be behind the #11121, but we should handle it more safely if it does.)
This commit is contained in:
Kaz Wesley 2024-09-20 06:23:52 -07:00 committed by GitHub
parent f05997d58c
commit e587d564f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 5 deletions

View File

@ -75,10 +75,10 @@ public final class Parser implements AutoCloseable {
return false;
}
private long state;
private long stateUnlessClosed;
private Parser(long stateIn) {
state = stateIn;
stateUnlessClosed = stateIn;
}
private static native long allocState();
@ -115,7 +115,16 @@ public final class Parser implements AutoCloseable {
return isIdentOrOperator(inputBuf);
}
private long getState() {
if (stateUnlessClosed != 0) {
return stateUnlessClosed;
} else {
throw new IllegalStateException("Parser used after close()");
}
}
public ByteBuffer parseInputLazy(CharSequence input) {
var state = getState();
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
@ -123,6 +132,7 @@ public final class Parser implements AutoCloseable {
}
public Tree parse(CharSequence input) {
var state = getState();
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
@ -140,7 +150,7 @@ public final class Parser implements AutoCloseable {
@Override
public void close() {
freeState(state);
state = 0;
freeState(stateUnlessClosed);
stateUnlessClosed = 0;
}
}

View File

@ -18,7 +18,7 @@ public final class EnsoMeta {
var ex =
new NullPointerException(
"Cannot get type for " + moduleName + " type: " + typeName + " at " + module);
ex.initCause(ex);
ex.initCause(e);
throw ex;
}
}