eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
149 stars 117 forks source link

completion returns no results with pattern matching #2106

Open snjeza opened 4 months ago

snjeza commented 4 months ago

The related issues:

From https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/3068 Consider the following code:

import java.util.ArrayList;
import java.util.List;

public class SimpleJavaClass {
  public static void main(String[] args) {
    Object unknown = new ArrayList<>();

    if (unknown instanceof List things) {
      things.is| // CA doesn't work
      Object nothing = null;
      things.is| // CA works
    }
  }
}

CA returns no results if requested for the first occurrence of things.is. It returns isEmpty(), however, for the second occurrence of thing.is

iloveeclipse commented 4 months ago

Can you please check with latest 4.32 I-Build?

srikanth-sankaran commented 4 months ago

Can you please check with latest 4.32 I-Build?

Unfortunately, the recent work on patterns is pure compiler work and does not cover code completion and https://github.com/eclipse-jdt/eclipse.jdt.core/pull/1349 and its cousins are also only focussed on code selection not completion.

@snjeza - the fix proposed in https://github.com/eclipse-jdt/eclipse.jdt.core/pull/2107 is suspect. You should never have to fiddle with ExtraCompilerModifiers.AccOutOfFlowScope bits.

srikanth-sankaran commented 4 months ago

The parse tree recovered in the working case you report is:

import java.util.ArrayList;
import java.util.List;
public class SimpleJavaClass {
  String things;
  public SimpleJavaClass() {
  }
  public static void main(String[] args) {
    Object unknown;
    {
      things.is Object;
      if ((unknown instanceof List things))
          <CompleteOnName:things.is>;
    }
  }
}

and the parse tree recovered in the non-working case is:

import java.util.ArrayList;
import java.util.List;
public class SimpleJavaClass {
  String things;
  public SimpleJavaClass() {
  }
  public static void main(String[] args) {
    Object unknown;
    {
      if ((unknown instanceof List things))
          <CompleteOnType:things.is> Object;
    }
  }
}

There in lies the problem.

Completion is being attempted on a Name in both instances. Why do we think it is being attempted on a Type ? I can help with reviewing an alternate fix. Thanks

srikanth-sankaran commented 4 months ago

See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=568934

snjeza commented 4 months ago

Can you please check with latest 4.32 I-Build?

I have reproduced the issue in the master branch - https://github.com/eclipse-jdt/eclipse.jdt.core/commit/d767ba20f5a1ae9ef4edec3b247d999f5c40a92c

snjeza commented 4 months ago

Completion is being attempted on a Name in both instances. Why do we think it is being attempted on a Type ? I can help with reviewing an alternate fix. Thanks

The parser returns CompletionOnQualifiedNameReference in line 10 (it works) and CompletionOnQualifiedTypeReference in line 8 (it doesn't work)

srikanth-sankaran commented 4 months ago

Completion is being attempted on a Name in both instances. Why do we think it is being attempted on a Type ? I can help with reviewing an alternate fix. Thanks

The parser returns CompletionOnQualifiedNameReference in line 10 (it works) and CompletionOnQualifiedTypeReference in line 8 (it doesn't work)

Right, that is the bug that needs fixing.

snjeza commented 4 months ago

the fix proposed in https://github.com/eclipse-jdt/eclipse.jdt.core/pull/2107 is suspect. You should never have to fiddle with ExtraCompilerModifiers.AccOutOfFlowScope bits.

I have restored it at https://github.com/eclipse-jdt/eclipse.jdt.core/pull/2107/commits/015c32e1e474ddbe8dbc2efbf6b6b3052cd87fa3#diff-8469e0f05a7f350e7047101a3e20fe0bf4d0f65df01fa0a18eb6378128c5de5aR2364

snjeza commented 4 months ago

I can help with reviewing an alternate fix. Right, that is the bug that needs fixing.

I will try. Thanks.

srikanth-sankaran commented 4 months ago

I can help with reviewing an alternate fix. Right, that is the bug that needs fixing.

I will try. Thanks.

Good luck! You may want to compare how the parse tree looks with code select when you control click on things.isEmpty() at both places.