Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ static OutputBuilder arguments(List<Expression> args, io.codelaser.maddi.cst.api
lookedUp = null;
}
if (lookedUp != null) {
if (lookedUp.typeParameter() != null
if (lookedUp.typeParameter() != null && lookedUp.typeParameter() != args.get(i).parameterizedType().typeParameter()
|| unwrap(args.get(i)).parameterizedType().typeInfo() != lookedUp.typeInfo()
&& KotlinNullability.nullableInKotlin(args.get(i))
// map.entries.contains(immutableEntry(key, value)) into a Set<Entry<K, V>>: the extension's
Expand Down Expand Up @@ -1261,6 +1261,12 @@ private static ParameterizedType nullableLookup(io.codelaser.maddi.cst.api.info.
}
ParameterizedType argumentType = argument.parameterizedType();
if (typeVariableLookup(element, argumentType)) return element;
if (element != null && argumentType != null && element.typeParameter() != null && element.arrays() == 0
&& element.wildcard() == null && argumentType.arrays() == 0
&& argumentType.typeParameter() == element.typeParameter()) {
// values.contains(v) with a V into a Set<V>: Kotlin's member takes the V as it is, null included
return element;
}
boolean found = element != null && argumentType != null && element.arrays() == argumentType.arrays()
&& element.wildcard() == null && element.typeInfo() != null
&& (element.typeInfo() == argumentType.typeInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,30 @@ boolean has(Object key, Object value) {
compiles(kotlin);
}

/**
* A V looked up in a Set<V> or a Map<V, String>: Java's contains and get take an Object, and a null V finds
* nothing (false, null). Kotlin's member takes the V itself, which may be null: no `!!`, which would throw.
*/
@Test
public void sameTypeVariableLookupNotAsserted() {
String kotlin = kotlin("""
package a;
import java.util.*;
class Look<V> {
private final Set<V> values = new HashSet<>();
private final Map<V, String> names = new HashMap<>();
boolean hasValue(V v) { return values.contains(v); }
boolean named(V v) { return names.get(v) != null; }
boolean add(V v) { return values.add(v); }
}
""");
contains(kotlin, "values.contains(v)");
contains(kotlin, "names.get(v) != null");
contains(kotlin, "values.add(v)");
assertFalse(kotlin.contains("v!!"), () -> "a null v finds nothing in Java; the printed Kotlin asserts it non-null:\n" + kotlin);
compiles(kotlin);
}

/**
* guava's collectors: {@code Collector.of(Acc::create, Acc::add, Acc::combine, Acc::result)} on a generic class
* of ours. Kotlin wants the receiver's type arguments on an unbound reference ({@code Acc<T>::add}, from the
Expand Down
Loading