Skip to content

Commit 247c6da

Browse files
committed
Add comments
1 parent 33ac95d commit 247c6da

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,8 @@ object Mode {
125125
*/
126126
val ForceInline: Mode = newMode(29, "ForceInline")
127127

128-
val RelaxedOverriding: Mode = newMode(30, "ForceInline")
128+
/** This mode is enabled when we check Java overriding in explicit nulls.
129+
* Type `Null` becomes a bottom type in TypeComparer.
130+
*/
131+
val RelaxedOverriding: Mode = newMode(30, "RelaxedOverriding")
129132
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,20 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
766766

767767
isSubType(hi1, tp2, approx.addLow) || compareGADT || tryLiftedToThis1
768768
case _ =>
769+
// `Mode.RelaxedOverriding` is only enabled when checking Java overriding
770+
// in explicit nulls, and `Null` becomes a bottom type, which allows
771+
// `T | Null` being a subtype of `T`.
772+
// A type varibale `T` from Java is translated to `T >: Nothing <: Any`.
773+
// However, `null` can always be a value of `T` for Java side.
774+
// So the best solution here is to let `Null` be bottom type temporarily.
769775
def isNullable(tp: Type): Boolean = ctx.mode.is(Mode.RelaxedOverriding) || {
770-
tp.widenDealias match {
776+
tp.widenDealias match
771777
case tp: TypeRef => tp.symbol.isNullableClass
772778
case tp: RefinedOrRecType => isNullable(tp.parent)
773779
case tp: AppliedType => isNullable(tp.tycon)
774780
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)
775781
case OrType(tp1, tp2) => isNullable(tp1) || isNullable(tp2)
776782
case _ => false
777-
}
778783
}
779784
val sym1 = tp1.symbol
780785
(sym1 eq NothingClass) && tp2.isValueTypeOrLambda ||

compiler/src/dotty/tools/dotc/transform/OverridingPairs.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,13 @@ object OverridingPairs:
216216
}
217217
)
218218
else
219-
def matchNullaryLoosely = member.matchNullaryLoosely || other.matchNullaryLoosely || fallBack
220-
// default getters are not checked for compatibility
221-
member.name.is(DefaultGetterName) || {
222-
if ctx.explicitNulls && (member.is(JavaDefined) || other.is(JavaDefined)) then
223-
// releaxed override check for explicit nulls if one of the symbols is Java defined,
224-
// force `Null` being a bottom types during override checking.
225-
val overrideCtx = ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding)
226-
memberTp.overrides(otherTp, matchNullaryLoosely)(using overrideCtx)
227-
else
228-
memberTp.overrides(otherTp, matchNullaryLoosely)
229-
}
219+
// releaxed override check for explicit nulls if one of the symbols is Java defined,
220+
// force `Null` being a bottom types during override checking.
221+
val overrideCtx = if ctx.explicitNulls && (member.is(JavaDefined) || other.is(JavaDefined))
222+
then ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding) else ctx
223+
member.name.is(DefaultGetterName) // default getters are not checked for compatibility
224+
|| memberTp.overrides(otherTp,
225+
member.matchNullaryLoosely || other.matchNullaryLoosely || fallBack
226+
)(using overrideCtx)
230227

231228
end OverridingPairs

compiler/src/dotty/tools/dotc/transform/ResolveSuper.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,11 @@ object ResolveSuper {
113113
val accTp = acc.asSeenFrom(base.typeRef).info
114114
// Since the super class can be Java defined,
115115
// we use releaxed overriding check for explicit nulls if one of the symbols is Java defined.
116-
// This forces `Null` being a subtype of reference types during override checking.
117-
val overridesSuper = if ctx.explicitNulls && (sym.is(JavaDefined) || acc.is(JavaDefined)) then
118-
val overrideCtx = ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding)
119-
otherTp.overrides(accTp, matchLoosely = true)(using overrideCtx)
120-
else
121-
otherTp.overrides(accTp, matchLoosely = true)
122-
if !overridesSuper then
116+
// This forces `Null` being a bottom type during override checking.
117+
val overrideCtx = if ctx.explicitNulls && (sym.is(JavaDefined) || acc.is(JavaDefined))
118+
then ctx.retractMode(Mode.SafeNulls).addMode(Mode.RelaxedOverriding) else ctx
119+
if !otherTp.overrides(accTp, matchLoosely = true)(using overrideCtx) then
123120
report.error(IllegalSuperAccessor(base, memberName, targetName, acc, accTp, other.symbol, otherTp), base.srcPos)
124-
125121
bcs = bcs.tail
126122
}
127123
assert(sym.exists, i"cannot rebind $acc, ${acc.targetName} $memberName")

0 commit comments

Comments
 (0)