Skip to content

Commit 941bafd

Browse files
committed
Fix #6914: Improve type inference for implicit Conversions
Make use of the knowlegde that a given instance used for a conversion must be an instance of `scala.Conversion`.
1 parent bc81953 commit 941bafd

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,11 +1367,21 @@ trait Implicits { self: Typer =>
13671367
if (argument.isEmpty)
13681368
adapt(generated, pt.widenExpr, locked)
13691369
else {
1370-
val untpdGenerated = untpd.TypedSplice(generated)
1371-
def tryConversion(implicit ctx: Context) =
1370+
def untpdGenerated = untpd.TypedSplice(generated)
1371+
def tryConversion(implicit ctx: Context) = {
1372+
val untpdConv =
1373+
if (ref.symbol.is(Given))
1374+
untpd.Select(
1375+
untpd.TypedSplice(
1376+
adapt(generated,
1377+
defn.ConversionClass.typeRef.appliedTo(argument.tpe.widen, pt),
1378+
locked)),
1379+
nme.apply)
1380+
else untpdGenerated
13721381
typed(
1373-
untpd.Apply(untpdGenerated, untpd.TypedSplice(argument) :: Nil),
1382+
untpd.Apply(untpdConv, untpd.TypedSplice(argument) :: Nil),
13741383
pt, locked)
1384+
}
13751385
if (cand.isExtension) {
13761386
val SelectionProto(name: TermName, mbrType, _, _) = pt
13771387
val result = extMethodApply(untpd.Select(untpdGenerated, name), argument, mbrType)

tests/pos/i6914.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait Expr[T]
2+
trait Liftable[T]
3+
4+
/*given autoToExpr[T] as Conversion[T, Expr[T]] given Liftable[T] {
5+
def apply(x: T): Expr[T] = ???
6+
}
7+
*/
8+
class ToExpr[T] given Liftable[T] extends Conversion[T, Expr[T]] {
9+
def apply(x: T): Expr[T] = ???
10+
}
11+
given toExpr[T] as ToExpr[T] given Liftable[T]
12+
13+
given as Liftable[Int] = ???
14+
given as Liftable[String] = ???
15+
16+
def x = the[ToExpr[String]]
17+
def y = the[Conversion[String, Expr[String]]]
18+
19+
def a: Expr[String] = "abc"

0 commit comments

Comments
 (0)