@@ -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
@@ -399,7 +400,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
399
400
def etaExpand (owner : Symbol ): Term = self.tpe.widen match {
400
401
case mtpe : Types .MethodType if ! mtpe.isParamDependent =>
401
402
val closureResType = mtpe.resType match {
402
- case t : Types .MethodType => t.toFunctionType(isJava = self.symbol.is(JavaDefined ))
403
+ case t : Types .MethodType => t.toFunctionType(isJava = self.symbol.is(dotc.core. Flags . JavaDefined ))
403
404
case t => t
404
405
}
405
406
val closureTpe = Types .MethodType (mtpe.paramNames, mtpe.paramInfos, closureResType)
@@ -812,7 +813,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
812
813
object Lambda extends LambdaModule :
813
814
def apply (owner : Symbol , tpe : MethodType , rhsFn : (Symbol , List [Tree ]) => Tree ): Block =
814
815
val meth = dotc.core.Symbols .newAnonFun(owner, tpe)
815
- withDefaultPos(tpd.Closure (meth, tss => xCheckMacroedOwners (xCheckMacroValidExpr(rhsFn(meth, tss.head.map(withDefaultPos))), meth)))
816
+ withDefaultPos(tpd.Closure (meth, tss => xCheckedMacroOwners (xCheckMacroValidExpr(rhsFn(meth, tss.head.map(withDefaultPos))), meth)))
816
817
817
818
def unapply (tree : Block ): Option [(List [ValDef ], Term )] = tree match {
818
819
case Block ((ddef @ DefDef (_, tpd.ValDefs (params) :: Nil , _, Some (body))) :: Nil , Closure (meth, _))
@@ -1483,6 +1484,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
1483
1484
1484
1485
object Bind extends BindModule :
1485
1486
def apply (sym : Symbol , pattern : Tree ): Bind =
1487
+ xCheckMacroAssert(sym.flags.is(Flags .Case ), " expected a symbol with `Case` flag set" )
1486
1488
withDefaultPos(tpd.Bind (sym, pattern))
1487
1489
def copy (original : Tree )(name : String , pattern : Tree ): Bind =
1488
1490
withDefaultPos(tpd.cpy.Bind (original)(name.toTermName, pattern))
@@ -2514,13 +2516,22 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2514
2516
def newMethod (owner : Symbol , name : String , tpe : TypeRepr ): Symbol =
2515
2517
newMethod(owner, name, tpe, Flags .EmptyFlags , noSymbol)
2516
2518
def newMethod (owner : Symbol , name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol ): Symbol =
2519
+ checkValidFlags(flags.toTermFlags, Flags .validMethodFlags)
2517
2520
dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | dotc.core.Flags .Method , tpe, privateWithin)
2518
2521
def newVal (owner : Symbol , name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol ): Symbol =
2522
+ checkValidFlags(flags.toTermFlags, Flags .validValFlags)
2519
2523
dotc.core.Symbols .newSymbol(owner, name.toTermName, flags, tpe, privateWithin)
2520
2524
def newBind (owner : Symbol , name : String , flags : Flags , tpe : TypeRepr ): Symbol =
2521
- dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | Case , tpe)
2525
+ checkValidFlags(flags.toTermFlags, Flags .validBindFlags)
2526
+ dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | dotc.core.Flags .Case , tpe)
2522
2527
def noSymbol : Symbol = dotc.core.Symbols .NoSymbol
2523
2528
2529
+ private inline def checkValidFlags (inline flags : Flags , inline valid : Flags ): Unit =
2530
+ xCheckMacroAssert(
2531
+ flags <= valid,
2532
+ s " Received invalid flags. Expected flags ${flags.show} to only contain a subset of ${valid.show}. "
2533
+ )
2534
+
2524
2535
def freshName (prefix : String ): String =
2525
2536
NameKinds .MacroNames .fresh(prefix.toTermName).toString
2526
2537
end Symbol
@@ -2593,7 +2604,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2593
2604
self.isTerm && ! self.is(dotc.core.Flags .Method ) && ! self.is(dotc.core.Flags .Case /* , FIXME add this check and fix sourcecode butNot = Enum | Module*/ )
2594
2605
def isDefDef : Boolean = self.is(dotc.core.Flags .Method )
2595
2606
def isBind : Boolean =
2596
- self.is(dotc.core.Flags .Case , butNot = Enum | Module ) && ! self.isClass
2607
+ self.is(dotc.core.Flags .Case , butNot = dotc.core. Flags . Enum | dotc.core. Flags . Module ) && ! self.isClass
2597
2608
def isNoSymbol : Boolean = self == Symbol .noSymbol
2598
2609
def exists : Boolean = self != Symbol .noSymbol
2599
2610
@@ -2829,6 +2840,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2829
2840
def Synthetic : Flags = dotc.core.Flags .Synthetic
2830
2841
def Trait : Flags = dotc.core.Flags .Trait
2831
2842
def Transparent : Flags = dotc.core.Flags .Transparent
2843
+
2844
+ private [QuotesImpl ] def validMethodFlags : Flags = Private | Protected | Override | Deferred | Final | Method | Implicit | Given | Local | JavaStatic | AbsOverride // Synthetic | ExtensionMethod | Exported | Erased | Infix | Invisible
2845
+ 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
2846
+ private [QuotesImpl ] def validBindFlags : Flags = Case // | Implicit | Given | Erased
2832
2847
end Flags
2833
2848
2834
2849
given FlagsMethods : FlagsMethods with
@@ -2949,7 +2964,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2949
2964
/** Checks that all definitions in this tree have the expected owner.
2950
2965
* Nested definitions are ignored and assumed to be correct by construction.
2951
2966
*/
2952
- private def xCheckMacroedOwners (tree : Option [Tree ], owner : Symbol ): tree.type =
2967
+ private def xCheckedMacroOwners (tree : Option [Tree ], owner : Symbol ): tree.type =
2953
2968
if xCheckMacro then
2954
2969
tree match
2955
2970
case Some (tree) =>
@@ -2960,7 +2975,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2960
2975
/** Checks that all definitions in this tree have the expected owner.
2961
2976
* Nested definitions are ignored and assumed to be correct by construction.
2962
2977
*/
2963
- private def xCheckMacroedOwners (tree : Tree , owner : Symbol ): tree.type =
2978
+ private def xCheckedMacroOwners (tree : Tree , owner : Symbol ): tree.type =
2964
2979
if xCheckMacro then
2965
2980
xCheckMacroOwners(tree, owner)
2966
2981
tree
@@ -3031,6 +3046,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
3031
3046
" Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
3032
3047
term
3033
3048
3049
+ private inline def xCheckMacroAssert (inline cond : Boolean , inline msg : String ): Unit =
3050
+ assert(! xCheckMacro || cond, msg)
3051
+
3034
3052
object Printer extends PrinterModule :
3035
3053
3036
3054
lazy val TreeCode : Printer [Tree ] = new Printer [Tree ]:
0 commit comments