Skip to content

Commit b992d98

Browse files
committed
Strip Nulls from Java calls under unsafeNulls
1 parent ea871c2 commit b992d98

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,6 +3746,15 @@ class Typer extends Namer
37463746
if target <:< pt then
37473747
return readapt(tree.cast(target))
37483748

3749+
// if unsafeNulls is enabled, try to strip nulls from Java function calls
3750+
if Nullables.unsafeNullsEnabled then
3751+
tree match
3752+
case _: Apply if tree.symbol.is(JavaDefined) =>
3753+
wtp match
3754+
case OrNull(wtp1) => return readapt(tree.cast(wtp1))
3755+
case _ =>
3756+
case _ =>
3757+
37493758
def recover(failure: SearchFailureType) =
37503759
if canDefineFurther(wtp) || canDefineFurther(pt) then readapt(tree)
37513760
else err.typeMismatch(tree, pt, failure)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class J {
2+
public String f1() {
3+
return "";
4+
}
5+
6+
public int f2() {
7+
return 0;
8+
}
9+
10+
public <T> T g1() {
11+
return null;
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.language.unsafeNulls
2+
3+
val j: J = new J
4+
5+
val s1: String = j.f1()
6+
7+
val i1: Int = j.f2()
8+
9+
val s2: String = j.g1[String]()
10+
11+
val i2: Int = j.g1[Int]()
12+
13+
val a1: Any = j.g1[Any]()
14+
15+
val ar1: AnyRef = j.g1[AnyRef]()
16+
17+
val n1: Null = j.g1[Null]()
18+
19+
val ar2: AnyRef = j.g1[Null]()
20+
21+
def clo[T]: T = j.g1[T]()

0 commit comments

Comments
 (0)