Skip to content

Commit ca6b5e1

Browse files
committed
Remove transparent parameters and use Constant types instead
1 parent 68cf530 commit ca6b5e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+101
-113
lines changed

bench/tests/power-macro/PowerMacro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted.Expr
22

33
object PowerMacro {
44

5-
transparent def power(transparent n: Long, x: Double) = ~powerCode(n, '(x))
5+
transparent def power(n: Long & Constant, x: Double) = ~powerCode(n, '(x))
66

77
def powerCode(n: Long, x: Expr[Double]): Expr[Double] =
88
if (n == 0) '(1.0)

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ class Definitions {
415415
enterCompleteClassSymbol(
416416
ScalaPackageClass, tpnme.Constant, PureInterfaceCreationFlags | Final,
417417
List(SingletonType), EmptyScope)
418-
lazy val ConstantType: TypeRef = ConstantClass.typeRef
419418

420419
lazy val SeqType: TypeRef =
421420
if (isNewCollections) ctx.requiredClassRef("scala.collection.immutable.Seq")
@@ -741,8 +740,6 @@ class Definitions {
741740
def ImplicitNotFoundAnnot(implicit ctx: Context) = ImplicitNotFoundAnnotType.symbol.asClass
742741
lazy val ForceInlineAnnotType = ctx.requiredClassRef("scala.forceInline")
743742
def ForceInlineAnnot(implicit ctx: Context) = ForceInlineAnnotType.symbol.asClass
744-
lazy val TransparentParamAnnotType = ctx.requiredClassRef("scala.annotation.internal.TransparentParam")
745-
def TransparentParamAnnot(implicit ctx: Context) = TransparentParamAnnotType.symbol.asClass
746743
lazy val InvariantBetweenAnnotType = ctx.requiredClassRef("scala.annotation.internal.InvariantBetween")
747744
def InvariantBetweenAnnot(implicit ctx: Context) = InvariantBetweenAnnotType.symbol.asClass
748745
lazy val MigrationAnnotType = ctx.requiredClassRef("scala.annotation.migration")

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,6 @@ object Flags {
563563
/** A transparent implicit method */
564564
final val TransparentImplicitMethod = allOf(Transparent, Implicit, Method)
565565

566-
/** A transparent parameter */
567-
final val TransparentParam = allOf(Transparent, Param)
568-
569566
/** An enum case */
570567
final val EnumCase = allOf(Enum, Case)
571568

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,15 +3016,8 @@ object Types {
30163016
* - add @inlineParam to transparent call-by-value parameters
30173017
*/
30183018
def fromSymbols(params: List[Symbol], resultType: Type)(implicit ctx: Context) = {
3019-
def translateTransparent(tp: Type): Type = tp match {
3020-
case _: ExprType => tp
3021-
case _ => AnnotatedType(tp, Annotation(defn.TransparentParamAnnot))
3022-
}
3023-
def paramInfo(param: Symbol) = {
3024-
val paramType = param.info.annotatedToRepeated
3025-
if (param.is(Transparent)) translateTransparent(paramType) else paramType
3026-
}
3027-
3019+
def paramInfo(param: Symbol) =
3020+
param.info.annotatedToRepeated
30283021
apply(params.map(_.name.asTermName))(
30293022
tl => params.map(p => tl.integrate(params, paramInfo(p))),
30303023
tl => tl.integrate(params, resultType))

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,12 +1913,12 @@ object Parsers {
19131913
/** ClsParamClauses ::= {ClsParamClause} [[nl] `(' [FunArgMods] ClsParams `)']
19141914
* ClsParamClause ::= [nl] `(' [`erased'] [ClsParams] ')'
19151915
* ClsParams ::= ClsParam {`' ClsParam}
1916-
* ClsParam ::= {Annotation} [{Modifier} (`val' | `var') | `inline'] Param
1916+
* ClsParam ::= {Annotation} [{Modifier} (`val' | `var')] Param
19171917
* DefParamClauses ::= {DefParamClause} [[nl] `(' [FunArgMods] DefParams `)']
19181918
* DefParamClause ::= [nl] `(' [`erased'] [DefParams] ')'
19191919
* DefParams ::= DefParam {`,' DefParam}
1920-
* DefParam ::= {Annotation} [`inline'] Param
1921-
* Param ::= id `:' ParamType [`=' Expr]
1920+
* DefParam ::= {Annotation} Param
1921+
* Param ::= id :' ParamType [`=' Expr]
19221922
*/
19231923
def paramClauses(owner: Name, ofCaseClass: Boolean = false): List[List[ValDef]] = {
19241924
var imods: Modifiers = EmptyModifiers
@@ -1940,15 +1940,14 @@ object Parsers {
19401940
addMod(mods, mod)
19411941
}
19421942
else {
1943-
if (!(mods.flags &~ (ParamAccessor | Transparent)).isEmpty)
1943+
if (!(mods.flags &~ ParamAccessor).isEmpty)
19441944
syntaxError("`val' or `var' expected")
19451945
if (firstClauseOfCaseClass) mods
19461946
else mods | PrivateLocal
19471947
}
19481948
}
19491949
}
19501950
else {
1951-
if (in.token == TRANSPARENT) mods = addModifier(mods)
19521951
mods = atPos(start) { mods | Param }
19531952
}
19541953
atPos(start, nameStart) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
254254
super.transform(_).asInstanceOf[Template]))
255255
}
256256
case tree: ValDef =>
257+
if (tree.symbol.is(Param) && !tree.symbol.owner.is(Transparent) && tree.tpt.tpe.derivesFrom(defn.ConstantClass))
258+
ctx.error("only parameters of transparent method can have Constant type", tree.pos)
257259
val tree1 = cpy.ValDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
258260
transformMemberDef(tree1)
259261
super.transform(tree1)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
504504
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
505505
val captured2 = capturer(captured)
506506

507-
outer.enteredSyms.foreach(sym => if (!sym.is(Transparent)) capturers.put(sym, captured2))
507+
outer.enteredSyms.foreach(sym => if (!sym.info.derivesFrom(defn.ConstantClass)) capturers.put(sym, captured2))
508508

509509
val tree2 = transform(tree)
510510
capturers --= outer.enteredSyms
@@ -550,7 +550,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
550550
splice(ref(splicedType).select(tpnme.UNARY_~).withPos(tree.pos))
551551
case tree: Select if tree.symbol.isSplice =>
552552
splice(tree)
553-
case tree: RefTree if tree.symbol.is(Transparent) && tree.symbol.is(Param) =>
553+
case tree: RefTree if tree.symbol.is(Param) && tree.tpe.derivesFrom(defn.ConstantClass) =>
554554
tree
555555
case tree: RefTree if isCaptured(tree.symbol, level) =>
556556
val t = capturers(tree.symbol).apply(tree)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ object Splicer {
240240

241241
def unexpectedTree(tree: tpd.Tree)(implicit env: Env): Boolean = {
242242
// Assuming that top-level splices can only be in transparent methods
243-
// and splices are expanded at inline site, references to transparent values
243+
// and splices are expanded at inline site, references to parameter wit constant type
244244
// will be know literal constant trees.
245-
tree.symbol.is(Transparent)
245+
tree.symbol.is(Param) && tree.tpe.derivesFrom(defn.ConstantClass)
246246
}
247247
}
248248

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,9 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
221221
bindingsBuf: mutable.ListBuffer[MemberDef]): MemberDef = {
222222
val argtpe = arg.tpe.dealiasKeepAnnots
223223
val isByName = paramtp.dealias.isInstanceOf[ExprType]
224-
val inlineFlag = if (paramtp.hasAnnotation(defn.TransparentParamAnnot)) Transparent else EmptyFlags
225224
val (bindingFlags, bindingType) =
226225
if (isByName) (Method, ExprType(argtpe.widen))
227-
else (inlineFlag, argtpe.widen)
226+
else (EmptyFlags, argtpe.widen)
228227
val boundSym = newSym(name, bindingFlags, bindingType).asTerm
229228
val binding =
230229
if (isByName) DefDef(boundSym, arg.changeOwner(ctx.owner, boundSym))

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,8 +2385,6 @@ class Typer extends Namer
23852385
readaptSimplified(Inliner.inlineCall(tree, pt))
23862386
}
23872387
else if (tree.tpe <:< pt) {
2388-
if (pt.hasAnnotation(defn.TransparentParamAnnot))
2389-
checkTransparentConformant(tree, isFinal = false, "argument to transparent parameter")
23902388
if (ctx.typeComparer.GADTused && pt.isValueType)
23912389
// Insert an explicit cast, so that -Ycheck in later phases succeeds.
23922390
// I suspect, but am not 100% sure that this might affect inferred types,

library/src/scala/annotation/internal/TransparentParam.scala

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/neg/i4433.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
object Foo {
3-
transparent def g(transparent p: Int => Boolean): Boolean = ~{ // error
3+
transparent def g(p: (Int => Boolean) & Constant): Boolean = ~{ // error
44
if(p(5)) '(true)
55
else '(false)
66
}

tests/neg/inlinevals-2.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
object Test {
2+
3+
def power0(x: Double, n: Int & Constant): Double = ??? // error
4+
5+
transparent def power(x: Double, n: Int & Constant): Double = ??? // ok
6+
7+
transparent val N = 10
8+
def X = 20
9+
10+
class C(x: Int & Constant, private val y: Int & Constant) // error // error
11+
12+
transparent def foo(x: Int) = {
13+
14+
def f(xs: List[Int] & Constant) = xs // error
15+
16+
transparent val y = { println("hi"); 1 } // ok
17+
transparent val z = x // ok
18+
19+
}
20+
}

tests/neg/inlinevals.scala

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
object Test {
22

3-
def power0(x: Double, transparent n: Int): Double = ??? // error
4-
5-
transparent def power(x: Double, transparent n: Int): Double = ??? // ok
3+
transparent def power(x: Double, n: Int & Constant): Double = ??? // ok
64

75
transparent val N = 10
86
def X = 20
97

108
transparent transparent val twice = 30 // error: repeated modifier
119

12-
class C(transparent x: Int, private transparent val y: Int) { // error // error
10+
class C {
1311
transparent val foo: Int // error: abstract member may not be inline
1412
transparent def bar: Int // error: abstract member may not be inline
1513
}
1614

17-
power(2.0, N) // ok, since it's a by-name parameter
15+
power(2.0, N) // ok, since parameter is inlined
1816
power(2.0, X) // error: argument to transparent parameter must be a constant expression
1917

2018
transparent val M = X // error: rhs must be constant expression
2119

2220
transparent val xs = List(1, 2, 3) // error: must be a constant expression
2321

24-
transparent def foo(x: Int) = {
25-
26-
def f(transparent xs: List[Int]) = xs // error
27-
28-
transparent val y = { println("hi"); 1 } // ok
29-
transparent val z = x // ok
30-
31-
}
32-
33-
transparent def byname(transparent f: => String): Int = ??? // ok
22+
transparent def byname(f: => String): Int = ??? // ok
3423

3524
}

tests/neg/quote-error-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import quoted._
22

33
object Macro_1 {
4-
transparent def foo(transparent b: Boolean): Unit = ~fooImpl(b)
4+
transparent def foo(b: Boolean & Constant): Unit = ~fooImpl(b)
55
def fooImpl(b: Boolean): Expr[Unit] =
66
'(println(~msg(b)))
77

tests/neg/quote-error/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import quoted._
22

33
object Macro_1 {
4-
transparent def foo(transparent b: Boolean): Unit = ~fooImpl(b)
4+
transparent def foo(b: Boolean & Constant): Unit = ~fooImpl(b)
55
def fooImpl(b: Boolean): Expr[Unit] =
66
if (b) '(println("foo(true)"))
77
else QuoteError("foo cannot be called with false")

tests/neg/quote-exception/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import quoted._
22

33
object Macro_1 {
4-
transparent def foo(transparent b: Boolean): Unit = ~fooImpl(b)
4+
transparent def foo(b: Boolean & Constant): Unit = ~fooImpl(b)
55
def fooImpl(b: Boolean): Expr[Unit] =
66
if (b) '(println("foo(true)"))
77
else ???
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted._
22

33
object Macros {
4-
transparent def foo(transparent i: Int, dummy: Int, j: Int): Int = ~bar(i + 1, '(j)) // error: i + 1 is not a parameter or field reference
4+
transparent def foo(i: Int & Constant, dummy: Int, j: Int): Int = ~bar(i + 1, '(j)) // error: i + 1 is not a parameter or field reference
55
def bar(x: Int, y: Expr[Int]): Expr[Int] = '{ ~x.toExpr + ~y }
66
}

tests/neg/quote-splice-interpret-1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import scala.quoted._
33

44
object Macros {
5-
transparent def isZero(transparent n: Int): Boolean = ~{ // error
5+
transparent def isZero(n: Int & Constant): Boolean = ~{ // error
66
if (n == 0) '(true)
77
else '(false)
88
}

tests/pos/constant-types.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Constants {
22

3-
def f[C <: Constant](c: C): Unit = ()
3+
transparent def f[C <: Constant](c: C): Unit = ()
44
f(true)
55
f(1)
66
f(2L)
@@ -9,19 +9,19 @@ object Constants {
99
f('a')
1010
f("abc")
1111

12-
def fBool(c: Boolean & Constant): Unit = ()
12+
transparent def fBool(c: Boolean & Constant): Unit = ()
1313
fBool(true)
1414
fBool(false)
1515
transparent def bb: Boolean = true
1616
fBool(bb)
1717

18-
def fInt(c: Int & Constant): Unit = ()
18+
transparent def fInt(c: Int & Constant): Unit = ()
1919
fInt(1)
2020
fInt(2)
2121
transparent def ii: Int = 4
2222
fInt(ii)
2323

24-
def fChar(c: Char & Constant): Unit = ()
24+
transparent def fChar(c: Char & Constant): Unit = ()
2525
fChar('a')
2626
fChar('b')
2727
transparent def cc: Char = 'd'

tests/pos/i1570.decompiled

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Decompiled from out/posTestFromTasty/pos/i1570/Test.class */
22
object Test {
3-
transparent def foo(n: scala.Int): scala.Int = Test.bar(n)
4-
transparent def bar(n: scala.Int): scala.Int = n
3+
transparent def foo(n: scala.Int & scala.Constant): scala.Int & scala.Constant = Test.bar(n)
4+
transparent def bar(n: scala.Int & scala.Constant): scala.Int & scala.Constant = n
55
}

tests/pos/i1570.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
object Test {
2-
transparent def foo(transparent n: Int) = bar(n)
3-
transparent def bar(transparent n: Int) = n
2+
transparent def foo(n: Int & Constant) = bar(n)
3+
transparent def bar(n: Int & Constant) = n
44
}

tests/pos/i3898b/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scala.quoted._
22
object Macro {
3-
transparent def ff(x: Int, transparent y: Int): String = ~impl('(x))
3+
transparent def ff(x: Int, y: Int & Constant): String = ~impl('(x))
44
def impl(x: Expr[Int]): Expr[String] = '("")
55
}

tests/pos/i3898c/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scala.quoted._
22
object Macro {
3-
transparent def ff(x: Int, transparent y: Int): String = ~impl('(x))
3+
transparent def ff(x: Int, y: Int & Constant): String = ~impl('(x))
44
def impl(x: Expr[Int]): Expr[String] = '("")
55
}

tests/pos/i4846.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted._
22

33
object Test {
4-
transparent def foo(transparent x: Int): Int = ~fooImpl(x, '(x), '( '(x) ), '( '( '(x) ) ))
4+
transparent def foo(x: Int & Constant): Int = ~fooImpl(x, '(x), '( '(x) ), '( '( '(x) ) ))
55
def fooImpl(a: Int, b: Expr[Int], c: Expr[Expr[Int]], d: Expr[Expr[Expr[Int]]]): Expr[Int] = ???
66
}

tests/pos/power-macro/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted.Expr
33

44
object PowerMacro {
55

6-
transparent def power(transparent n: Long, x: Double) = ~powerCode(n, '(x))
6+
transparent def power(n: Long & Constant, x: Double) = ~powerCode(n, '(x))
77

88
def powerCode(n: Long, x: Expr[Double]): Expr[Double] =
99
if (n == 0) '(1.0)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object PowerInlined1 {
22
import PowerMacro._
33

4-
power(1, 5.0) // 1 quotes to unpickle
4+
power(1L, 5.0) // 1 quotes to unpickle
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import scala.quoted.Expr
22
import quoted.Liftable.{IntIsLiftable => _}
33
object Macro {
4-
transparent def foo(transparent n: Int): Int = ~{
4+
transparent def foo(n: Int & Constant): Int = ~{
55
'(n)
66
}
77
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import scala.quoted.Expr
22
object Macro {
33
import quoted.Liftable.{IntIsLiftable => _}
4-
transparent def foo(transparent n: Int): Int = ~{
4+
transparent def foo(n: Int & Constant): Int = ~{
55
'(n)
66
}
7-
}
7+
}

tests/pos/quote-nested-object/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ object Macro {
66

77
object Implementation {
88

9-
transparent def plus(transparent n: Int, m: Int): Int = ~plus(n, '(m))
9+
transparent def plus(n: Int & Constant, m: Int): Int = ~plus(n, '(m))
1010

1111
def plus(n: Int, m: Expr[Int]): Expr[Int] =
1212
if (n == 0) m
1313
else '{ ~n.toExpr + ~m }
1414

1515
object Implementation2 {
1616

17-
transparent def plus(transparent n: Int, m: Int): Int = ~plus(n, '(m))
17+
transparent def plus(n: Int & Constant, m: Int): Int = ~plus(n, '(m))
1818

1919
def plus(n: Int, m: Expr[Int]): Expr[Int] =
2020
if (n == 0) m

tests/run/i1569.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Test {
2-
transparent def foo(transparent n: => Int) = n + n
2+
transparent def foo(n: => Int & Constant) = n + n
33

44
def main(args: Array[String]): Unit = foo({ println("foo"); 42 })
55
}

0 commit comments

Comments
 (0)