JDK-8260566 : Pattern type X is a subtype of expression type Y message is incorrect
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 16,17
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2021-01-28
  • Updated: 2021-02-04
  • Resolved: 2021-01-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17
17 b08Fixed
Related Reports
Relates :  
Description
E.g. compile the following code:

public class Test {
  void test(String s) {
    if(s instanceof Object obj) {}
  }
}

>"C:\Program Files\Java\jdk-17\bin\javac.exe" --enable-preview --release=17 Test.java
Test.java:3: error: pattern type Object is a subtype of expression type String
    if(s instanceof Object obj) {}
         ^
1 error

Clearly, Object is not a subtype of String. It's vice versa: String is subtype of Object.
Comments
Changeset: 739bbd03 Author: Guoxiong Li <lgxbslgx@gmail.com> Committer: Vicente Romero <vromero@openjdk.org> Date: 2021-01-29 18:27:03 +0000 URL: https://git.openjdk.java.net/jdk/commit/739bbd03
29-01-2021