Skip to content

Commit bad3c4c

Browse files
committed
use a flexible type when nullifying an upper bound due to Null lower bound
1 parent 095de88 commit bad3c4c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ object Nullables:
3333
&& hi.isValueType
3434
// We cannot check if hi is nullable, because it can cause cyclic reference.
3535

36-
/** Create a nullable type bound
36+
private def nullifiedHi(lo: Type, hi: Type)(using Context): Type =
37+
if needNullifyHi(lo, hi) then
38+
if ctx.flexibleTypes then FlexibleType(hi) else OrType(hi, defn.NullType, soft = false)
39+
else hi
40+
41+
/** Create a nullable type bound
3742
* If lo is `Null`, `| Null` is added to hi
3843
*/
3944
def createNullableTypeBounds(lo: Type, hi: Type)(using Context): TypeBounds =
40-
val newHi = if needNullifyHi(lo, hi) then OrType(hi, defn.NullType, soft = false) else hi
41-
TypeBounds(lo, newHi)
45+
TypeBounds(lo, nullifiedHi(lo, hi))
4246

4347
/** Create a nullable type bound tree
4448
* If lo is `Null`, `| Null` is added to hi
4549
*/
4650
def createNullableTypeBoundsTree(lo: Tree, hi: Tree, alias: Tree = EmptyTree)(using Context): TypeBoundsTree =
47-
val hiTpe = hi.typeOpt
48-
val newHi = if needNullifyHi(lo.typeOpt, hiTpe) then TypeTree(OrType(hiTpe, defn.NullType, soft = false)) else hi
49-
TypeBoundsTree(lo, newHi, alias)
51+
val hiTpe = nullifiedHi(lo.typeOpt, hi.typeOpt)
52+
val hiTree = if(hiTpe eq hi.typeOpt) hi else TypeTree(hiTpe)
53+
TypeBoundsTree(lo, hiTree, alias)
5054

5155
/** A set of val or var references that are known to be not null, plus a set of
5256
* variable references that are not known (anymore) to be not null

0 commit comments

Comments
 (0)