Skip to content

Commit 5c7c94a

Browse files
committed
@transientParam annotation
1 parent 59e7e96 commit 5c7c94a

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,8 @@ class Definitions {
790790
def TASTYLongSignatureAnnot(implicit ctx: Context): ClassSymbol = TASTYLongSignatureAnnotType.symbol.asClass
791791
lazy val TailrecAnnotType: TypeRef = ctx.requiredClassRef("scala.annotation.tailrec")
792792
def TailrecAnnot(implicit ctx: Context): ClassSymbol = TailrecAnnotType.symbol.asClass
793+
lazy val TransientParamAnnotType: TypeRef = ctx.requiredClassRef("scala.annotation.transientParam")
794+
def TransientParamAnnot(implicit ctx: Context): ClassSymbol = TransientParamAnnotType.symbol.asClass
793795
lazy val SwitchAnnotType: TypeRef = ctx.requiredClassRef("scala.annotation.switch")
794796
def SwitchAnnot(implicit ctx: Context): ClassSymbol = SwitchAnnotType.symbol.asClass
795797
lazy val ThrowsAnnotType: TypeRef = ctx.requiredClassRef("scala.throws")

compiler/src/dotty/tools/dotc/transform/Constructors.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ class Constructors extends MiniPhase with IdentityDenotTransformer { thisPhase =
224224
dropped += acc
225225
Nil
226226
} else {
227+
if (acc.hasAnnotation(defn.TransientParamAnnot))
228+
ctx.error(em"transient parameter $acc is retained as field in class ${acc.owner}", acc.pos)
227229
val target = if (acc.is(Method)) acc.field else acc
228230
if (!target.exists) Nil // this case arises when the parameter accessor is an alias
229231
else {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
9+
package scala.annotation
10+
11+
import scala.annotation.meta._
12+
13+
/** An annotation that goes on parameters of classes or traits. It asserts
14+
* that the parameter is used only for initialization and is not kept in
15+
* the class as a field. Violations of this assertion are flagged as
16+
* compile errors. The annotatoon is particularly useful for implicit
17+
* parameters since for these a textual scan is not sufficient to know
18+
* where they are used.
19+
*/
20+
@param class transientParam extends scala.annotation.StaticAnnotation

tests/neg/transient-param.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import annotation.transientParam
2+
class Context
3+
4+
class Test1()(implicit @transientParam ctx: Context) { // error
5+
def test1 = implicitly[Context]
6+
}
7+
8+
class Test2()(@transientParam ctx: Context) { // error
9+
def test1 = ctx
10+
}
11+
12+
class Test3()(implicit @transientParam ctx: Context) { // OK
13+
val foo = implicitly[Context]
14+
}

0 commit comments

Comments
 (0)