Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c05091c

Browse files
committed
Avoid spurious ambiguity warning with inherited overloaded symbols
1 parent 49d5507 commit c05091c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,11 @@ trait Contexts { self: Analyzer =>
16371637
done = (cx eq NoContext) || foundCompetingSymbol()
16381638
if (!done && (cx ne NoContext)) cx = cx.outer
16391639
}
1640-
if (defSym.exists && (defSym ne defSym0)) {
1640+
val nonOverlapping = defSym.exists && {
1641+
if (defSym.isOverloaded || defSym0.isOverloaded) !defSym.alternatives.exists(defSym0.alternatives.contains)
1642+
else defSym ne defSym0
1643+
}
1644+
if (nonOverlapping) {
16411645
val ambiguity =
16421646
if (preferDef) ambiguousDefinitions(defSym, defSym0, wasFoundInSuper, cx0.enclClass.owner, thisContext.enclClass.owner)
16431647
else Some(ambiguousDefnAndImport(owner = defSym.owner, imp1))

test/files/neg/t11921b.check

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
t11921b.scala:135: error: could not find implicit value for parameter i: Int
22
def u = t // doesn't compile in Scala 2 (maybe there's a ticket for that)
33
^
4+
t11921b.scala:151: error: package object inheritance is deprecated (https://github.com/scala/scala-dev/issues/441);
5+
drop the `extends` clause or use a regular object instead
6+
Scala 3 migration messages are errors under -Xsource:3. Use -Wconf / @nowarn to filter them or add -Xmigration to demote them to warnings.
7+
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration
8+
package object pt12850 extends t12850 {
9+
^
410
t11921b.scala:11: error: reference to x is ambiguous;
511
it is both defined in the enclosing object Test and inherited in the enclosing class D as value x (defined in class C)
612
In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
@@ -73,4 +79,4 @@ Scala 3 migration messages are errors under -Xsource:3. Use -Wconf / @nowarn to
7379
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=test10.C.v
7480
def v = t(lo) // error
7581
^
76-
9 errors
82+
10 errors

test/files/neg/t11921b.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,19 @@ package scala {
143143
def t = new Option[String] {} // OK, competing scala.Option is not defined in the same compilation unit
144144
}
145145
}
146+
147+
trait t12850 {
148+
def pm(x: Int) = 1
149+
def pm(x: String) = 2
150+
}
151+
package object pt12850 extends t12850 {
152+
def t = pm(1) // no error
153+
}
154+
155+
trait t12850b {
156+
def pm(x: Int) = 1
157+
def pm(x: String) = 2
158+
object O extends t12850b {
159+
def t = pm(1) // no error
160+
}
161+
}

0 commit comments

Comments
 (0)