Skip to content

Commit 167db24

Browse files
committed
Fix #9839: Drop old extension method syntax
1 parent 03a02de commit 167db24

File tree

4 files changed

+13
-44
lines changed

4 files changed

+13
-44
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,40 +3286,10 @@ object Parsers {
32863286
makeConstructor(Nil, vparamss, rhs).withMods(mods).setComment(in.getDocComment(start))
32873287
}
32883288
else {
3289-
var mods1 = addFlag(mods, Method)
3290-
var isInfix = false
3291-
def extParamss() =
3292-
try paramClause(0, prefix = true) :: Nil
3293-
finally
3294-
mods1 = addFlag(mods, ExtensionMethod)
3295-
if in.token == DOT then in.nextToken()
3296-
else
3297-
isInfix = true
3298-
newLineOpt()
3299-
val (leadingTparams, leadingVparamss) =
3300-
if in.token == LBRACKET then
3301-
(typeParamClause(ParamOwner.Def), extParamss())
3302-
else if in.token == LPAREN then
3303-
(Nil, extParamss())
3304-
else
3305-
(Nil, Nil)
33063289
val ident = termIdent()
33073290
var name = ident.name.asTermName
3308-
if mods1.is(ExtensionMethod) then name = name.toExtensionName
3309-
if isInfix && !name.isOperatorName then
3310-
val infixAnnot = Apply(wrapNew(scalaAnnotationDot(tpnme.infix)), Nil)
3311-
.withSpan(Span(start, start))
3312-
mods1 = mods1.withAddedAnnotation(infixAnnot)
3313-
val tparams =
3314-
if in.token == LBRACKET then
3315-
if mods1.is(ExtensionMethod) then syntaxError("no type parameters allowed here")
3316-
typeParamClause(ParamOwner.Def)
3317-
else leadingTparams
3318-
val vparamss = paramClauses() match
3319-
case rparams :: rparamss if leadingVparamss.nonEmpty && ident.name.isRightAssocOperatorName =>
3320-
rparams :: leadingVparamss ::: rparamss
3321-
case rparamss =>
3322-
leadingVparamss ::: rparamss
3291+
val tparams = typeParamClauseOpt(ParamOwner.Def)
3292+
val vparamss = paramClauses()
33233293
var tpt = fromWithinReturnType {
33243294
if in.token == COLONEOL then in.token = COLON
33253295
// a hack to allow
@@ -3347,7 +3317,7 @@ object Parsers {
33473317

33483318
val ddef = DefDef(name, tparams, vparamss, tpt, rhs)
33493319
if (isBackquoted(ident)) ddef.pushAttachment(Backquoted, ())
3350-
finalizeDef(ddef, mods1, start)
3320+
finalizeDef(ddef, addFlag(mods, Method), start)
33513321
}
33523322
}
33533323

tests/pos/i7700.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ object Macros:
77
extension (sc: StringContext) inline def show(args: =>Any*): String = ???
88

99
object Show:
10-
def[A] (a: A) show(using S: Show[A]): String = S.show(a)
10+
extension [A] (a: A) def show(using S: Show[A]): String = S.show(a)
1111

1212
export Macros.show

tests/pos/i8198.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
trait Eq[A] {
2-
def (x: A) === (y: A): Boolean
3-
def (x: A) /== (y: A): Boolean = !(x === y)
2+
extension (x: A)
3+
def === (y: A): Boolean
4+
def /== (y: A): Boolean = !(x === y)
45
}
56

67
case class Id[T](id: T)
78

89
given idEq[A](using eqA: Eq[A]) as Eq[Id[A]] = new {
9-
def (i1: Id[A]) === (i2: Id[A]) = !(i1.id /== i2.id)
10+
extension (i1: Id[A]) def === (i2: Id[A]) = !(i1.id /== i2.id)
1011
}

tests/run/extension-methods.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ object Test extends App {
6262
}
6363

6464
class ListOrd[T: Ord] extends Ord[List[T]] {
65-
def (xs: List[T])
66-
compareTo (ys: List[T]): Int = (xs, ys) match {
65+
extension (xs: List[T])
66+
def compareTo (ys: List[T]): Int = (xs, ys) match {
6767
case (Nil, Nil) => 0
6868
case (Nil, _) => -1
6969
case (_, Nil) => +1
@@ -89,11 +89,9 @@ object Test extends App {
8989
}
9090

9191
trait Monad[F[_]] extends Functor[F] {
92-
def [A, B](x: F[A])
93-
flatMap (f: A => F[B]): F[B]
94-
95-
def [A, B](x: F[A])
96-
map (f: A => B) = x.flatMap(f `andThen` pure)
92+
extension [A, B](x: F[A])
93+
def flatMap (f: A => F[B]): F[B]
94+
def map (f: A => B) = x.flatMap(f `andThen` pure)
9795

9896
def pure[A](x: A): F[A]
9997
}

0 commit comments

Comments
 (0)