Bug 563514

Summary: Inlining a method reference fails to consider uses
Product: [Eclipse Project] JDT Reporter: Alex Blewitt <alex.blewitt>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: stephan.herrmann
Version: 4.15   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard: stalebug
Attachments:
Description Flags
Screenshot of bug
none
Inlining with lambda none

Description Alex Blewitt CLA 2020-05-24 07:41:48 EDT
Created attachment 283000 [details]
Screenshot of bug

When inlining the method 'inlineMe' in the following code:

--- 8< ---
package inlining.bug;
public class Bug {
	static void inlineMe() {
		System.out.println("Inline me");
	}
	static Runnable methodReference() {
		final Runnable runnable = Bug::inlineMe;
		return runnable;
	}
}
--- 8< ---

the method reference is just deleted from the code, not in-lined as expected. As a result, the inlining operation results in a compile time error.
Comment 1 Alex Blewitt CLA 2020-05-24 07:42:31 EDT
Tested this against 4.15, haven't verified if the same bug exists in 4.16
Comment 2 Stephan Herrmann CLA 2020-05-24 07:58:54 EDT
Idea: should we actually disallow inlining methods used in a method reference?

Users can still achieve it in two steps:
- convert method reference to lambda
- inline method

Hoping that this works...
Comment 3 Alex Blewitt CLA 2020-05-24 08:06:33 EDT
I think deletion of inlining should be prevented if there are method references. However, if the method is used elsewhere then perhaps that could be inlined.

--
void foo() {
  System.out.print("foo");
}
void bar() {
  foo();
}
Runnable ref() {
  return this::foo;
}
--

In this case, it might be OK to in-line the 'foo' call in bar' but while there's still a reference to this::foo, it probably wouldn't be good to delete it.

What I was actually hoping for was to replace the this::foo with a lambda, so:

--
public class Bug {
	static void inlineMe() {
		System.out.println("Inline me");
	}
	static Runnable methodReference() {
		final Runnable runnable = () -> Bug.inlineMe();
		return runnable;
	}
}
--

That doesn't work either, because 'inlineMe' needs to be a set of statements, not an expression.

However, this works:

--
package inlining.bug;
public class Bug {
	static void inlineMe() {
		System.out.println("Inline me");
	}
	static Runnable methodReference() {
		final Runnable runnable = () -> {
			Bug.inlineMe();
		};
		return runnable;
	}
}
--
Comment 4 Alex Blewitt CLA 2020-05-24 08:06:57 EDT
Created attachment 283001 [details]
Inlining with lambda
Comment 5 Alex Blewitt CLA 2020-05-24 08:14:06 EDT
The point is that while it's possible to allow method references to be inlined, the current behaviour should be to prevent its deletion while there are still method references out there; but preferably, it should be possible to inline a method reference and treat it as if the user had converted it to a lambda as an interim step as per comment 3.
Comment 6 Eclipse Genie CLA 2022-05-15 13:31:21 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 7 Eclipse Genie CLA 2024-05-10 15:40:42 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.