@@ -35,7 +35,7 @@ import dotty.tools.dotc.core.Decorators.i
3535 * to handle the full spectrum of Scala types. Additionally, some kinds of symbols like constructors and
3636 * enum instances get special treatment.
3737 */
38- object JavaNullInterop {
38+ object ImplicitNullInterop {
3939
4040 /** Transforms the type `tp` of Java member `sym` to be explicitly nullable.
4141 * `tp` is needed because the type inside `sym` might not be set when this method is called.
@@ -55,11 +55,11 @@ object JavaNullInterop {
5555 */
5656 def nullifyMember (sym : Symbol , tp : Type , isEnumValueDef : Boolean )(using Context ): Type = trace(i " nullifyMember ${sym}, ${tp}" ){
5757 assert(ctx.explicitNulls)
58- assert(sym.is(JavaDefined ), " can only nullify java-defined members" )
5958
6059 // Some special cases when nullifying the type
61- if isEnumValueDef || sym.name == nme.TYPE_ then
62- // Don't nullify the `TYPE` field in every class and Java enum instances
60+ if isEnumValueDef || sym.name == nme.TYPE_ // Don't nullify the `TYPE` field in every class and Java enum instances
61+ || sym.is(Flags .ModuleVal ) // Don't nullify Modules
62+ then
6363 tp
6464 else if sym.name == nme.toString_ || sym.isConstructor || hasNotNullAnnot(sym) then
6565 // Don't nullify the return type of the `toString` method.
@@ -80,14 +80,14 @@ object JavaNullInterop {
8080 * but the result type is not nullable.
8181 */
8282 private def nullifyExceptReturnType (tp : Type )(using Context ): Type =
83- new JavaNullMap (outermostLevelAlreadyNullable = true )(tp)
83+ new ImplicitNullMap (outermostLevelAlreadyNullable = true )(tp)
8484
85- /** Nullifies a Java type by adding `| Null` in the relevant places. */
85+ /** Nullifies a type by adding `| Null` in the relevant places. */
8686 private def nullifyType (tp : Type )(using Context ): Type =
87- new JavaNullMap (outermostLevelAlreadyNullable = false )(tp)
87+ new ImplicitNullMap (outermostLevelAlreadyNullable = false )(tp)
8888
89- /** A type map that implements the nullification function on types. Given a Java-sourced type, this adds `| Null`
90- * in the right places to make the nulls explicit in Scala .
89+ /** A type map that implements the nullification function on types. Given a Java-sourced type or an
90+ * implicitly null type, this adds `| Null` in the right places to make the nulls explicit.
9191 *
9292 * @param outermostLevelAlreadyNullable whether this type is already nullable at the outermost level.
9393 * For example, `Array[String] | Null` is already nullable at the
@@ -97,17 +97,16 @@ object JavaNullInterop {
9797 * This is useful for e.g. constructors, and also so that `A & B` is nullified
9898 * to `(A & B) | Null`, instead of `(A | Null & B | Null) | Null`.
9999 */
100- private class JavaNullMap (var outermostLevelAlreadyNullable : Boolean )(using Context ) extends TypeMap {
100+ private class ImplicitNullMap (var outermostLevelAlreadyNullable : Boolean )(using Context ) extends TypeMap {
101101 def nullify (tp : Type ): Type = if ctx.flexibleTypes then FlexibleType (tp) else OrNull (tp)
102102
103103 /** Should we nullify `tp` at the outermost level? */
104104 def needsNull (tp : Type ): Boolean =
105105 if outermostLevelAlreadyNullable then false
106106 else tp match
107- case tp : TypeRef if ! tp.hasSimpleKind => false
108- case tp : TypeRef if
107+ case tp : TypeRef if ! tp.hasSimpleKind
109108 // We don't modify value types because they're non-nullable even in Java.
110- tp.symbol.isValueClass
109+ || tp.symbol.isValueClass
111110 // We don't modify unit types.
112111 || tp.isRef(defn.UnitClass )
113112 // We don't modify `Any` because it's already nullable.
@@ -147,6 +146,7 @@ object JavaNullInterop {
147146 outermostLevelAlreadyNullable = oldOutermostNullable
148147 derivedLambdaType(mtp)(paramInfos2, this (mtp.resType))
149148 case tp : TypeAlias => mapOver(tp)
149+ case tp : TypeBounds => mapOver(tp)
150150 case tp : AndType =>
151151 // nullify(A & B) = (nullify(A) & nullify(B)) | Null, but take care not to add
152152 // duplicate `Null`s at the outermost level inside `A` and `B`.
0 commit comments