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

[BUG] Class<?> inference is confused about capture<?> #2256

Open
Frontear opened this issue Oct 13, 2019 · 1 comment
Open

[BUG] Class<?> inference is confused about capture<?> #2256

Frontear opened this issue Oct 13, 2019 · 1 comment

Comments

@Frontear
Copy link

Frontear commented Oct 13, 2019

Describe the bug
Title sounds a bit weird, but please bear with me.

Lombok will infer Object#getClass as Class<capture of ? extends Object> instead of Class<? extends Object> or something similar. If I then use a Class#getSuperclass, I get Class<capture of ? super capture of ? extends Object rather than Class<capture of ? super Object> or such.

To Reproduce

val obj = new Object(); // doesn't matter, any type works
var type = obj.getClass();
var super_type = type.getSuperclass(); // this causes a compile error, however IntelliJ isn't bothered

/*
Main.java:8: error: incompatible types: Class<CAP#1> cannot be converted to Class<? super Object>
        var super_type = type.getSuperclass(); // this causes a compile error, however IntelliJ isn't bothered
                                           ^
  where CAP#1,CAP#2 are fresh type-variables:
    CAP#1 extends Object super: CAP#2 from capture of ? super CAP#2
    CAP#2 extends Object from capture of ?
*/

Expected behavior
var super_type will be inferred to at the very least Class<?>.

Version info (please complete the following information):

  • Lombok version; 1.18.10
  • Platform: javac 1.8.0_222
@rspilker
Copy link
Collaborator

The problem here is that the signature of getSuperclass() of Class<T> returns Class<? super T>. Which makes sense. The type of the superclass of T has an upper bound of T.

However, if I have a variable of a certain type T, the value can be any subclass of T. So the type of getClass() is ? extends T. Now the lower bound is T.

We would need Class<? super (? extends T)>. That doesn't exist. The only thing that we could generate would be Class<?>.

For the resolution we depend on javac to give us the correct type. We're not sure if at that point in time we have the right information to fix this problem. We cannot replace all ? super X results, we should only do that it X is itself a wildcard (? or ? extends X, ? super X would be fine).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants