Skip to content

Commit 5df71f0

Browse files
authored
Merge pull request #43 from scala/backport-lts-3.3-21785
Backport "Apply implicit conversion from derived Conversion instance defined as implicit rather than given" to 3.3 LTS
2 parents d002fe1 + 2417d74 commit 5df71f0

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ trait Implicits:
11341134
case _ => info.derivesFrom(defn.ConversionClass)
11351135
def tryConversion(using Context) = {
11361136
val untpdConv =
1137-
if ref.symbol.is(Given) && producesConversion(ref.symbol.info) then
1137+
if ref.symbol.isOneOf(GivenOrImplicit) && producesConversion(ref.symbol.info) then
11381138
untpd.Select(
11391139
untpd.TypedSplice(
11401140
adapt(generated,

tests/pos/i21757.scala

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
object ConversionChain {
2+
3+
class X(val value: Int)
4+
5+
class Y(val x: X)
6+
7+
class Z(val y: Y)
8+
9+
trait Conv[A, B] extends Conversion[A, B]
10+
11+
given xy: Conv[X, Y] = { (x: X) => new Y(x) }
12+
13+
given yz: Conv[Y, Z] = { (y: Y) => new Z(y) }
14+
15+
object ConvUtils {
16+
implicit def hypotheticalSyllogism[A, B, C]( // implicit def instead of given
17+
using
18+
ab: Conv[A, B],
19+
bc: Conv[B, C]
20+
): Conv[A, C] = {
21+
22+
new Conv[A, C] {
23+
def apply(a: A): C = bc(ab(a))
24+
}
25+
}
26+
}
27+
import ConvUtils.hypotheticalSyllogism
28+
29+
def test(): Unit = {
30+
val x = new X(42)
31+
val z: Z = x
32+
}
33+
}

0 commit comments

Comments
 (0)