@@ -22,10 +22,22 @@ import NameOps._
22
22
import StdNames ._
23
23
24
24
25
- /** The first tree transform
26
- * - ensures there are companion objects for all classes except module classes
27
- * - eliminates some kinds of trees: Imports, NamedArgs
28
- * - stubs out native methods
25
+ /** A no-op transform that checks whether the compiled sources are re-entrant.
26
+ * It makes sure that there are no variables that are accessible from a global
27
+ * object. It excludes from checking paths that are labeled with one of the annotations
28
+ *
29
+ * @sharable Indicating a class or val can be safely shared
30
+ * @unshared Indicating an object will not be accessed from multiple threads
31
+ *
32
+ * Currently the analysis is only intended to check the dotty compiler itself. To make
33
+ * it generally useful we'd need to add at least the following:
34
+ *
35
+ * - Handle polymorphic instantiation: We might instantiate a generic class
36
+ * with a type that contains vars. If the class contains fields of the generic
37
+ * type, this may constitute a path to a shared var, which currently goes undetected.
38
+ * - Handle arrays: Array elements are currently ignored because they are often used
39
+ * in an immutable way anyway. To do better, it would be helpful to have a type
40
+ * for immutable array.
29
41
*/
30
42
class CheckReentrant extends MiniPhaseTransform { thisTransformer =>
31
43
import ast .tpd ._
@@ -50,7 +62,7 @@ class CheckReentrant extends MiniPhaseTransform { thisTransformer =>
50
62
sym.hasAnnotation(unsharedAnnot())
51
63
52
64
def scanning (sym : Symbol )(op : => Unit )(implicit ctx : Context ): Unit = {
53
- println (i " ${" " * indent}scanning $sym" )
65
+ ctx.log (i " ${" " * indent}scanning $sym" )
54
66
indent += 1
55
67
try op
56
68
finally indent -= 1
@@ -63,7 +75,9 @@ class CheckReentrant extends MiniPhaseTransform { thisTransformer =>
63
75
for (sym <- cls.classInfo.decls)
64
76
if (sym.isTerm && ! sym.isSetter && ! isIgnored(sym))
65
77
if (sym.is(Mutable )) {
66
- println(i " GLOBAL ${sym.showLocated}: ${sym.info} <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< " )
78
+ ctx.error(
79
+ i """ possible data race involving globally reachable ${sym.showLocated}: ${sym.info}
80
+ | use -Ylog:checkReentrant+ to find out more about why the variable is reachable. """ .stripMargin)
67
81
shared += sym
68
82
} else if (! sym.is(Method ) || sym.is(Accessor | ParamAccessor )) {
69
83
scanning(sym) {
0 commit comments