Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javac strange syntax - error illegal start of expression

Tags:

java

javac

I encountered a strange error, which I believe is a bug. Here is a minimal case, please do not comment on the usefulness of the code :)

class Foo {

    static public <X> int bar() { return 42; }

    public int baz() {
        return true ? 42 : (
            Foo.<Void>bar() > 42 ? 41 : 43
        )
        ;
    }
}

Result:

err.java:7: illegal start of expression
        Foo.<Void>bar() > 42 ? 41 : 43
            ^

I have tried SUN SDK javac 1.6.0_13 and 1.6.0_21.
The error goes away, when I either

  • make bar() non-generic (just for curiosity, not really an option)
  • remove the parentheses around the ternary expression on line 7

So it looks like that if e is an expression, it is not always valid to write (e)?

like image 223
Ingo Avatar asked Mar 23 '11 11:03

Ingo


1 Answers

The posted code compiles (and runs) just fine for me using Eclipse, but I can confirm that javac fails to compile this. I suspect you've found a compiler bug in javac.

It would probably be a good idea to report it.

like image 60
Joachim Sauer Avatar answered Nov 20 '22 11:11

Joachim Sauer