lambda bugs - trouble with `this` in lambda body in default method

Zhong Yu zhong.j.yu at gmail.com
Mon Sep 9 21:09:35 PDT 2013


On Mon, Sep 9, 2013 at 7:46 PM, Stuart Marks <stuart.marks at oracle.com> wrote:
> Nice bugs! These reproduce on b106, which was recently released as the
> official JDK 8 Developer Preview build.
>
> Well, more precisely, the first example still crashes, and the second
> example crashes at runtime with a VerifyError instead of printing "null".

In my actual application code I was hit by
IncompatibleClassChangeError when `this` is accessed (after a clean
rebuild). However I couldn't reproduce that bug in a simplified
example. Anyhow, the behavior is certainly whimsical.


> Still a bug though obviously.
>
> I've filed a bug which should eventually appear here:
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8024497
>
> Thanks,
>
> s'marks
>
>
>
> On 9/6/13 8:07 PM, Zhong Yu wrote:
>>
>> This code crashes javac (jdk8 ea build 105)
>>
>> public interface Foo
>> {
>>      default Runnable getAction()
>>      {
>>          return ()->{
>>              System.out.println(this);
>>          };
>>      }
>> }
>>
>> Insert a random line, the code compiles; but at runtime, `this`
>> evaluates to null
>>
>> public interface Foo
>> {
>>      default Runnable getAction()
>>      {
>>          return ()->{
>>              int x = 0; // a random line
>>              System.out.println(this);  // prints "null"
>>          };
>>      }
>>
>>      public static void main(String[] args)
>>      {
>>          Foo foo = new Foo(){};
>>          foo.getAction().run();
>>      }
>> }
>>
>>
>> Zhong Yu
>>
>


More information about the lambda-dev mailing list