Skip to content

Commit 1e322e4

Browse files
committed
Make sharing violations errors
and use ctx.log for other output.
1 parent 8cf7f0e commit 1e322e4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/dotty/tools/dotc/transform/CheckReentrant.scala

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,22 @@ import NameOps._
2222
import StdNames._
2323

2424

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.
2941
*/
3042
class CheckReentrant extends MiniPhaseTransform { thisTransformer =>
3143
import ast.tpd._
@@ -50,7 +62,7 @@ class CheckReentrant extends MiniPhaseTransform { thisTransformer =>
5062
sym.hasAnnotation(unsharedAnnot())
5163

5264
def scanning(sym: Symbol)(op: => Unit)(implicit ctx: Context): Unit = {
53-
println(i"${" " * indent}scanning $sym")
65+
ctx.log(i"${" " * indent}scanning $sym")
5466
indent += 1
5567
try op
5668
finally indent -= 1
@@ -63,7 +75,9 @@ class CheckReentrant extends MiniPhaseTransform { thisTransformer =>
6375
for (sym <- cls.classInfo.decls)
6476
if (sym.isTerm && !sym.isSetter && !isIgnored(sym))
6577
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)
6781
shared += sym
6882
} else if (!sym.is(Method) || sym.is(Accessor | ParamAccessor)) {
6983
scanning(sym) {

0 commit comments

Comments
 (0)