explicting type of shortcut refinement in anyonymous class inside generic function causes error
Consider:
abstract class Bla<A>()
{
shared formal A a;
}
Anything foo<out Bar>(Bla<Bar> bar) => object
extends Bla<Bla<Bar>>()
{
a => bar;
};
This compiles without error. However, expliciting the type of a in the inline anonymous class causes an error:
abstract class Bla<A>()
{
shared formal A a;
}
Anything foo<out Bar>(Bla<Bar> bar) => object
extends Bla<Bla<Bar>>()
{
shared actual Bla<Bar> a => bar;
};
The error happens whenever Bar is not invariant.
The error elided by @Zambonifofex on the explicit type Bla<Bar> is:
covariant ('out') type parameter 'Bar' of 'foo' occurs at a contravariant or invariant location in type: 'Bla<Bar>'
In theory, I think, Bar should be treated as invariant despite the out annotation. The type checker is currently inconsistent on this:
-
extends Bla<Bla<Bar>>()is allowed;Baris treated as invariant inextends, but - as reported,
Bla<Bar>inshared actual Bla<Bar> a => bar;is disallowed, treatingBaras covariant - there's also an error if
foo's return type is changed toBla<Bla<Bar>>(Bla<out Bla<out Bar>>is allowed)
In theory, I think,
Barshould be treated as invariant despite theoutannotation.
Yes, I agree. The method foo() doesn't define any sort of type schema, so there's absolutely no reason to check the variance of occurrences of any of its type parameters, AFAICT.
This should be rather easy to fix. On the other hand, it's not a very severe problem.
Vaguely related to #4386, I suppose.