From 1f0a1283d029962a238c21f0c1f2607d47e1f20e Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 8 Mar 2024 12:28:13 -0800 Subject: [PATCH] Fix parsing of `open module ... {}` module declarations Prompted by https://github.com/google/error-prone/pull/4311 PiperOrigin-RevId: 614012108 --- java/com/google/turbine/parse/Parser.java | 6 +++++- .../turbine/lower/ModuleIntegrationTest.java | 3 ++- .../lower/moduletestdata/module-info-open.test | 5 +++++ .../com/google/turbine/parse/ParseErrorTest.java | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 javatests/com/google/turbine/lower/moduletestdata/module-info-open.test diff --git a/java/com/google/turbine/parse/Parser.java b/java/com/google/turbine/parse/Parser.java index 0f2a46ff..3336ae8d 100644 --- a/java/com/google/turbine/parse/Parser.java +++ b/java/com/google/turbine/parse/Parser.java @@ -210,7 +210,11 @@ public CompUnit compilationUnit() { && (ident.value().equals("module") || ident.value().equals("open"))) { boolean open = false; if (ident.value().equals("open")) { - ident = eatIdent(); + next(); + if (token != IDENT) { + throw error(token); + } + ident = ident(); open = true; } if (!ident.value().equals("module")) { diff --git a/javatests/com/google/turbine/lower/ModuleIntegrationTest.java b/javatests/com/google/turbine/lower/ModuleIntegrationTest.java index 0157fea3..7cc7952e 100644 --- a/javatests/com/google/turbine/lower/ModuleIntegrationTest.java +++ b/javatests/com/google/turbine/lower/ModuleIntegrationTest.java @@ -47,6 +47,7 @@ public static Iterable parameters() { "module-info.test", // "classpath.test", "multimodule.test", + "module-info-open.test", }; return ImmutableList.copyOf(testCases).stream().map(x -> new Object[] {x}).collect(toList()); } @@ -61,7 +62,7 @@ public ModuleIntegrationTest(String test) { @Test public void test() throws Exception { - if (Double.parseDouble(JAVA_CLASS_VERSION.value()) < 53) { + if (Runtime.version().feature() < 9) { // only run on JDK 9 and later return; } diff --git a/javatests/com/google/turbine/lower/moduletestdata/module-info-open.test b/javatests/com/google/turbine/lower/moduletestdata/module-info-open.test new file mode 100644 index 00000000..70bd1a9c --- /dev/null +++ b/javatests/com/google/turbine/lower/moduletestdata/module-info-open.test @@ -0,0 +1,5 @@ +=== module-info.java === +open module com.google.foop.annotation { + requires java.base; + requires java.compiler; +} diff --git a/javatests/com/google/turbine/parse/ParseErrorTest.java b/javatests/com/google/turbine/parse/ParseErrorTest.java index f23b9575..9abb562d 100644 --- a/javatests/com/google/turbine/parse/ParseErrorTest.java +++ b/javatests/com/google/turbine/parse/ParseErrorTest.java @@ -475,6 +475,22 @@ public void typeAnnotationBeforeParam() { " ^")); } + @Test + public void moduleInfoOpen() { + String input = + lines( + "open {", // + "}"); + TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input)); + assertThat(e) + .hasMessageThat() + .isEqualTo( + lines( + "<>:1: error: unexpected token: {", // + "open {", + " ^")); + } + private static String lines(String... lines) { return Joiner.on(System.lineSeparator()).join(lines); }