Treat null Tree.OprApp.getRhs() as UnexpectedExpression syntax error (#3882)

Fixes `NullPointerException` in `TreeToIr` by detecting the case and raising `UnexpectedExpression` syntax error.

# Important Notes
This PR prevents the `NullPointerException`. It doesn't change the meaning of `x.` (followed by space) `length`. It just detects when `Tree.OprApp.getRhs() == null` and treats that as an error.
This commit is contained in:
Jaroslav Tulach 2022-11-21 09:48:21 +01:00 committed by GitHub
parent 3d6f2b04f3
commit 79329ef00f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -550,7 +550,14 @@ final class TreeToIr {
yield switch (op.codeRepr()) {
case "." -> {
final Option<IdentifiedLocation> loc = getIdentifiedLocation(tree);
yield buildQualifiedName(app, loc, false);
try {
yield buildQualifiedName(app, loc, false);
} catch (UnhandledEntity ex) {
if (ex.entity() instanceof IR.Expression expr) {
yield expr;
}
throw ex;
}
}
case "->" -> {
@ -1161,7 +1168,12 @@ final class TreeToIr {
if (app.getOpr().getRight() == null || !operator.equals(app.getOpr().getRight().codeRepr())) {
break;
}
segments.add(app.getRhs());
if (app.getRhs() != null) {
segments.add(app.getRhs());
} else {
var ir = translateSyntaxError(app, IR$Error$Syntax$UnexpectedExpression$.MODULE$);
throw new UnhandledEntity(ir, "unrollOprRhs");
}
list = app.getLhs();
}
segments.add(list);

View File

@ -326,6 +326,19 @@ public class ErrorCompilerTest {
assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidEscapeSequence$.MODULE$.apply("wrong sequence"), "Invalid escape sequence wrong sequence.", 9, 28);
}
@Test
public void testNPE183814303() throws Exception {
var ir = parseTest("""
from Standard.Base import all
main =
x = "foo"
z = x. length
IO.println z
""");
assertSingleSyntaxError(ir, IR$Error$Syntax$UnexpectedExpression$.MODULE$, "Unexpected expression.", 60, 62);
}
private void assertSingleSyntaxError(
IR.Module ir, IR$Error$Syntax$Reason type,
String msg, int start, int end