@@ -7,6 +7,7 @@ import ast.tpd._
7
7
import core ._
8
8
import Decorators ._
9
9
import Types ._ , Symbols ._ , Contexts ._
10
+ import util .NoSourcePosition
10
11
11
12
import Effects ._ , Potentials ._
12
13
@@ -18,13 +19,16 @@ object Errors {
18
19
errs.map(_.show).mkString(" , " )
19
20
20
21
sealed trait Error {
22
+ def source : Tree
21
23
def trace : Vector [Tree ]
22
- def report (implicit ctx : Context ): Unit
23
24
def show (implicit ctx : Context ): String
24
25
26
+ def report (implicit ctx : Context ): Unit =
27
+ ctx.warning(show + " Calling trace:\n " + stacktrace, source.sourcePos)
28
+
25
29
def toErrors : Errors = Set (this )
26
30
27
- def stacktrace (implicit ctx : Context ): String = {
31
+ def stacktrace (implicit ctx : Context ): String = if (trace.isEmpty) " " else " Calling trace: \n " + {
28
32
var indentCount = 0
29
33
var last = " "
30
34
val sb = new StringBuilder
@@ -43,68 +47,68 @@ object Errors {
43
47
}
44
48
sb.toString
45
49
}
50
+
51
+ /** Flatten UnsafePromotion errors
52
+ */
53
+ def flatten : Errors = this match {
54
+ case unsafe : UnsafePromotion => unsafe.errors.flatMap(_.flatten)
55
+ case _ => Set (this )
56
+ }
46
57
}
47
58
48
59
/** Access non-initialized field */
49
60
case class AccessNonInit (field : Symbol , trace : Vector [Tree ]) extends Error {
61
+ def source : Tree = trace.last
50
62
def show (implicit ctx : Context ): String =
51
- " Access non-initialized field " + field.show + " . Calling trace: \n " + stacktrace
63
+ " Access non-initialized field " + field.show + " ."
52
64
53
- def report (implicit ctx : Context ): Unit = ???
65
+ override def report (implicit ctx : Context ): Unit = ctx.error(show + stacktrace, field.sourcePos)
54
66
}
55
67
56
68
/** Promote `this` under initialization to fully-initialized */
57
69
case class PromoteThis (pot : ThisRef , source : Tree , trace : Vector [Tree ]) extends Error {
58
- def show (implicit ctx : Context ): String = " Promote `this` to be initialized while it is not. Calling trace:\n " + stacktrace
59
- def report (implicit ctx : Context ): Unit = ???
70
+ def show (implicit ctx : Context ): String = " Promote `this` to be initialized while it is not."
60
71
}
61
72
62
73
/** Promote `this` under initialization to fully-initialized */
63
74
case class PromoteWarm (pot : Warm , source : Tree , trace : Vector [Tree ]) extends Error {
64
75
def show (implicit ctx : Context ): String =
65
- " Promoting the value under initialization to be initialized: " + source.show +
66
- " . Calling trace:\n " + stacktrace
67
-
68
- def report (implicit ctx : Context ): Unit = ???
76
+ " Promoting the value under initialization to be initialized: " + source.show + " ."
69
77
}
70
78
71
79
/** Promote a cold value under initialization to fully-initialized */
72
80
case class PromoteCold (source : Tree , trace : Vector [Tree ]) extends Error {
73
81
def show (implicit ctx : Context ): String =
74
- " Promoting the value " + source.show + " to be initialized while it is under initialization" +
75
- " . Calling trace:\n " + stacktrace
76
-
77
- def report (implicit ctx : Context ): Unit = ???
82
+ " Promoting the value " + source.show + " to be initialized while it is under initialization" + " ."
78
83
}
79
84
80
85
case class AccessCold (field : Symbol , source : Tree , trace : Vector [Tree ]) extends Error {
81
86
def show (implicit ctx : Context ): String =
82
- " Access field " + source.show + " on a value under unknown initialization status" +
83
- " . Calling trace:\n " + stacktrace
84
-
85
- def report (implicit ctx : Context ): Unit = ???
87
+ " Access field " + source.show + " on a value under unknown initialization status" + " ."
86
88
}
87
89
88
90
case class CallCold (meth : Symbol , source : Tree , trace : Vector [Tree ]) extends Error {
89
91
def show (implicit ctx : Context ): String =
90
- " Call method " + source.show + " on a value under unknown initialization" +
91
- " . Calling trace:\n " + stacktrace
92
-
93
- def report (implicit ctx : Context ): Unit = ???
92
+ " Call method " + source.show + " on a value under unknown initialization" + " ."
94
93
}
95
94
96
95
case class CallUnknown (meth : Symbol , source : Tree , trace : Vector [Tree ]) extends Error {
97
96
def show (implicit ctx : Context ): String =
98
- " Calling the external method " + meth.show +
99
- " may cause initialization errors" + " . Calling trace:\n " + stacktrace
100
-
101
- def report (implicit ctx : Context ): Unit = ???
97
+ " Calling the external method " + meth.show + " may cause initialization errors" + " ."
102
98
}
103
99
104
100
/** Promote a value under initialization to fully-initialized */
105
- case class UnsafePromotion (pot : Potential , source : Tree , trace : Vector [Tree ], errors : Set [Error ]) extends Error {
106
- def show (implicit ctx : Context ): String = ???
107
-
108
- def report (implicit ctx : Context ): Unit = ???
101
+ case class UnsafePromotion (pot : Potential , source : Tree , trace : Vector [Tree ], errors : Errors ) extends Error {
102
+ assert(errors.nonEmpty)
103
+
104
+ def show (implicit ctx : Context ): String = {
105
+ var index = 0
106
+ " Promoting the value " + source.show + " to initialized is unsafe.\n " + stacktrace +
107
+ " \n The unsafe promotion is caused by the following problem(s):" +
108
+ errors.map { error =>
109
+ index += 1
110
+ s " \n $index" + error.show + error.stacktrace
111
+ }.mkString
112
+ }
109
113
}
110
114
}
0 commit comments