Skip to content

Commit fce9cac

Browse files
committed
Drop extension_ name mangling
For the moment will still support extension_name references to extension methods in order to work with non-bootstrapped libraries. But we do not generate these names anymore.
1 parent 418650f commit fce9cac

Some content is hidden

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

43 files changed

+145
-197
lines changed

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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/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/NameOps.scala

Lines changed: 1 addition & 7 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.

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,8 +2535,8 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
25352535
object FlagsMethodsImpl extends FlagsMethods:
25362536
extension (self: Flags):
25372537
def is(that: Flags): Boolean = self.isAllOf(that)
2538-
def |(that: Flags): Flags = dotc.core.Flags.extension_|(self)(that)
2539-
def &(that: Flags): Flags = dotc.core.Flags.extension_&(self)(that)
2538+
def |(that: Flags): Flags = dotc.core.Flags.or(self, that) // TODO: Replace with dotc.core.Flags.|(self)(that) once extension names have stabilized
2539+
def &(that: Flags): Flags = dotc.core.Flags.and(self, that)// TODO: Replace with dotc.core.Flags.&(self)(that) once extension names have stabilized
25402540
def showExtractors: String =
25412541
Extractors.showFlags(using QuoteContextImpl.this)(self)
25422542
def show: String =

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ trait Applications extends Compatibility {
13921392
}
13931393

13941394
/** Does `tp` have an extension method named `xname` with this-argument `argType` and
1395-
* result matching `resultType`? `xname` is supposed to start with `extension_`.
1395+
* result matching `resultType`?
13961396
*/
13971397
def hasExtensionMethodNamed(tp: Type, xname: TermName, argType: Type, resultType: Type)(using Context) = {
13981398
def qualifies(mbr: Denotation) =

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ object Implicits:
7575
* method with the selecting name? False otherwise.
7676
*/
7777
def hasExtMethod(tp: Type, expected: Type)(using Context) = expected match
78-
case selProto @ SelectionProto(_: TermName, _, _, _) =>
79-
tp.memberBasedOnFlags(selProto.extensionName, required = ExtensionMethod).exists
78+
case selProto @ SelectionProto(selName: TermName, _, _, _) =>
79+
tp.memberBasedOnFlags(selName, required = ExtensionMethod).exists
80+
|| tp.memberBasedOnFlags(selProto.extensionName, required = ExtensionMethod).exists
8081
case _ => false
8182

8283
def strictEquality(using Context): Boolean =
@@ -993,9 +994,14 @@ trait Implicits:
993994
pt, locked)
994995
}
995996
pt match
996-
case selProto @ SelectionProto(_: TermName, mbrType, _, _) if cand.isExtension =>
997+
case selProto @ SelectionProto(selName: TermName, mbrType, _, _) if cand.isExtension =>
997998
def tryExtension(using Context) =
998-
extMethodApply(untpd.Select(untpdGenerated, selProto.extensionName), argument, mbrType)
999+
val xname =
1000+
if ref.memberBasedOnFlags(selProto.extensionName, required = ExtensionMethod).exists then
1001+
selProto.extensionName
1002+
else
1003+
selName
1004+
extMethodApply(untpd.Select(untpdGenerated, xname), argument, mbrType)
9991005
if cand.isConversion then
10001006
val extensionCtx, conversionCtx = ctx.fresh.setNewTyperState()
10011007
val extensionResult = tryExtension(using extensionCtx)

compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ trait ImportSuggestions:
219219
* applicable to `argType`.
220220
*/
221221
def extensionMethod(site: TermRef, name: TermName, argType: Type): Option[TermRef] =
222-
site.member(name.toExtensionName)
222+
site.member(name)
223223
.alternatives
224224
.map(mbr => TermRef(site, mbr.symbol))
225225
.filter(ref =>
@@ -320,7 +320,7 @@ trait ImportSuggestions:
320320
def importString(ref: TermRef): String =
321321
val imported =
322322
if ref.symbol.is(ExtensionMethod) then
323-
s"${ctx.printer.toTextPrefix(ref.prefix).show}${ref.symbol.name.dropExtension}"
323+
s"${ctx.printer.toTextPrefix(ref.prefix).show}${ref.symbol.name}"
324324
else
325325
ctx.printer.toTextRef(ref).show
326326
s" import $imported"

0 commit comments

Comments
 (0)