Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference issue between the diamond syntax and pattern matching (switch on objects) #1788

Closed
forax opened this issue Dec 24, 2023 · 3 comments · Fixed by #1790
Closed

Inference issue between the diamond syntax and pattern matching (switch on objects) #1788

forax opened this issue Dec 24, 2023 · 3 comments · Fixed by #1790
Assignees
Milestone

Comments

@forax
Copy link

forax commented Dec 24, 2023

Hello,
the following code does not compile while it compiles with javac 21.

"Cannot infer type arguments for Entry<>" at line 24.


import java.util.Map;

public class EclipseSwitchInferenceBug {
  
  final static class Entry<K,V> {
    K key;
    V value;
    
    public Entry(K key, V value) {
      this.key = key;
      this.value = value;
    }
    
    public K key() { return key; }
    public V value() { return value; }
    public void value(V value) { this.value = value; }
  }
  
  sealed interface I<V> {}
  record A<V>(V value) implements I<V> {}
  record B<V>(V value) implements I<V> {}
  
  private static <K, V> Entry<K,V> foo(Entry<K, I<V>> entry) {
    return new Entry<>(entry.key(), switch (entry.value()) {
      case A<V>(V value) -> {
        entry.value(new B<>(value));
        yield value;
      }
      case B<V>(V value) -> value;
    });
  }
}
@snjeza
Copy link
Contributor

snjeza commented Dec 24, 2023

It works fine in JDT master. You can try https://download.eclipse.org/eclipse/downloads/drops4/I20231223-1800/

@srikanth-sankaran srikanth-sankaran self-assigned this Dec 25, 2023
@srikanth-sankaran
Copy link
Contributor

Seems to have been fixed by the commit d4e6217 made for #1726

@srikanth-sankaran
Copy link
Contributor

Seems to have been fixed by the commit d4e6217 made for #1726

Yep, I remember that fix and can see how that can address this problem. The parse tree for the return expression was messed up before the fix

srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Dec 25, 2023
srikanth-sankaran added a commit that referenced this issue Dec 25, 2023
* Regression test for
#1788
@srikanth-sankaran srikanth-sankaran added this to the 4.31 M1 milestone Dec 25, 2023
rgrunber pushed a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants