Skip to content

Commit bd24735

Browse files
Keep qualifier of Ident when selecting setter (#18714)
We already keep the qualifier as a typed splice if the prefix is an explicit Select. Fixes #18713
2 parents 92a82ac + e0f036c commit bd24735

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,19 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
11191119
case Apply(fn, _) if fn.symbol.is(ExtensionMethod) =>
11201120
def toSetter(fn: Tree): untpd.Tree = fn match
11211121
case fn @ Ident(name: TermName) =>
1122-
untpd.cpy.Ident(fn)(name.setterName)
1122+
// We need to make sure that the prefix of this extension getter is
1123+
// retained when we transform it into a setter. Otherwise, we could
1124+
// end up resolving an unrelated setter from another extension. We
1125+
// transform the `Ident` into a `Select` to ensure that the prefix
1126+
// is retained with a `TypedSplice` (see `case Select` bellow).
1127+
// See tests/pos/i18713.scala for an example.
1128+
fn.tpe match
1129+
case TermRef(qual: TermRef, _) =>
1130+
toSetter(ref(qual).select(fn.symbol).withSpan(fn.span))
1131+
case TermRef(qual: ThisType, _) =>
1132+
toSetter(This(qual.cls).select(fn.symbol).withSpan(fn.span))
1133+
case TermRef(NoPrefix, _) =>
1134+
untpd.cpy.Ident(fn)(name.setterName)
11231135
case fn @ Select(qual, name: TermName) =>
11241136
untpd.cpy.Select(fn)(untpd.TypedSplice(qual), name.setterName)
11251137
case fn @ TypeApply(fn1, targs) =>

tests/pos/i18713.scala

+18
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)