JDK-8318203 : javac does not reject private method reference with type-variable receiver
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 22
  • Submitted: 2023-10-17
  • Updated: 2024-01-31
  • Resolved: 2023-10-23
Related Reports
CSR :  
Description
Summary
-------

The javac compiler should not accept private method references with a type variable receiver.

Problem
-------

JLS 4.4 specifies that:

    The members of a type variable X with bound T & I 1 & ... & I n are the members of
    the intersection type (ยง4.9) T & I 1 & ... & I n appearing at the point where the type
    variable is declared.

So code like:

    import java.util.function.*;
    class Test {
        private String asString() {
            return "bar";
        }

        static <T extends Test> Function<T, String> foo() {
            return T::asString;
        }
    }

should be rejected by javac as private method `asString` is not a member of type variable `T` 

Solution
--------

The proposed solution is to sync javac with the specification so that private method references with type variable receiver are rejected by javac

Specification
-------------

No specification changes are required.
Comments
[~darcy] thanks for the review
24-10-2023

Moving to Approved for adding this check for all source levels, contingent on a release note being written.
23-10-2023

Moving to Provisional, not Approved. Hmm. I think this change needs a bit more compatibility consideration. I assume the proposal is to make this change independent of source level? A more compatible change would be for --source 22 and later only.
19-10-2023