hg: lambda/lambda/langtools: Conformance fix: adjust parsing of '_' as an identifier

bitter_fox bitterfoxc at gmail.com
Tue Jan 8 10:36:00 PST 2013


Hi, Maurizio.

I think there are some problems on _ for lambda parameters, as a Type and a
name.

1. javac considers a legal lambda as a illegal lambda in several situations.

For example:

public class Main
{
    class _ {}

    interface F
    {
        void m(_ arg);
    }

    public static void main(String[] args)
    {
        F f = (_ arg) -> {}; /* legal explicit lambda */
    }
}

Lambda expression for f is a legal explicit lambda(_ is a Type and arg is a
parameter name). However, javac considers it as a implicit lambda and `_'
as a lambda parameter name and makes a compiler error.

Maybe you forgot to add analyzation for UNDERSCORE (IDENT | UNDERSCORE) ->
explicit lambda.

--- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java    Tue
Jan 08 13:31:41 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java    Wed
Jan 09 03:32:57 2013 +0900
@@ -1385,18 +1385,13 @@
                             return ParensResult.PARENS;
                     }
                 case IDENTIFIER:
+                case UNDERSCORE:
                     if (peekToken(lookahead, IDENTIFIER) ||
                             peekToken(lookahead, UNDERSCORE)) {
-                        // Identifier, Identifier/'_' -> explicit lambda
+                        // Identifier/'_', Identifier/'_' -> explicit
lambda
                         return ParensResult.EXPLICIT_LAMBDA;
                     } else if (peekToken(lookahead, RPAREN, ARROW)) {
-                        // Identifier, ')' '->' -> implicit lambda
-                        return ParensResult.IMPLICIT_LAMBDA;
-                    }
-                    break;
-                case UNDERSCORE:
-                    if (peekToken(lookahead, RPAREN, ARROW)) {
-                        // '_', ')' '->' -> implicit lambda
+                        // Identifier/'_', ')' '->' -> implicit lambda
                         return ParensResult.IMPLICIT_LAMBDA;
                     }
                     break;

2. JavacParser misses _ for the second and the following explicit lambda
parameter names.

For example:

public class Main
{
    interface F
    {
        void m(int n1, int n2);
    }

    public static void main(String[] args)
    {
        F f = (int n, int _) -> {}; /* illegal explicit lambda */
    }
}

`_' is used for the second lambda parameter name. However, javac accepts it.

Maybe you didn't call formalParameter with `lambdaParameters' for the
second and the following explicit lambda parameters.

--- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java    Tue
Jan 08 13:31:41 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java    Wed
Jan 09 03:22:59 2013 +0900
@@ -3241,7 +3236,7 @@
             params.append(lastParam = formalParameter(lambdaParameters));
             while ((lastParam.mods.flags & Flags.VARARGS) == 0 &&
token.kind == COMMA) {
                 nextToken();
-                params.append(lastParam = formalParameter());
+                params.append(lastParam =
formalParameter(lambdaParameters));
             }
         }
         accept(RPAREN);

Regards,
bitter_fox


2013/1/9 Maurizio Cimadamore <maurizio.cimadamore at oracle.com>

>  Thanks!
>
> Maurizio
>
>
> On 08/01/13 17:46, bitter_fox wrote:
>
> Hi, Maurizio.
>
> Maybe you forgot to add code for javac to retain `_' packages.
> Your commit makes a compiler error for a legal package declaration:
>
> package _;
>
> I think there is the same problem for `assert' packages.
>
> --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java    Tue
> Jan 08 15:24:05 2013 +0100
> +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java    Wed
> Jan 09 01:33:39 2013 +0900
> @@ -2701,7 +2701,7 @@
>              mods = modifiersOpt(true);
>
>          if (token.kind == PACKAGE) {
> -            if (peekToken(IDENTIFIER)) {
> +            if (peekToken(IDENTIFIER) | peekToken(UNDERSCORE)) {
>                  seenPackage = true;
>                  if (mods != null) {
>                      checkNoMods(mods.flags);
>
> Regards,
> bitter_fox
>
> 2013/1/8 <maurizio.cimadamore at oracle.com>
>
>> Changeset: 2e8ccb7ba243
>> Author:    mcimadamore
>> Date:      2013-01-08 13:31 +0100
>> URL:
>> http://hg.openjdk.java.net/lambda/lambda/langtools/rev/2e8ccb7ba243
>>
>> Conformance fix: adjust parsing of '_' as an identifier
>> *) '_' cannot be used as a lambda parameter name
>> *) use of '_' as an identifier will cause compile-time warnings
>>
>> ! src/share/classes/com/sun/tools/javac/parser/JavacParser.java
>> ! src/share/classes/com/sun/tools/javac/parser/Tokens.java
>> ! src/share/classes/com/sun/tools/javac/resources/compiler.properties
>> ! test/tools/javac/diags/examples.not-yet.txt
>> ! test/tools/javac/lambda/LambdaParserTest.java
>> + test/tools/javac/lambda/WarnUnderscoreAsIdent.java
>> + test/tools/javac/lambda/WarnUnderscoreAsIdent.out
>>
>>
>>
>
>


More information about the lambda-dev mailing list