Skip to content

Drop extension_ prefix for extension methods #10128

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 19 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from 18 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
28 changes: 14 additions & 14 deletions compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class JSCodeGen()(using genCtx: Context) {
withScopedVars(
currentClassSym := sym
) {
val tree = if (isJSType(sym)) {
val tree = if (sym.isJSType) {
if (!sym.is(Trait) && sym.isNonNativeJSClass)
genNonNativeJSClass(td)
else
Expand Down Expand Up @@ -702,7 +702,7 @@ class JSCodeGen()(using genCtx: Context) {
Nil
} else {
val moduleClass = module.moduleClass
if (!isJSType(moduleClass))
if (!moduleClass.isJSType)
genStaticForwardersFromModuleClass(existingMembers, moduleClass)
else
Nil
Expand Down Expand Up @@ -1678,7 +1678,7 @@ class JSCodeGen()(using genCtx: Context) {
}

fun match {
case _ if isJSDefaultParam(sym) =>
case _ if sym.isJSDefaultParam =>
js.Transient(UndefinedParam)(toIRType(sym.info.finalResultType))

case Select(Super(_, _), _) =>
Expand Down Expand Up @@ -1771,7 +1771,7 @@ class JSCodeGen()(using genCtx: Context) {
} else /*if (translatedAnonFunctions contains tpe.typeSymbol) {
val functionMaker = translatedAnonFunctions(tpe.typeSymbol)
functionMaker(args map genExpr)
} else*/ if (isJSType(clsSym)) {
} else*/ if (clsSym.isJSType) {
genNewJSClass(tree)
} else {
toTypeRef(tpe) match {
Expand Down Expand Up @@ -2406,7 +2406,7 @@ class JSCodeGen()(using genCtx: Context) {
* and `-0.0`, see Javadoc (scala-dev#329, scala-js#2799).
*/
val mustUseAnyComparator: Boolean = {
isJSType(lsym) || isJSType(rsym) || {
lsym.isJSType || rsym.isJSType || {
val p = ctx.platform
p.isMaybeBoxed(lsym) && p.isMaybeBoxed(rsym) && {
val areSameFinals = lsym.is(Final) && rsym.is(Final) && (ltpe =:= rtpe)
Expand Down Expand Up @@ -2603,7 +2603,7 @@ class JSCodeGen()(using genCtx: Context) {

if (isMethodStaticInIR(sym)) {
genApplyStatic(sym, genActualArgs(sym, args))
} else if (isJSType(sym.owner)) {
} else if (sym.owner.isJSType) {
if (!sym.owner.isNonNativeJSClass || sym.isJSExposed)
genApplyJSMethodGeneric(sym, genExprOrGlobalScope(receiver), genActualJSArgs(sym, args), isStat)(tree.sourcePos)
else
Expand Down Expand Up @@ -2689,13 +2689,13 @@ class JSCodeGen()(using genCtx: Context) {
}
}

if (isJSGetter(sym)) {
if (sym.isJSGetter) {
assert(noSpread && argc == 0)
genSelectGet(jsFunName)
} else if (isJSSetter(sym)) {
} else if (sym.isJSSetter) {
assert(noSpread && argc == 1)
genSelectSet(jsFunName, requireNotSpread(args.head))
} else if (isJSBracketAccess(sym)) {
} else if (sym.isJSBracketAccess) {
assert(noSpread && (argc == 1 || argc == 2),
s"@JSBracketAccess methods should have 1 or 2 non-varargs arguments")
(args: @unchecked) match {
Expand All @@ -2704,7 +2704,7 @@ class JSCodeGen()(using genCtx: Context) {
case List(keyArg, valueArg) =>
genSelectSet(requireNotSpread(keyArg), requireNotSpread(valueArg))
}
} else if (isJSBracketCall(sym)) {
} else if (sym.isJSBracketCall) {
val (methodName, actualArgs) = extractFirstArg(args)
genCall(methodName, actualArgs)
} else {
Expand Down Expand Up @@ -3187,7 +3187,7 @@ class JSCodeGen()(using genCtx: Context) {

val sym = to.typeSymbol

if (sym == defn.ObjectClass || isJSType(sym)) {
if (sym == defn.ObjectClass || sym.isJSType) {
/* asInstanceOf[Object] always succeeds, and
* asInstanceOf to a raw JS type is completely erased.
*/
Expand Down Expand Up @@ -3217,7 +3217,7 @@ class JSCodeGen()(using genCtx: Context) {

if (sym == defn.ObjectClass) {
js.BinaryOp(js.BinaryOp.!==, value, js.Null())
} else if (isJSType(sym)) {
} else if (sym.isJSType) {
if (sym.is(Trait)) {
report.error(
s"isInstanceOf[${sym.fullName}] not supported because it is a JS trait",
Expand Down Expand Up @@ -3347,7 +3347,7 @@ class JSCodeGen()(using genCtx: Context) {
arg match {
case Literal(value) if value.tag == Constants.ClazzTag =>
val classSym = value.typeValue.typeSymbol
if (isJSType(classSym) && !classSym.is(Trait) && !classSym.is(ModuleClass))
if (classSym.isJSType && !classSym.is(Trait) && !classSym.is(ModuleClass))
classSym
else
fail()
Expand Down Expand Up @@ -3999,7 +3999,7 @@ class JSCodeGen()(using genCtx: Context) {
} else {
val cls = encodeClassName(sym)
val tree =
if (isJSType(sym)) js.LoadJSModule(cls)
if (sym.isJSType) js.LoadJSModule(cls)
else js.LoadModule(cls)
MaybeGlobalScope.NotGlobalScope(tree)
}
Expand Down
33 changes: 9 additions & 24 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ object desugar {
}

flatTree(cdef1 :: companions ::: implicitWrappers ::: enumScaffolding)
}.reporting(i"desugared: $result", Printers.desugar)
}.showing(i"desugared: $result", Printers.desugar)

/** Expand
*
Expand Down Expand Up @@ -895,7 +895,7 @@ object desugar {
mdef.tparams.head.srcPos)
defDef(
cpy.DefDef(mdef)(
name = normalizeName(mdef, ext).toExtensionName,
name = normalizeName(mdef, ext).asTermName,
tparams = ext.tparams ++ mdef.tparams,
vparamss = mdef.vparamss match
case vparams1 :: vparamss1 if mdef.name.isRightAssocOperatorName =>
Expand Down Expand Up @@ -928,10 +928,10 @@ object desugar {

/** The normalized name of `mdef`. This means
* 1. Check that the name does not redefine a Scala core class.
* If it does redefine, issue an error and return a mangled name instead of the original one.
* 2. Check that the name does not start with `extension_` unless the
* method is an extension method.
* 3. If the name is missing (this can be the case for instance definitions), invent one instead.
* If it does redefine, issue an error and return a mangled name instead
* of the original one.
* 2. If the name is missing (this can be the case for instance definitions),
* invent one instead.
*/
def normalizeName(mdef: MemberDef, impl: Tree)(using Context): Name = {
var name = mdef.name
Expand All @@ -942,31 +942,16 @@ object desugar {
report.error(IllegalRedefinitionOfStandardKind(kind, name), errPos)
name = name.errorName
}
mdef match {
case vdef: ValDef if name.isExtension && isSetterNeeded(vdef) =>
report.error(em"illegal setter name: `extension_=`", errPos)
case memDef if name.isExtensionName && !mdef.mods.is(ExtensionMethod) =>
report.error(em"illegal name: $name may not start with `extension_`", errPos)
case _ =>
}
name
}

/** Invent a name for an anonympus given or extension of type or template `impl`. */
/** Invent a name for an anonympus given of type or template `impl`. */
def inventGivenOrExtensionName(impl: Tree)(using Context): SimpleName =
val str = impl match
case impl: Template =>
if impl.parents.isEmpty then
impl.body.find {
case dd: DefDef if dd.mods.is(ExtensionMethod) => true
case _ => false
}
match
case Some(DefDef(name, _, (vparam :: _) :: _, _, _)) =>
s"extension_${name}_${inventTypeName(vparam.tpt)}"
case _ =>
report.error(AnonymousInstanceCannotBeEmpty(impl), impl.srcPos)
nme.ERROR.toString
report.error(AnonymousInstanceCannotBeEmpty(impl), impl.srcPos)
nme.ERROR.toString
else
impl.parents.map(inventTypeName(_)).mkString("given_", "_", "")
case impl: Tree =>
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,7 @@ object Trees {
if (rawMods.is(Synthetic) || span.isSynthetic || name.toTermName == nme.ERROR) Span(point)
else {
val realName = name.stripModuleClassSuffix.lastPart
var length = realName.length
if (rawMods.is(ExtensionMethod) || symbol.is(ExtensionMethod)) && name.isExtensionName then
length -= "extension_".length
Span(point, point + length, point)
Span(point, point + realName.length, point)
}
}
else span
Expand Down
13 changes: 9 additions & 4 deletions compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
}

/** A typed subtree of an untyped tree needs to be wrapped in a TypedSplice
* @param owner The current owner at the time the tree was defined
* @param owner The current owner at the time the tree was defined
* @param isExtensionReceiver The splice was created from the receiver `e` in an extension
* method call `e.f(...)`
*/
abstract case class TypedSplice(splice: tpd.Tree)(val owner: Symbol)(implicit @constructorOnly src: SourceFile) extends ProxyTree {
abstract case class TypedSplice(splice: tpd.Tree)(val owner: Symbol, val isExtensionReceiver: Boolean)(implicit @constructorOnly src: SourceFile) extends ProxyTree {
def forwardTo: tpd.Tree = splice
override def toString =
def ext = if isExtensionReceiver then ", isExtensionReceiver = true" else ""
s"TypedSplice($splice$ext)"
}

object TypedSplice {
def apply(tree: tpd.Tree)(using Context): TypedSplice =
new TypedSplice(tree)(ctx.owner) {}
def apply(tree: tpd.Tree, isExtensionReceiver: Boolean = false)(using Context): TypedSplice =
new TypedSplice(tree)(ctx.owner, isExtensionReceiver) {}
}

/** mods object name impl */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ trait ConstraintHandling {
val bound = adjust(rawBound)
bound.exists
&& addOneBound(param, bound, isUpper) && others.forall(addOneBound(_, bound, isUpper))
.reporting(i"added $description = $result$location", constr)
.showing(i"added $description = $result$location", constr)
end addBoundTransitively

protected def addLess(p1: TypeParamRef, p2: TypeParamRef)(using Context): Boolean = {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ object Decorators {
}

extension [T](x: T)
def reporting(
def showing(
op: WrappedResult[T] ?=> String,
printer: config.Printers.Printer = config.Printers.default): T = {
printer.println(op(using WrappedResult(x)))
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ class Definitions {
@tu lazy val Compiletime_requireConst: Symbol = CompiletimePackageObject.requiredMethod("requireConst")
@tu lazy val Compiletime_constValue : Symbol = CompiletimePackageObject.requiredMethod("constValue")
@tu lazy val Compiletime_constValueOpt: Symbol = CompiletimePackageObject.requiredMethod("constValueOpt")
@tu lazy val Compiletime_code : Symbol = CompiletimePackageObject.requiredMethod("extension_code")
@tu lazy val Compiletime_summonFrom : Symbol = CompiletimePackageObject.requiredMethod("summonFrom")
@tu lazy val CompiletimeTestingPackageObject: Symbol = requiredModule("scala.compiletime.testing.package")
@tu lazy val CompiletimeTesting_typeChecks: Symbol = CompiletimeTestingPackageObject.requiredMethod("typeChecks")
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ object Flags {
def flagsString: String = x.flagStrings("").mkString(" ")
}

// Temporary while extension names are in flux
def or(x1: FlagSet, x2: FlagSet) = x1 | x2
def and(x1: FlagSet, x2: FlagSet) = x1 & x2

def termFlagSet(x: Long) = FlagSet(TERMS | x)

private inline val TYPESHIFT = 2
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/GadtConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ final class ProperGadtConstraint private(

// The replaced symbols are picked up here.
addToConstraint(poly1, tvars)
.reporting(i"added to constraint: [$poly1] $params%, %\n$debugBoundsDescription", gadts)
.showing(i"added to constraint: [$poly1] $params%, %\n$debugBoundsDescription", gadts)
}

override def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean = {
Expand Down Expand Up @@ -158,7 +158,7 @@ final class ProperGadtConstraint private(
case bound =>
addBoundTransitively(symTvar.origin, bound, isUpper)
}
).reporting({
).showing({
val descr = if (isUpper) "upper" else "lower"
val op = if (isUpper) "<:" else ">:"
i"adding $descr bound $sym $op $bound = $result"
Expand Down Expand Up @@ -187,7 +187,7 @@ final class ProperGadtConstraint private(
case tb => tb
}
retrieveBounds
//.reporting(i"gadt bounds $sym: $result", gadts)
//.showing(i"gadt bounds $sym: $result", gadts)
//.ensuring(containsNoInternalTypes(_))
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,7 @@ object Mode {

/** Are we in a quote in a pattern? */
val QuotedPattern: Mode = newMode(25, "QuotedPattern")

/** Are we typechecking the rhs of an extension method? */
val InExtensionMethod: Mode = newMode(26, "InExtensionMethod")
}
10 changes: 2 additions & 8 deletions compiler/src/dotty/tools/dotc/core/NameOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,10 @@ object NameOps {
case name: SimpleName => name.startsWith("extension_")
case _ => false

// TODO: Drop next 3 methods once extension names have stabilized
/** Add an `extension_` in front of this name */
def toExtensionName(using Context): SimpleName = "extension_".concat(name)

/** Drop `extension_` in front of this name, if it has this prefix */
def dropExtension = name match
case name: SimpleName if name.startsWith("extension_") =>
name.drop("extension_".length)
case _ =>
name

/** The expanded name.
* This is the fully qualified name of `base` with `ExpandPrefixName` as separator,
* followed by `kind` and the name.
Expand Down Expand Up @@ -218,7 +212,7 @@ object NameOps {
/** Same as `funArity`, except that it returns -1 if the prefix
* is not one of "", "Context", "Erased", "ErasedContext"
*/
private def checkedFunArity(suffixStart: Int) =
private def checkedFunArity(suffixStart: Int): Int =
if suffixStart == 0
|| isPreceded("Context", suffixStart)
|| isPreceded("Erased", suffixStart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
merge(this.boundsMap, that.boundsMap, mergeEntries),
merge(this.lowerMap, that.lowerMap, mergeParams),
merge(this.upperMap, that.upperMap, mergeParams))
}.reporting(i"constraint merge $this with $other = $result", constr)
}.showing(i"constraint merge $this with $other = $result", constr)

def rename(tl: TypeLambda)(using Context): OrderingConstraint = {
assert(contains(tl))
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ object SymDenotations {
case _ =>
Nil

ensureCompleted()
if rawParamss.isEmpty then recurWithoutParamss(info)
else recurWithParamss(info, rawParamss)
end paramSymss
Expand All @@ -339,6 +340,7 @@ object SymDenotations {
case (fst :: Nil) :: _ => fst
case _ => NoSymbol
assert(isAllOf(ExtensionMethod))
ensureCompleted()
leadParam(rawParamss)

/** The denotation is completed: info is not a lazy type and attributes have defined values */
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ object TypeOps:
}
if needsRefinement then
RefinedType(parent, decl.name, decl.info)
.reporting(i"add ref $parent $decl --> " + result, typr)
.showing(i"add ref $parent $decl --> " + result, typr)
else parent
}

Expand Down
8 changes: 3 additions & 5 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
private def useSymbol(tree: untpd.Tree) =
tree.hasType && tree.symbol.exists && ctx.settings.YprintSyms.value

protected def nameIdText[T >: Untyped](tree: NameTree[T], dropExtension: Boolean = false): Text =
protected def nameIdText[T >: Untyped](tree: NameTree[T]): Text =
if (tree.hasType && tree.symbol.exists) {
var str = nameString(tree.symbol)
if tree.symbol.is(ExtensionMethod) && dropExtension && str.startsWith("extension_") then
str = str.drop("extension_".length)
val str = nameString(tree.symbol)
tree match {
case tree: RefTree => withPos(str, tree.sourcePos)
case tree: MemberDef => withPos(str, tree.sourcePos.withSpan(tree.nameSpan))
Expand Down Expand Up @@ -808,7 +806,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case vparams1 :: rest =>
(vparams1, rest)
(keywordStr("extension") ~~ paramsText(leadingParams)
~~ (defKeyword ~~ valDefText(nameIdText(tree, dropExtension = true))).close,
~~ (defKeyword ~~ valDefText(nameIdText(tree))).close,
otherParamss)
else (defKeyword ~~ valDefText(nameIdText(tree)), tree.vparamss)

Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ object QuoteContextImpl {
def showDecompiledTree(tree: tpd.Tree)(using Context): String = {
val qctx: QuoteContextImpl = new QuoteContextImpl(MacroExpansion.context(tree))
if ctx.settings.color.value == "always" then
qctx.reflect.TreeMethodsImpl.extension_showAnsiColored(tree)
qctx.reflect.TreeMethodsImpl.temporaryShowAnsiColored(tree)
else
qctx.reflect.TreeMethodsImpl.extension_show(tree)
qctx.reflect.TreeMethodsImpl.temporaryShow(tree)
}

private[dotty] def checkScopeId(id: ScopeId)(using Context): Unit =
Expand Down Expand Up @@ -2535,8 +2535,8 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
object FlagsMethodsImpl extends FlagsMethods:
extension (self: Flags):
def is(that: Flags): Boolean = self.isAllOf(that)
def |(that: Flags): Flags = dotc.core.Flags.extension_|(self)(that)
def &(that: Flags): Flags = dotc.core.Flags.extension_&(self)(that)
def |(that: Flags): Flags = dotc.core.Flags.or(self, that) // TODO: Replace with dotc.core.Flags.|(self)(that) once extension names have stabilized
def &(that: Flags): Flags = dotc.core.Flags.and(self, that)// TODO: Replace with dotc.core.Flags.&(self)(that) once extension names have stabilized
def showExtractors: String =
Extractors.showFlags(using QuoteContextImpl.this)(self)
def show: String =
Expand Down
Loading