Skip to content

Commit 69505e1

Browse files
author
EnzeXing
committed
Rename BaseValue to SafeValue
1 parent 6794689 commit 69505e1

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Objects(using Context @constructorOnly):
9393
* | OfClass(class, vs[outer], ctor, args, env) // instance of a class
9494
* | OfArray(object[owner], regions)
9595
* | Fun(..., env) // value elements that can be contained in ValueSet
96-
* | BaseValue // Int, String, etc.
96+
* | SafeValue // values on which method calls and fields won't cause warnings. Int, String, etc.
9797
* vs ::= ValueSet(ve) // set of abstract values
9898
* Bottom ::= ValueSet(Empty)
9999
* val ::= ve | UnknownValue | vs | Package // all possible abstract values in domain
@@ -229,8 +229,9 @@ class Objects(using Context @constructorOnly):
229229

230230
/** Represents common base values like Int, String, etc.
231231
*/
232-
case object BaseValue extends ValueElement:
233-
def show(using Context): String = "BaseValue"
232+
case object SafeValue extends ValueElement:
233+
val safeTypes = defn.ScalaNumericValueTypeList ++ List(defn.UnitType, defn.BooleanType, defn.StringType)
234+
def show(using Context): String = "SafeValue"
234235

235236
/**
236237
* Represents a set of values
@@ -661,7 +662,7 @@ class Objects(using Context @constructorOnly):
661662
a match
662663
case UnknownValue => UnknownValue
663664
case Package(_) => a
664-
case BaseValue => BaseValue
665+
case SafeValue => SafeValue
665666
case ref: Ref => if ref.klass.isSubClass(klass) then ref else Bottom
666667
case ValueSet(values) => values.map(v => v.filterClass(klass)).join
667668
case arr: OfArray => if defn.ArrayClass.isSubClass(klass) then arr else Bottom
@@ -703,8 +704,8 @@ class Objects(using Context @constructorOnly):
703704
report.warning("[Internal error] Unexpected call on package = " + value.show + ", meth = " + meth.show + Trace.show, Trace.position)
704705
Bottom
705706

706-
case BaseValue =>
707-
if reportUnknown then UnknownValue else BaseValue
707+
case SafeValue =>
708+
SafeValue // Check return type, if not safe, try to analyze body, 1.until(2).map(i => UninitializedObject)
708709

709710
case Bottom =>
710711
Bottom
@@ -731,7 +732,7 @@ class Objects(using Context @constructorOnly):
731732
Bottom
732733
else
733734
// Array.length is OK
734-
BaseValue
735+
SafeValue
735736

736737
case ref: Ref =>
737738
val isLocal = !meth.owner.isClass
@@ -752,10 +753,10 @@ class Objects(using Context @constructorOnly):
752753
arr
753754
else if target.equals(defn.Predef_classOf) then
754755
// Predef.classOf is a stub method in tasty and is replaced in backend
755-
BaseValue
756+
SafeValue
756757
else if target.equals(defn.ClassTagModule_apply) then
757758
// ClassTag and other reflection related values are considered safe
758-
BaseValue
759+
SafeValue
759760
else if target.hasSource then
760761
val cls = target.owner.enclosingClass.asClass
761762
val ddef = target.defTree.asInstanceOf[DefDef]
@@ -869,8 +870,8 @@ class Objects(using Context @constructorOnly):
869870
else
870871
UnknownValue
871872

872-
case BaseValue =>
873-
if reportUnknown then UnknownValue else BaseValue
873+
case SafeValue =>
874+
SafeValue
874875

875876
case Package(packageSym) =>
876877
if field.isStaticObject then
@@ -954,7 +955,7 @@ class Objects(using Context @constructorOnly):
954955
case arr: OfArray =>
955956
report.warning("[Internal error] unexpected tree in assignment, array = " + arr.show + " field = " + field + Trace.show, Trace.position)
956957

957-
case BaseValue | UnknownValue =>
958+
case SafeValue | UnknownValue =>
958959
report.warning("Assigning to base or unknown value is forbidden. " + Trace.show, Trace.position)
959960

960961
case ValueSet(values) =>
@@ -986,7 +987,7 @@ class Objects(using Context @constructorOnly):
986987
*/
987988
def instantiate(outer: Value, klass: ClassSymbol, ctor: Symbol, args: List[ArgInfo]): Contextual[Value] = log("instantiating " + klass.show + ", outer = " + outer + ", args = " + args.map(_.value.show), printer, (_: Value).show) {
988989
outer.filterClass(klass.owner) match
989-
case _ : Fun | _: OfArray | BaseValue =>
990+
case _ : Fun | _: OfArray | SafeValue =>
990991
report.warning("[Internal error] unexpected outer in instantiating a class, outer = " + outer.show + ", class = " + klass.show + ", " + Trace.show, Trace.position)
991992
Bottom
992993

@@ -1081,7 +1082,7 @@ class Objects(using Context @constructorOnly):
10811082
case UnknownValue =>
10821083
report.warning("Calling on unknown value. " + Trace.show, Trace.position)
10831084
Bottom
1084-
case _: ValueSet | _: Ref | _: OfArray | _: Package | BaseValue =>
1085+
case _: ValueSet | _: Ref | _: OfArray | _: Package | SafeValue =>
10851086
report.warning("[Internal error] Unexpected by-name value " + value.show + ". " + Trace.show, Trace.position)
10861087
Bottom
10871088
else
@@ -1269,7 +1270,7 @@ class Objects(using Context @constructorOnly):
12691270
evalType(expr.tpe, thisV, klass)
12701271

12711272
case Literal(_) =>
1272-
BaseValue
1273+
SafeValue
12731274

12741275
case Typed(expr, tpt) =>
12751276
if tpt.tpe.hasAnnotation(defn.UncheckedAnnot) then
@@ -1555,7 +1556,7 @@ class Objects(using Context @constructorOnly):
15551556

15561557
// call .apply
15571558
val applyDenot = getMemberMethod(scrutineeType, nme.apply, applyType(elemType))
1558-
val applyRes = call(scrutinee, applyDenot.symbol, ArgInfo(BaseValue, summon[Trace], EmptyTree) :: Nil, scrutineeType, superType = NoType, needResolve = true)
1559+
val applyRes = call(scrutinee, applyDenot.symbol, ArgInfo(SafeValue, summon[Trace], EmptyTree) :: Nil, scrutineeType, superType = NoType, needResolve = true)
15591560

15601561
if isWildcardStarArgList(pats) then
15611562
if pats.size == 1 then
@@ -1566,7 +1567,7 @@ class Objects(using Context @constructorOnly):
15661567
else
15671568
// call .drop
15681569
val dropDenot = getMemberMethod(scrutineeType, nme.drop, dropType(elemType))
1569-
val dropRes = call(scrutinee, dropDenot.symbol, ArgInfo(BaseValue, summon[Trace], EmptyTree) :: Nil, scrutineeType, superType = NoType, needResolve = true)
1570+
val dropRes = call(scrutinee, dropDenot.symbol, ArgInfo(SafeValue, summon[Trace], EmptyTree) :: Nil, scrutineeType, superType = NoType, needResolve = true)
15701571
for pat <- pats.init do evalPattern(applyRes, pat)
15711572
evalPattern(dropRes, pats.last)
15721573
end if
@@ -1608,7 +1609,7 @@ class Objects(using Context @constructorOnly):
16081609
def evalType(tp: Type, thisV: ThisValue, klass: ClassSymbol, elideObjectAccess: Boolean = false): Contextual[Value] = log("evaluating " + tp.show, printer, (_: Value).show) {
16091610
tp match
16101611
case _: ConstantType =>
1611-
BaseValue
1612+
SafeValue
16121613

16131614
case tmref: TermRef if tmref.prefix == NoPrefix =>
16141615
val sym = tmref.symbol
@@ -1858,7 +1859,7 @@ class Objects(using Context @constructorOnly):
18581859
resolveThis(target, ref.outerValue(klass), outerCls)
18591860
case ValueSet(values) =>
18601861
values.map(ref => resolveThis(target, ref, klass)).join
1861-
case _: Fun | _ : OfArray | _: Package | BaseValue =>
1862+
case _: Fun | _ : OfArray | _: Package | SafeValue =>
18621863
report.warning("[Internal error] unexpected thisV = " + thisV + ", target = " + target.show + ", klass = " + klass.show + Trace.show, Trace.position)
18631864
Bottom
18641865
}

0 commit comments

Comments
 (0)