Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Feb 10, 2022
1 parent 33ac95d commit 247c6da
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,8 @@ object Mode {
*/
val ForceInline: Mode = newMode(29, "ForceInline")

val RelaxedOverriding: Mode = newMode(30, "ForceInline")
/** This mode is enabled when we check Java overriding in explicit nulls.
* Type `Null` becomes a bottom type in TypeComparer.
*/
val RelaxedOverriding: Mode = newMode(30, "RelaxedOverriding")
}
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -766,15 +766,20 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling

isSubType(hi1, tp2, approx.addLow) || compareGADT || tryLiftedToThis1
case _ =>
// `Mode.RelaxedOverriding` is only enabled when checking Java overriding
// in explicit nulls, and `Null` becomes a bottom type, which allows
// `T | Null` being a subtype of `T`.
// A type varibale `T` from Java is translated to `T >: Nothing <: Any`.
// However, `null` can always be a value of `T` for Java side.
// So the best solution here is to let `Null` be bottom type temporarily.
def isNullable(tp: Type): Boolean = ctx.mode.is(Mode.RelaxedOverriding) || {
tp.widenDealias match {
tp.widenDealias match
case tp: TypeRef => tp.symbol.isNullableClass
case tp: RefinedOrRecType => isNullable(tp.parent)
case tp: AppliedType => isNullable(tp.tycon)
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)
case OrType(tp1, tp2) => isNullable(tp1) || isNullable(tp2)
case _ => false
}
}
val sym1 = tp1.symbol
(sym1 eq NothingClass) && tp2.isValueTypeOrLambda ||
Expand Down
19 changes: 8 additions & 11 deletions compiler/src/dotty/tools/dotc/transform/OverridingPairs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,13 @@ object OverridingPairs:
}
)
else
def matchNullaryLoosely = member.matchNullaryLoosely || other.matchNullaryLoosely || fallBack
// default getters are not checked for compatibility
member.name.is(DefaultGetterName) || {
if ctx.explicitNulls && (member.is(JavaDefined) || other.is(JavaDefined)) then
// releaxed override check for explicit nulls if one of the symbols is Java defined,
// force `Null` being a bottom types during override checking.
val overrideCtx = ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding)
memberTp.overrides(otherTp, matchNullaryLoosely)(using overrideCtx)
else
memberTp.overrides(otherTp, matchNullaryLoosely)
}
// releaxed override check for explicit nulls if one of the symbols is Java defined,
// force `Null` being a bottom types during override checking.
val overrideCtx = if ctx.explicitNulls && (member.is(JavaDefined) || other.is(JavaDefined))
then ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding) else ctx
member.name.is(DefaultGetterName) // default getters are not checked for compatibility
|| memberTp.overrides(otherTp,
member.matchNullaryLoosely || other.matchNullaryLoosely || fallBack
)(using overrideCtx)

end OverridingPairs
12 changes: 4 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/ResolveSuper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ object ResolveSuper {
val accTp = acc.asSeenFrom(base.typeRef).info
// Since the super class can be Java defined,
// we use releaxed overriding check for explicit nulls if one of the symbols is Java defined.
// This forces `Null` being a subtype of reference types during override checking.
val overridesSuper = if ctx.explicitNulls && (sym.is(JavaDefined) || acc.is(JavaDefined)) then
val overrideCtx = ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding)
otherTp.overrides(accTp, matchLoosely = true)(using overrideCtx)
else
otherTp.overrides(accTp, matchLoosely = true)
if !overridesSuper then
// This forces `Null` being a bottom type during override checking.
val overrideCtx = if ctx.explicitNulls && (sym.is(JavaDefined) || acc.is(JavaDefined))
then ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding) else ctx
if !otherTp.overrides(accTp, matchLoosely = true)(using overrideCtx) then
report.error(IllegalSuperAccessor(base, memberName, targetName, acc, accTp, other.symbol, otherTp), base.srcPos)

bcs = bcs.tail
}
assert(sym.exists, i"cannot rebind $acc, ${acc.targetName} $memberName")
Expand Down

0 comments on commit 247c6da

Please sign in to comment.