@@ -6,19 +6,27 @@ package init
66import ast .tpd ._
77import core ._
88import util .SourcePosition
9+ import util .Property
910import Decorators ._ , printing .SyntaxHighlighting
1011import Types ._ , Symbols ._ , Contexts ._
1112
1213import scala .collection .mutable
1314
1415object Errors :
16+ private val IsFromPromotion = new Property .Key [Boolean ]
17+
1518 sealed trait Error :
1619 def trace : Seq [Tree ]
1720 def show (using Context ): String
1821
1922 def pos (using Context ): SourcePosition = trace.last.sourcePos
2023
21- def stacktrace (preamble : String = " Calling trace:\n " )(using Context ): String = buildStacktrace(trace, preamble)
24+ def stacktrace (using Context ): String =
25+ val preamble : String =
26+ if ctx.property(IsFromPromotion ).nonEmpty
27+ then " Promotion trace:\n "
28+ else " Calling trace:\n "
29+ buildStacktrace(trace, preamble)
2230
2331 def issue (using Context ): Unit =
2432 report.warning(show, this .pos)
@@ -74,41 +82,43 @@ object Errors:
7482 case class AccessNonInit (field : Symbol , trace : Seq [Tree ]) extends Error :
7583 def source : Tree = trace.last
7684 def show (using Context ): String =
77- " Access non-initialized " + field.show + " ." + stacktrace()
85+ " Access non-initialized " + field.show + " ." + stacktrace
7886
7987 override def pos (using Context ): SourcePosition = field.sourcePos
8088
8189 /** Promote a value under initialization to fully-initialized */
8290 case class PromoteError (msg : String , trace : Seq [Tree ]) extends Error :
83- def show (using Context ): String = msg + stacktrace()
91+ def show (using Context ): String = msg + stacktrace
8492
8593 case class AccessCold (field : Symbol , trace : Seq [Tree ]) extends Error :
8694 def show (using Context ): String =
87- " Access field " + field.show + " on a cold object." + stacktrace()
95+ " Access field " + field.show + " on a cold object." + stacktrace
8896
8997 case class CallCold (meth : Symbol , trace : Seq [Tree ]) extends Error :
9098 def show (using Context ): String =
91- " Call method " + meth.show + " on a cold object." + stacktrace()
99+ " Call method " + meth.show + " on a cold object." + stacktrace
92100
93101 case class CallUnknown (meth : Symbol , trace : Seq [Tree ]) extends Error :
94102 def show (using Context ): String =
95103 val prefix = if meth.is(Flags .Method ) then " Calling the external method " else " Accessing the external field"
96- prefix + meth.show + " may cause initialization errors." + stacktrace()
104+ prefix + meth.show + " may cause initialization errors." + stacktrace
97105
98106 /** Promote a value under initialization to fully-initialized */
99107 case class UnsafePromotion (msg : String , trace : Seq [Tree ], error : Error ) extends Error :
100108 def show (using Context ): String =
101- msg + stacktrace() + " \n " +
102- " Promoting the value to hot failed due to the following problem:\n " +
103- error.show
109+ msg + stacktrace + " \n " +
110+ " Promoting the value to hot failed due to the following problem:\n " + {
111+ val ctx2 = ctx.withProperty(IsFromPromotion , Some (true ))
112+ error.show(using ctx2)
113+ }
104114
105115 /** Unsafe leaking a non-hot value as constructor arguments
106116 *
107117 * Invariant: argsIndices.nonEmpty
108118 */
109119 case class UnsafeLeaking (trace : Seq [Tree ], error : Error , nonHotOuterClass : Symbol , argsIndices : List [Int ]) extends Error :
110120 def show (using Context ): String =
111- " Problematic object instantiation: " + argumentInfo() + stacktrace() + " \n " +
121+ " Problematic object instantiation: " + argumentInfo() + stacktrace + " \n " +
112122 " It leads to the following error during object initialization:\n " +
113123 error.show
114124
0 commit comments