Skip to content

Commit

Permalink
Address review: Refactor code for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Sep 2, 2024
1 parent 533ee8a commit 76498dc
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ class Objects(using Context @constructorOnly):

/**
* Handle new expression `new p.C(args)`.
* The actual instance might be cached without running the constructor.
* The actual instance might be cached without running the constructor.
* See tests/init-global/pos/cache-constructor.scala
*
* @param outer The value for `p`.
Expand Down Expand Up @@ -1574,22 +1574,30 @@ class Objects(using Context @constructorOnly):
* The default widening is 1 for most values, 2 for function values.
* User-specified widening annotations are repected.
*/
def widenEscapedValue(value: Value, expr: Tree): Contextual[Value] =
expr.tpe.getAnnotation(defn.InitWidenAnnot) match
case Some(annot) =>
annot.argument(0).get match
case arg @ Literal(c: Constants.Constant) =>
val height = c.intValue
if height < 0 then
report.warning("The argument should be positive", arg)
value.widen(1)
else
value.widen(c.intValue)
case arg =>
report.warning("The argument should be a constant integer value", arg)
value.widen(1)
case _ =>
if value.isInstanceOf[Fun] then value.widen(2) else value.widen(1)
def widenEscapedValue(value: Value, annotatedTree: Tree): Contextual[Value] =
def parseAnnotation: Option[Int] =
annotatedTree.tpe.getAnnotation(defn.InitWidenAnnot).flatMap: annot =>
annot.argument(0).get match
case arg @ Literal(c: Constants.Constant) =>
val height = c.intValue
if height < 0 then
report.warning("The argument should be positive", arg)
None
else
Some(height)
case arg =>
report.warning("The argument should be a constant integer value", arg)
None
end parseAnnotation

parseAnnotation match
case Some(i) =>
value.widen(i)

case None =>
if value.isInstanceOf[Fun]
then value.widen(2)
else value.widen(1)

/** Evaluate arguments of methods and constructors */
def evalArgs(args: List[Arg], thisV: ThisValue, klass: ClassSymbol): Contextual[List[ArgInfo]] =
Expand Down

0 comments on commit 76498dc

Please sign in to comment.