Skip to content

Commit 5596727

Browse files
committed
Add @sharable annotation
Add @sharable annotation for classes and vals that are presumed to be safely sharable between threads. Also: Document CtxLazy.
1 parent ea5624b commit 5596727

18 files changed

+59
-43
lines changed

src/dotty/tools/dotc/Bench.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import reporting.Reporter
1010

1111
object Bench extends Driver {
1212

13-
private var numRuns = 1
13+
@sharable private var numRuns = 1
1414

1515
def newCompiler(): Compiler = new Compiler
1616

src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ object desugar {
209209
else tdef
210210
}
211211

212-
private val synthetic = Modifiers(Synthetic)
212+
@sharable private val synthetic = Modifiers(Synthetic)
213213

214214
private def toDefParam(tparam: TypeDef): TypeDef =
215215
tparam.withFlags(Param)

src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package ast
34

45
import core._
@@ -26,7 +27,7 @@ object Trees {
2627
type Untyped = Null
2728

2829
/** The total number of created tree nodes, maintained if Stats.enabled */
29-
var ntrees = 0
30+
@sharable var ntrees = 0
3031

3132
/** Modifiers and annotations for definitions
3233
* @param flags The set flags
@@ -68,7 +69,7 @@ object Trees {
6869
def tokenPos: Seq[(Token, Position)] = ???
6970
}
7071

71-
private var nextId = 0 // for debugging
72+
@sharable private var nextId = 0 // for debugging
7273

7374
type LazyTree = AnyRef /* really: Tree | Lazy[Tree] */
7475
type LazyTreeList = AnyRef /* really: List[Tree] | Lazy[List[Tree]] */
@@ -723,9 +724,9 @@ object Trees {
723724
setMods(Modifiers[T](PrivateLocal))
724725
}
725726

726-
val theEmptyTree: Thicket[Type] = Thicket(Nil)
727-
val theEmptyValDef = new EmptyValDef[Type]
728-
val theEmptyModifiers = new Modifiers()
727+
@sharable val theEmptyTree: Thicket[Type] = Thicket(Nil)
728+
@sharable val theEmptyValDef = new EmptyValDef[Type]
729+
@sharable val theEmptyModifiers = new Modifiers()
729730

730731
def genericEmptyValDef[T >: Untyped]: ValDef[T] = theEmptyValDef.asInstanceOf[ValDef[T]]
731732
def genericEmptyTree[T >: Untyped]: Thicket[T] = theEmptyTree.asInstanceOf[Thicket[T]]
@@ -845,9 +846,9 @@ object Trees {
845846
type Annotated = Trees.Annotated[T]
846847
type Thicket = Trees.Thicket[T]
847848

848-
val EmptyTree: Thicket = genericEmptyTree
849-
val EmptyValDef: ValDef = genericEmptyValDef
850-
val EmptyModifiers: Modifiers = genericEmptyModifiers
849+
@sharable val EmptyTree: Thicket = genericEmptyTree
850+
@sharable val EmptyValDef: ValDef = genericEmptyValDef
851+
@sharable val EmptyModifiers: Modifiers = genericEmptyModifiers
851852

852853
// ----- Auxiliary creation methods ------------------
853854

src/dotty/tools/dotc/config/Properties.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Properties extends PropertiesTrait {
1212

1313
/** Scala manifest attributes.
1414
*/
15-
val ScalaCompilerVersion = new AttributeName("Scala-Compiler-Version")
15+
@sharable val ScalaCompilerVersion = new AttributeName("Scala-Compiler-Version")
1616
}
1717

1818
trait PropertiesTrait {
@@ -23,7 +23,7 @@ trait PropertiesTrait {
2323
protected val propFilename = "/" + propCategory + ".properties"
2424

2525
/** The loaded properties */
26-
protected lazy val scalaProps: java.util.Properties = {
26+
@sharable protected lazy val scalaProps: java.util.Properties = {
2727
val props = new java.util.Properties
2828
val stream = pickJarBasedOn getResourceAsStream propFilename
2929
if (stream ne null)

src/dotty/tools/dotc/config/ScalaVersion.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @author James Iry
22
*/
3-
package dotty.tools.dotc.config
3+
package dotty.tools
4+
package dotc.config
45

56
import scala.util.{Try, Success, Failure}
67

@@ -15,7 +16,7 @@ sealed abstract class ScalaVersion extends Ordered[ScalaVersion] {
1516
/**
1617
* A scala version that sorts higher than all actual versions
1718
*/
18-
case object NoScalaVersion extends ScalaVersion {
19+
@sharable case object NoScalaVersion extends ScalaVersion {
1920
def unparse = "none"
2021

2122
def compare(that: ScalaVersion): Int = that match {

src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import printing._
2828
import config.{Settings, ScalaSettings, Platform, JavaPlatform}
2929
import language.implicitConversions
3030
import DenotTransformers.DenotTransformer
31+
3132
object Contexts {
3233

3334
/** A context is passed basically everywhere in dotc.
@@ -473,7 +474,7 @@ object Contexts {
473474
gadt = new GADTMap(SimpleMap.Empty)
474475
}
475476

476-
object NoContext extends Context {
477+
@sharable object NoContext extends Context {
477478
lazy val base = unsupported("base")
478479
override val implicits: ContextualImplicits = new ContextualImplicits(Nil, null)(this)
479480
}
@@ -620,7 +621,7 @@ object Contexts {
620621
/** implicit conversion that injects all ContextBase members into a context */
621622
implicit def toBase(ctx: Context): ContextBase = ctx.base
622623

623-
val theBase = new ContextBase // !!! DEBUG, so that we can use a minimal context for reporting even in code that normally cannot access a context
624+
// @sharable val theBase = new ContextBase // !!! DEBUG, so that we can use a minimal context for reporting even in code that normally cannot access a context
624625
}
625626

626627
/** Info that changes on each compiler run */

src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package core
34

45
import Periods._, Contexts._, Symbols._, Denotations._, Names._, NameOps._, Annotations._
@@ -1713,8 +1714,8 @@ object SymDenotations {
17131714
validFor = Period.allInRun(NoRunId) // will be brought forward automatically
17141715
}
17151716

1716-
val NoDenotation = new NoDenotation
1717-
val NotDefinedHereDenotation = new NoDenotation
1717+
@sharable val NoDenotation = new NoDenotation
1718+
@sharable val NotDefinedHereDenotation = new NoDenotation
17181719

17191720
// ---- Completion --------------------------------------------------------
17201721

@@ -1757,7 +1758,7 @@ object SymDenotations {
17571758
val NoSymbolFn = (ctx: Context) => NoSymbol
17581759

17591760
/** A missing completer */
1760-
class NoCompleter extends LazyType {
1761+
@sharable class NoCompleter extends LazyType {
17611762
def complete(denot: SymDenotation)(implicit ctx: Context): Unit = unsupported("complete")
17621763
}
17631764

src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package core
34

45
import Contexts._, Types._, Symbols._, Names._, Flags._, Scopes._
@@ -572,5 +573,5 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
572573

573574
object TypeOps {
574575
val emptyDNF = (Nil, Set[Name]()) :: Nil
575-
var track = false // !!!DEBUG
576+
@sharable var track = false // !!!DEBUG
576577
}

src/dotty/tools/dotc/core/Types.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package core
34

45
import util.common._
@@ -35,7 +36,7 @@ import language.implicitConversions
3536

3637
object Types {
3738

38-
private var nextId = 0
39+
@sharable private var nextId = 0
3940

4041
/** The class of types.
4142
* The principal subclasses and sub-objects are as follows:
@@ -73,6 +74,7 @@ object Types {
7374

7475
// ----- Tests -----------------------------------------------------
7576

77+
// debug only: a unique identifier for a type
7678
val uniqId = {
7779
nextId = nextId + 1
7880
// if (nextId == 19555)
@@ -2752,13 +2754,13 @@ object Types {
27522754
case class ImportType(expr: Tree) extends UncachedGroundType
27532755

27542756
/** Sentinel for "missing type" */
2755-
case object NoType extends CachedGroundType {
2757+
@sharable case object NoType extends CachedGroundType {
27562758
override def exists = false
27572759
override def computeHash = hashSeed
27582760
}
27592761

27602762
/** Missing prefix */
2761-
case object NoPrefix extends CachedGroundType {
2763+
@sharable case object NoPrefix extends CachedGroundType {
27622764
override def computeHash = hashSeed
27632765
}
27642766

@@ -2775,7 +2777,7 @@ object Types {
27752777

27762778
final class CachedWildcardType(optBounds: Type) extends WildcardType(optBounds)
27772779

2778-
object WildcardType extends WildcardType(NoType) {
2780+
@sharable object WildcardType extends WildcardType(NoType) {
27792781
def apply(bounds: TypeBounds)(implicit ctx: Context) = unique(new CachedWildcardType(bounds))
27802782
}
27812783

@@ -2983,7 +2985,7 @@ object Types {
29832985
}
29842986
}
29852987

2986-
object IdentityTypeMap extends TypeMap()(NoContext) {
2988+
@sharable object IdentityTypeMap extends TypeMap()(NoContext) {
29872989
override def stopAtStatic = true
29882990
def apply(tp: Type) = tp
29892991
}
@@ -3208,7 +3210,7 @@ object Types {
32083210

32093211
// ----- Debug ---------------------------------------------------------
32103212

3211-
var debugTrace = false
3213+
@sharable var debugTrace = false
32123214

32133215
val watchList = List[String](
32143216
) map (_.toTypeName)

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ object Parsers {
3636
def nonePositive: Boolean = parCounts forall (_ <= 0)
3737
}
3838

39-
object Location extends Enumeration {
39+
@sharable object Location extends Enumeration {
4040
val InParens, InBlock, InPattern, ElseWhere = Value
4141
}
4242

43-
object ParamOwner extends Enumeration {
43+
@sharable object ParamOwner extends Enumeration {
4444
val Class, Type, TypeParam, Def = Value
4545
}
4646

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import typer.Checking
2020
import Names.Name
2121
import NameOps._
2222
import StdNames._
23-
import util.CtxLazy
2423

2524

2625
/** The first tree transform

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package dotty.tools.dotc
2-
package util
2+
package transform
33
import core.Contexts.Context
44

5+
/** Utility class for lazy values whose evaluation depends on a context.
6+
* This should be used whenever the evaluation of a lazy expression
7+
* depends on some context, but the value can be re-used afterwards
8+
* with a different context.
9+
*
10+
* A typical use case is a lazy val in a phase object which exists once per root context where
11+
* the expression intiializing the lazy val depends only on the root context, but not any changes afterwards.
12+
*/
513
class CtxLazy[T](expr: Context => T) {
614
private var myValue: T = _
715
private var forced = false

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package transform
34

45
import dotty.tools.dotc.ast.tpd
@@ -194,7 +195,7 @@ object TreeTransforms {
194195
}
195196
}
196197

197-
val NoTransform = new TreeTransform {
198+
@sharable val NoTransform = new TreeTransform {
198199
def phase = unsupported("phase")
199200
idx = -1
200201
}

src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,4 +677,4 @@ class TermRefSet(implicit ctx: Context) extends mutable.Traversable[TermRef] {
677677
f(TermRef(pre, sym))
678678
}
679679

680-
object EmptyTermRefSet extends TermRefSet()(NoContext)
680+
@sharable object EmptyTermRefSet extends TermRefSet()(NoContext)

src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ trait Inferencing { this: Checking =>
257257
}
258258

259259
/** An enumeration controlling the degree of forcing in "is-dully-defined" checks. */
260-
object ForceDegree extends Enumeration {
260+
@sharable object ForceDegree extends Enumeration {
261261
val none, // don't force type variables
262262
noBottom, // force type variables, fail if forced to Nothing or Null
263263
all = Value // force type variables, don't fail

src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ object ProtoTypes {
151151
* operation is further selection. In this case, the expression need not be a value.
152152
* @see checkValue
153153
*/
154-
object AnySelectionProto extends SelectionProto(nme.WILDCARD, WildcardType, NoViewsAllowed)
154+
@sharable object AnySelectionProto extends SelectionProto(nme.WILDCARD, WildcardType, NoViewsAllowed)
155155

156156
/** A prototype for selections in pattern constructors */
157157
class UnapplySelectionProto(name: Name) extends SelectionProto(name, WildcardType, NoViewsAllowed)
@@ -308,7 +308,7 @@ object ProtoTypes {
308308
*
309309
* [] _
310310
*/
311-
object AnyFunctionProto extends UncachedGroundType with MatchAlways
311+
@sharable object AnyFunctionProto extends UncachedGroundType with MatchAlways
312312

313313
/** Add all parameters in given polytype `pt` to the constraint's domain.
314314
* If the constraint contains already some of these parameters in its domain,

src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) {
124124
override def toString = file.toString
125125
}
126126

127-
object NoSource extends SourceFile("<no source>", Nil) {
127+
@sharable object NoSource extends SourceFile("<no source>", Nil) {
128128
override def exists = false
129129
}
130130

src/dotty/tools/dotc/util/Stats.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package util
34

45
import core.Contexts._
56
import collection.mutable
67

7-
object Stats {
8+
@sharable object Stats {
89

910
final val enabled = true
1011

0 commit comments

Comments
 (0)