Skip to content

Commit 3e6a261

Browse files
Assert that symbols created using reflect API have correct privateWithin symbols (#17352)
closes lampepfl#17351
2 parents f128063 + 76ec705 commit 3e6a261

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ object Symbols {
170170
asInstanceOf[TermSymbol]
171171
}
172172
final def asType(using Context): TypeSymbol = {
173-
assert(isType, s"isType called on not-a-Type $this");
173+
assert(isType, s"asType called on not-a-Type $this");
174174
asInstanceOf[TypeSymbol]
175175
}
176176

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25042504

25052505
def newModule(owner: Symbol, name: String, modFlags: Flags, clsFlags: Flags, parents: List[TypeRepr], decls: Symbol => List[Symbol], privateWithin: Symbol): Symbol =
25062506
assert(parents.nonEmpty && !parents.head.typeSymbol.is(dotc.core.Flags.Trait), "First parent must be a class")
2507+
assert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
25072508
val mod = dotc.core.Symbols.newNormalizedModuleSymbol(
25082509
owner,
25092510
name.toTermName,
@@ -2520,8 +2521,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25202521
def newMethod(owner: Symbol, name: String, tpe: TypeRepr): Symbol =
25212522
newMethod(owner, name, tpe, Flags.EmptyFlags, noSymbol)
25222523
def newMethod(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
2524+
assert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
25232525
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin)
25242526
def newVal(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
2527+
assert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
25252528
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags, tpe, privateWithin)
25262529
def newBind(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr): Symbol =
25272530
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | Case, tpe)

tests/neg/i17351/Macro_1.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.quoted.*
2+
3+
inline def gen: Unit = ${ genImpl }
4+
5+
def genImpl(using Quotes): Expr[Unit] = {
6+
import quotes.reflect.*
7+
8+
val valDefSymbol = Symbol.newVal(Symbol.spliceOwner, "bar", TypeRepr.of[Unit], Flags.EmptyFlags, Symbol.spliceOwner)
9+
10+
val valDef = ValDef(valDefSymbol, Some('{ () }.asTerm))
11+
12+
Block(
13+
List(valDef),
14+
'{ () }.asTerm
15+
).asExprOf[Unit]
16+
}

tests/neg/i17351/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val foo = gen // error

0 commit comments

Comments
 (0)