Skip to content

Fix #8802: Use wildcards for result type approximation of implicits #8824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,15 @@ object ProtoTypes {
/** The result type of `mt`, where all references to parameters of `mt` are
* replaced by either wildcards (if typevarsMissContext) or TypeParamRefs.
*/
def resultTypeApprox(mt: MethodType)(using Context): Type =
if (mt.isResultDependent) {
def resultTypeApprox(mt: MethodType, wildcardOnly: Boolean = false)(using Context): Type =
if mt.isResultDependent then
def replacement(tp: Type) =
if (ctx.mode.is(Mode.TypevarsMissContext) ||
!tp.widenExpr.isValueTypeOrWildcard) WildcardType
if wildcardOnly
|| ctx.mode.is(Mode.TypevarsMissContext)
|| !tp.widenExpr.isValueTypeOrWildcard
then WildcardType
else newDepTypeVar(tp)
mt.resultType.substParams(mt, mt.paramInfos.map(replacement))
}
else mt.resultType

/** The normalized form of a type
Expand All @@ -568,7 +569,7 @@ object ProtoTypes {
case poly: PolyType =>
normalize(constrained(poly).resultType, pt)
case mt: MethodType =>
if (mt.isImplicitMethod) normalize(resultTypeApprox(mt), pt)
if (mt.isImplicitMethod) normalize(resultTypeApprox(mt, wildcardOnly = true), pt)
else if (mt.isResultDependent) tp
else {
val rt = normalize(mt.resultType, pt)
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i8802.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trait Foo[A, B] {
type Out
}

object Test {

type Bar[A]

def unit: Bar[Unit] = ???
def product[A, B](fst: Bar[A], snd: Bar[B])(implicit foo: Foo[A, B]): Bar[foo.Out] = ???

implicit def foo[A]: Foo[A, Unit] { type Out = A } = ???

def check[A](bar: Bar[A])(a: A): Unit = {}

check(product(unit, unit))(()) // error
}