Skip to content

Generalize function symbol synthesis logic #2398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Definitions {
* def apply(implicit $x0: T0, ..., $x{N_1}: T{N-1}): R
* }
*/
def newFunctionNTrait(name: TypeName) = {
def newFunctionNTrait(name: TypeName): ClassSymbol = {
val completer = new LazyType {
def complete(denot: SymDenotation)(implicit ctx: Context): Unit = {
val cls = denot.asClass.classSymbol
Expand Down Expand Up @@ -189,7 +189,14 @@ class Definitions {

lazy val ScalaPackageVal = ctx.requiredPackage("scala")
lazy val ScalaMathPackageVal = ctx.requiredPackage("scala.math")
lazy val ScalaPackageClass = ScalaPackageVal.moduleClass.asClass
lazy val ScalaPackageClass = {
val cls = ScalaPackageVal.moduleClass.asClass
cls.info.decls.openForMutations.useSynthesizer(
name => ctx =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the ctx parameter used here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be needed for other synthetic functions like the functions that take phantom parameters.

if (name.isTypeName && name.isSyntheticFunction) newFunctionNTrait(name.asTypeName)
else NoSymbol)
cls
}
lazy val JavaPackageVal = ctx.requiredPackage("java")
lazy val JavaLangPackageVal = ctx.requiredPackage("java.lang")
// fundamental modules
Expand Down
19 changes: 18 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Scopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ object Scopes {
*/
private final val MaxRecursions = 1000

/** A function that optionally produces synthesized symbols with
* the given name in the given context. Returns `NoSymbol` if the
* no symbol should be synthesized for the given name.
*/
type SymbolSynthesizer = Name => Context => Symbol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For efficiency, I would consider using (Name, Context) => Symbol instead, but that might be a premature optimization.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume that with our current simple use cases the JVM must be capable of inlineing the this additional lambda. Seem a bit premature optimization.


class ScopeEntry private[Scopes] (val name: Name, _sym: Symbol, val owner: Scope) {

var sym: Symbol = _sym
Expand Down Expand Up @@ -204,6 +210,12 @@ object Scopes {
*/
private var elemsCache: List[Symbol] = null

/** The synthesizer to be used, or `null` if no synthesis is done on this scope */
private var synthesize: SymbolSynthesizer = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that with both scalac and doty, not using private[this] here will generate a pair of getter and setter, this is arguably something that we should fix in the compiler since private[this] and private should be considered equivalent in an object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave it like this. Do we have an issue for this?


/** Use specified synthesize for this scope */
def useSynthesizer(s: SymbolSynthesizer): Unit = synthesize = s

protected def newScopeLikeThis() = new MutableScope()

/** Clone scope, taking care not to force the denotations of any symbols in the scope.
Expand All @@ -220,6 +232,7 @@ object Scopes {
val e = entries(i)
scope.newScopeEntry(e.name, e.sym)
}
scope.synthesize = synthesize
scope
}

Expand Down Expand Up @@ -350,7 +363,11 @@ object Scopes {
e = e.prev
}
}
e
if ((e eq null) && (synthesize != null)) {
val sym = synthesize(name)(ctx)
if (sym.exists) newScopeEntry(sym) else e
}
else e
}

/** lookup next entry with same name as this one */
Expand Down
3 changes: 0 additions & 3 deletions compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ class SymbolLoaders {
val mangled = name.mangled
val e = super.lookupEntry(mangled)
if (e != null) e
else if (_sourceModule.initialDenot.name == nme.scala_ && _sourceModule == defn.ScalaPackageVal &&
name.isTypeName && name.isSyntheticFunction)
newScopeEntry(defn.newFunctionNTrait(name.asTypeName))
else if (isFlatName(mangled.toSimpleName) && enterFlatClasses.isDefined) {
Stats.record("package scopes with flatnames entered")
enterFlatClasses.get(ctx)
Expand Down