Skip to content

Commit 5076b01

Browse files
olhotaknoti0na1
authored andcommitted
fix SAM type recognition in parameters to JavaDefined methods
1 parent c44112a commit 5076b01

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
@@ -976,6 +976,8 @@ object Types {
976976
if (keepOnly(pre, tp.refinedName)) ns + tp.refinedName else ns
977977
case tp: TypeProxy =>
978978
tp.superType.memberNames(keepOnly, pre)
979+
case tp: FlexibleType =>
980+
tp.underlying.memberNames(keepOnly, pre)
979981
case tp: AndType =>
980982
tp.tp1.memberNames(keepOnly, pre) | tp.tp2.memberNames(keepOnly, pre)
981983
case tp: OrType =>
@@ -3408,6 +3410,12 @@ object Types {
34083410

34093411
// --- FlexibleType -----------------------------------------------------------------
34103412

3413+
object FlexibleType {
3414+
def apply(underlying: Type) = underlying match {
3415+
case ft: FlexibleType => ft
3416+
case _ => new FlexibleType(underlying)
3417+
}
3418+
}
34113419
case class FlexibleType(underlying: Type) extends CachedGroundType with ValueType {
34123420
def lo(using Context): Type = OrNull(underlying)
34133421
override def show(using Context) = i"FlexibleType($underlying)"
@@ -5652,11 +5660,15 @@ object Types {
56525660
foldOver(vmap, t)
56535661
val vmap = accu(VarianceMap.empty, samMeth.info)
56545662
val tparams = tycon.typeParamSymbols
5663+
def boundFollowingVariance(lo: Type, hi: Type, tparam: TypeSymbol) =
5664+
val v = vmap.computedVariance(tparam)
5665+
if v.uncheckedNN < 0 then lo
5666+
else hi
56555667
val args1 = args.zipWithConserve(tparams):
56565668
case (arg @ TypeBounds(lo, hi), tparam) =>
5657-
val v = vmap.computedVariance(tparam)
5658-
if v.uncheckedNN < 0 then lo
5659-
else hi
5669+
boundFollowingVariance(lo, hi, tparam)
5670+
case (arg @ FlexibleType(lo, hi), tparam) =>
5671+
boundFollowingVariance(arg.lo, hi, tparam)
56605672
case (arg, _) => arg
56615673
tp.derivedAppliedType(tycon, args1)
56625674
case tp: RefinedType =>
@@ -5692,6 +5704,8 @@ object Types {
56925704
samClass(tp.underlying)
56935705
case tp: AnnotatedType =>
56945706
samClass(tp.underlying)
5707+
case tp: FlexibleType =>
5708+
samClass(tp.underlying)
56955709
case _ =>
56965710
NoSymbol
56975711

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)