Skip to content

Commit eddd4da

Browse files
authored
Merge pull request #10128 from dotty-staging/drop-extension-mangling
Drop extension_ prefix for extension methods
2 parents 67bd66f + 98bb872 commit eddd4da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+403
-431
lines changed

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class JSCodeGen()(using genCtx: Context) {
206206
withScopedVars(
207207
currentClassSym := sym
208208
) {
209-
val tree = if (isJSType(sym)) {
209+
val tree = if (sym.isJSType) {
210210
if (!sym.is(Trait) && sym.isNonNativeJSClass)
211211
genNonNativeJSClass(td)
212212
else
@@ -702,7 +702,7 @@ class JSCodeGen()(using genCtx: Context) {
702702
Nil
703703
} else {
704704
val moduleClass = module.moduleClass
705-
if (!isJSType(moduleClass))
705+
if (!moduleClass.isJSType)
706706
genStaticForwardersFromModuleClass(existingMembers, moduleClass)
707707
else
708708
Nil
@@ -1678,7 +1678,7 @@ class JSCodeGen()(using genCtx: Context) {
16781678
}
16791679

16801680
fun match {
1681-
case _ if isJSDefaultParam(sym) =>
1681+
case _ if sym.isJSDefaultParam =>
16821682
js.Transient(UndefinedParam)(toIRType(sym.info.finalResultType))
16831683

16841684
case Select(Super(_, _), _) =>
@@ -1771,7 +1771,7 @@ class JSCodeGen()(using genCtx: Context) {
17711771
} else /*if (translatedAnonFunctions contains tpe.typeSymbol) {
17721772
val functionMaker = translatedAnonFunctions(tpe.typeSymbol)
17731773
functionMaker(args map genExpr)
1774-
} else*/ if (isJSType(clsSym)) {
1774+
} else*/ if (clsSym.isJSType) {
17751775
genNewJSClass(tree)
17761776
} else {
17771777
toTypeRef(tpe) match {
@@ -2406,7 +2406,7 @@ class JSCodeGen()(using genCtx: Context) {
24062406
* and `-0.0`, see Javadoc (scala-dev#329, scala-js#2799).
24072407
*/
24082408
val mustUseAnyComparator: Boolean = {
2409-
isJSType(lsym) || isJSType(rsym) || {
2409+
lsym.isJSType || rsym.isJSType || {
24102410
val p = ctx.platform
24112411
p.isMaybeBoxed(lsym) && p.isMaybeBoxed(rsym) && {
24122412
val areSameFinals = lsym.is(Final) && rsym.is(Final) && (ltpe =:= rtpe)
@@ -2603,7 +2603,7 @@ class JSCodeGen()(using genCtx: Context) {
26032603

26042604
if (isMethodStaticInIR(sym)) {
26052605
genApplyStatic(sym, genActualArgs(sym, args))
2606-
} else if (isJSType(sym.owner)) {
2606+
} else if (sym.owner.isJSType) {
26072607
if (!sym.owner.isNonNativeJSClass || sym.isJSExposed)
26082608
genApplyJSMethodGeneric(sym, genExprOrGlobalScope(receiver), genActualJSArgs(sym, args), isStat)(tree.sourcePos)
26092609
else
@@ -2689,13 +2689,13 @@ class JSCodeGen()(using genCtx: Context) {
26892689
}
26902690
}
26912691

2692-
if (isJSGetter(sym)) {
2692+
if (sym.isJSGetter) {
26932693
assert(noSpread && argc == 0)
26942694
genSelectGet(jsFunName)
2695-
} else if (isJSSetter(sym)) {
2695+
} else if (sym.isJSSetter) {
26962696
assert(noSpread && argc == 1)
26972697
genSelectSet(jsFunName, requireNotSpread(args.head))
2698-
} else if (isJSBracketAccess(sym)) {
2698+
} else if (sym.isJSBracketAccess) {
26992699
assert(noSpread && (argc == 1 || argc == 2),
27002700
s"@JSBracketAccess methods should have 1 or 2 non-varargs arguments")
27012701
(args: @unchecked) match {
@@ -2704,7 +2704,7 @@ class JSCodeGen()(using genCtx: Context) {
27042704
case List(keyArg, valueArg) =>
27052705
genSelectSet(requireNotSpread(keyArg), requireNotSpread(valueArg))
27062706
}
2707-
} else if (isJSBracketCall(sym)) {
2707+
} else if (sym.isJSBracketCall) {
27082708
val (methodName, actualArgs) = extractFirstArg(args)
27092709
genCall(methodName, actualArgs)
27102710
} else {
@@ -3187,7 +3187,7 @@ class JSCodeGen()(using genCtx: Context) {
31873187

31883188
val sym = to.typeSymbol
31893189

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

32183218
if (sym == defn.ObjectClass) {
32193219
js.BinaryOp(js.BinaryOp.!==, value, js.Null())
3220-
} else if (isJSType(sym)) {
3220+
} else if (sym.isJSType) {
32213221
if (sym.is(Trait)) {
32223222
report.error(
32233223
s"isInstanceOf[${sym.fullName}] not supported because it is a JS trait",
@@ -3347,7 +3347,7 @@ class JSCodeGen()(using genCtx: Context) {
33473347
arg match {
33483348
case Literal(value) if value.tag == Constants.ClazzTag =>
33493349
val classSym = value.typeValue.typeSymbol
3350-
if (isJSType(classSym) && !classSym.is(Trait) && !classSym.is(ModuleClass))
3350+
if (classSym.isJSType && !classSym.is(Trait) && !classSym.is(ModuleClass))
33513351
classSym
33523352
else
33533353
fail()
@@ -3999,7 +3999,7 @@ class JSCodeGen()(using genCtx: Context) {
39993999
} else {
40004000
val cls = encodeClassName(sym)
40014001
val tree =
4002-
if (isJSType(sym)) js.LoadJSModule(cls)
4002+
if (sym.isJSType) js.LoadJSModule(cls)
40034003
else js.LoadModule(cls)
40044004
MaybeGlobalScope.NotGlobalScope(tree)
40054005
}

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ object desugar {
814814
}
815815

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

819819
/** Expand
820820
*
@@ -895,7 +895,7 @@ object desugar {
895895
mdef.tparams.head.srcPos)
896896
defDef(
897897
cpy.DefDef(mdef)(
898-
name = normalizeName(mdef, ext).toExtensionName,
898+
name = normalizeName(mdef, ext).asTermName,
899899
tparams = ext.tparams ++ mdef.tparams,
900900
vparamss = mdef.vparamss match
901901
case vparams1 :: vparamss1 if mdef.name.isRightAssocOperatorName =>
@@ -928,10 +928,10 @@ object desugar {
928928

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

955-
/** Invent a name for an anonympus given or extension of type or template `impl`. */
948+
/** Invent a name for an anonympus given of type or template `impl`. */
956949
def inventGivenOrExtensionName(impl: Tree)(using Context): SimpleName =
957950
val str = impl match
958951
case impl: Template =>
959952
if impl.parents.isEmpty then
960-
impl.body.find {
961-
case dd: DefDef if dd.mods.is(ExtensionMethod) => true
962-
case _ => false
963-
}
964-
match
965-
case Some(DefDef(name, _, (vparam :: _) :: _, _, _)) =>
966-
s"extension_${name}_${inventTypeName(vparam.tpt)}"
967-
case _ =>
968-
report.error(AnonymousInstanceCannotBeEmpty(impl), impl.srcPos)
969-
nme.ERROR.toString
953+
report.error(AnonymousInstanceCannotBeEmpty(impl), impl.srcPos)
954+
nme.ERROR.toString
970955
else
971956
impl.parents.map(inventTypeName(_)).mkString("given_", "_", "")
972957
case impl: Tree =>

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,7 @@ object Trees {
343343
if (rawMods.is(Synthetic) || span.isSynthetic || name.toTermName == nme.ERROR) Span(point)
344344
else {
345345
val realName = name.stripModuleClassSuffix.lastPart
346-
var length = realName.length
347-
if (rawMods.is(ExtensionMethod) || symbol.is(ExtensionMethod)) && name.isExtensionName then
348-
length -= "extension_".length
349-
Span(point, point + length, point)
346+
Span(point, point + realName.length, point)
350347
}
351348
}
352349
else span

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
2424
}
2525

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

3338
object TypedSplice {
34-
def apply(tree: tpd.Tree)(using Context): TypedSplice =
35-
new TypedSplice(tree)(ctx.owner) {}
39+
def apply(tree: tpd.Tree, isExtensionReceiver: Boolean = false)(using Context): TypedSplice =
40+
new TypedSplice(tree)(ctx.owner, isExtensionReceiver) {}
3641
}
3742

3843
/** mods object name impl */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ trait ConstraintHandling {
159159
val bound = adjust(rawBound)
160160
bound.exists
161161
&& addOneBound(param, bound, isUpper) && others.forall(addOneBound(_, bound, isUpper))
162-
.reporting(i"added $description = $result$location", constr)
162+
.showing(i"added $description = $result$location", constr)
163163
end addBoundTransitively
164164

165165
protected def addLess(p1: TypeParamRef, p2: TypeParamRef)(using Context): Boolean = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ object Decorators {
232232
}
233233

234234
extension [T](x: T)
235-
def reporting(
235+
def showing(
236236
op: WrappedResult[T] ?=> String,
237237
printer: config.Printers.Printer = config.Printers.default): T = {
238238
printer.println(op(using WrappedResult(x)))

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ class Definitions {
229229
@tu lazy val Compiletime_requireConst: Symbol = CompiletimePackageObject.requiredMethod("requireConst")
230230
@tu lazy val Compiletime_constValue : Symbol = CompiletimePackageObject.requiredMethod("constValue")
231231
@tu lazy val Compiletime_constValueOpt: Symbol = CompiletimePackageObject.requiredMethod("constValueOpt")
232-
@tu lazy val Compiletime_code : Symbol = CompiletimePackageObject.requiredMethod("extension_code")
233232
@tu lazy val Compiletime_summonFrom : Symbol = CompiletimePackageObject.requiredMethod("summonFrom")
234233
@tu lazy val CompiletimeTestingPackageObject: Symbol = requiredModule("scala.compiletime.testing.package")
235234
@tu lazy val CompiletimeTesting_typeChecks: Symbol = CompiletimeTestingPackageObject.requiredMethod("typeChecks")

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ object Flags {
152152
def flagsString: String = x.flagStrings("").mkString(" ")
153153
}
154154

155+
// Temporary while extension names are in flux
156+
def or(x1: FlagSet, x2: FlagSet) = x1 | x2
157+
def and(x1: FlagSet, x2: FlagSet) = x1 & x2
158+
155159
def termFlagSet(x: Long) = FlagSet(TERMS | x)
156160

157161
private inline val TYPESHIFT = 2

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ final class ProperGadtConstraint private(
125125

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

131131
override def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean = {
@@ -158,7 +158,7 @@ final class ProperGadtConstraint private(
158158
case bound =>
159159
addBoundTransitively(symTvar.origin, bound, isUpper)
160160
}
161-
).reporting({
161+
).showing({
162162
val descr = if (isUpper) "upper" else "lower"
163163
val op = if (isUpper) "<:" else ">:"
164164
i"adding $descr bound $sym $op $bound = $result"
@@ -187,7 +187,7 @@ final class ProperGadtConstraint private(
187187
case tb => tb
188188
}
189189
retrieveBounds
190-
//.reporting(i"gadt bounds $sym: $result", gadts)
190+
//.showing(i"gadt bounds $sym: $result", gadts)
191191
//.ensuring(containsNoInternalTypes(_))
192192
}
193193

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,7 @@ object Mode {
112112

113113
/** Are we in a quote in a pattern? */
114114
val QuotedPattern: Mode = newMode(25, "QuotedPattern")
115+
116+
/** Are we typechecking the rhs of an extension method? */
117+
val InExtensionMethod: Mode = newMode(26, "InExtensionMethod")
115118
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,10 @@ object NameOps {
145145
case name: SimpleName => name.startsWith("extension_")
146146
case _ => false
147147

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

151-
/** Drop `extension_` in front of this name, if it has this prefix */
152-
def dropExtension = name match
153-
case name: SimpleName if name.startsWith("extension_") =>
154-
name.drop("extension_".length)
155-
case _ =>
156-
name
157-
158152
/** The expanded name.
159153
* This is the fully qualified name of `base` with `ExpandPrefixName` as separator,
160154
* followed by `kind` and the name.
@@ -218,7 +212,7 @@ object NameOps {
218212
/** Same as `funArity`, except that it returns -1 if the prefix
219213
* is not one of "", "Context", "Erased", "ErasedContext"
220214
*/
221-
private def checkedFunArity(suffixStart: Int) =
215+
private def checkedFunArity(suffixStart: Int): Int =
222216
if suffixStart == 0
223217
|| isPreceded("Context", suffixStart)
224218
|| isPreceded("Erased", suffixStart)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
506506
merge(this.boundsMap, that.boundsMap, mergeEntries),
507507
merge(this.lowerMap, that.lowerMap, mergeParams),
508508
merge(this.upperMap, that.upperMap, mergeParams))
509-
}.reporting(i"constraint merge $this with $other = $result", constr)
509+
}.showing(i"constraint merge $this with $other = $result", constr)
510510

511511
def rename(tl: TypeLambda)(using Context): OrderingConstraint = {
512512
assert(contains(tl))

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ object SymDenotations {
325325
case _ =>
326326
Nil
327327

328+
ensureCompleted()
328329
if rawParamss.isEmpty then recurWithoutParamss(info)
329330
else recurWithParamss(info, rawParamss)
330331
end paramSymss
@@ -339,6 +340,7 @@ object SymDenotations {
339340
case (fst :: Nil) :: _ => fst
340341
case _ => NoSymbol
341342
assert(isAllOf(ExtensionMethod))
343+
ensureCompleted()
342344
leadParam(rawParamss)
343345

344346
/** The denotation is completed: info is not a lazy type and attributes have defined values */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ object TypeOps:
370370
}
371371
if needsRefinement then
372372
RefinedType(parent, decl.name, decl.info)
373-
.reporting(i"add ref $parent $decl --> " + result, typr)
373+
.showing(i"add ref $parent $decl --> " + result, typr)
374374
else parent
375375
}
376376

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
752752
private def useSymbol(tree: untpd.Tree) =
753753
tree.hasType && tree.symbol.exists && ctx.settings.YprintSyms.value
754754

755-
protected def nameIdText[T >: Untyped](tree: NameTree[T], dropExtension: Boolean = false): Text =
755+
protected def nameIdText[T >: Untyped](tree: NameTree[T]): Text =
756756
if (tree.hasType && tree.symbol.exists) {
757-
var str = nameString(tree.symbol)
758-
if tree.symbol.is(ExtensionMethod) && dropExtension && str.startsWith("extension_") then
759-
str = str.drop("extension_".length)
757+
val str = nameString(tree.symbol)
760758
tree match {
761759
case tree: RefTree => withPos(str, tree.sourcePos)
762760
case tree: MemberDef => withPos(str, tree.sourcePos.withSpan(tree.nameSpan))
@@ -808,7 +806,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
808806
case vparams1 :: rest =>
809807
(vparams1, rest)
810808
(keywordStr("extension") ~~ paramsText(leadingParams)
811-
~~ (defKeyword ~~ valDefText(nameIdText(tree, dropExtension = true))).close,
809+
~~ (defKeyword ~~ valDefText(nameIdText(tree))).close,
812810
otherParamss)
813811
else (defKeyword ~~ valDefText(nameIdText(tree)), tree.vparamss)
814812

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ object QuoteContextImpl {
2929
def showDecompiledTree(tree: tpd.Tree)(using Context): String = {
3030
val qctx: QuoteContextImpl = new QuoteContextImpl(MacroExpansion.context(tree))
3131
if ctx.settings.color.value == "always" then
32-
qctx.reflect.TreeMethodsImpl.extension_showAnsiColored(tree)
32+
qctx.reflect.TreeMethodsImpl.temporaryShowAnsiColored(tree)
3333
else
34-
qctx.reflect.TreeMethodsImpl.extension_show(tree)
34+
qctx.reflect.TreeMethodsImpl.temporaryShow(tree)
3535
}
3636

3737
// TODO Explore more fine grained scope ids.
@@ -2531,8 +2531,8 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
25312531
object FlagsMethodsImpl extends FlagsMethods:
25322532
extension (self: Flags):
25332533
def is(that: Flags): Boolean = self.isAllOf(that)
2534-
def |(that: Flags): Flags = dotc.core.Flags.extension_|(self)(that)
2535-
def &(that: Flags): Flags = dotc.core.Flags.extension_&(self)(that)
2534+
def |(that: Flags): Flags = dotc.core.Flags.or(self, that) // TODO: Replace with dotc.core.Flags.|(self)(that) once extension names have stabilized
2535+
def &(that: Flags): Flags = dotc.core.Flags.and(self, that)// TODO: Replace with dotc.core.Flags.&(self)(that) once extension names have stabilized
25362536
def showExtractors: String =
25372537
Extractors.showFlags(using QuoteContextImpl.this)(self)
25382538
def show: String =

0 commit comments

Comments
 (0)