Skip to content

Commit 617b95c

Browse files
oderskyanatoliykmetyuk
authored andcommitted
Allow ambiguous references under Scala-2 mode
And offer an automatic rewrite rule.
1 parent 44b6d43 commit 617b95c

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ class Typer extends Namer
125125
val refctx = ctx
126126
val noImports = ctx.mode.is(Mode.InPackageClauseName)
127127

128-
def ambiguous(newPrec: BindingPrec, prevPrec: BindingPrec, prevCtx: Context)(using Context) =
129-
refctx.error(AmbiguousReference(name, newPrec, prevPrec, prevCtx), posd.sourcePos)
130-
131128
/** A symbol qualifies if it really exists and is not a package class.
132129
* In addition, if we are in a constructor of a pattern, we ignore all definitions
133130
* which are methods and not accessors (note: if we don't do that
@@ -173,7 +170,7 @@ class Typer extends Namer
173170
found
174171
else
175172
if !scala2pkg && !previous.isError && !found.isError then
176-
ambiguous(newPrec, prevPrec, prevCtx)
173+
refctx.error(AmbiguousReference(name, newPrec, prevPrec, prevCtx), posd.sourcePos)
177174
previous
178175

179176
/** Recurse in outer context. If final result is same as `previous`, check that it
@@ -317,7 +314,13 @@ class Typer extends Namer
317314
if scope.lookup(name).exists then
318315
val symsMatch = scope.lookupAll(name).exists(denot.containsSym)
319316
if !symsMatch then
320-
ambiguous(Definition, Inheritance, prevCtx)(using outer)
317+
refctx.errorOrMigrationWarning(
318+
AmbiguousReference(name, Definition, Inheritance, prevCtx)(using outer),
319+
posd.sourcePos)
320+
if ctx.scala2CompatMode then
321+
patch(Span(posd.span.start),
322+
if prevCtx.owner == refctx.owner.enclosingClass then "this."
323+
else s"${prevCtx.owner.name}.this.")
321324
else
322325
checkNoOuterDefs(denot, outer, prevCtx)
323326

tests/neg/ambiref.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ longer explanation available when compiling with `-explain`
2222
| and inherited subsequently in anonymous class D {...}
2323

2424
longer explanation available when compiling with `-explain`
25+
-- [E049] Reference Error: tests/neg/ambiref.scala:25:16 ---------------------------------------------------------------
26+
25 | println(y) // error
27+
| ^
28+
| Reference to y is ambiguous,
29+
| it is both defined in method c
30+
| and inherited subsequently in class E
31+
32+
longer explanation available when compiling with `-explain`

tests/neg/ambiref.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ object test2:
1717
println(y) // error
1818

1919
object test3:
20+
def c(y: Float) =
21+
class D:
22+
val y = 2
23+
class E extends D:
24+
class F:
25+
println(y) // error
26+
27+
object test4:
2028

2129
class C:
2230
val x = 0
@@ -26,7 +34,7 @@ object test3:
2634
def x(y: Int) = 3
2735
val y: Int = this.x // OK
2836
val z: Int = x // OK
29-
end test3
37+
end test4
3038

3139
val global = 0
3240
class C:

tests/pos-scala2/rewrites.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,26 @@ class Stream[+A] {
3434

3535
}
3636

37+
object test2 {
38+
def c(y: Float) = {
39+
class D {
40+
val y = 2
41+
}
42+
new D {
43+
println(y)
44+
}
45+
}
46+
}
47+
48+
object test3 {
49+
def c(y: Float) = {
50+
class D {
51+
val y = 2
52+
}
53+
class E extends D {
54+
class F {
55+
println(y)
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)