Skip to content

Commit 2ecc091

Browse files
olhotaknoti0na1
authored andcommitted
fix SAM type recognition in parameters to JavaDefined methods
1 parent c11baf4 commit 2ecc091

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

compiler/src/dotty/tools/dotc/core/JavaNullInterop.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ object JavaNullInterop {
122122
// We don't make the outmost levels of type arguments nullable if tycon is Java-defined.
123123
// This is because Java classes are _all_ nullified, so both `java.util.List[String]` and
124124
// `java.util.List[String|Null]` contain nullable elements.
125-
outermostLevelAlreadyNullable = tp.classSymbol.is(JavaDefined)
125+
outermostLevelAlreadyNullable = tp.classSymbol.is(JavaDefined) && !ctx.flexibleTypes
126126
val targs2 = targs map this
127127
outermostLevelAlreadyNullable = oldOutermostNullable
128128
val appTp2 = derivedAppliedType(appTp, tycon, targs2)

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ object Types {
970970
if (keepOnly(pre, tp.refinedName)) ns + tp.refinedName else ns
971971
case tp: TypeProxy =>
972972
tp.superType.memberNames(keepOnly, pre)
973+
case tp: FlexibleType =>
974+
tp.underlying.memberNames(keepOnly, pre)
973975
case tp: AndType =>
974976
tp.tp1.memberNames(keepOnly, pre) | tp.tp2.memberNames(keepOnly, pre)
975977
case tp: OrType =>
@@ -3402,6 +3404,12 @@ object Types {
34023404

34033405
// --- FlexibleType -----------------------------------------------------------------
34043406

3407+
object FlexibleType {
3408+
def apply(underlying: Type) = underlying match {
3409+
case ft: FlexibleType => ft
3410+
case _ => new FlexibleType(underlying)
3411+
}
3412+
}
34053413
case class FlexibleType(underlying: Type) extends CachedGroundType with ValueType {
34063414
def lo(using Context): Type = OrNull(underlying)
34073415
override def show(using Context) = i"FlexibleType($underlying)"
@@ -5643,11 +5651,15 @@ object Types {
56435651
foldOver(vmap, t)
56445652
val vmap = accu(VarianceMap.empty, samMeth.info)
56455653
val tparams = tycon.typeParamSymbols
5654+
def boundFollowingVariance(lo: Type, hi: Type, tparam: TypeSymbol) =
5655+
val v = vmap.computedVariance(tparam)
5656+
if v.uncheckedNN < 0 then lo
5657+
else hi
56465658
val args1 = args.zipWithConserve(tparams):
56475659
case (arg @ TypeBounds(lo, hi), tparam) =>
5648-
val v = vmap.computedVariance(tparam)
5649-
if v.uncheckedNN < 0 then lo
5650-
else hi
5660+
boundFollowingVariance(lo, hi, tparam)
5661+
case (arg @ FlexibleType(lo, hi), tparam) =>
5662+
boundFollowingVariance(arg.lo, hi, tparam)
56515663
case (arg, _) => arg
56525664
tp.derivedAppliedType(tycon, args1)
56535665
case tp: RefinedType =>
@@ -5683,6 +5695,8 @@ object Types {
56835695
samClass(tp.underlying)
56845696
case tp: AnnotatedType =>
56855697
samClass(tp.underlying)
5698+
case tp: FlexibleType =>
5699+
samClass(tp.underlying)
56865700
case _ =>
56875701
NoSymbol
56885702

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class injava {
2+
static void overloaded(Runnable r) {}
3+
static void overloaded(int i) {}
4+
5+
static void notoverloaded(Runnable r) {}
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def foo = {
2+
def unit: Unit = ()
3+
4+
injava.overloaded({ () => unit } : Runnable )
5+
injava.overloaded({ () => unit } )
6+
7+
injava.notoverloaded({ () => unit } : Runnable )
8+
injava.notoverloaded({ () => unit } )
9+
10+
val list = new java.util.Vector[Int]()
11+
java.util.Collections.sort[Int](list, { (a,b) => a - b } : java.util.Comparator[Int] )
12+
java.util.Collections.sort[Int](list, { (a,b) => a - b })
13+
14+
new Thread({ () => unit } : Runnable )
15+
new Thread({ () => unit } )
16+
17+
val cf = new java.util.concurrent.CompletableFuture[String]
18+
cf.handle[Unit]({
19+
case (string, null) => unit
20+
case (string, throwable) => unit
21+
})
22+
}

0 commit comments

Comments
 (0)