Skip to content

Commit bd6ac1c

Browse files
dwijnandWojciechMazur
authored andcommitted
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. [Cherry-picked a175525]
1 parent 8fa14e5 commit bd6ac1c

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
@@ -826,7 +826,7 @@ trait Implicits:
826826
&& !to.isError
827827
&& !ctx.isAfterTyper
828828
&& ctx.mode.is(Mode.ImplicitsEnabled)
829-
&& from.widen.isValueType
829+
&& from.isValueType
830830
&& ( from.isValueSubType(to)
831831
|| inferView(dummyTreeOfType(from), to)
832832
(using ctx.fresh.addMode(Mode.ImplicitExploration).setExploreTyperState()).isSuccess
@@ -958,7 +958,7 @@ trait Implicits:
958958
.filter { imp =>
959959
!isImplicitDefConversion(imp.underlying)
960960
&& imp.symbol != defn.Predef_conforms
961-
&& viewExists(imp, fail.expectedType)
961+
&& viewExists(imp.underlying.resultType, fail.expectedType)
962962
}
963963
else
964964
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 foo2[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)