Skip to content

Commit fa23429

Browse files
authored
Merge pull request #8458 from dotty-staging/implicit-package-object
Always exclude package objects from the implicit scope
2 parents e2036ec + 686a1e4 commit fa23429

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,10 @@ trait ImplicitRunInfo {
507507
val incomplete: mutable.Set[Type] = mutable.Set()
508508

509509
/** Is `sym` an anchor type for which givens may exist? Anchor types are classes,
510-
* opaque type aliases, and abstract types, but not type parameters
510+
* opaque type aliases, and abstract types, but not type parameters or package objects.
511511
*/
512512
def isAnchor(sym: Symbol) =
513-
sym.isClass && !sym.is(Package)
513+
sym.isClass && !sym.is(Package) && (!sym.isPackageObject || ctx.scala2CompatMode)
514514
|| sym.isOpaqueAlias
515515
|| sym.is(Deferred, butNot = Param)
516516

@@ -584,7 +584,7 @@ trait ImplicitRunInfo {
584584
addPath(pre.prefix)
585585
}
586586
}
587-
else {
587+
else if (!pre.symbol.isPackageObject || ctx.scala2CompatMode) {
588588
comps += pre
589589
addPath(pre.prefix)
590590
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
trait ToString[A] {
2+
def print(a: A): Unit
3+
}
4+
5+
package A {
6+
case class AA(text: String)
7+
given ToString[AA] = aa => println(aa.text)
8+
9+
opaque type AB = String
10+
given ToString[AB] = ab => println(ab)
11+
12+
opaque type AC = String
13+
given ToString[AC] {
14+
def print(ac: AC): Unit = println(ac)
15+
}
16+
}
17+
18+
package B {
19+
case class BA(text: String)
20+
object BA {
21+
given ToString[BA] = ba => println(ba.text)
22+
}
23+
24+
opaque type BB = String
25+
object BB {
26+
given ToString[BB] = bb => println(bb)
27+
}
28+
29+
opaque type BC = String
30+
object BC {
31+
given ToString[BC] {
32+
def print(bc: BC): Unit = println(bc)
33+
}
34+
}
35+
}
36+
37+
object Test {
38+
val AA = summon[ToString[A.AA]] // error
39+
val AB = summon[ToString[A.AB]] // error, used to compile
40+
val AC = summon[ToString[A.AC]] // error
41+
42+
val BA = summon[ToString[B.BA]]
43+
val BB = summon[ToString[B.BB]]
44+
val BC = summon[ToString[B.BC]]
45+
}

tests/pos/toplevel-opaque-xm/Logarithm_1.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package logs
22

33
opaque type Logarithm = Double
44

5-
implicit object Logarithm {
5+
object Logarithm {
66

77
// These are the ways to lift to the logarithm type
88
def apply(d: Double): Logarithm = math.log(d)
@@ -13,10 +13,11 @@ implicit object Logarithm {
1313
// This is the first way to unlift the logarithm type
1414
def exponent(l: Logarithm): Double = l
1515

16-
// Extension methods define opaque types' public APIs
1716

18-
// This is the second way to unlift the logarithm type
19-
def (x: Logarithm).toDouble: Double = math.exp(x)
20-
def (x: Logarithm) + (y: Logarithm) = Logarithm(math.exp(x) + math.exp(y))
21-
def (x: Logarithm) * (y: Logarithm): Logarithm = Logarithm(x + y)
17+
given AnyRef {
18+
// This is the second way to unlift the logarithm type
19+
def (x: Logarithm).toDouble: Double = math.exp(x)
20+
def (x: Logarithm) + (y: Logarithm) = Logarithm(math.exp(x) + math.exp(y))
21+
def (x: Logarithm) * (y: Logarithm): Logarithm = Logarithm(x + y)
22+
}
2223
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package logs
22

3-
import Predef.{any2stringadd => _, _}
4-
53
object Test {
64
val l = Logarithm(1.0)
75
val l2 = Logarithm(2.0)
86
val l3 = l * l2
9-
val l4 = l + l2 // currently requires any2stringadd to be disabled because
10-
// as a contextual implicit this takes precedence over the
11-
// implicit scope implicit LogarithmOps.
12-
// TODO: Remove any2stringadd
13-
val d = Logarithm.toDouble(l3)
7+
val l4 = l + l2
8+
val d = l3.toDouble
149
val l5: Logarithm = (1.0).asInstanceOf[Logarithm]
1510
}

0 commit comments

Comments
 (0)