File tree 4 files changed +57
-16
lines changed
compiler/src/dotty/tools/dotc/typer 4 files changed +57
-16
lines changed Original file line number Diff line number Diff line change @@ -507,10 +507,10 @@ trait ImplicitRunInfo {
507
507
val incomplete : mutable.Set [Type ] = mutable.Set ()
508
508
509
509
/** 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.
511
511
*/
512
512
def isAnchor (sym : Symbol ) =
513
- sym.isClass && ! sym.is(Package )
513
+ sym.isClass && ! sym.is(Package ) && ( ! sym.isPackageObject || ctx.scala2CompatMode)
514
514
|| sym.isOpaqueAlias
515
515
|| sym.is(Deferred , butNot = Param )
516
516
@@ -584,7 +584,7 @@ trait ImplicitRunInfo {
584
584
addPath(pre.prefix)
585
585
}
586
586
}
587
- else {
587
+ else if ( ! pre.symbol.isPackageObject || ctx.scala2CompatMode) {
588
588
comps += pre
589
589
addPath(pre.prefix)
590
590
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ package logs
2
2
3
3
opaque type Logarithm = Double
4
4
5
- implicit object Logarithm {
5
+ object Logarithm {
6
6
7
7
// These are the ways to lift to the logarithm type
8
8
def apply (d : Double ): Logarithm = math.log(d)
@@ -13,10 +13,11 @@ implicit object Logarithm {
13
13
// This is the first way to unlift the logarithm type
14
14
def exponent (l : Logarithm ): Double = l
15
15
16
- // Extension methods define opaque types' public APIs
17
16
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
+ }
22
23
}
Original file line number Diff line number Diff line change 1
1
package logs
2
2
3
- import Predef .{any2stringadd => _ , _ }
4
-
5
3
object Test {
6
4
val l = Logarithm (1.0 )
7
5
val l2 = Logarithm (2.0 )
8
6
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
14
9
val l5 : Logarithm = (1.0 ).asInstanceOf [Logarithm ]
15
10
}
You can’t perform that action at this time.
0 commit comments