Inference with unexpected outcome

In Java Iterable interface is invariant, and in Kotlin it’s covariant. Usually Java methods taking Iterable are declared as:

private static <T> void fun(Iterable<? extends T> it, T value) { }

And that allows calling it the same way as in Kotlin: fun(it, 5); where it is a List<String>.

Casting Iterable<Int> to Iterable<Any> is an upcast in Kotlin, same as casting Int to Any. Do you feel that the latter should be prohibited?

As to why Any is inferred for T there — it’s the same reason why we infer a common supertype of two types in listOf(A, B) where A and B are some subtypes of that common supertype.

There are some internal annotations that alter how type inference and overload resolution work, e.g. NoInfer, OnlyInputTypes, Exact, see https://github.com/JetBrains/kotlin/blob/1.2.50/libraries/stdlib/src/kotlin/internal/Annotations.kt.
However we haven’t made them available public yet, because we’re unsure of the design of these features.

We admit that the use cases for these annotations exist, we know that because the introduction of these annotations was driven by the needs of the standard library functions, for example Iterable.contains.