Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 428694

Summary: [1.8][clean up] Lambdification produces code that does not compile (Lambda expression's parameter z cannot redeclare another local variable defined in an enclosing scope)
Product: [Eclipse Project] JDT Reporter: Steve Northover <steve_northover>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: manju656, stephan.herrmann
Version: 4.4   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X   
Whiteboard:

Description Steve Northover CLA 2014-02-20 17:13:22 EST
When lambdified, junk() produces the code found in junkBAD() that does not compile.  

The issue is the variable "z" that is both a local and a parameter to the lambda.  Even though you might expect it to shadow (like parameters shadow fields), it does not in the JDK.

It might be nice for the parameter to be renamed or Eclipse to refuse to lambdify the expression, but it should  not produce code that does not compile.

package junk;

public class Junk8 {
    interface Z {
        int fred(int z);
    }
    private static void junk() {
        int z = 12;
        Z z2 = new Z() {
            public int fred (int z) {
                return z + 1;
            }
        };
        z2.fred(z);
    }
    
    //BAD LAMBDIFICATION - creates a compile error over "z"
    private static void junkBAD() {
        int z = 12;
        Z z2 = z -> z + 1;
        z2.fred(z);
    }
}
Comment 1 Stephan Herrmann CLA 2014-02-20 17:38:20 EST
Moving to JDT/UI
Comment 2 Martin Mathew CLA 2014-02-20 20:08:14 EST

*** This bug has been marked as a duplicate of bug 424223 ***