Bug 159021 - [compiler] Unused locals initialisation is optimized out when it is a single name reference
Summary: [compiler] Unused locals initialisation is optimized out when it is a single ...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 critical (vote)
Target Milestone: 3.2.2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-27 15:38 EDT by Olivier Thomann CLA
Modified: 2007-01-16 06:30 EST (History)
0 users

See Also:


Attachments
Proposed fix (3.44 KB, patch)
2006-09-27 15:39 EDT, Olivier Thomann CLA
no flags Details | Diff
Regression tests updated (8.84 KB, patch)
2006-09-27 21:20 EDT, Olivier Thomann CLA
no flags Details | Diff
Better patch (35.95 KB, patch)
2006-09-28 07:32 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2006-09-27 15:38:29 EDT
Using HEAD,

interface I {
    int CONST = A.foo();
}

class A {
	static int foo() {
        System.out.println("SUCCESS");
        return 0;
    }
}
class B implements I {
    B() {
        int i = CONST;
    }
}
public class X {
    public static void main(String[] args) {
    	new B();
    }
}

Compiling this code with optimizing out unused locals ends up with no call for the CONST field inside the constructor of B.
-preserveAllLocals should not change the behavior of the code.
The code seems to be in SingleNameReference. I'll attach a patch.
Comment 1 Olivier Thomann CLA 2006-09-27 15:39:48 EDT
Created attachment 51035 [details]
Proposed fix
Comment 2 Olivier Thomann CLA 2006-09-27 21:20:35 EDT
Created attachment 51055 [details]
Regression tests updated

Updated the BooleanTest. We optimized out completely the generation of the static field.
Comment 3 Philipe Mulet CLA 2006-09-28 04:27:54 EDT
Not all SingleNameReferences need to be deoptimized as in previous patch.

interface I {
    int CONST = A.foo();
}

class A {
        static int foo() {
        System.out.println("SUCCESS");
        return 0;
    }
}
class B implements I {
	static int LOCAL_STATIC;
    B(int param) {
        int i = CONST; // keep for possible <clinit>
        int j = param; // may optimize out
        int k = LOCAL_STATIC; // may optimize out
    }
}
public class X {
    public static void main(String[] args) {
        new B(12);
    }
}
Comment 4 Philipe Mulet CLA 2006-09-28 06:11:50 EDT
Testcase for QualifiedNameReference:
"[I.CONST]" should be printed (amongst others)

interface I {
	Value CONST = A.foo("[I.CONST]");
}
class Value {
	static String NAME = "";
	String v;
	Value(String v) {
		this.v = v;
	}
}
class A {
	static Value foo(String str) {
		System.out.print(str);
		return new Value("str");
	}
}
class B implements I {
	static Value LOCAL_STATIC = A.foo("[B.LOCAL_STATIC]");
	Value local_field = A.foo("[B.local_field]");
	B(Value param) {
		String i = CONST.NAME; // keep for possible <clinit>
		String j = param.NAME; // may optimize out
		String k = LOCAL_STATIC.NAME; // may optimize out
		String l = local_field.NAME; // may optimize out
	}
}
public class X {
	public static void main(String[] args) {
		new B(new Value("[PARAM]"));
	}
}
Comment 5 Philipe Mulet CLA 2006-09-28 06:49:28 EDT
Same issue with FieldReference:
"[I.CONST]" should be printed (amongst others)

interface I {
	Value CONST = A.foo("[I.CONST]");
}
class Value {
	static String NAME = "";
	String v;
	Value(String v) {
		this.v = v;
	}
}
class A {
	static Value foo(String str) {
		System.out.print(str);
		return new Value("str");
	}
}
class B implements I {
	static Value LOCAL_STATIC = A.foo("[B.LOCAL_STATIC]");
	Value local_field = A.foo("[B.local_field]");
	B(Value param) {
		String i = this.CONST.NAME; // keep for possible <clinit>
		String k = this.LOCAL_STATIC.NAME; // may optimize out
		String l = this.local_field.NAME; // may optimize out
	}
}
public class X {
	public static void main(String[] args) {
		new B(new Value("[PARAM]"));
	}
}
Comment 6 Philipe Mulet CLA 2006-09-28 07:31:36 EDT
Added InitializationTest#test189-191; GenericTypeTest#test1039-1041.
Comment 7 Philipe Mulet CLA 2006-09-28 07:32:26 EDT
Created attachment 51079 [details]
Better patch

Solves issues for single/qualified and field references
Comment 8 Philipe Mulet CLA 2006-09-28 07:32:49 EDT
Released for 3.3M3.
Fixed
Comment 9 Philipe Mulet CLA 2006-09-28 09:24:21 EDT
Considering for 3.2.2.
Comment 10 Philipe Mulet CLA 2006-09-28 10:33:43 EDT
Released for 3.2.2
Comment 11 David Audel CLA 2006-10-30 09:05:48 EST
Verified for 3.3 M3 using build I20061030-0010
Comment 12 Eric Jodet CLA 2007-01-16 06:29:45 EST
verified for 3.2.2 using build M20070112-1200