Skip to content

Commit 731ec42

Browse files
committed
Tweak convertible implicits fix
Rather than widen in viewExists, widen earlier, past type lambda parameters. This allows `foo2` in `i16453b2` from being listed as a possible implicit, as appropriate.
1 parent 2fa54e8 commit 731ec42

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Implicits.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ trait Implicits:
850850
&& !to.isError
851851
&& !ctx.isAfterTyper
852852
&& ctx.mode.is(Mode.ImplicitsEnabled)
853-
&& from.widen.isValueType
853+
&& from.isValueType
854854
&& ( from.isValueSubType(to)
855855
|| inferView(dummyTreeOfType(from), to)
856856
(using ctx.fresh.addMode(Mode.ImplicitExploration).setExploreTyperState()).isSuccess
@@ -982,7 +982,7 @@ trait Implicits:
982982
.filter { imp =>
983983
!isImplicitDefConversion(imp.underlying)
984984
&& imp.symbol != defn.Predef_conforms
985-
&& viewExists(imp, fail.expectedType)
985+
&& viewExists(imp.underlying.resultType, fail.expectedType)
986986
}
987987
else
988988
Nil

Diff for: tests/neg/i16453b1.check

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E172] Type Error: tests/neg/i16453b1.scala:11:19 -------------------------------------------------------------------
2+
11 | val ko = get[Int] // error
3+
| ^
4+
|No given instance of type Ctx => Option[Int] was found for parameter fn of method get
5+
|
6+
|Note: implicit conversions are not automatically applied to arguments of using clauses. You will have to pass the argument explicitly.
7+
|The following implicits in scope can be implicitly converted to Ctx => Option[Int]:
8+
|- final lazy given val foo: Ctx => Int

Diff for: tests/neg/i16453b1.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.language.implicitConversions
2+
3+
sealed trait Ctx
4+
given ct[T]: Conversion[Ctx => T, Ctx => Option[T]] = fn => fn.andThen(Option.apply)
5+
6+
def get[T](using fn: Ctx => Option[T]): Option[T] = ???
7+
8+
def Test = {
9+
given foo: (Ctx => Int) = _ => 42
10+
val ok = get[Int](using summon[Ctx => Int])
11+
val ko = get[Int] // error
12+
}

Diff for: tests/neg/i16453b2.check

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E172] Type Error: tests/neg/i16453b2.scala:11:19 -------------------------------------------------------------------
2+
11 | val ko = get[Int] // error
3+
| ^
4+
|No given instance of type Ctx => Option[Int] was found for parameter fn of method get
5+
|
6+
|Note: implicit conversions are not automatically applied to arguments of using clauses. You will have to pass the argument explicitly.
7+
|The following implicits in scope can be implicitly converted to Ctx => Option[Int]:
8+
|- final given def foo[A]: Ctx => Int

Diff for: tests/neg/i16453b2.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.language.implicitConversions
2+
3+
sealed trait Ctx
4+
given ct[T]: Conversion[Ctx => T, Ctx => Option[T]] = fn => fn.andThen(Option.apply)
5+
6+
def get[T](using fn: Ctx => Option[T]): Option[T] = ???
7+
8+
def Test = {
9+
given foo2[A]: (Ctx => Int) = _ => 42
10+
val ok = get[Int](using summon[Ctx => Int])
11+
val ko = get[Int] // error
12+
}

0 commit comments

Comments
 (0)