Skip to content

Commit 42b89dc

Browse files
committed
Fix #10311: Inaccessible members do not qualify for SelectionProtos
1 parent 6b9796c commit 42b89dc

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,12 @@ object ProtoTypes {
180180
{
181181
val mbr = if (privateOK) tp1.member(name) else tp1.nonPrivateMember(name)
182182
def qualifies(m: SingleDenotation) =
183-
memberProto.isRef(defn.UnitClass) ||
184-
tp1.isValueType && compat.normalizedCompatible(NamedType(tp1, name, m), memberProto, keepConstraint)
185-
// Note: can't use `m.info` here because if `m` is a method, `m.info`
186-
// loses knowledge about `m`'s default arguments.
183+
val isAccessible = !m.symbol.exists || m.symbol.isAccessibleFrom(tp1, superAccess = true)
184+
isAccessible
185+
&& (memberProto.isRef(defn.UnitClass)
186+
|| tp1.isValueType && compat.normalizedCompatible(NamedType(tp1, name, m), memberProto, keepConstraint))
187+
// Note: can't use `m.info` here because if `m` is a method, `m.info`
188+
// loses knowledge about `m`'s default arguments.
187189
mbr match { // hasAltWith inlined for performance
188190
case mbr: SingleDenotation => mbr.exists && qualifies(mbr)
189191
case _ => mbr hasAltWith qualifies

tests/neg/overloading-specifity.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ object Test extends App {
2323
def foo[T]: Show[T] = new Show[T](2)
2424
}
2525

26-
assert(a.foo[Int].i == 2) // error: no implicit argument of type Test.Context was found for parameter ctx
26+
val b = a.foo[Int] // error: no implicit argument of type context found
27+
assert(b.i == 1) // error
2728
}

tests/pos/i10311.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Module {
2+
class MyInt(private val x: Int, private val y: Int)
3+
object MyInt {
4+
implicit class Ops(self: MyInt) extends AnyVal {
5+
def x: Int = self.x
6+
}
7+
extension (self: MyInt) def y: Int = self.y
8+
}
9+
}
10+
object test:
11+
import Module._
12+
13+
val a = new MyInt(42, 43)
14+
val b = a.x
15+
val c = a.y
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Shows that overloading resolution does not test implicits to decide
2+
// applicability. A best alternative is picked first, and then implicits
3+
// are searched for this one.
4+
case class Show[T](val i: Int)
5+
class Show1[T](i: Int) extends Show[T](i)
6+
7+
class Generic
8+
object Generic {
9+
implicit val gen: Generic = new Generic
10+
implicit def showGen[T](implicit gen: Generic): Show[T] = new Show[T](2)
11+
}
12+
13+
object Test extends App {
14+
trait Context
15+
//given ctx as Context
16+
17+
object a {
18+
def foo[T](implicit gen: Generic): Show[T] = new Show[T](1)
19+
def foo[T](implicit gen: Generic, ctx: Context): Show1[T] = new Show1[T](2)
20+
}
21+
object b {
22+
def foo[T](implicit gen: Generic): Show[T] = new Show[T](1)
23+
def foo[T]: Show[T] = new Show[T](2)
24+
}
25+
26+
assert(a.foo[Int].i == 1) // error: no implicit argument of type Test.Context was found for parameter ctx
27+
}

0 commit comments

Comments
 (0)