-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use all available context info for healing ambiguous implicits #11274
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,25 +49,34 @@ object ProtoTypes { | |
/** Test compatibility after normalization. | ||
* Do this in a fresh typerstate unless `keepConstraint` is true. | ||
*/ | ||
def normalizedCompatible(tp: Type, pt: Type, keepConstraint: Boolean)(using Context): Boolean = { | ||
def testCompat(using Context): Boolean = { | ||
def normalizedCompatible(tp: Type, pt: Type, keepConstraint: Boolean)(using Context): Boolean = | ||
|
||
def testCompat(using Context): Boolean = | ||
val normTp = normalize(tp, pt) | ||
isCompatible(normTp, pt) || pt.isRef(defn.UnitClass) && normTp.isParameterless | ||
} | ||
if (keepConstraint) | ||
tp.widenSingleton match { | ||
|
||
if keepConstraint then | ||
tp.widenSingleton match | ||
case poly: PolyType => | ||
// We can't keep the constraint in this case, since we have to add type parameters | ||
// to it, but there's no place to associate them with type variables. | ||
// So we'd get a "inconsistent: no typevars were added to committable constraint" | ||
// assertion failure in `constrained`. To do better, we'd have to change the | ||
// constraint handling architecture so that some type parameters are committable | ||
// and others are not. But that's a whole different ballgame. | ||
normalizedCompatible(tp, pt, keepConstraint = false) | ||
val newctx = ctx.fresh.setNewTyperState() | ||
val result = testCompat(using newctx) | ||
typr.println( | ||
i"""normalizedCompatible for $poly, $pt = $result | ||
|constraint was: ${ctx.typerState.constraint} | ||
|constraint now: ${newctx.typerState.constraint}""") | ||
val existingVars = ctx.typerState.uninstVars.toSet | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a def or moved inside the if condition to avoid calling |
||
if result | ||
&& (ctx.typerState.constraint ne newctx.typerState.constraint) | ||
&& newctx.typerState.uninstVars.forall(existingVars.contains) | ||
then newctx.typerState.commit() | ||
// If the new constrait contains fresh type variables we cannot keep it, | ||
// since those type variables are not instantiated anywhere in the source. | ||
// See pos/i6682a.scala for a test case. See pos/11243.scala and pos/i5773b.scala | ||
// for tests where it matters that we keep the constraint otherwise. | ||
result | ||
case _ => testCompat | ||
} | ||
else explore(testCompat) | ||
} | ||
end normalizedCompatible | ||
|
||
private def disregardProto(pt: Type)(using Context): Boolean = | ||
pt.dealias.isRef(defn.UnitClass) | ||
|
@@ -79,10 +88,18 @@ object ProtoTypes { | |
val savedConstraint = ctx.typerState.constraint | ||
val res = pt.widenExpr match { | ||
case pt: FunProto => | ||
mt match { | ||
case mt: MethodType => constrainResult(resultTypeApprox(mt), pt.resultType) | ||
mt match | ||
case mt: MethodType => | ||
constrainResult(resultTypeApprox(mt), pt.resultType) | ||
&& { | ||
if pt.constrainResultDeep | ||
&& mt.isImplicitMethod == (pt.applyKind == ApplyKind.Using) | ||
then | ||
val tpargs = pt.args.lazyZip(mt.paramInfos).map(pt.typedArg) | ||
tpargs.tpes.corresponds(mt.paramInfos)(_ <:< _) | ||
odersky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else true | ||
} | ||
case _ => true | ||
} | ||
case _: ValueTypeOrProto if !disregardProto(pt) => | ||
necessarilyCompatible(mt, pt) | ||
case pt: WildcardType if pt.optBounds.exists => | ||
|
@@ -123,6 +140,7 @@ object ProtoTypes { | |
abstract case class IgnoredProto(ignored: Type) extends CachedGroundType with MatchAlways: | ||
override def revealIgnored = ignored | ||
override def deepenProto(using Context): Type = ignored | ||
override def deepenProtoTrans(using Context): Type = ignored.deepenProtoTrans | ||
|
||
override def computeHash(bs: Hashable.Binders): Int = doHash(bs, ignored) | ||
|
||
|
@@ -202,7 +220,12 @@ object ProtoTypes { | |
def map(tm: TypeMap)(using Context): SelectionProto = derivedSelectionProto(name, tm(memberProto), compat) | ||
def fold[T](x: T, ta: TypeAccumulator[T])(using Context): T = ta(x, memberProto) | ||
|
||
override def deepenProto(using Context): SelectionProto = derivedSelectionProto(name, memberProto.deepenProto, compat) | ||
override def deepenProto(using Context): SelectionProto = | ||
derivedSelectionProto(name, memberProto.deepenProto, compat) | ||
|
||
override def deepenProtoTrans(using Context): SelectionProto = | ||
derivedSelectionProto(name, memberProto.deepenProtoTrans, compat) | ||
|
||
override def computeHash(bs: Hashable.Binders): Int = { | ||
val delta = (if (compat eq NoViewsAllowed) 1 else 0) | (if (privateOK) 2 else 0) | ||
addDelta(doHash(bs, name, memberProto), delta) | ||
|
@@ -276,9 +299,21 @@ object ProtoTypes { | |
/** A prototype for expressions that appear in function position | ||
* | ||
* [](args): resultType | ||
* | ||
* @param args The untyped arguments to which the function is applied | ||
* @param resType The expeected result type | ||
* @param typer The typer to use for typing the arguments | ||
* @param applyKind The kind of application (regular/using/tupled infix operand) | ||
* @param state The state object to use for tracking the changes to this prototype | ||
* @param constrainResultDeep | ||
* A flag to indicate that constrainResult on this prototype | ||
* should typecheck and compare the arguments. | ||
*/ | ||
case class FunProto(args: List[untpd.Tree], resType: Type)(typer: Typer, | ||
override val applyKind: ApplyKind, state: FunProtoState = new FunProtoState)(using protoCtx: Context) | ||
case class FunProto(args: List[untpd.Tree], resType: Type)( | ||
typer: Typer, | ||
override val applyKind: ApplyKind, | ||
state: FunProtoState = new FunProtoState, | ||
val constrainResultDeep: Boolean = false)(using protoCtx: Context) | ||
extends UncachedGroundType with ApplyingProto with FunOrPolyProto { | ||
override def resultType(using Context): Type = resType | ||
|
||
|
@@ -290,9 +325,17 @@ object ProtoTypes { | |
typer.isApplicableType(tp, args, resultType, keepConstraint && !args.exists(isPoly)) | ||
} | ||
|
||
def derivedFunProto(args: List[untpd.Tree] = this.args, resultType: Type, typer: Typer = this.typer): FunProto = | ||
if ((args eq this.args) && (resultType eq this.resultType) && (typer eq this.typer)) this | ||
else new FunProto(args, resultType)(typer, applyKind) | ||
def derivedFunProto( | ||
args: List[untpd.Tree] = this.args, | ||
resultType: Type = this.resultType, | ||
typer: Typer = this.typer, | ||
constrainResultDeep: Boolean = this.constrainResultDeep): FunProto = | ||
if (args eq this.args) | ||
&& (resultType eq this.resultType) | ||
&& (typer eq this.typer) | ||
&& constrainResultDeep == this.constrainResultDeep | ||
then this | ||
else new FunProto(args, resultType)(typer, applyKind, constrainResultDeep = constrainResultDeep) | ||
|
||
/** @return True if all arguments have types. | ||
*/ | ||
|
@@ -419,7 +462,11 @@ object ProtoTypes { | |
def fold[T](x: T, ta: TypeAccumulator[T])(using Context): T = | ||
ta(ta.foldOver(x, typedArgs().tpes), resultType) | ||
|
||
override def deepenProto(using Context): FunProto = derivedFunProto(args, resultType.deepenProto, typer) | ||
override def deepenProto(using Context): FunProto = | ||
derivedFunProto(args, resultType.deepenProto) | ||
|
||
override def deepenProtoTrans(using Context): FunProto = | ||
derivedFunProto(args, resultType.deepenProtoTrans, constrainResultDeep = true) | ||
|
||
override def withContext(newCtx: Context): ProtoType = | ||
if newCtx `eq` protoCtx then this | ||
|
@@ -472,7 +519,11 @@ object ProtoTypes { | |
def fold[T](x: T, ta: TypeAccumulator[T])(using Context): T = | ||
ta(ta(x, argType), resultType) | ||
|
||
override def deepenProto(using Context): ViewProto = derivedViewProto(argType, resultType.deepenProto) | ||
override def deepenProto(using Context): ViewProto = | ||
derivedViewProto(argType, resultType.deepenProto) | ||
|
||
override def deepenProtoTrans(using Context): ViewProto = | ||
derivedViewProto(argType, resultType.deepenProtoTrans) | ||
} | ||
|
||
class CachedViewProto(argType: Type, resultType: Type) extends ViewProto(argType, resultType) { | ||
|
@@ -522,7 +573,11 @@ object ProtoTypes { | |
def fold[T](x: T, ta: TypeAccumulator[T])(using Context): T = | ||
ta(ta.foldOver(x, targs.tpes), resultType) | ||
|
||
override def deepenProto(using Context): PolyProto = derivedPolyProto(targs, resultType.deepenProto) | ||
override def deepenProto(using Context): PolyProto = | ||
derivedPolyProto(targs, resultType.deepenProto) | ||
|
||
override def deepenProtoTrans(using Context): PolyProto = | ||
derivedPolyProto(targs, resultType.deepenProtoTrans) | ||
} | ||
|
||
/** A prototype for expressions [] that are known to be functions: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
object Test { | ||
def foo(x: String, y: x.type): Any = ??? | ||
val f = foo // error // error: cannot convert to closure | ||
} | ||
val f = foo // error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
object WriterTest extends App { | ||
|
||
object Functor: | ||
def apply[F[_]](using f: Functor[F]) = f | ||
|
||
trait Functor[F[_]]: | ||
extension [A, B](x: F[A]) | ||
def map(f: A => B): F[B] | ||
|
||
object Applicative: | ||
def apply[F[_]](using a: Applicative[F]) = a | ||
|
||
trait Applicative[F[_]] extends Functor[F]: | ||
def pure[A](x:A):F[A] | ||
|
||
extension [A,B](x: F[A]) | ||
def ap(f: F[A => B]): F[B] | ||
|
||
def map(f: A => B): F[B] = { | ||
x.ap(pure(f)) | ||
} | ||
|
||
extension [A,B,C](fa: F[A]) def map2(fb: F[B])(f: (A,B) => C): F[C] = { | ||
val fab: F[B => C] = fa.map((a: A) => (b: B) => f(a,b)) | ||
fb.ap(fab) | ||
} | ||
|
||
end Applicative | ||
|
||
|
||
object Monad: | ||
def apply[F[_]](using m: Monad[F]) = m | ||
|
||
trait Monad[F[_]] extends Applicative[F]: | ||
|
||
// The unit value for a monad | ||
def pure[A](x:A):F[A] | ||
|
||
extension[A,B](fa :F[A]) | ||
// The fundamental composition operation | ||
def flatMap(f :A=>F[B]):F[B] | ||
|
||
// Monad can also implement `ap` in terms of `map` and `flatMap` | ||
def ap(fab: F[A => B]): F[B] = { | ||
fab.flatMap { | ||
f => | ||
fa.flatMap { | ||
a => | ||
pure(f(a)) | ||
} | ||
} | ||
|
||
} | ||
|
||
end Monad | ||
|
||
given eitherMonad[Err]: Monad[[X] =>> Either[Err,X]] with | ||
def pure[A](a: A): Either[Err, A] = Right(a) | ||
extension [A,B](x: Either[Err,A]) def flatMap(f: A => Either[Err, B]) = { | ||
x match { | ||
case Right(a) => f(a) | ||
case Left(err) => Left(err) | ||
} | ||
} | ||
|
||
given optionMonad: Monad[Option] with | ||
def pure[A](a: A) = Some(a) | ||
extension[A,B](fa: Option[A]) | ||
def flatMap(f: A => Option[B]) = { | ||
fa match { | ||
case Some(a) => | ||
f(a) | ||
case None => | ||
None | ||
} | ||
} | ||
|
||
given listMonad: Monad[List] with | ||
def pure[A](a: A): List[A] = List(a) | ||
|
||
extension[A,B](x: List[A]) | ||
def flatMap(f: A => List[B]): List[B] = { | ||
x match { | ||
case hd :: tl => f(hd) ++ tl.flatMap(f) | ||
case Nil => Nil | ||
} | ||
} | ||
|
||
case class Transformer[F[_]: Monad,A](val wrapped: F[A]) | ||
|
||
given transformerMonad[F[_]: Monad]: Monad[[X] =>> Transformer[F,X]] with { | ||
|
||
def pure[A](a: A): Transformer[F,A] = Transformer(summon[Monad[F]].pure(a)) | ||
|
||
extension [A,B](fa: Transformer[F,A]) | ||
def flatMap(f: A => Transformer[F,B]) = { | ||
val ffa: F[B] = Monad[F].flatMap(fa.wrapped) { | ||
case a => { | ||
f(a).wrapped.map { | ||
case b => | ||
b | ||
} | ||
} | ||
} | ||
Transformer(ffa) | ||
} | ||
} | ||
|
||
type EString[A] = Either[String,A] | ||
|
||
def incrementEven(a: Int): Transformer[EString,Int] = { | ||
if(a % 2 == 1) Transformer(Left("Odd number provided")) | ||
else Transformer(Right(a + 1)) | ||
} | ||
|
||
def doubleOdd(a: Int): Transformer[EString, Int] = { | ||
if(a % 2 == 0) Transformer(Left("Even number provided")) | ||
else Transformer(Right(a * 2)) | ||
} | ||
|
||
val writerExample = incrementEven(8) | ||
val example = | ||
WriterTest.transformerMonad.flatMap(writerExample)(doubleOdd) | ||
//writerExample.flatMap(doubleOdd) // Error ambiguous F | ||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.