@@ -9,7 +9,6 @@ import dotty.tools.dotc.ast.untpd
9
9
import dotty .tools .dotc .core .Annotations
10
10
import dotty .tools .dotc .core .Contexts ._
11
11
import dotty .tools .dotc .core .Decorators ._
12
- import dotty .tools .dotc .core .Flags ._
13
12
import dotty .tools .dotc .core .NameKinds
14
13
import dotty .tools .dotc .core .NameOps ._
15
14
import dotty .tools .dotc .core .StdNames ._
@@ -276,12 +275,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
276
275
277
276
object DefDef extends DefDefModule :
278
277
def apply (symbol : Symbol , rhsFn : List [List [Tree ]] => Option [Term ]): DefDef =
279
- assert(symbol.isTerm, s " expected a term symbol but received $symbol" )
278
+ xCheckMacroAssert(symbol.isTerm, s " expected a term symbol but received $symbol" )
279
+ xCheckMacroAssert(symbol.flags.is(Flags .Method ), " expected a symbol with `Method` flag set" )
280
280
withDefaultPos(tpd.DefDef (symbol.asTerm, prefss =>
281
- xCheckMacroedOwners (xCheckMacroValidExpr(rhsFn(prefss)), symbol).getOrElse(tpd.EmptyTree )
281
+ xCheckedMacroOwners (xCheckMacroValidExpr(rhsFn(prefss)), symbol).getOrElse(tpd.EmptyTree )
282
282
))
283
283
def copy (original : Tree )(name : String , paramss : List [ParamClause ], tpt : TypeTree , rhs : Option [Term ]): DefDef =
284
- tpd.cpy.DefDef (original)(name.toTermName, paramss, tpt, xCheckMacroedOwners (rhs, original.symbol).getOrElse(tpd.EmptyTree ))
284
+ tpd.cpy.DefDef (original)(name.toTermName, paramss, tpt, xCheckedMacroOwners (rhs, original.symbol).getOrElse(tpd.EmptyTree ))
285
285
def unapply (ddef : DefDef ): (String , List [ParamClause ], TypeTree , Option [Term ]) =
286
286
(ddef.name.toString, ddef.paramss, ddef.tpt, optional(ddef.rhs))
287
287
end DefDef
@@ -307,9 +307,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
307
307
308
308
object ValDef extends ValDefModule :
309
309
def apply (symbol : Symbol , rhs : Option [Term ]): ValDef =
310
- withDefaultPos(tpd.ValDef (symbol.asTerm, xCheckMacroedOwners(xCheckMacroValidExpr(rhs), symbol).getOrElse(tpd.EmptyTree )))
310
+ xCheckMacroAssert(! symbol.flags.is(Flags .Method ), " expected a symbol without `Method` flag set" )
311
+ withDefaultPos(tpd.ValDef (symbol.asTerm, xCheckedMacroOwners(xCheckMacroValidExpr(rhs), symbol).getOrElse(tpd.EmptyTree )))
311
312
def copy (original : Tree )(name : String , tpt : TypeTree , rhs : Option [Term ]): ValDef =
312
- tpd.cpy.ValDef (original)(name.toTermName, tpt, xCheckMacroedOwners (xCheckMacroValidExpr(rhs), original.symbol).getOrElse(tpd.EmptyTree ))
313
+ tpd.cpy.ValDef (original)(name.toTermName, tpt, xCheckedMacroOwners (xCheckMacroValidExpr(rhs), original.symbol).getOrElse(tpd.EmptyTree ))
313
314
def unapply (vdef : ValDef ): (String , TypeTree , Option [Term ]) =
314
315
(vdef.name.toString, vdef.tpt, optional(vdef.rhs))
315
316
@@ -398,7 +399,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
398
399
def etaExpand (owner : Symbol ): Term = self.tpe.widen match {
399
400
case mtpe : Types .MethodType if ! mtpe.isParamDependent =>
400
401
val closureResType = mtpe.resType match {
401
- case t : Types .MethodType => t.toFunctionType(isJava = self.symbol.is(JavaDefined ))
402
+ case t : Types .MethodType => t.toFunctionType(isJava = self.symbol.is(dotc.core. Flags . JavaDefined ))
402
403
case t => t
403
404
}
404
405
val closureTpe = Types .MethodType (mtpe.paramNames, mtpe.paramInfos, closureResType)
@@ -811,7 +812,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
811
812
object Lambda extends LambdaModule :
812
813
def apply (owner : Symbol , tpe : MethodType , rhsFn : (Symbol , List [Tree ]) => Tree ): Block =
813
814
val meth = dotc.core.Symbols .newAnonFun(owner, tpe)
814
- withDefaultPos(tpd.Closure (meth, tss => xCheckMacroedOwners (xCheckMacroValidExpr(rhsFn(meth, tss.head.map(withDefaultPos))), meth)))
815
+ withDefaultPos(tpd.Closure (meth, tss => xCheckedMacroOwners (xCheckMacroValidExpr(rhsFn(meth, tss.head.map(withDefaultPos))), meth)))
815
816
816
817
def unapply (tree : Block ): Option [(List [ValDef ], Term )] = tree match {
817
818
case Block ((ddef @ DefDef (_, tpd.ValDefs (params) :: Nil , _, Some (body))) :: Nil , Closure (meth, _))
@@ -1482,6 +1483,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
1482
1483
1483
1484
object Bind extends BindModule :
1484
1485
def apply (sym : Symbol , pattern : Tree ): Bind =
1486
+ xCheckMacroAssert(sym.flags.is(Flags .Case ), " expected a symbol with `Case` flag set" )
1485
1487
withDefaultPos(tpd.Bind (sym, pattern))
1486
1488
def copy (original : Tree )(name : String , pattern : Tree ): Bind =
1487
1489
withDefaultPos(tpd.cpy.Bind (original)(name.toTermName, pattern))
@@ -2520,13 +2522,22 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2520
2522
def newMethod (owner : Symbol , name : String , tpe : TypeRepr ): Symbol =
2521
2523
newMethod(owner, name, tpe, Flags .EmptyFlags , noSymbol)
2522
2524
def newMethod (owner : Symbol , name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol ): Symbol =
2525
+ checkValidFlags(flags.toTermFlags, Flags .validMethodFlags)
2523
2526
dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | dotc.core.Flags .Method , tpe, privateWithin)
2524
2527
def newVal (owner : Symbol , name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol ): Symbol =
2528
+ checkValidFlags(flags.toTermFlags, Flags .validValFlags)
2525
2529
dotc.core.Symbols .newSymbol(owner, name.toTermName, flags, tpe, privateWithin)
2526
2530
def newBind (owner : Symbol , name : String , flags : Flags , tpe : TypeRepr ): Symbol =
2527
- dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | Case , tpe)
2531
+ checkValidFlags(flags.toTermFlags, Flags .validBindFlags)
2532
+ dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | dotc.core.Flags .Case , tpe)
2528
2533
def noSymbol : Symbol = dotc.core.Symbols .NoSymbol
2529
2534
2535
+ private inline def checkValidFlags (inline flags : Flags , inline valid : Flags ): Unit =
2536
+ xCheckMacroAssert(
2537
+ flags <= valid,
2538
+ s " Received invalid flags. Expected flags ${flags.show} to only contain a subset of ${valid.show}. "
2539
+ )
2540
+
2530
2541
def freshName (prefix : String ): String =
2531
2542
NameKinds .MacroNames .fresh(prefix.toTermName).toString
2532
2543
end Symbol
@@ -2599,7 +2610,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2599
2610
self.isTerm && ! self.is(dotc.core.Flags .Method ) && ! self.is(dotc.core.Flags .Case /* , FIXME add this check and fix sourcecode butNot = Enum | Module*/ )
2600
2611
def isDefDef : Boolean = self.is(dotc.core.Flags .Method )
2601
2612
def isBind : Boolean =
2602
- self.is(dotc.core.Flags .Case , butNot = Enum | Module ) && ! self.isClass
2613
+ self.is(dotc.core.Flags .Case , butNot = dotc.core. Flags . Enum | dotc.core. Flags . Module ) && ! self.isClass
2603
2614
def isNoSymbol : Boolean = self == Symbol .noSymbol
2604
2615
def exists : Boolean = self != Symbol .noSymbol
2605
2616
@@ -2843,6 +2854,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2843
2854
def Synthetic : Flags = dotc.core.Flags .Synthetic
2844
2855
def Trait : Flags = dotc.core.Flags .Trait
2845
2856
def Transparent : Flags = dotc.core.Flags .Transparent
2857
+
2858
+ private [QuotesImpl ] def validMethodFlags : Flags = Private | Protected | Override | Deferred | Final | Method | Implicit | Given | Local | JavaStatic | AbsOverride // Synthetic | ExtensionMethod | Exported | Erased | Infix | Invisible
2859
+ private [QuotesImpl ] def validValFlags : Flags = Private | Protected | Override | Deferred | Final | Param | Implicit | Lazy | Mutable | Local | ParamAccessor | Module | Package | Case | CaseAccessor | Given | Enum | JavaStatic | AbsOverride // Synthetic | Erased | Invisible
2860
+ private [QuotesImpl ] def validBindFlags : Flags = Case // | Implicit | Given | Erased
2846
2861
end Flags
2847
2862
2848
2863
given FlagsMethods : FlagsMethods with
@@ -2963,7 +2978,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2963
2978
/** Checks that all definitions in this tree have the expected owner.
2964
2979
* Nested definitions are ignored and assumed to be correct by construction.
2965
2980
*/
2966
- private def xCheckMacroedOwners (tree : Option [Tree ], owner : Symbol ): tree.type =
2981
+ private def xCheckedMacroOwners (tree : Option [Tree ], owner : Symbol ): tree.type =
2967
2982
if xCheckMacro then
2968
2983
tree match
2969
2984
case Some (tree) =>
@@ -2974,7 +2989,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2974
2989
/** Checks that all definitions in this tree have the expected owner.
2975
2990
* Nested definitions are ignored and assumed to be correct by construction.
2976
2991
*/
2977
- private def xCheckMacroedOwners (tree : Tree , owner : Symbol ): tree.type =
2992
+ private def xCheckedMacroOwners (tree : Tree , owner : Symbol ): tree.type =
2978
2993
if xCheckMacro then
2979
2994
xCheckMacroOwners(tree, owner)
2980
2995
tree
@@ -3045,6 +3060,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
3045
3060
" Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
3046
3061
term
3047
3062
3063
+ private inline def xCheckMacroAssert (inline cond : Boolean , inline msg : String ): Unit =
3064
+ assert(! xCheckMacro || cond, msg)
3065
+
3048
3066
object Printer extends PrinterModule :
3049
3067
3050
3068
lazy val TreeCode : Printer [Tree ] = new Printer [Tree ]:
0 commit comments