RFR: 8267580: The method JavacParser#peekToken is wrong when the first parameter is not zero

Guoxiong Li gli at openjdk.java.net
Sun May 23 18:11:46 UTC 2021


Hi all,

The method `JavacParser#peekToken(int lookahead, Predicate<TokenKind>... kinds)` succeeds only when the first parameter `lookahead` is zero. If the `lookahead` is not zero, it is wrong and returns unexpected result.

The parameter `kinds` should not be dereferenced by `lookahead` and should be always started at zero.

But currently, the method `JavacParser#peekToken(int lookahead, Predicate<TokenKind>... kinds)` has not been used so that this issue doesn't effect the parser.

An alternative way:  remove the following methods, because they are not used.

    protected boolean peekToken(Predicate<TokenKind>... kinds)
    protected boolean peekToken(int lookahead, Predicate<TokenKind>... kinds) 


To test this patch, we can comment out the following methods  and  run the tests locally.
Note: some methods may need to add `@SuppressWarnings("unchecked")`.

    protected boolean peekToken(Predicate<TokenKind> tk1, Predicate<TokenKind> tk2, Predicate<TokenKind> tk3) {
        return peekToken(0, tk1, tk2, tk3);
    }

    protected boolean peekToken(int lookahead, Predicate<TokenKind> tk1, Predicate<TokenKind> tk2, Predicate<TokenKind> tk3) {
        return tk1.test(S.token(lookahead + 1).kind) &&
                tk2.test(S.token(lookahead + 2).kind) &&
                tk3.test(S.token(lookahead + 3).kind);
    }


Thanks for your review.

Best Regards,
-- Guoxiong

-------------

Commit messages:
 - 8267580: The method JavacParser#peekToken is wrong when the first parameter is not zero

Changes: https://git.openjdk.java.net/jdk/pull/4158/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=4158&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8267580
  Stats: 2 lines in 1 file changed: 0 ins; 0 del; 2 mod
  Patch: https://git.openjdk.java.net/jdk/pull/4158.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/4158/head:pull/4158

PR: https://git.openjdk.java.net/jdk/pull/4158


More information about the compiler-dev mailing list