Skip to content

Commit 67a46ef

Browse files
committed
Rename functions
1 parent 355daf3 commit 67a46ef

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ import Types._
3737
*/
3838
object JavaNullInterop {
3939

40-
/** Should we try to convert values ignoring Null type at this moment? */
41-
def convertUnsafeNulls(using Context): Boolean =
42-
ctx.explicitNulls && (
43-
config.Feature.enabled(nme.unsafeNulls) ||
44-
ctx.mode.is(Mode.UnsafeNullConversion))
45-
4640
/** Transforms the type `tp` of Java member `sym` to be explicitly nullable.
4741
* `tp` is needed because the type inside `sym` might not be set when this method is called.
4842
*

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@ object NullOpsDecorator {
6969
self.isNullType || self <:< defn.ObjectType
7070
}
7171

72+
def isUnsafelyNulltoAnyRef(pt: Type)(using Context): Boolean =
73+
self.isNullType && pt.isNullableAfterErasure
74+
7275
/** Can we convert a tree with type `self` to type `pt` unsafely.
7376
*/
74-
def isUnsafeConvertable(pt: Type)(using Context): Boolean =
75-
(self.isNullType && pt.isNullableAfterErasure) ||
76-
(self.stripAllNulls <:< pt.stripAllNulls)
77+
def isUnsafelyConvertable(pt: Type, relaxedSubtype: Boolean = false)(using Context): Boolean =
78+
self.isUnsafelyNulltoAnyRef(pt) ||
79+
(if relaxedSubtype then
80+
self.stripAllNulls relaxed_<:< pt.stripAllNulls
81+
else
82+
self.stripAllNulls <:< pt.stripAllNulls)
7783
}
7884
}

compiler/src/dotty/tools/dotc/typer/Nullables.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import ast.Trees.mods
2020
object Nullables:
2121
import ast.tpd._
2222

23+
/** Should we try to convert values ignoring Null type at this moment? */
24+
def convertUnsafeNulls(using Context): Boolean =
25+
ctx.explicitNulls && (
26+
config.Feature.enabled(nme.unsafeNulls) ||
27+
ctx.mode.is(Mode.UnsafeNullConversion))
28+
2329
/** A set of val or var references that are known to be not null, plus a set of
2430
* variable references that are not known (anymore) to be not null
2531
*/

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ object ProtoTypes {
4040
val tpw = tp.widenExpr
4141
val ptw = pt.widenExpr
4242
(tpw relaxed_<:< ptw) ||
43-
ctx.explicitNulls &&
44-
// If unsafeNulls is enabled, we relax the condition by striping all nulls from the types
45-
// before subtype check. We use Feature to check language feature. However, when we search implicits,
46-
// the context is from ContextualImplicits; hence, we don't know whether unsafeNulls is enabled.
47-
// We have to add Mode.UnsafeNullConversion before implicit search.
48-
(config.Feature.enabled(nme.unsafeNulls) || ctx.mode.is(Mode.UnsafeNullConversion)) &&
49-
(tpw.stripAllNulls relaxed_<:< ptw.stripAllNulls) ||
43+
// If unsafeNulls is enabled, we relax the condition by striping all nulls from the types
44+
// before subtype check. We use Feature to check language feature. However, when we search implicits,
45+
// the context is from ContextualImplicits; hence, we don't know whether unsafeNulls is enabled.
46+
// We have to add Mode.UnsafeNullConversion before implicit search.
47+
Nullables.convertUnsafeNulls && tpw.isUnsafelyConvertable(ptw, true) ||
5048
viewExists(tp, pt)
5149

5250
/** Like isCompatibe, but using a subtype comparison with necessary eithers
@@ -56,12 +54,12 @@ object ProtoTypes {
5654
val tpw = tp.widenExpr
5755
val ptw = pt.widenExpr
5856
necessarySubType(tpw, ptw) || tpw.isValueSubType(ptw) ||
59-
ctx.explicitNulls && {
57+
Nullables.convertUnsafeNulls && {
6058
val tpwsn = tpw.stripAllNulls
6159
val ptwsn = ptw.stripAllNulls
6260
// See comments in `isCompatible`
63-
(config.Feature.enabled(nme.unsafeNulls) || ctx.mode.is(Mode.UnsafeNullConversion)) &&
64-
(necessarySubType(tpwsn, ptwsn) || tpwsn.isValueSubType(ptwsn))
61+
necessarySubType(tpwsn, ptwsn) || tpwsn.isValueSubType(ptwsn) ||
62+
tpwsn.isUnsafelyNulltoAnyRef(ptwsn)
6563
} ||
6664
viewExists(tp, pt)
6765

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ class Typer extends Namer
12661266
case SAMType(sam)
12671267
if !defn.isFunctionType(pt1) && (
12681268
mt <:< sam ||
1269-
JavaNullInterop.convertUnsafeNulls && mt.stripAllNulls <:< sam.stripAllNulls) =>
1269+
Nullables.convertUnsafeNulls && mt.stripAllNulls <:< sam.stripAllNulls) =>
12701270
// SAMs of the form C[?] where C is a class cannot be conversion targets.
12711271
// The resulting class `class $anon extends C[?] {...}` would be illegal,
12721272
// since type arguments to `C`'s super constructor cannot be constructed.
@@ -3446,7 +3446,7 @@ class Typer extends Namer
34463446
pt1 match {
34473447
case SAMType(sam)
34483448
if wtp <:< sam.toFunctionType() ||
3449-
(JavaNullInterop.convertUnsafeNulls &&
3449+
(Nullables.convertUnsafeNulls &&
34503450
wtp.stripAllNulls <:< sam.toFunctionType().stripAllNulls) =>
34513451
// was ... && isFullyDefined(pt, ForceDegree.flipBottom)
34523452
// but this prevents case blocks from implementing polymorphic partial functions,
@@ -3521,7 +3521,7 @@ class Typer extends Namer
35213521
def tryUnsafeNullConver(fail: => Tree)(using Context): Tree =
35223522
// If explicitNulls and unsafeNulls are enabled, and
35233523
if ctx.mode.is(Mode.UnsafeNullConversion) && pt.isValueType &&
3524-
treeTpe.isUnsafeConvertable(pt)
3524+
treeTpe.isUnsafelyConvertable(pt)
35253525
then tree.cast(pt)
35263526
else fail
35273527

@@ -3544,9 +3544,8 @@ class Typer extends Namer
35443544
if ctx.mode.is(Mode.ImplicitsEnabled) && tree.typeOpt.isValueType then
35453545
if pt.isRef(defn.AnyValClass) || pt.isRef(defn.ObjectClass) then
35463546
// We want to allow `null` to `AnyRef` if UnsafeNullConversion is enabled
3547-
if !(ctx.explicitNulls &&
3548-
treeTpe.isUnsafeConvertable(pt) &&
3549-
ctx.mode.is(Mode.UnsafeNullConversion)) then
3547+
if !(ctx.mode.is(Mode.UnsafeNullConversion) &&
3548+
treeTpe.isUnsafelyConvertable(pt)) then
35503549
report.error(em"the result of an implicit conversion must be more specific than $pt", tree.srcPos)
35513550
tree.cast(pt)
35523551
else

docs/docs/internals/explicit-nulls.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ There are some utility functions for nullable types in `NullOpsDecorator.scala`
3535
ones (as `stripNull` does).
3636
- `isNullableUnion` determines whether `this` is a nullable union.
3737
- `isNullableAfterErasure` determines whether `this` type can have `null` value after erasure.
38-
- `isUnsafeConvertable` determines whether we can convert `this` type to `pt` unsafely if we ignore `Null` type.
38+
- `isUnsafelyConvertable` determines whether we can convert `this` type to `pt` unsafely if we ignore `Null` type.
3939

4040
Within `Types.scala`, we also defined an extractor `OrNull` to extract the non-nullable part of a nullable unions .
4141

@@ -97,7 +97,7 @@ Since we want to allow selecting member on nullable values, when searching a mem
9797
During adapting, if the type of the tree is not a subtype of the expected type, the `adaptToSubType` in `Typer.scala` will run. The implicit search is invoked to find conversions for the tree. If `unsafeNulls` is enabled, we use the new search scheme:
9898
1. If the `tree.tpe` is nullable, we strip `Null` from the tree then search.
9999
2. If the `tree.tpe` is not nullable or the last step fails, we search on the tree directly.
100-
3. If the last step fails, we try to cast tree to `pt` if the two types `isUnsafeConvertable`.
100+
3. If the last step fails, we try to cast tree to `pt` if the two types `isUnsafelyConvertable`.
101101

102102
Since implicit search (finding candidates and trying to type the new tree) could run in some different contexts, we have to pass the `UnsafeNullConversion` mode to the search context.
103103

0 commit comments

Comments
 (0)