Skip to content

Commit

Permalink
Backport "Keep qualifier of Ident when selecting setter" to LTS (#20747)
Browse files Browse the repository at this point in the history
Backports #18714 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jun 23, 2024
2 parents a7608fe + 8fa14e5 commit a0d53d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case Apply(fn, _) if fn.symbol.is(ExtensionMethod) =>
def toSetter(fn: Tree): untpd.Tree = fn match
case fn @ Ident(name: TermName) =>
untpd.cpy.Ident(fn)(name.setterName)
// We need to make sure that the prefix of this extension getter is
// retained when we transform it into a setter. Otherwise, we could
// end up resolving an unrelated setter from another extension. We
// transform the `Ident` into a `Select` to ensure that the prefix
// is retained with a `TypedSplice` (see `case Select` bellow).
// See tests/pos/i18713.scala for an example.
(fn.tpe match
case TermRef(qual: TermRef, _) =>
toSetter(ref(qual).select(fn.symbol).withSpan(fn.span))
case TermRef(qual: ThisType, _) =>
toSetter(This(qual.cls).select(fn.symbol).withSpan(fn.span))
case TermRef(NoPrefix, _) =>
untpd.cpy.Ident(fn)(name.setterName)
): @annotation.nowarn // false-positive match exhastivity warning
case fn @ Select(qual, name: TermName) =>
untpd.cpy.Select(fn)(untpd.TypedSplice(qual), name.setterName)
case fn @ TypeApply(fn1, targs) =>
Expand Down
18 changes: 18 additions & 0 deletions tests/pos/i18713.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import language.experimental.relaxedExtensionImports

class A
object AA:
extension (a: A)
def f = ???
def f_=(x: String) = ???

object BB:
extension (b: Long)
def f = ???
def f_=(x: String) = ???

def test(a: A) =
import AA.*
import BB.*
a.f
a.f = "aa"

0 comments on commit a0d53d6

Please sign in to comment.