Bug 576152 - Compilation error: "The target type of this expression must be a functional interface"
Summary: Compilation error: "The target type of this expression must be a functional i...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.21   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: 4.22 M1   Edit
Assignee: Andrey Loskutov CLA
QA Contact:
URL:
Whiteboard:
Keywords: regression
: 576174 576437 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-09-21 10:18 EDT by Andy Wilkinson CLA
Modified: 2021-10-05 06:51 EDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andy Wilkinson CLA 2021-09-21 10:18:35 EDT
I'm in the process of upgrading from 2021-03 to 2021-09 and believe I have found a regression in JDT's handling of a method reference that points to an overloaded method.

Here's some sample code that will hopefully illustrate the problem:

package example;

class Example {

    Example() {
        inspect(this::singleMethod);
        inspect(this::overloadedMethod);
    }

    void inspect(Inspector... inspectors) { }

    void singleMethod(byte[] input, int offset, int len) { }

    void overloadedMethod() { }

    void overloadedMethod(byte[] input, int offset, int len) { }

}

@FunctionalInterface
interface Inspector {

    void update(byte[] input, int offset, int len);

}

It compiles without error using 2021-03. With 2021-09 it fails on the line "inspect(this::overloadedMethod);" stating that "The target type of this expression must be a functional interface". I've also tried the I20210920-1800 build of 2021-12 and see the same compilation failure.
Comment 1 Andrey Loskutov CLA 2021-09-22 00:37:22 EDT
See also bug 576174
Comment 2 Andrey Loskutov CLA 2021-09-22 04:11:58 EDT
The regression is between SDK-I20210624-1800 (OK) and SDK-I20210627-1800 (BAD).
I will try to find offending commit.

Combined example from this and bug 576174:

import java.util.function.Supplier;

class Example {
    Example() {
        inspect(this::singleMethod);
        inspect(this::overloadedMethod);
    }
    void inspect(Inspector... inspectors) { }
    void singleMethod(byte[] input, int offset, int len) { }
    void overloadedMethod() { }
    void overloadedMethod(byte[] input, int offset, int len) { }

    public static void main(String[] args) {
    	String s1 = hoge1(String::new);
    	String s2 = hoge2(String::new);	// Error. See the attached file.
    }
    static <T> T hoge1(Supplier<T> sup) {
    	return null;
    }
    static <T> T hoge2(Supplier<T>... sup) {
    	return null;
    }
}

@FunctionalInterface
interface Inspector {
    void update(byte[] input, int offset, int len);
}
Comment 3 Andrey Loskutov CLA 2021-09-22 04:12:25 EDT
*** Bug 576174 has been marked as a duplicate of this bug. ***
Comment 4 Andrey Loskutov CLA 2021-09-22 04:26:46 EDT
(In reply to Andrey Loskutov from comment #2)
> The regression is between SDK-I20210624-1800 (OK) and SDK-I20210627-1800
> (BAD).
> I will try to find offending commit.

This is a regression from bug 573933. 

Reverting commit http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=67749b18db7e9a1536311a699e298db1d2da7f86 fixes the compilation.
Comment 5 Eclipse Genie CLA 2021-09-22 07:39:28 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/185699
Comment 7 Andrey Loskutov CLA 2021-09-26 12:04:39 EDT
Verified with I20210926-0600
Comment 8 Andrey Loskutov CLA 2021-10-05 06:51:40 EDT
*** Bug 576437 has been marked as a duplicate of this bug. ***