Skip to content

Commit 0365fd7

Browse files
committed
Use nullLiteral utility method
1 parent 2452ac8 commit 0365fd7

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
5757
def unitLiteral(implicit ctx: Context): Literal =
5858
Literal(Constant(()))
5959

60+
def nullLiteral(implicit ctx: Context): Literal =
61+
Literal(Constant(null))
62+
6063
def New(tpt: Tree)(implicit ctx: Context): New =
6164
ta.assignType(untpd.New(tpt), tpt)
6265

@@ -496,7 +499,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
496499
else if (tpw isRef defn.DoubleClass) Literal(Constant(0d))
497500
else if (tpw isRef defn.ByteClass) Literal(Constant(0.toByte))
498501
else if (tpw isRef defn.ShortClass) Literal(Constant(0.toShort))
499-
else Literal(Constant(null)).select(defn.Any_asInstanceOf).appliedToType(tpe)
502+
else nullLiteral.select(defn.Any_asInstanceOf).appliedToType(tpe)
500503
}
501504

502505
private class FindLocalDummyAccumulator(cls: ClassSymbol)(implicit ctx: Context) extends TreeAccumulator[Symbol] {
@@ -915,7 +918,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
915918
Typed(tree, TypeTree(defn.AnyRefType))
916919
}
917920
else tree.ensureConforms(defn.ObjectType)
918-
receiver.select(defn.Object_ne).appliedTo(Literal(Constant(null)))
921+
receiver.select(defn.Object_ne).appliedTo(nullLiteral)
919922
}
920923

921924
/** If inititializer tree is `_', the default value of its type,

compiler/src/dotty/tools/dotc/transform/Constructors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class Constructors extends MiniPhase with IdentityDenotTransformer { thisPhase =
235235
else {
236236
// insert test: if ($outer eq null) throw new NullPointerException
237237
val nullTest =
238-
If(ref(param).select(defn.Object_eq).appliedTo(Literal(Constant(null))),
238+
If(ref(param).select(defn.Object_eq).appliedTo(nullLiteral),
239239
Throw(New(defn.NullPointerExceptionClass.typeRef, Nil)),
240240
unitLiteral)
241241
nullTest :: assigns

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ object Erasure {
218218
adaptToType(tree, underlying)
219219
else if (!(tree.tpe <:< tycon)) {
220220
assert(!(tree.tpe.typeSymbol.isPrimitiveValueClass))
221-
val nullTree = Literal(Constant(null))
221+
val nullTree = nullLiteral
222222
val unboxedNull = adaptToType(nullTree, underlying)
223223

224224
evalOnce(tree) { t =>

compiler/src/dotty/tools/dotc/transform/LazyVals.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,12 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
189189
holders:::stats
190190
}
191191

192-
private def nullOut(nullables: List[Symbol])(implicit ctx: Context): List[Tree] = {
193-
val nullConst = Literal(Constant(null))
192+
private def nullOut(nullables: List[Symbol])(implicit ctx: Context): List[Tree] =
194193
nullables.map { field =>
195194
assert(field.isField)
196195
field.setFlag(Mutable)
197-
ref(field).becomes(nullConst)
196+
ref(field).becomes(nullLiteral)
198197
}
199-
}
200198

201199
/** Create non-threadsafe lazy accessor equivalent to such code
202200
* ```
@@ -237,7 +235,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
237235
val targetRef = ref(target)
238236
val stats = targetRef.becomes(rhs) :: nullOut(nullableFor(sym))
239237
val init = If(
240-
targetRef.select(nme.eq).appliedTo(Literal(Constant(null))),
238+
targetRef.select(nme.eq).appliedTo(nullLiteral),
241239
Block(stats.init, stats.last),
242240
unitLiteral
243241
)

compiler/src/dotty/tools/dotc/transform/Memoize.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
106106
val NoFieldNeeded = Lazy | Deferred | JavaDefined | (if (ctx.settings.YnoInline.value) EmptyFlags else Inline)
107107

108108
def erasedBottomTree(sym: Symbol) = {
109-
if (sym eq defn.NothingClass) Throw(Literal(Constant(null)))
110-
else if (sym eq defn.NullClass) Literal(Constant(null))
109+
if (sym eq defn.NothingClass) Throw(nullLiteral)
110+
else if (sym eq defn.NullClass) nullLiteral
111111
else if (sym eq defn.BoxedUnitClass) ref(defn.BoxedUnit_UNIT)
112112
else {
113113
assert(false, sym + " has no erased bottom tree")

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ object PatternMatcher {
361361
val unappPlan = if (defn.isBottomType(scrutinee.info)) {
362362
// Generate a throwaway but type-correct plan.
363363
// This plan will never execute because it'll be guarded by a `NonNullTest`.
364-
ResultPlan(tpd.Throw(tpd.Literal(Constant(null))))
364+
ResultPlan(tpd.Throw(tpd.nullLiteral))
365365
} else {
366366
val mt @ MethodType(_) = extractor.tpe.widen
367367
var unapp = extractor.appliedTo(ref(scrutinee).ensureConforms(mt.paramInfos.head))

0 commit comments

Comments
 (0)