Skip to content

Use new extension syntax in compiler #9337

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
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
* All interfaces implemented by a class, except for those inherited through the superclass.
* Redundant interfaces are removed unless there is a super call to them.
*/
def (sym: Symbol).superInterfaces: List[Symbol] = {
extension (sym: Symbol) def superInterfaces: List[Symbol] = {
val directlyInheritedTraits = sym.directlyInheritedTraits
val directlyInheritedTraitsSet = directlyInheritedTraits.toSet
val allBaseClasses = directlyInheritedTraits.iterator.flatMap(_.asClass.baseClasses.drop(1)).toSet
Expand Down Expand Up @@ -275,8 +275,9 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
* object T { def f { object U } }
* the owner of U is T, so UModuleClass.isStatic is true. Phase travel does not help here.
*/
private def (sym: Symbol).isOriginallyStaticOwner: Boolean =
sym.is(PackageClass) || sym.is(ModuleClass) && sym.originalOwner.originalLexicallyEnclosingClass.isOriginallyStaticOwner
extension (sym: Symbol):
private def isOriginallyStaticOwner: Boolean =
sym.is(PackageClass) || sym.is(ModuleClass) && sym.originalOwner.originalLexicallyEnclosingClass.isOriginallyStaticOwner

/**
* Return the Java modifiers for the given symbol.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ object Trees {
def namedType: NamedType = tpe.asInstanceOf[NamedType]
}

def (mdef: untpd.DefTree).mods: untpd.Modifiers = mdef.rawMods
extension (mdef: untpd.DefTree) def mods: untpd.Modifiers = mdef.rawMods

abstract class NamedDefTree[-T >: Untyped](implicit @constructorOnly src: SourceFile) extends NameTree[T] with DefTree[T] {
type ThisTree[-T >: Untyped] <: NamedDefTree[T]
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
}

/** Is there a subtree of this tree that satisfies predicate `p`? */
def (tree: Tree) existsSubTree(p: Tree => Boolean)(using Context): Boolean = {
extension (tree: Tree) def existsSubTree(p: Tree => Boolean)(using Context): Boolean = {
val acc = new UntypedTreeAccumulator[Boolean] {
def apply(x: Boolean, t: Tree)(using Context) = x || p(t) || foldOver(x, t)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ object Decorators {
* a simple collective extension.
*/
implicit object PreNamedString:
def (pn: PreName).toTypeName: TypeName = pn match
extension (pn: PreName) def toTypeName: TypeName = pn match
case s: String => typeName(s)
case n: Name => n.toTypeName
def (pn: PreName).toTermName: TermName = pn match
extension (pn: PreName) def toTermName: TermName = pn match
case s: String => termName(s)
case n: Name => n.toTermName

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object Flags {

type Flag = opaques.Flag

extension on (x: FlagSet) {
extension (x: FlagSet) {

def bits: Long = opaques.toBits(x)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
}

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

private def Modifiers(sym: Symbol): Modifiers = untpd.Modifiers(
Expand Down
63 changes: 34 additions & 29 deletions compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ class ExtractSemanticDB extends Phase:

case _ => None

private inline def (tpe: Types.Type) isAnnotatedByUnchecked(using Context) = tpe match
case Types.AnnotatedType(_, annot) => annot.symbol == defn.UncheckedAnnot
case _ => false
extension (tpe: Types.Type):
private inline def isAnnotatedByUnchecked(using Context) = tpe match
case Types.AnnotatedType(_, annot) => annot.symbol == defn.UncheckedAnnot
case _ => false

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

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

end PatternValDef

private def (tree: NamedDefTree) adjustedNameSpan(using Context): Span =
if tree.span.exists && tree.name.isAnonymousFunctionName || tree.name.isAnonymousClassName
Span(tree.span.point)
else
tree.nameSpan
extension (tree: NamedDefTree):
private def adjustedNameSpan(using Context): Span =
if tree.span.exists && tree.name.isAnonymousFunctionName || tree.name.isAnonymousClassName
Span(tree.span.point)
else
tree.nameSpan

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

private inline def (list: List[List[ValDef]]) isSingleArg = list match
case (_::Nil)::Nil => true
case _ => false
extension (list: List[List[ValDef]]):
private inline def isSingleArg = list match
case (_::Nil)::Nil => true
case _ => false

private def (tree: DefDef) isSetterDef(using Context): Boolean =
tree.name.isSetterName && tree.mods.is(Accessor) && tree.vparamss.isSingleArg
extension (tree: DefDef):
private def isSetterDef(using Context): Boolean =
tree.name.isSetterName && tree.mods.is(Accessor) && tree.vparamss.isSingleArg

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

private extension on (span: Span):
def hasLength: Boolean = span.start != span.end
def zeroLength: Boolean = span.start == span.end
extension (span: Span):
private def hasLength: Boolean = span.start != span.end
private def zeroLength: Boolean = span.start == span.end

/**Consume head while not an import statement.
* Returns the rest of the list after the first import, or else the empty list
*/
@tailrec
private def (body: List[Tree]) foreachUntilImport(op: Tree => Unit): List[Tree] = body match
case ((_: Import) :: rest) => rest
case stat :: rest =>
op(stat)
rest.foreachUntilImport(op)
case Nil => Nil

private def (sym: Symbol) adjustIfCtorTyparam(using Context) =
if sym.isType && sym.owner.exists && sym.owner.isConstructor
matchingMemberType(sym, sym.owner.owner)
else
sym
extension (body: List[Tree]):
@tailrec private def foreachUntilImport(op: Tree => Unit): List[Tree] = body match
case ((_: Import) :: rest) => rest
case stat :: rest =>
op(stat)
rest.foreachUntilImport(op)
case Nil => Nil

extension (sym: Symbol):
private def adjustIfCtorTyparam(using Context) =
if sym.isType && sym.owner.exists && sym.owner.isConstructor
matchingMemberType(sym, sym.owner.owner)
else
sym

private inline def matchingMemberType(ctorTypeParam: Symbol, classSym: Symbol)(using Context) =
classSym.info.member(ctorTypeParam.name).symbol
Expand Down
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ object Scala3:

val setterName = sym.name.toTermName.setterName

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

Expand Down Expand Up @@ -139,9 +139,10 @@ object Scala3:

end LocalSymbol

private inline def (char: Char) isGlobalTerminal = (char: @switch) match
case '/' | '.' | '#' | ']' | ')' => true
case _ => false
extension (char: Char):
private inline def isGlobalTerminal = (char: @switch) match
case '/' | '.' | '#' | ']' | ')' => true
case _ => false

extension StringOps on (symbol: String):

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/semanticdb/Tools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,5 @@ object Tools:
sb.append(if occ.role.isReference then " -> " else " <- ").append(occ.symbol).nl
end processOccurrence

private inline def (sb: StringBuilder) nl = sb.append(System.lineSeparator)
extension (sb: StringBuilder):
private inline def nl = sb.append(System.lineSeparator)
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ object ExplicitOuter {
tpe
}

def (sym: Symbol).isOuterParamAccessor(using Context): Boolean =
extension (sym: Symbol) def isOuterParamAccessor(using Context): Boolean =
sym.is(ParamAccessor) && sym.name == nme.OUTER

def outer(using Context): OuterOps = new OuterOps(ctx)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class SymUtils(val self: Symbol) extends AnyVal {
* all refinements for opaque types.
*/
def declaredSelfTypeAsSeenFrom(site: Type)(using Context) =
def (tp: Type).stripOpaques: Type = tp match
extension (tp: Type) def stripOpaques: Type = tp match
case RefinedType(parent, name, _) if self.info.decl(name).symbol.isOpaqueAlias =>
parent.stripOpaques
case _ =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/init/Effects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object Effects {

// ------------------ operations on effects ------------------

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

def asSeenFrom(eff: Effect, thisValue: Potential, currentClass: ClassSymbol, outer: Potentials)(implicit env: Env): Effects =
trace(eff.show + " asSeenFrom " + thisValue.show + ", current = " + currentClass.show + ", outer = " + Potentials.show(outer), init, effs => show(effs.asInstanceOf[Effects])) { eff match {
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/init/Potentials.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ object Potentials {

// ------------------ operations on potentials ------------------

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

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

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

def asSeenFrom(pot: Potential, thisValue: Potential, currentClass: ClassSymbol, outer: Potentials)(implicit env: Env): Potentials =
trace(pot.show + " asSeenFrom " + thisValue.show + ", current = " + currentClass.show + ", outer = " + show(outer), init, pots => show(pots.asInstanceOf[Potentials])) { pot match {
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/init/Summary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ object Summary {
s"([$pots], [$effs])"
}

def (summary1: Summary) union (summary2: Summary): Summary =
extension (summary1: Summary) def union (summary2: Summary): Summary =
(summary1._1 ++ summary2._1, summary1._2 ++ summary2._2)

def (summary: Summary) + (pot: Potential): Summary =
extension (summary: Summary) def + (pot: Potential): Summary =
(summary._1 + pot, summary._2)

def (summary: Summary) + (eff: Effect): Summary =
extension (summary: Summary) def + (eff: Effect): Summary =
(summary._1, summary._2 + eff)

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

def (summary: Summary) withEffs (effs: Effects): Summary =
extension (summary: Summary) def withEffs (effs: Effects): Summary =
(summary._1, summary._2 ++ effs)
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/init/Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object Util {
traceIndented(s"<== ${msg}", printer)
}

def (symbol: Symbol) isInternal(using Context): Boolean =
extension (symbol: Symbol) def isInternal(using Context): Boolean =
!symbol.defTree.isEmpty

def resolve(cls: ClassSymbol, sym: Symbol)(using Context): Symbol =
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ trait ImportSuggestions:
/** The `ref` parts of this list of pairs, discarding subsequent elements that
* have the same String part. Elements are sorted by their String parts.
*/
def (refs: List[(TermRef, String)]).distinctRefs(using Context): List[TermRef] = refs match
extension (refs: List[(TermRef, String)]) def distinctRefs(using Context): List[TermRef] = refs match
case (ref, str) :: refs1 =>
ref :: refs1.dropWhile(_._2 == str).distinctRefs
case Nil =>
Expand All @@ -276,7 +276,7 @@ trait ImportSuggestions:
* `compare` is a partial order. If there's a tie, we take elements
* in the order thy appear in the list.
*/
def (refs: List[TermRef]).best(n: Int)(using Context): List[TermRef] =
extension (refs: List[TermRef]) def best(n: Int)(using Context): List[TermRef] =
val top = new Array[TermRef](n)
var filled = 0
val rest = new mutable.ListBuffer[TermRef]
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ object RefChecks {

def isImplemented(mbr: Symbol) =
val mbrType = clazz.thisType.memberInfo(mbr)
def (sym: Symbol).isConcrete = sym.exists && !sym.is(Deferred)
extension (sym: Symbol) def isConcrete = sym.exists && !sym.is(Deferred)
clazz.nonPrivateMembersNamed(mbr.name)
.filterWithPredicate(
impl => impl.symbol.isConcrete && mbrType.matchesLoosely(impl.info))
Expand Down