Skip to content

Commit

Permalink
Assert that symbols created using reflect API have correct privateWit…
Browse files Browse the repository at this point in the history
…hin symbols (scala#17352)

closes lampepfl#17351
  • Loading branch information
nicolasstucki authored Apr 28, 2023
2 parents f128063 + 76ec705 commit 3e6a261
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ object Symbols {
asInstanceOf[TermSymbol]
}
final def asType(using Context): TypeSymbol = {
assert(isType, s"isType called on not-a-Type $this");
assert(isType, s"asType called on not-a-Type $this");
asInstanceOf[TypeSymbol]
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

def newModule(owner: Symbol, name: String, modFlags: Flags, clsFlags: Flags, parents: List[TypeRepr], decls: Symbol => List[Symbol], privateWithin: Symbol): Symbol =
assert(parents.nonEmpty && !parents.head.typeSymbol.is(dotc.core.Flags.Trait), "First parent must be a class")
assert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
val mod = dotc.core.Symbols.newNormalizedModuleSymbol(
owner,
name.toTermName,
Expand All @@ -2520,8 +2521,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def newMethod(owner: Symbol, name: String, tpe: TypeRepr): Symbol =
newMethod(owner, name, tpe, Flags.EmptyFlags, noSymbol)
def newMethod(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
assert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin)
def newVal(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
assert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags, tpe, privateWithin)
def newBind(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr): Symbol =
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | Case, tpe)
Expand Down
16 changes: 16 additions & 0 deletions tests/neg/i17351/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.quoted.*

inline def gen: Unit = ${ genImpl }

def genImpl(using Quotes): Expr[Unit] = {
import quotes.reflect.*

val valDefSymbol = Symbol.newVal(Symbol.spliceOwner, "bar", TypeRepr.of[Unit], Flags.EmptyFlags, Symbol.spliceOwner)

val valDef = ValDef(valDefSymbol, Some('{ () }.asTerm))

Block(
List(valDef),
'{ () }.asTerm
).asExprOf[Unit]
}
1 change: 1 addition & 0 deletions tests/neg/i17351/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val foo = gen // error

0 comments on commit 3e6a261

Please sign in to comment.