Bug 33533 - Inlining method with assert has unexpected result
Summary: Inlining method with assert has unexpected result
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 2.1 RC2   Edit
Assignee: Dirk Baeumer CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 33949
Blocks:
  Show dependency tree
 
Reported: 2003-02-28 08:14 EST by Boris Pruessmann CLA
Modified: 2003-03-07 09:47 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Boris Pruessmann CLA 2003-02-28 08:14:46 EST
I2003-02-14

Situation: I have a class Assert that has a static method notNull which I want 
to inline in an attempt of switching to JDK 1.4's assert facility.

The notNull method looks like:

public static final void notNull(Object ref, String message)
{
    assert ref != null : message;
}

A method that uses the notNull method looks like:

void method(String param1, String param2, String param3)
{
    Assert.notNull(param1, "param1 != null");
    Assert.notNull(param2, "param2 != null");
    Assert.notNull(param3, "param3 != null");
}

Unfortunately, after inlinig, it looks like:

void method(String param1, String param2)
{
    assert param1 != null : "param1 != null";
    Object ref = param2;
    assert ref != null : "param2 != null";
    Object ref1 = param3;
    assert ref1 != null : "param3 != null";
}

What I had expected was something like:

void method(String param1, String param2)
{
    assert param1 != null : "param1 != null";
    assert param2 != null : "param2 != null";
    assert param3 != null : "param3 != null";
}
Comment 1 Dirk Baeumer CLA 2003-03-06 10:11:03 EST
Fixed for RC2.
Comment 2 Boris Pruessmann CLA 2003-03-07 07:38:21 EST
With I20030306, the following has the same result as inlining Strings before:

void method(String[] param)
{
    Assert.notNull(param[0], "param[0] != null");
    Assert.notNull(param[1], "param[1] != null");
    Assert.notNull(param[2], "param[2] != null");
}
Comment 3 Boris Pruessmann CLA 2003-03-07 07:46:09 EST
Another non-working one:

void method(String param1, String param2, String param3)
{
    Assert.notNull(param1, "param1 != null");
    Assert.notNull(param2, "param2 != null");
    Assert.notNull(param3, "param3 != null");

   param1 = param1;
   param2 = param2;
   param3 = param3;
}

It seems as if inlinig doesn't produce the correct result once there is read 
access to a specific variable.
Comment 4 Dirk Baeumer CLA 2003-03-07 08:40:15 EST
Ok, these are different case. The problem here is that the flow analysis 
figures out that param[x] might be changed after reading it so it evalutes it 
instead of passing it. But since the method to be inline uses the arg only 
ones this is not necessary.

The last example it even more complicated and will not be addressed for 2.1 
since it is unusual anyways.
Comment 5 Boris Pruessmann CLA 2003-03-07 08:55:34 EST
I don't think the second example is that unusual, I just tried to make it as 
simple as possible. The original code was someting like:

void compress(File in, File out9
{
    Assert.notNull(in, "in != null");
    Assert.notNull(out, "out != null");

    FileInputStream in = new FileInputStream(in);
    FileOutputStream os = new FileOutputStream(out);
...
Comment 6 Dirk Baeumer CLA 2003-03-07 09:44:15 EST
I misinterpreted the second example (took it as the method to be inlined). You 
are correct. What will not work is the following:

public void foo(int x) {
   int y= x;
   x= x;
}

public void use() {
  foo(value());
}

inlining foo will produce 

public void use() {
	int x = value();
	int y= x;
	x= x;
}

also the flow analysis could figure out that x= x is a noop. 
Comment 7 Dirk Baeumer CLA 2003-03-07 09:47:44 EST
Fixed the cases provided in comment #2 and #3. The problem was:

- forgot to handle boolean literals.
- no optimazation of a single read access in the method to be inlined.

Boris, thanks for providing the test cases.