8345058: Javac issues different error messages for the modifiers of the requires directive

Reviewed-by: mcimadamore
This commit is contained in:
Vicente Romero 2024-11-26 20:53:12 +00:00
parent 8da6435d4d
commit 8389e24d38
2 changed files with 38 additions and 0 deletions

View File

@ -4215,6 +4215,9 @@ public class JavacParser implements Parser {
}
isTransitive = true;
break;
} else if (token.name() == names.transitive && isTransitive) {
log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier);
break;
} else {
break loop;
}

View File

@ -221,4 +221,39 @@ public class RequiresTransitiveTest extends ModuleTestBase {
return src;
}
@Test
public void testRepeatedModifiers(Path base) throws Exception {
Path src = base.resolve("src");
Path src_m1 = src.resolve("m1");
tb.writeJavaFiles(src_m1,
"""
module m1 {
requires static static java.sql;
requires transitive transitive java.desktop;
}
"""
);
Path classes = base.resolve("classes");
Files.createDirectories(classes);
String log = new JavacTask(tb, Task.Mode.CMDLINE)
.options("-XDrawDiagnostics",
"--module-source-path", src.toString())
.files(findJavaFiles(src))
.outdir(classes)
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
String[] expect = {
"module-info.java:2:21: compiler.err.repeated.modifier",
"module-info.java:3:25: compiler.err.repeated.modifier"
};
for (String e: expect) {
if (!log.contains(e))
throw new Exception("expected output not found: " + e);
}
}
}