@@ -76,16 +76,22 @@ object ImplicitNullInterop:
7676 val skipResultType = sym.isConstructor || hasNotNullAnnot(sym)
7777 // Don't nullify Given/implicit parameters
7878 val skipCurrentLevel = sym.isOneOf(GivenOrImplicitVal )
79+ // Use OrNull instead of flexible types if symbol is explicitly nullable
80+ val explicitlyNullable = hasNullableAnnot(sym)
7981
8082 val map = new ImplicitNullMap (
8183 javaDefined = sym.is(JavaDefined ),
8284 skipResultType = skipResultType,
83- skipCurrentLevel = skipCurrentLevel)
85+ skipCurrentLevel = skipCurrentLevel,
86+ explicitlyNullable = explicitlyNullable)
8487 map(tp)
8588
8689 private def hasNotNullAnnot (sym : Symbol )(using Context ): Boolean =
8790 ctx.definitions.NotNullAnnots .exists(nna => sym.unforcedAnnotation(nna).isDefined)
8891
92+ private def hasNullableAnnot (sym : Symbol )(using Context ): Boolean =
93+ ctx.definitions.NullableAnnots .exists(nna => sym.unforcedAnnotation(nna).isDefined)
94+
8995 /** A type map that implements the nullification function on types. Given a Java-sourced type or a type
9096 * coming from Scala code compiled without explicit nulls, this adds `| Null` or `FlexibleType` in the
9197 * right places to make nullability explicit in a conservative way (without forcing incomplete symbols).
@@ -98,10 +104,15 @@ object ImplicitNullInterop:
98104 private class ImplicitNullMap (
99105 val javaDefined : Boolean ,
100106 var skipResultType : Boolean = false ,
101- var skipCurrentLevel : Boolean = false
107+ var skipCurrentLevel : Boolean = false ,
108+ var explicitlyNullable : Boolean = false
102109 )(using Context ) extends TypeMap :
103110
104- def nullify (tp : Type ): Type = if ctx.flexibleTypes then FlexibleType (tp) else OrNull (tp)
111+ def nullify (tp : Type ): Type =
112+ if ctx.flexibleTypes then
113+ if explicitlyNullable then OrNull (tp) else FlexibleType (tp)
114+ else
115+ OrNull (tp)
105116
106117 /** Should we nullify `tp` at the outermost level?
107118 * The symbols are still under construction, so we don't have precise information.
0 commit comments