Skip to content

Commit

Permalink
Add restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Feb 18, 2022
1 parent 4472e61 commit 939ec21
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ object Mode {
val ForceInline: Mode = newMode(29, "ForceInline")

/** This mode is enabled when we check Java overriding in explicit nulls.
* Type `Null` becomes a bottom type in TypeComparer.
* Type `Null` becomes a subtype of non-primitive value types in TypeComparer.
*/
val RelaxedOverriding: Mode = newMode(30, "RelaxedOverriding")
}
23 changes: 13 additions & 10 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -771,16 +771,19 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
// `T | Null` being a subtype of `T`.
// A type variable `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
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
}
// So the best solution here is to let `Null` be a subtype of non-primitive
// value types temporarily.
def isNullable(tp: Type): Boolean = tp.widenDealias match
case tp: TypeRef =>
val tpSym = tp.symbol
ctx.mode.is(Mode.RelaxedOverriding) && !tpSym.isPrimitiveValueClass ||
tpSym.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 ||
(sym1 eq NullClass) && isNullable(tp2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ object OverridingPairs:
)
else
// releaxed override check for explicit nulls if one of the symbols is Java defined,
// force `Null` to be a bottom type during override checking.
// force `Null` to be a subtype of non-primitive value 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
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/ResolveSuper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ object ResolveSuper {
val accTp = acc.asSeenFrom(base.typeRef).info
// Since the super class can be Java defined,
// we use relaxed overriding check for explicit nulls if one of the symbols is Java defined.
// This forces `Null` to be a bottom type during override checking.
// This forces `Null` to be a subtype of non-primitive value types 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
Expand Down

0 comments on commit 939ec21

Please sign in to comment.