Skip to content

Commit b49c9e7

Browse files
authored
Merge pull request #9129 from dotty-staging/fix-#9051
Fix #9051: Fix accessibility test for implicit candidates
2 parents 16b75fb + a285ee4 commit b49c9e7

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ object Implicits {
100100
if (wtp.derivesFrom(SingletonClass)) defn.AnyType else wtp
101101
}
102102

103+
protected def isAccessible(ref: TermRef)(using Context): Boolean
104+
103105
/** Return those references in `refs` that are compatible with type `pt`. */
104106
protected def filterMatching(pt: Type)(using Context): List[Candidate] = {
105107
record("filterMatching")
@@ -198,7 +200,8 @@ object Implicits {
198200
case _ => tp
199201

200202
var ckind =
201-
if (!ref.symbol.isAccessibleFrom(ref.prefix)) Candidate.None
203+
if !isAccessible(ref) then
204+
Candidate.None
202205
else pt match {
203206
case pt: ViewProto =>
204207
viewCandidateKind(ref.widen, pt.argType, pt.resType)
@@ -259,6 +262,9 @@ object Implicits {
259262
filterMatching(tp)
260263
}
261264

265+
override def isAccessible(ref: TermRef)(using Context): Boolean =
266+
ref.symbol.exists && !ref.symbol.is(Private)
267+
262268
override def toString: String =
263269
i"OfTypeImplicits($tp), companions = ${companionRefs.toList}%, %; refs = $refs%, %."
264270
}
@@ -326,6 +332,9 @@ object Implicits {
326332
}
327333
}
328334

335+
override def isAccessible(ref: TermRef)(using Context): Boolean =
336+
ref.symbol.isAccessibleFrom(ref.prefix)
337+
329338
override def toString: String = {
330339
val own = i"(implicits: $refs%, %)"
331340
if (isOuterMost) own else own + "\n " + outerImplicits
@@ -960,7 +969,6 @@ trait Implicits { self: Typer =>
960969
implicits.println(i"committing ${result.tstate.constraint} yielding ${ctx.typerState.constraint} in ${ctx.typerState}")
961970
result
962971
case result: SearchFailure if result.isAmbiguous =>
963-
//println(i"FAIL for $pt / $argument: $result0")
964972
val deepPt = pt.deepenProto
965973
if (deepPt ne pt) inferImplicit(deepPt, argument, span)
966974
else if (migrateTo3 && !ctx.mode.is(Mode.OldOverloadingResolution))
@@ -975,10 +983,8 @@ trait Implicits { self: Typer =>
975983
}
976984
else result
977985
case NoMatchingImplicitsFailure =>
978-
//println(i"FAIL for $pt / $argument: $result0")
979986
SearchFailure(new NoMatchingImplicits(pt, argument, ctx.typerState.constraint))
980987
case _ =>
981-
//println(i"FAIL for $pt / $argument: $result0")
982988
result0
983989
}
984990
// If we are at the outermost implicit search then emit the implicit dictionary, if any.
@@ -1024,7 +1030,9 @@ trait Implicits { self: Typer =>
10241030
case _ =>
10251031
tryConversion
10261032
}
1027-
if (ctx.reporter.hasErrors) {
1033+
if ctx.reporter.hasErrors
1034+
|| !cand.ref.symbol.isAccessibleFrom(cand.ref.prefix)
1035+
then
10281036
ctx.reporter.removeBufferedMessages
10291037
adapted.tpe match {
10301038
case _: SearchFailureType => SearchFailure(adapted)
@@ -1036,13 +1044,11 @@ trait Implicits { self: Typer =>
10361044
else
10371045
SearchFailure(adapted.withType(new MismatchedImplicit(ref, pt, argument)))
10381046
}
1039-
}
1040-
else {
1047+
else
10411048
val returned =
10421049
if (cand.isExtension) Applications.ExtMethodApply(adapted)
10431050
else adapted
10441051
SearchSuccess(returned, ref, cand.level)(ctx.typerState, ctx.gadt)
1045-
}
10461052
}
10471053

10481054
/** An implicit search; parameters as in `inferImplicit` */
@@ -1067,8 +1073,6 @@ trait Implicits { self: Typer =>
10671073

10681074
val isNot: Boolean = wildProto.classSymbol == defn.NotClass
10691075

1070-
//println(i"search implicits $pt / ${eligible.map(_.ref)}")
1071-
10721076
/** Try to type-check implicit reference, after checking that this is not
10731077
* a diverging search
10741078
*/

tests/neg/i9051.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package zio:
2+
3+
class ZRef
4+
object ZRef:
5+
6+
private[zio] implicit class ZRefSyntax(private val self: ZRef):
7+
def unsafeUpdate: Boolean = true
8+
9+
object Main:
10+
val ref = new zio.ZRef
11+
println(ref.unsafeUpdate) // error

tests/pos/i9051.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package zio
2+
3+
class ZRef
4+
5+
object ZRef {
6+
7+
private[zio] implicit class ZRefSyntax(private val self: ZRef) extends AnyVal {
8+
def unsafeUpdate: Boolean = true
9+
}
10+
}
11+
12+
object Main extends App {
13+
val ref = new ZRef
14+
println(ref.unsafeUpdate)
15+
}

0 commit comments

Comments
 (0)