Skip to content

Commit 64f9878

Browse files
committed
Fix #9562: Fix handling of identifiers in extension methods
Fix 1: Right associative methods
1 parent 239254a commit 64f9878

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,14 @@ class Typer extends Namer
452452
*/
453453
def extensionMethodSelect: untpd.Tree =
454454
val xmethod = ctx.owner.enclosingExtensionMethod
455+
def leadingParam(paramss: List[List[Symbol]]): Symbol = paramss match
456+
case (param :: _) :: paramss1 if param.isType => leadingParam(paramss1)
457+
case _ :: (snd :: Nil) :: _ if !isLeftAssoc(xmethod.name) => snd
458+
case (fst :: Nil) :: _ => fst
459+
case _ => NoSymbol
455460
val qualifier =
456-
if xmethod.exists then // TODO: see whether we can use paramss for that
457-
val leadParamName = xmethod.info.paramNamess.head.head
458-
def isLeadParam(sym: Symbol) =
459-
sym.is(Param) && sym.owner.owner == xmethod.owner && sym.name == leadParamName
460-
def leadParam(ctx: Context): Symbol =
461-
ctx.scope.lookupAll(leadParamName).find(isLeadParam) match
462-
case Some(param) => param
463-
case None => leadParam(ctx.outersIterator.dropWhile(_.scope eq ctx.scope).next)
464-
untpd.ref(leadParam(ctx).termRef)
461+
if xmethod.exists then
462+
untpd.ref(leadingParam(xmethod.rawParamss).termRef)
465463
else
466464
untpd.This(untpd.EmptyTypeIdent)
467465
untpd.cpy.Select(tree)(qualifier, name)

tests/pos/i9562.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Foo:
2+
def foo = 23
3+
4+
object Unrelated:
5+
extension (f: Foo)
6+
def g = f.foo // OK
7+
8+
extension (f: Foo)
9+
def h1: Int = 0
10+
def h2: Int = h1 + 1 // OK
11+
def ++: (x: Int): Int = h2 + x // OK

0 commit comments

Comments
 (0)