Skip to content

Commit b3cf4e7

Browse files
Merge pull request #9337 from dotty-staging/use-new-extension-syntax-in-compiler
Use new extension syntax in compiler
2 parents 701ccdb + 93a2a60 commit b3cf4e7

17 files changed

+66
-58
lines changed

compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
123123
* All interfaces implemented by a class, except for those inherited through the superclass.
124124
* Redundant interfaces are removed unless there is a super call to them.
125125
*/
126-
def (sym: Symbol).superInterfaces: List[Symbol] = {
126+
extension (sym: Symbol) def superInterfaces: List[Symbol] = {
127127
val directlyInheritedTraits = sym.directlyInheritedTraits
128128
val directlyInheritedTraitsSet = directlyInheritedTraits.toSet
129129
val allBaseClasses = directlyInheritedTraits.iterator.flatMap(_.asClass.baseClasses.drop(1)).toSet
@@ -275,8 +275,9 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
275275
* object T { def f { object U } }
276276
* the owner of U is T, so UModuleClass.isStatic is true. Phase travel does not help here.
277277
*/
278-
private def (sym: Symbol).isOriginallyStaticOwner: Boolean =
279-
sym.is(PackageClass) || sym.is(ModuleClass) && sym.originalOwner.originalLexicallyEnclosingClass.isOriginallyStaticOwner
278+
extension (sym: Symbol):
279+
private def isOriginallyStaticOwner: Boolean =
280+
sym.is(PackageClass) || sym.is(ModuleClass) && sym.originalOwner.originalLexicallyEnclosingClass.isOriginallyStaticOwner
280281

281282
/**
282283
* Return the Java modifiers for the given symbol.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ object Trees {
333333
def namedType: NamedType = tpe.asInstanceOf[NamedType]
334334
}
335335

336-
def (mdef: untpd.DefTree).mods: untpd.Modifiers = mdef.rawMods
336+
extension (mdef: untpd.DefTree) def mods: untpd.Modifiers = mdef.rawMods
337337

338338
abstract class NamedDefTree[-T >: Untyped](implicit @constructorOnly src: SourceFile) extends NameTree[T] with DefTree[T] {
339339
type ThisTree[-T >: Untyped] <: NamedDefTree[T]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
775775
}
776776

777777
/** Is there a subtree of this tree that satisfies predicate `p`? */
778-
def (tree: Tree) existsSubTree(p: Tree => Boolean)(using Context): Boolean = {
778+
extension (tree: Tree) def existsSubTree(p: Tree => Boolean)(using Context): Boolean = {
779779
val acc = new UntypedTreeAccumulator[Boolean] {
780780
def apply(x: Boolean, t: Tree)(using Context) = x || p(t) || foldOver(x, t)
781781
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ object Decorators {
2121
* a simple collective extension.
2222
*/
2323
implicit object PreNamedString:
24-
def (pn: PreName).toTypeName: TypeName = pn match
24+
extension (pn: PreName) def toTypeName: TypeName = pn match
2525
case s: String => typeName(s)
2626
case n: Name => n.toTypeName
27-
def (pn: PreName).toTermName: TermName = pn match
27+
extension (pn: PreName) def toTermName: TermName = pn match
2828
case s: String => termName(s)
2929
case n: Name => n.toTermName
3030

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object Flags {
2222

2323
type Flag = opaques.Flag
2424

25-
extension on (x: FlagSet) {
25+
extension (x: FlagSet) {
2626

2727
def bits: Long = opaques.toBits(x)
2828

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
727727
}
728728

729729
/** Print modifiers from symbols if tree has type, overriding the behavior in Trees. */
730-
def (mdef: untpd.DefTree).mods: untpd.Modifiers =
730+
extension (mdef: untpd.DefTree) def mods: untpd.Modifiers =
731731
if mdef.hasType then Modifiers(mdef.symbol) else mdef.rawMods
732732

733733
private def Modifiers(sym: Symbol): Modifiers = untpd.Modifiers(

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ class ExtractSemanticDB extends Phase:
260260

261261
case _ => None
262262

263-
private inline def (tpe: Types.Type) isAnnotatedByUnchecked(using Context) = tpe match
264-
case Types.AnnotatedType(_, annot) => annot.symbol == defn.UncheckedAnnot
265-
case _ => false
263+
extension (tpe: Types.Type):
264+
private inline def isAnnotatedByUnchecked(using Context) = tpe match
265+
case Types.AnnotatedType(_, annot) => annot.symbol == defn.UncheckedAnnot
266+
case _ => false
266267

267268
def collectPats(pat: Tree): List[Tree] =
268269

@@ -282,11 +283,12 @@ class ExtractSemanticDB extends Phase:
282283

283284
end PatternValDef
284285

285-
private def (tree: NamedDefTree) adjustedNameSpan(using Context): Span =
286-
if tree.span.exists && tree.name.isAnonymousFunctionName || tree.name.isAnonymousClassName
287-
Span(tree.span.point)
288-
else
289-
tree.nameSpan
286+
extension (tree: NamedDefTree):
287+
private def adjustedNameSpan(using Context): Span =
288+
if tree.span.exists && tree.name.isAnonymousFunctionName || tree.name.isAnonymousClassName
289+
Span(tree.span.point)
290+
else
291+
tree.nameSpan
290292

291293
/** Add semanticdb name of the given symbol to string builder */
292294
private def addSymName(b: StringBuilder, sym: Symbol)(using Context): Unit =
@@ -496,12 +498,14 @@ class ExtractSemanticDB extends Phase:
496498
val start = if idx >= 0 then idx else span.start
497499
Span(start, start + sym.name.show.length, start)
498500

499-
private inline def (list: List[List[ValDef]]) isSingleArg = list match
500-
case (_::Nil)::Nil => true
501-
case _ => false
501+
extension (list: List[List[ValDef]]):
502+
private inline def isSingleArg = list match
503+
case (_::Nil)::Nil => true
504+
case _ => false
502505

503-
private def (tree: DefDef) isSetterDef(using Context): Boolean =
504-
tree.name.isSetterName && tree.mods.is(Accessor) && tree.vparamss.isSingleArg
506+
extension (tree: DefDef):
507+
private def isSetterDef(using Context): Boolean =
508+
tree.name.isSetterName && tree.mods.is(Accessor) && tree.vparamss.isSingleArg
505509

506510
private def findGetters(ctorParams: Set[Names.TermName], body: List[Tree])(using Context): Map[Names.TermName, ValDef] =
507511
if ctorParams.isEmpty || body.isEmpty then
@@ -525,26 +529,27 @@ class ExtractSemanticDB extends Phase:
525529
else limit
526530
Span(start max limit, end)
527531

528-
private extension on (span: Span):
529-
def hasLength: Boolean = span.start != span.end
530-
def zeroLength: Boolean = span.start == span.end
532+
extension (span: Span):
533+
private def hasLength: Boolean = span.start != span.end
534+
private def zeroLength: Boolean = span.start == span.end
531535

532536
/**Consume head while not an import statement.
533537
* Returns the rest of the list after the first import, or else the empty list
534538
*/
535-
@tailrec
536-
private def (body: List[Tree]) foreachUntilImport(op: Tree => Unit): List[Tree] = body match
537-
case ((_: Import) :: rest) => rest
538-
case stat :: rest =>
539-
op(stat)
540-
rest.foreachUntilImport(op)
541-
case Nil => Nil
542-
543-
private def (sym: Symbol) adjustIfCtorTyparam(using Context) =
544-
if sym.isType && sym.owner.exists && sym.owner.isConstructor
545-
matchingMemberType(sym, sym.owner.owner)
546-
else
547-
sym
539+
extension (body: List[Tree]):
540+
@tailrec private def foreachUntilImport(op: Tree => Unit): List[Tree] = body match
541+
case ((_: Import) :: rest) => rest
542+
case stat :: rest =>
543+
op(stat)
544+
rest.foreachUntilImport(op)
545+
case Nil => Nil
546+
547+
extension (sym: Symbol):
548+
private def adjustIfCtorTyparam(using Context) =
549+
if sym.isType && sym.owner.exists && sym.owner.isConstructor
550+
matchingMemberType(sym, sym.owner.owner)
551+
else
552+
sym
548553

549554
private inline def matchingMemberType(ctorTypeParam: Symbol, classSym: Symbol)(using Context) =
550555
classSym.info.member(ctorTypeParam.name).symbol

compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ object Scala3:
105105

106106
val setterName = sym.name.toTermName.setterName
107107

108-
inline def (t: Type) matchingType = t.paramInfoss match
108+
extension (t: Type) inline def matchingType = t.paramInfoss match
109109
case (arg::Nil)::Nil => t.resultType == defn.UnitType && arg == sym.info
110110
case _ => false
111111

@@ -139,9 +139,10 @@ object Scala3:
139139

140140
end LocalSymbol
141141

142-
private inline def (char: Char) isGlobalTerminal = (char: @switch) match
143-
case '/' | '.' | '#' | ']' | ')' => true
144-
case _ => false
142+
extension (char: Char):
143+
private inline def isGlobalTerminal = (char: @switch) match
144+
case '/' | '.' | '#' | ']' | ')' => true
145+
case _ => false
145146

146147
extension StringOps on (symbol: String):
147148

compiler/src/dotty/tools/dotc/semanticdb/Tools.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,5 @@ object Tools:
137137
sb.append(if occ.role.isReference then " -> " else " <- ").append(occ.symbol).nl
138138
end processOccurrence
139139

140-
private inline def (sb: StringBuilder) nl = sb.append(System.lineSeparator)
140+
extension (sb: StringBuilder):
141+
private inline def nl = sb.append(System.lineSeparator)

compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ object ExplicitOuter {
324324
tpe
325325
}
326326

327-
def (sym: Symbol).isOuterParamAccessor(using Context): Boolean =
327+
extension (sym: Symbol) def isOuterParamAccessor(using Context): Boolean =
328328
sym.is(ParamAccessor) && sym.name == nme.OUTER
329329

330330
def outer(using Context): OuterOps = new OuterOps(ctx)

compiler/src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class SymUtils(val self: Symbol) extends AnyVal {
241241
* all refinements for opaque types.
242242
*/
243243
def declaredSelfTypeAsSeenFrom(site: Type)(using Context) =
244-
def (tp: Type).stripOpaques: Type = tp match
244+
extension (tp: Type) def stripOpaques: Type = tp match
245245
case RefinedType(parent, name, _) if self.info.decl(name).symbol.isOpaqueAlias =>
246246
parent.stripOpaques
247247
case _ =>

compiler/src/dotty/tools/dotc/transform/init/Effects.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object Effects {
6161

6262
// ------------------ operations on effects ------------------
6363

64-
def (eff: Effect) toEffs: Effects = Effects.empty + eff
64+
extension (eff: Effect) def toEffs: Effects = Effects.empty + eff
6565

6666
def asSeenFrom(eff: Effect, thisValue: Potential, currentClass: ClassSymbol, outer: Potentials)(implicit env: Env): Effects =
6767
trace(eff.show + " asSeenFrom " + thisValue.show + ", current = " + currentClass.show + ", outer = " + Potentials.show(outer), init, effs => show(effs.asInstanceOf[Effects])) { eff match {

compiler/src/dotty/tools/dotc/transform/init/Potentials.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ object Potentials {
147147

148148
// ------------------ operations on potentials ------------------
149149

150-
def (pot: Potential) toPots: Potentials = Potentials.empty + pot
150+
extension (pot: Potential) def toPots: Potentials = Potentials.empty + pot
151151

152-
def (ps: Potentials) select (symbol: Symbol, source: Tree)(using Context): Summary =
152+
extension (ps: Potentials) def select (symbol: Symbol, source: Tree)(using Context): Summary =
153153
ps.foldLeft(Summary.empty) { case ((pots, effs), pot) =>
154154
// max potential length
155155
// TODO: it can be specified on a project basis via compiler options
@@ -166,7 +166,7 @@ object Potentials {
166166
(pots + FieldReturn(pot, symbol)(source), effs + FieldAccess(pot, symbol)(source))
167167
}
168168

169-
def (ps: Potentials) promote(source: Tree): Effects = ps.map(Promote(_)(source))
169+
extension (ps: Potentials) def promote(source: Tree): Effects = ps.map(Promote(_)(source))
170170

171171
def asSeenFrom(pot: Potential, thisValue: Potential, currentClass: ClassSymbol, outer: Potentials)(implicit env: Env): Potentials =
172172
trace(pot.show + " asSeenFrom " + thisValue.show + ", current = " + currentClass.show + ", outer = " + show(outer), init, pots => show(pots.asInstanceOf[Potentials])) { pot match {

compiler/src/dotty/tools/dotc/transform/init/Summary.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,18 @@ object Summary {
8686
s"([$pots], [$effs])"
8787
}
8888

89-
def (summary1: Summary) union (summary2: Summary): Summary =
89+
extension (summary1: Summary) def union (summary2: Summary): Summary =
9090
(summary1._1 ++ summary2._1, summary1._2 ++ summary2._2)
9191

92-
def (summary: Summary) + (pot: Potential): Summary =
92+
extension (summary: Summary) def + (pot: Potential): Summary =
9393
(summary._1 + pot, summary._2)
9494

95-
def (summary: Summary) + (eff: Effect): Summary =
95+
extension (summary: Summary) def + (eff: Effect): Summary =
9696
(summary._1, summary._2 + eff)
9797

98-
def (summary: Summary) withPots (pots: Potentials): Summary =
98+
extension (summary: Summary) def withPots (pots: Potentials): Summary =
9999
(summary._1 ++ pots, summary._2)
100100

101-
def (summary: Summary) withEffs (effs: Effects): Summary =
101+
extension (summary: Summary) def withEffs (effs: Effects): Summary =
102102
(summary._1, summary._2 ++ effs)
103103
}

compiler/src/dotty/tools/dotc/transform/init/Util.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object Util {
1919
traceIndented(s"<== ${msg}", printer)
2020
}
2121

22-
def (symbol: Symbol) isInternal(using Context): Boolean =
22+
extension (symbol: Symbol) def isInternal(using Context): Boolean =
2323
!symbol.defTree.isEmpty
2424

2525
def resolve(cls: ClassSymbol, sym: Symbol)(using Context): Symbol =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ trait ImportSuggestions:
266266
/** The `ref` parts of this list of pairs, discarding subsequent elements that
267267
* have the same String part. Elements are sorted by their String parts.
268268
*/
269-
def (refs: List[(TermRef, String)]).distinctRefs(using Context): List[TermRef] = refs match
269+
extension (refs: List[(TermRef, String)]) def distinctRefs(using Context): List[TermRef] = refs match
270270
case (ref, str) :: refs1 =>
271271
ref :: refs1.dropWhile(_._2 == str).distinctRefs
272272
case Nil =>
@@ -276,7 +276,7 @@ trait ImportSuggestions:
276276
* `compare` is a partial order. If there's a tie, we take elements
277277
* in the order thy appear in the list.
278278
*/
279-
def (refs: List[TermRef]).best(n: Int)(using Context): List[TermRef] =
279+
extension (refs: List[TermRef]) def best(n: Int)(using Context): List[TermRef] =
280280
val top = new Array[TermRef](n)
281281
var filled = 0
282282
val rest = new mutable.ListBuffer[TermRef]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ object RefChecks {
465465

466466
def isImplemented(mbr: Symbol) =
467467
val mbrType = clazz.thisType.memberInfo(mbr)
468-
def (sym: Symbol).isConcrete = sym.exists && !sym.is(Deferred)
468+
extension (sym: Symbol) def isConcrete = sym.exists && !sym.is(Deferred)
469469
clazz.nonPrivateMembersNamed(mbr.name)
470470
.filterWithPredicate(
471471
impl => impl.symbol.isConcrete && mbrType.matchesLoosely(impl.info))

0 commit comments

Comments
 (0)