Skip to content

Commit bf8a800

Browse files
committed
clean up nullifyType and nullifyExceptReturnType
1 parent c82d199 commit bf8a800

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,29 @@ object JavaNullInterop {
6363
// Don't nullify the return type of the `toString` method.
6464
// Don't nullify the return type of constructors.
6565
// Don't nullify the return type of methods with a not-null annotation.
66-
nullifyExceptReturnType(tp, sym.owner.isClass)
66+
nullifyExceptReturnType(tp)
6767
else
6868
// Otherwise, nullify everything
69-
nullifyType(tp, sym.owner.isClass)
69+
nullifyType(tp)
7070
}
7171

7272
private def hasNotNullAnnot(sym: Symbol)(using Context): Boolean =
7373
ctx.definitions.NotNullAnnots.exists(nna => sym.unforcedAnnotation(nna).isDefined)
7474

75+
private def javaNullMap(outermostLevelAlreadyNullable: Boolean)(using Context): TypeMap =
76+
if ctx.flexibleTypes then new JavaFlexibleMap(outermostLevelAlreadyNullable) else new JavaNullMap(outermostLevelAlreadyNullable)
77+
7578
/** If tp is a MethodType, the parameters and the inside of return type are nullified,
7679
* but the result return type is not nullable.
7780
* If tp is a type of a field, the inside of the type is nullified,
7881
* but the result type is not nullable.
7982
*/
80-
private def nullifyExceptReturnType(tp: Type, ownerIsClass: Boolean)(using Context): Type =
81-
if ctx.flexibleTypes /*&& ownerIsClass*/ then new JavaFlexibleMap(true)(tp) else new JavaNullMap(true)(tp) // FLEX PARAMS
83+
private def nullifyExceptReturnType(tp: Type)(using Context): Type =
84+
javaNullMap(outermostLevelAlreadyNullable = true)(tp)
8285

8386
/** Nullifies a Java type by adding `| Null` in the relevant places. */
84-
private def nullifyType(tp: Type, ownerIsClass: Boolean)(using Context): Type =
85-
if ctx.flexibleTypes /*&& ownerIsClass*/ then new JavaFlexibleMap(false)(tp) else new JavaNullMap(false)(tp) // FLEX PARAMS
87+
private def nullifyType(tp: Type)(using Context): Type =
88+
javaNullMap(outermostLevelAlreadyNullable = false)(tp)
8689

8790
/** A type map that implements the nullification function on types. Given a Java-sourced type, this adds `| Null`
8891
* in the right places to make the nulls explicit in Scala.

0 commit comments

Comments
 (0)