Skip to content

Commit a0d53d6

Browse files
Backport "Keep qualifier of Ident when selecting setter" to LTS (#20747)
Backports #18714 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents a7608fe + 8fa14e5 commit a0d53d6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
10981098
case Apply(fn, _) if fn.symbol.is(ExtensionMethod) =>
10991099
def toSetter(fn: Tree): untpd.Tree = fn match
11001100
case fn @ Ident(name: TermName) =>
1101-
untpd.cpy.Ident(fn)(name.setterName)
1101+
// We need to make sure that the prefix of this extension getter is
1102+
// retained when we transform it into a setter. Otherwise, we could
1103+
// end up resolving an unrelated setter from another extension. We
1104+
// transform the `Ident` into a `Select` to ensure that the prefix
1105+
// is retained with a `TypedSplice` (see `case Select` bellow).
1106+
// See tests/pos/i18713.scala for an example.
1107+
(fn.tpe match
1108+
case TermRef(qual: TermRef, _) =>
1109+
toSetter(ref(qual).select(fn.symbol).withSpan(fn.span))
1110+
case TermRef(qual: ThisType, _) =>
1111+
toSetter(This(qual.cls).select(fn.symbol).withSpan(fn.span))
1112+
case TermRef(NoPrefix, _) =>
1113+
untpd.cpy.Ident(fn)(name.setterName)
1114+
): @annotation.nowarn // false-positive match exhastivity warning
11021115
case fn @ Select(qual, name: TermName) =>
11031116
untpd.cpy.Select(fn)(untpd.TypedSplice(qual), name.setterName)
11041117
case fn @ TypeApply(fn1, targs) =>

tests/pos/i18713.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import language.experimental.relaxedExtensionImports
2+
3+
class A
4+
object AA:
5+
extension (a: A)
6+
def f = ???
7+
def f_=(x: String) = ???
8+
9+
object BB:
10+
extension (b: Long)
11+
def f = ???
12+
def f_=(x: String) = ???
13+
14+
def test(a: A) =
15+
import AA.*
16+
import BB.*
17+
a.f
18+
a.f = "aa"

0 commit comments

Comments
 (0)