Skip to content

Commit 0969035

Browse files
committed
Clean up naming of top and bottom class tests
isNothing -> isExactlyNothing isTopType -> isExactlyAny Note: there is alrady an isAny, which is isRef(AnyClass) and therefore more general. Also: Move isBottomType from defn to Type.
1 parent 37098b1 commit 0969035

File tree

13 files changed

+50
-49
lines changed

13 files changed

+50
-49
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
586586
val tree2: Select = tree.tpe match {
587587
case tpe: NamedType =>
588588
val qualType = qualifier.tpe.widenIfUnstable
589-
if qualType.isNothing then tree1.withTypeUnchecked(tree.tpe)
589+
if qualType.isExactlyNothing then tree1.withTypeUnchecked(tree.tpe)
590590
else tree1.withType(tpe.derivedSelect(qualType))
591591
case _ => tree1.withTypeUnchecked(tree.tpe)
592592
}
@@ -980,7 +980,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
980980

981981
/** `tree ne null` (might need a cast to be type correct) */
982982
def testNotNull(using Context): Tree = {
983-
val receiver = if (defn.isBottomType(tree.tpe))
983+
val receiver = if (tree.tpe.isBottomType)
984984
// If the receiver is of type `Nothing` or `Null`, add an ascription so that the selection
985985
// succeeds: e.g. `null.ne(null)` doesn't type, but `(null: AnyRef).ne(null)` does.
986986
Typed(tree, TypeTree(defn.AnyRefType))

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,13 +1174,6 @@ class Definitions {
11741174

11751175
def isBottomClassAfterErasure(cls: Symbol): Boolean = cls == NothingClass || cls == NullClass
11761176

1177-
def isBottomType(tp: Type): Boolean =
1178-
if ctx.explicitNulls && !ctx.phase.erasedTypes then tp.hasClassSymbol(NothingClass)
1179-
else isBottomTypeAfterErasure(tp)
1180-
1181-
def isBottomTypeAfterErasure(tp: Type): Boolean =
1182-
tp.hasClassSymbol(NothingClass) || tp.hasClassSymbol(NullClass)
1183-
11841177
/** Is a function class.
11851178
* - FunctionXXL
11861179
* - FunctionN for N >= 0

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ class TypeApplications(val self: Type) extends AnyVal {
407407
if (self.derivesFrom(from)) {
408408
def elemType(tp: Type): Type = tp.widenDealias match
409409
case tp: OrType =>
410-
if defn.isBottomType(tp.tp1) then elemType(tp.tp2)
411-
else if defn.isBottomType(tp.tp2) then elemType(tp.tp1)
410+
if tp.tp1.isBottomType then elemType(tp.tp2)
411+
else if tp.tp2.isBottomType then elemType(tp.tp1)
412412
else tp.derivedOrType(elemType(tp.tp1), elemType(tp.tp2))
413413
case tp: AndType => tp.derivedAndType(elemType(tp.tp1), elemType(tp.tp2))
414414
case _ => tp.baseType(from).argInfos.headOption.getOrElse(defn.NothingType)
@@ -503,8 +503,8 @@ class TypeApplications(val self: Type) extends AnyVal {
503503
def elemType(using Context): Type = self.widenDealias match {
504504
case defn.ArrayOf(elemtp) => elemtp
505505
case JavaArrayType(elemtp) => elemtp
506-
case tp: OrType if defn.isBottomType(tp.tp1) => tp.tp2.elemType
507-
case tp: OrType if defn.isBottomType(tp.tp2) => tp.tp1.elemType
506+
case tp: OrType if tp.tp1.isBottomType => tp.tp2.elemType
507+
case tp: OrType if tp.tp2.isBottomType => tp.tp1.elemType
508508
case _ => self.baseType(defn.SeqClass).argInfos.headOption.getOrElse(NoType)
509509
}
510510
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
715715
}
716716
compareTypeBounds
717717
case tp2: AnnotatedType if tp2.isRefining =>
718-
(tp1.derivesAnnotWith(tp2.annot.sameAnnotation) || defn.isBottomType(tp1)) &&
718+
(tp1.derivesAnnotWith(tp2.annot.sameAnnotation) || tp1.isBottomType) &&
719719
recur(tp1, tp2.parent)
720720
case ClassInfo(pre2, cls2, _, _, _) =>
721721
def compareClassInfo = tp1 match {
@@ -2338,7 +2338,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
23382338
*/
23392339
def provablyEmpty(tp: Type): Boolean =
23402340
tp.dealias match {
2341-
case tp if tp.isNothing => true
2341+
case tp if tp.isExactlyNothing => true
23422342
case AndType(tp1, tp2) => provablyDisjoint(tp1, tp2)
23432343
case OrType(tp1, tp2) => provablyEmpty(tp1) && provablyEmpty(tp2)
23442344
case at @ AppliedType(tycon, args) =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ object TypeErasure {
291291
// We need to short-circuit this case here because the regular lub logic below
292292
// relies on the class hierarchy, which doesn't properly capture `Null`s subtyping
293293
// behaviour.
294-
if (defn.isBottomTypeAfterErasure(tp1) && tp2.derivesFrom(defn.ObjectClass)) return tp2
295-
if (defn.isBottomTypeAfterErasure(tp2) && tp1.derivesFrom(defn.ObjectClass)) return tp1
294+
if (tp1.isBottomTypeAfterErasure && tp2.derivesFrom(defn.ObjectClass)) return tp2
295+
if (tp2.isBottomTypeAfterErasure && tp1.derivesFrom(defn.ObjectClass)) return tp1
296296
tp1 match {
297297
case JavaArrayType(elem1) =>
298298
import dotty.tools.dotc.transform.TypeUtils._

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ object TypeOps:
152152
simplify(l, theMap) & simplify(r, theMap)
153153
case tp as OrType(l, r)
154154
if !ctx.mode.is(Mode.Type)
155-
&& (tp.isSoft || defn.isBottomType(l) || defn.isBottomType(r)) =>
155+
&& (tp.isSoft || l.isBottomType || r.isBottomType) =>
156156
// Normalize A | Null and Null | A to A even if the union is hard (i.e.
157157
// explicitly declared), but not if -Yexplicit-nulls is set. The reason is
158158
// that in this case the normal asSeenFrom machinery is not prepared to deal

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,27 @@ object Types {
209209
def isAnyRef(using Context): Boolean = isRef(defn.ObjectClass, skipRefined = false)
210210
def isAnyKind(using Context): Boolean = isRef(defn.AnyKindClass, skipRefined = false)
211211

212-
def isFromJavaObject(using Context): Boolean = typeSymbol eq defn.FromJavaObjectSymbol
212+
/** Is this type exactly Nothing (no vars, aliases, refinements etc allowed)? */
213+
def isExactlyNothing(using Context): Boolean = this match {
214+
case tp: TypeRef =>
215+
tp.name == tpnme.Nothing && (tp.symbol eq defn.NothingClass)
216+
case _ => false
217+
}
218+
219+
/** Is this type exactly Any (no vars, aliases, refinements etc allowed)? */
220+
def isExactlyAny(using Context): Boolean = this match {
221+
case tp: TypeRef =>
222+
tp.name == tpnme.Any && (tp.symbol eq defn.AnyClass)
223+
case _ => false
224+
}
225+
226+
def isBottomType(using Context): Boolean =
227+
if ctx.explicitNulls && !ctx.phase.erasedTypes then hasClassSymbol(defn.NothingClass)
228+
else isBottomTypeAfterErasure
229+
230+
def isBottomTypeAfterErasure(using Context): Boolean =
231+
val d = defn
232+
hasClassSymbol(d.NothingClass) || hasClassSymbol(d.NullClass)
213233

214234
/** Does this type refer exactly to class symbol `sym`, instead of to a subclass of `sym`?
215235
* Implemented like `isRef`, but follows more types: all type proxies as well as and- and or-types
@@ -244,9 +264,9 @@ object Types {
244264
// If the type is `T | Null` or `T | Nothing`, the class is != Nothing,
245265
// and `T` derivesFrom the class, then the OrType derivesFrom the class.
246266
// Otherwise, we need to check both sides derivesFrom the class.
247-
if defn.isBottomType(tp.tp1) && cls != defn.NothingClass then
267+
if tp.tp1.isBottomType && cls != defn.NothingClass then
248268
loop(tp.tp2)
249-
else if defn.isBottomType(tp.tp2) && cls != defn.NothingClass then
269+
else if tp.tp2.isBottomType && cls != defn.NothingClass then
250270
loop(tp.tp1)
251271
else
252272
loop(tp.tp1) && loop(tp.tp2)
@@ -258,6 +278,8 @@ object Types {
258278
loop(this)
259279
}
260280

281+
def isFromJavaObject(using Context): Boolean = typeSymbol eq defn.FromJavaObjectSymbol
282+
261283
/** True iff `symd` is a denotation of a class type parameter and the reference
262284
* `<this> . <symd>` is an actual argument reference, i.e. `this` is different
263285
* from the ThisType of `symd`'s owner.
@@ -271,20 +293,6 @@ object Types {
271293
}
272294
}
273295

274-
/** Is this type exactly Nothing (no vars, aliases, refinements etc allowed)? */
275-
def isNothing(using Context): Boolean = this match {
276-
case tp: TypeRef =>
277-
tp.name == tpnme.Nothing && (tp.symbol eq defn.NothingClass)
278-
case _ => false
279-
}
280-
281-
/** Is this type exactly Any (no vars, aliases, refinements etc allowed)? */
282-
def isTopType(using Context): Boolean = this match {
283-
case tp: TypeRef =>
284-
tp.name == tpnme.Any && (tp.symbol eq defn.AnyClass)
285-
case _ => false
286-
}
287-
288296
/** Is this type a (possibly aliased) singleton type? */
289297
def isSingleton(using Context): Boolean = dealias.isInstanceOf[SingletonType]
290298

@@ -2156,8 +2164,8 @@ object Types {
21562164
case arg: TypeBounds =>
21572165
val v = param.paramVarianceSign
21582166
val pbounds = param.paramInfo
2159-
if (v > 0 && pbounds.loBound.dealiasKeepAnnots.isNothing) TypeAlias(arg.hiBound & rebase(pbounds.hiBound))
2160-
else if (v < 0 && pbounds.hiBound.dealiasKeepAnnots.isTopType) TypeAlias(arg.loBound | rebase(pbounds.loBound))
2167+
if (v > 0 && pbounds.loBound.dealiasKeepAnnots.isExactlyNothing) TypeAlias(arg.hiBound & rebase(pbounds.hiBound))
2168+
else if (v < 0 && pbounds.hiBound.dealiasKeepAnnots.isExactlyAny) TypeAlias(arg.loBound | rebase(pbounds.loBound))
21612169
else arg recoverable_& rebase(pbounds)
21622170
case arg => TypeAlias(arg)
21632171
}
@@ -2319,7 +2327,7 @@ object Types {
23192327
if (base.isAnd == variance >= 0) tp1 & tp2 else tp1 | tp2
23202328
case _ =>
23212329
if (pre.termSymbol.is(Package)) argForParam(pre.select(nme.PACKAGE))
2322-
else if (pre.isNothing) pre
2330+
else if (pre.isExactlyNothing) pre
23232331
else NoType
23242332
}
23252333
}
@@ -2338,7 +2346,7 @@ object Types {
23382346
*/
23392347
def derivedSelect(prefix: Type)(using Context): Type =
23402348
if (prefix eq this.prefix) this
2341-
else if (prefix.isNothing) prefix
2349+
else if (prefix.isExactlyNothing) prefix
23422350
else {
23432351
if (isType) {
23442352
val res =
@@ -4326,7 +4334,7 @@ object Types {
43264334

43274335
/** For uninstantiated type variables: Is the lower bound different from Nothing? */
43284336
def hasLowerBound(using Context): Boolean =
4329-
!ctx.typerState.constraint.entry(origin).loBound.isNothing
4337+
!ctx.typerState.constraint.entry(origin).loBound.isExactlyNothing
43304338

43314339
/** For uninstantiated type variables: Is the upper bound different from Any? */
43324340
def hasUpperBound(using Context): Boolean =
@@ -5331,7 +5339,7 @@ object Types {
53315339
case _ =>
53325340
def propagate(lo: Type, hi: Type) =
53335341
range(derivedRefinedType(tp, parent, lo), derivedRefinedType(tp, parent, hi))
5334-
if (parent.isNothing) parent
5342+
if (parent.isExactlyNothing) parent
53355343
else info match {
53365344
case Range(infoLo: TypeBounds, infoHi: TypeBounds) =>
53375345
assert(variance == 0)
@@ -5424,7 +5432,7 @@ object Types {
54245432
case Range(lo, hi) =>
54255433
range(tp.derivedAnnotatedType(lo, annot), tp.derivedAnnotatedType(hi, annot))
54265434
case _ =>
5427-
if (underlying.isNothing) underlying
5435+
if (underlying.isExactlyNothing) underlying
54285436
else tp.derivedAnnotatedType(underlying, annot)
54295437
}
54305438
override protected def derivedWildcardType(tp: WildcardType, bounds: Type): WildcardType =
@@ -5672,7 +5680,7 @@ object Types {
56725680
else {
56735681
seen += tp
56745682
tp match {
5675-
case tp if tp.isTopType || tp.isNothing =>
5683+
case tp if tp.isExactlyAny || tp.isExactlyNothing =>
56765684
cs
56775685
case tp: AppliedType =>
56785686
foldOver(cs + tp.typeSymbol, tp)

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ object Completion {
205205
* considered.
206206
*/
207207
def addMemberCompletions(qual: Tree)(using Context): Unit =
208-
if (!qual.tpe.widenDealias.isNothing) {
208+
if (!qual.tpe.widenDealias.isExactlyNothing) {
209209
addAccessibleMembers(qual.tpe)
210210
if (!mode.is(Mode.Import) && !qual.tpe.isNullType)
211211
// Implicit conversions do not kick in when importing

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ import transform.SymUtils._
269269
if expected.isAny
270270
|| expected.isAnyRef
271271
|| expected.isRef(defn.AnyValClass)
272-
|| defn.isBottomType(found)
272+
|| found.isBottomType
273273
then ""
274274
else ctx.typer.importSuggestionAddendum(ViewProto(found.widen, expected))
275275
val (where, printCtx) = Formatting.disambiguateTypes(found2, expected2)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ object PatternMatcher {
373373
patternPlan(casted, pat, onSuccess)
374374
})
375375
case UnApply(extractor, implicits, args) =>
376-
val unappPlan = if (defn.isBottomType(scrutinee.info))
376+
val unappPlan = if (scrutinee.info.isBottomType)
377377
// Generate a throwaway but type-correct plan.
378378
// This plan will never execute because it'll be guarded by a `NonNullTest`.
379379
ResultPlan(tpd.Throw(tpd.nullLiteral))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ abstract class TransformByNameApply extends MiniPhase { thisPhase: DenotTransfor
4242
def transformArg(arg: Tree, formal: Type): Tree = formal.dealias match {
4343
case formalExpr: ExprType =>
4444
var argType = arg.tpe.widenIfUnstable
45-
if (defn.isBottomType(argType)) argType = formal.widenExpr
45+
if (argType.isBottomType) argType = formal.widenExpr
4646
def wrap(arg: Tree) =
4747
ref(defn.cbnArg).appliedToType(argType).appliedTo(arg).withSpan(arg.span)
4848
arg match {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ object ErrorReporting {
165165
|Note that `${tree.name}` is treated as an infix operator in Scala 3.
166166
|If you do not want that, insert a `;` or empty line in front
167167
|or drop any spaces behind the operator."""
168-
else if qualType.isNothing then
168+
else if qualType.isExactlyNothing then
169169
""
170170
else
171171
val add = suggestImports(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ trait ImplicitRunInfo:
715715
seen += t
716716
t.underlying match
717717
case TypeBounds(lo, hi) =>
718-
if defn.isBottomTypeAfterErasure(lo) then apply(hi)
718+
if lo.isBottomTypeAfterErasure then apply(hi)
719719
else AndType.make(apply(lo), apply(hi))
720720
case u => apply(u)
721721

0 commit comments

Comments
 (0)