Skip to content

Commit 58818e9

Browse files
committed
fix SAM type recognition in parameters to JavaDefined methods
1 parent a2a6f5b commit 58818e9

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

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

+1-1
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

+17-3
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ object Types {
948948
if (keepOnly(pre, tp.refinedName)) ns + tp.refinedName else ns
949949
case tp: TypeProxy =>
950950
tp.superType.memberNames(keepOnly, pre)
951+
case tp: FlexibleType =>
952+
tp.underlying.memberNames(keepOnly, pre)
951953
case tp: AndType =>
952954
tp.tp1.memberNames(keepOnly, pre) | tp.tp2.memberNames(keepOnly, pre)
953955
case tp: OrType =>
@@ -3361,6 +3363,12 @@ object Types {
33613363

33623364
// --- FlexibleType -----------------------------------------------------------------
33633365

3366+
object FlexibleType {
3367+
def apply(underlying: Type) = underlying match {
3368+
case ft: FlexibleType => ft
3369+
case _ => new FlexibleType(underlying)
3370+
}
3371+
}
33643372
case class FlexibleType(underlying: Type) extends CachedGroundType with ValueType {
33653373
def lo(using Context): Type = OrNull(underlying)
33663374
override def show(using Context) = i"FlexibleType($underlying)"
@@ -5601,11 +5609,15 @@ object Types {
56015609
foldOver(vmap, t)
56025610
val vmap = accu(VarianceMap.empty, samMeth.info)
56035611
val tparams = tycon.typeParamSymbols
5612+
def boundFollowingVariance(lo: Type, hi: Type, tparam: TypeSymbol) =
5613+
val v = vmap.computedVariance(tparam)
5614+
if v.uncheckedNN < 0 then lo
5615+
else hi
56045616
val args1 = args.zipWithConserve(tparams):
56055617
case (arg @ TypeBounds(lo, hi), tparam) =>
5606-
val v = vmap.computedVariance(tparam)
5607-
if v.uncheckedNN < 0 then lo
5608-
else hi
5618+
boundFollowingVariance(lo, hi, tparam)
5619+
case (arg @ FlexibleType(hi), tparam) =>
5620+
boundFollowingVariance(arg.lo, hi, tparam)
56095621
case (arg, _) => arg
56105622
tp.derivedAppliedType(tycon, args1)
56115623
case _ =>
@@ -5637,6 +5649,8 @@ object Types {
56375649
samClass(tp.underlying)
56385650
case tp: AnnotatedType =>
56395651
samClass(tp.underlying)
5652+
case tp: FlexibleType =>
5653+
samClass(tp.underlying)
56405654
case _ =>
56415655
NoSymbol
56425656

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+
}
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)