Skip to content

Commit dcce753

Browse files
committed
Check flags for newMethod, newVal and newBind
1 parent 0397de1 commit dcce753

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import dotty.tools.dotc.ast.untpd
99
import dotty.tools.dotc.core.Annotations
1010
import dotty.tools.dotc.core.Contexts._
1111
import dotty.tools.dotc.core.Types
12-
import dotty.tools.dotc.core.Flags._
1312
import dotty.tools.dotc.core.NameKinds
1413
import dotty.tools.dotc.core.StdNames._
1514
import dotty.tools.dotc.quoted.reflect._
@@ -390,7 +389,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
390389
def etaExpand(owner: Symbol): Term = self.tpe.widen match {
391390
case mtpe: Types.MethodType if !mtpe.isParamDependent =>
392391
val closureResType = mtpe.resType match {
393-
case t: Types.MethodType => t.toFunctionType(isJava = self.symbol.is(JavaDefined))
392+
case t: Types.MethodType => t.toFunctionType(isJava = self.symbol.is(dotc.core.Flags.JavaDefined))
394393
case t => t
395394
}
396395
val closureTpe = Types.MethodType(mtpe.paramNames, mtpe.paramInfos, closureResType)
@@ -2484,13 +2483,31 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
24842483
def newMethod(owner: Symbol, name: String, tpe: TypeRepr): Symbol =
24852484
newMethod(owner, name, tpe, Flags.EmptyFlags, noSymbol)
24862485
def newMethod(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
2486+
import Flags.*
2487+
// TODO: missing AbsOverride
2488+
checkValidFlags(flags.toTermFlags, Private | Protected | Override | Deferred | Final | Method | Implicit | Given | Local) // Synthetic | ExtensionMethod | Exported | Erased | Infix | Invisible
24872489
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin)
24882490
def newVal(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
2491+
import Flags.*
2492+
// TODO: missing AbsOverride
2493+
checkValidFlags(flags.toTermFlags, Private | Protected | Override | Deferred | Final | Param | Implicit | Lazy | Mutable | Local | ParamAccessor | Module | Package | Case | CaseAccessor | Given | Enum) // Synthetic | Erased | Invisible
24892494
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags, tpe, privateWithin)
24902495
def newBind(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr): Symbol =
2491-
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | Case, tpe)
2496+
import Flags.*
2497+
checkValidFlags(flags.toTermFlags, Case) // | Implicit | Given | Erased
2498+
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Case, tpe)
24922499
def noSymbol: Symbol = dotc.core.Symbols.NoSymbol
24932500

2501+
private def checkValidFlags(flags: Flags, valid: Flags): Unit =
2502+
// TODO remove xCheckMacro.
2503+
// We introduce this check under this flag to not break published macros that may have accidentally used a wrong flag but happen to work.
2504+
// Once macro implementation had a compiler version or two too find their bugs and patch them we can enforce it on every use site.
2505+
assert(
2506+
xCheckMacro && flags <= valid,
2507+
s"Received invalid flags. Expected flags ${flags.show} to only contain a subset of ${valid.show}."
2508+
)
2509+
2510+
24942511
def freshName(prefix: String): String =
24952512
NameKinds.MacroNames.fresh(prefix.toTermName).toString
24962513
end Symbol
@@ -2563,7 +2580,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25632580
self.isTerm && !self.is(dotc.core.Flags.Method) && !self.is(dotc.core.Flags.Case/*, FIXME add this check and fix sourcecode butNot = Enum | Module*/)
25642581
def isDefDef: Boolean = self.is(dotc.core.Flags.Method)
25652582
def isBind: Boolean =
2566-
self.is(dotc.core.Flags.Case, butNot = Enum | Module) && !self.isClass
2583+
self.is(dotc.core.Flags.Case, butNot = dotc.core.Flags.Enum | dotc.core.Flags.Module) && !self.isClass
25672584
def isNoSymbol: Boolean = self == Symbol.noSymbol
25682585
def exists: Boolean = self != Symbol.noSymbol
25692586

library/src/scala/quoted/Quotes.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,7 +3664,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
36643664
* @param parent The owner of the method
36653665
* @param name The name of the method
36663666
* @param tpe The type of the method (MethodType, PolyType, ByNameType)
3667-
* @param flags extra flags to with which the symbol should be constructed
3667+
* @param flags extra flags to with which the symbol should be constructed. `Method` flag will be added. Can be `Private | Protected | Override | Deferred | Final | Method | Implicit | Given | Local`
36683668
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
36693669
*/
36703670
def newMethod(parent: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol
@@ -3680,7 +3680,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
36803680
* @param parent The owner of the val/var/lazy val
36813681
* @param name The name of the val/var/lazy val
36823682
* @param tpe The type of the val/var/lazy val
3683-
* @param flags extra flags to with which the symbol should be constructed
3683+
* @param flags extra flags to with which the symbol should be constructed. Can be `Private | Protected | Override | Deferred | Final | Param | Implicit | Lazy | Mutable | Local | ParamAccessor | Module | Package | Case | CaseAccessor | Given | Enum`
36843684
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
36853685
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
36863686
* direct or indirect children of the reflection context's owner.
@@ -3695,7 +3695,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
36953695
*
36963696
* @param parent The owner of the binding
36973697
* @param name The name of the binding
3698-
* @param flags extra flags to with which the symbol should be constructed
3698+
* @param flags extra flags to with which the symbol should be constructed. `Case` flag will be added. Can be `Case`
36993699
* @param tpe The type of the binding
37003700
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
37013701
* direct or indirect children of the reflection context's owner.

0 commit comments

Comments
 (0)