@@ -910,7 +910,7 @@ class Objects(using Context @constructorOnly):
910
910
911
911
/**
912
912
* Handle new expression `new p.C(args)`.
913
- * The actual instance might be cached without running the constructor.
913
+ * The actual instance might be cached without running the constructor.
914
914
* See tests/init-global/pos/cache-constructor.scala
915
915
*
916
916
* @param outer The value for `p`.
@@ -1574,22 +1574,30 @@ class Objects(using Context @constructorOnly):
1574
1574
* The default widening is 1 for most values, 2 for function values.
1575
1575
* User-specified widening annotations are repected.
1576
1576
*/
1577
- def widenEscapedValue (value : Value , expr : Tree ): Contextual [Value ] =
1578
- expr.tpe.getAnnotation(defn.InitWidenAnnot ) match
1579
- case Some (annot) =>
1580
- annot.argument(0 ).get match
1581
- case arg @ Literal (c : Constants .Constant ) =>
1582
- val height = c.intValue
1583
- if height < 0 then
1584
- report.warning(" The argument should be positive" , arg)
1585
- value.widen(1 )
1586
- else
1587
- value.widen(c.intValue)
1588
- case arg =>
1589
- report.warning(" The argument should be a constant integer value" , arg)
1590
- value.widen(1 )
1591
- case _ =>
1592
- if value.isInstanceOf [Fun ] then value.widen(2 ) else value.widen(1 )
1577
+ def widenEscapedValue (value : Value , annotatedTree : Tree ): Contextual [Value ] =
1578
+ def parseAnnotation : Option [Int ] =
1579
+ annotatedTree.tpe.getAnnotation(defn.InitWidenAnnot ).flatMap: annot =>
1580
+ annot.argument(0 ).get match
1581
+ case arg @ Literal (c : Constants .Constant ) =>
1582
+ val height = c.intValue
1583
+ if height < 0 then
1584
+ report.warning(" The argument should be positive" , arg)
1585
+ None
1586
+ else
1587
+ Some (height)
1588
+ case arg =>
1589
+ report.warning(" The argument should be a constant integer value" , arg)
1590
+ None
1591
+ end parseAnnotation
1592
+
1593
+ parseAnnotation match
1594
+ case Some (i) =>
1595
+ value.widen(i)
1596
+
1597
+ case None =>
1598
+ if value.isInstanceOf [Fun ]
1599
+ then value.widen(2 )
1600
+ else value.widen(1 )
1593
1601
1594
1602
/** Evaluate arguments of methods and constructors */
1595
1603
def evalArgs (args : List [Arg ], thisV : ThisValue , klass : ClassSymbol ): Contextual [List [ArgInfo ]] =
0 commit comments