From 890b8a45cc9d83a6ef67fee3ee05084313f34688 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 22 Jun 2015 18:12:43 +0200 Subject: [PATCH 1/7] Avoid follow-on errors after implicit argument errors. Previously, we could die with a when an implicit argument was not found. --- src/dotty/tools/dotc/typer/Implicits.scala | 2 +- src/dotty/tools/dotc/typer/Typer.scala | 3 ++- test/dotc/tests.scala | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/dotty/tools/dotc/typer/Implicits.scala b/src/dotty/tools/dotc/typer/Implicits.scala index a3ddca5d99db..8a92eb726602 100644 --- a/src/dotty/tools/dotc/typer/Implicits.scala +++ b/src/dotty/tools/dotc/typer/Implicits.scala @@ -450,7 +450,7 @@ trait Implicits { self: Typer => // Not clear whether we need to drop the `.widen` here. All tests pass with it in place, though. assert(argument.isEmpty || argument.tpe.isValueType || argument.tpe.isInstanceOf[ExprType], - d"found: ${argument.tpe}, expected: $pt") + d"found: $argument: ${argument.tpe}, expected: $pt") /** The expected type for the searched implicit */ lazy val fullProto = implicitProto(pt, identity) diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 97bd0f514aee..3f878509050a 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1299,7 +1299,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit implicitArgError(d"no implicit argument of type $formal found for $where" + failure.postscript) } } - adapt(tpd.Apply(tree, args), pt) + if (args.exists(_.isEmpty)) tree + else adapt(tpd.Apply(tree, args), pt) } if ((pt eq WildcardType) || original.isEmpty) addImplicitArgs else diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index 1aa35e3ee66b..f96e77f0026f 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -121,7 +121,7 @@ class tests extends CompilerTest { @Test def neg_t1843_variances = compileFile(negDir, "t1843-variances", xerrors = 1) @Test def neg_t2660_ambi = compileFile(negDir, "t2660", xerrors = 2) @Test def neg_t2994 = compileFile(negDir, "t2994", xerrors = 2) - @Test def neg_subtyping = compileFile(negDir, "subtyping", xerrors = 4) + @Test def neg_subtyping = compileFile(negDir, "subtyping", xerrors = 5) @Test def neg_variances = compileFile(negDir, "variances", xerrors = 2) @Test def neg_badAuxConstr = compileFile(negDir, "badAuxConstr", xerrors = 2) @Test def neg_typetest = compileFile(negDir, "typetest", xerrors = 1) From ffac03ab718637c414f5e23524f458b4f4e8f817 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 23 Jun 2015 11:59:28 +0200 Subject: [PATCH 2/7] Handle normalization of implicit dependent methods. Now handles included test if toplevel implicit is given, but not yet without. --- src/dotty/tools/dotc/typer/ProtoTypes.scala | 26 ++++++++++++------- .../pos/depmet_implicit_chaining_zw.scala | 6 ++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/dotty/tools/dotc/typer/ProtoTypes.scala b/src/dotty/tools/dotc/typer/ProtoTypes.scala index 9a012c30e0b0..13011cb85ba8 100644 --- a/src/dotty/tools/dotc/typer/ProtoTypes.scala +++ b/src/dotty/tools/dotc/typer/ProtoTypes.scala @@ -341,7 +341,8 @@ object ProtoTypes { /** The normalized form of a type * - unwraps polymorphic types, tracking their parameters in the current constraint - * - skips implicit parameters + * - skips implicit parameters; if result type depends on implicit parameter, + * replace with Wildcard. * - converts non-dependent method types to the corresponding function types * - dereferences parameterless method types * - dereferences nullary method types provided the corresponding function type @@ -356,17 +357,22 @@ object ProtoTypes { def normalize(tp: Type, pt: Type)(implicit ctx: Context): Type = Stats.track("normalize") { tp.widenSingleton match { case poly: PolyType => normalize(constrained(poly).resultType, pt) - case mt: MethodType if !mt.isDependent /*&& !pt.isInstanceOf[ApplyingProto]*/ => - if (mt.isImplicit) mt.resultType - else { - val rt = normalize(mt.resultType, pt) - if (pt.isInstanceOf[ApplyingProto]) - mt.derivedMethodType(mt.paramNames, mt.paramTypes, rt) + case mt: MethodType => + if (mt.isImplicit) + if (mt.isDependent) + mt.resultType.substParams(mt, mt.paramTypes.map(Function.const(WildcardType))) + else mt.resultType + else + if (mt.isDependent) tp else { - val ft = defn.FunctionType(mt.paramTypes, rt) - if (mt.paramTypes.nonEmpty || ft <:< pt) ft else rt + val rt = normalize(mt.resultType, pt) + if (pt.isInstanceOf[ApplyingProto]) + mt.derivedMethodType(mt.paramNames, mt.paramTypes, rt) + else { + val ft = defn.FunctionType(mt.paramTypes, rt) + if (mt.paramTypes.nonEmpty || ft <:< pt) ft else rt + } } - } case et: ExprType => et.resultType case _ => tp } diff --git a/tests/pending/pos/depmet_implicit_chaining_zw.scala b/tests/pending/pos/depmet_implicit_chaining_zw.scala index a9da1e9764bd..f0e8a373b954 100644 --- a/tests/pending/pos/depmet_implicit_chaining_zw.scala +++ b/tests/pending/pos/depmet_implicit_chaining_zw.scala @@ -22,7 +22,11 @@ object ZipWith { // thus, I present ?: implicitly on steroids! def ?[T <: AnyRef](implicit w: T): w.type = w + type _0 = Zero + type _1 = Succ[Zero] type _2 = Succ[Succ[Zero]] - val zw = ?[ZipWith[_2, Int => String => Boolean]].x // : Stream[Int] => Stream[String] => Stream[Boolean] + val zw = ?[ZipWith[_2, Int => String => Boolean]]( + SuccZipWith[_1, Int, String => Boolean]( + SuccZipWith[_0, String, Boolean])).x // val zw = implicitly[ZipWith[Succ[Succ[Zero]], Int => String => Boolean]{type T = Stream[Int] => Stream[String] => Stream[Boolean]}].x } From 5395414a68bdcf682bbfc8de5d1c47e86fce708a Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 24 Jun 2015 15:26:44 +0200 Subject: [PATCH 3/7] Drop redundant adapt. The term in a SearchSuccess has already been adapted to the expected type. --- src/dotty/tools/dotc/typer/Typer.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 3f878509050a..a8e28b8c2c5e 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1292,7 +1292,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit def where = d"parameter $pname of $methodStr" inferImplicit(formal, EmptyTree, tree.pos.endPos) match { case SearchSuccess(arg, _, _) => - adapt(arg, formal) + arg case ambi: AmbiguousImplicits => implicitArgError(s"ambiguous implicits: ${ambi.explanation} of $where") case failure: SearchFailure => From bb90c8457ab91e3c4cd707fa1a68e75e6dd96128 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 24 Jun 2015 15:29:04 +0200 Subject: [PATCH 4/7] setNewTyperState -> setExploreTyperState when computing shadoing implicits Nothing will be committed here, so setExplore... is the right method to pick. --- src/dotty/tools/dotc/typer/Implicits.scala | 6 +++++- src/dotty/tools/dotc/typer/Typer.scala | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/dotty/tools/dotc/typer/Implicits.scala b/src/dotty/tools/dotc/typer/Implicits.scala index 8a92eb726602..f79288e74384 100644 --- a/src/dotty/tools/dotc/typer/Implicits.scala +++ b/src/dotty/tools/dotc/typer/Implicits.scala @@ -421,6 +421,7 @@ trait Implicits { self: Typer => assert(!ctx.isAfterTyper, if (argument.isEmpty) i"missing implicit parameter of type $pt after typer" else i"type error: ${argument.tpe} does not conform to $pt${err.whyNoMatchStr(argument.tpe, pt)}") + val prevConstr = ctx.typerState.constraint ctx.traceIndented(s"search implicit ${pt.show}, arg = ${argument.show}: ${argument.tpe.show}", implicits, show = true) { assert(!pt.isInstanceOf[ExprType]) val isearch = @@ -435,6 +436,7 @@ trait Implicits { self: Typer => val deepPt = pt.deepenProto if (deepPt ne pt) inferImplicit(deepPt, argument, pos) else result case _ => + assert(prevConstr eq ctx.typerState.constraint) result } } @@ -472,9 +474,11 @@ trait Implicits { self: Typer => /** Search a list of eligible implicit references */ def searchImplicits(eligible: List[TermRef], contextual: Boolean): SearchResult = { + val constr = ctx.typerState.constraint /** Try to typecheck an implicit reference */ def typedImplicit(ref: TermRef)(implicit ctx: Context): SearchResult = track("typedImplicit") { ctx.traceIndented(i"typed implicit $ref, pt = $pt, implicitsEnabled == ${ctx.mode is ImplicitsEnabled}", implicits, show = true) { + assert(constr eq ctx.typerState.constraint) var generated: Tree = tpd.ref(ref).withPos(pos) if (!argument.isEmpty) generated = typedUnadapted( @@ -483,7 +487,7 @@ trait Implicits { self: Typer => val generated1 = adapt(generated, pt) lazy val shadowing = typed(untpd.Ident(ref.name) withPos pos.toSynthetic, funProto) - (nestedContext.addMode(Mode.ImplicitShadowing).setNewTyperState) + (nestedContext.addMode(Mode.ImplicitShadowing).setExploreTyperState) def refMatches(shadowing: Tree): Boolean = ref.symbol == closureBody(shadowing).symbol || { shadowing match { diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index a8e28b8c2c5e..af6adb6a66cf 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1283,6 +1283,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit case wtp: ExprType => adaptInterpolated(tree.withType(wtp.resultType), pt, original) case wtp: ImplicitMethodType if constrainResult(wtp, pt) => + val constr = ctx.typerState.constraint def addImplicitArgs = { def implicitArgError(msg: => String): Tree = { ctx.error(msg, tree.pos.endPos) @@ -1299,7 +1300,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit implicitArgError(d"no implicit argument of type $formal found for $where" + failure.postscript) } } - if (args.exists(_.isEmpty)) tree + if (args.exists(_.isEmpty)) { assert(constr eq ctx.typerState.constraint); tree } else adapt(tpd.Apply(tree, args), pt) } if ((pt eq WildcardType) || original.isEmpty) addImplicitArgs From f3f75cad600d616db43138b09b01fcea93affd58 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 25 Jun 2015 09:31:34 +0200 Subject: [PATCH 5/7] Sharpen prototypes of implicit methods. Necessary to make implicit resolution of type-level peano numbers work. The current commit makes takes the inimal steps to make this happen. We could also consider sharpening using followAlias every type we constrain a result, or every time we adapt a type. --- src/dotty/tools/dotc/typer/Typer.scala | 28 +++++++++++++++++++++++++- tests/pos/implicitNums.scala | 15 ++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/pos/implicitNums.scala diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index af6adb6a66cf..02b740dd30bc 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1279,10 +1279,36 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit } } + /** If `tp` is a TypeVar which is fully constrained (i.e. its upper bound `hi` conforms + * to its lower bound `lo`), replace `tp` by `hi`. This is necessary to + * keep the right constraints for some implicit search problems. The paradigmatic case + * is `implicitNums.scala`. Without the healing done in `followAlias`, we cannot infer + * implicitly[_3], where _2 is the typelevel number 3. The problem here is that if a + * prototype is, say, Succ[Succ[Zero]], we can infer that it's argument type is Succ[Zero]. + * But if the prototype is N? >: Succ[Succ[Zero]] <: Succ[Succ[Zero]], the same + * decomposition does not work - we'd get a N?#M where M is the element type name of Succ + * instead. + */ + def followAlias(tp: Type)(implicit ctx: Context): Type = { + val constraint = ctx.typerState.constraint + def inst(tp: Type): Type = tp match { + case TypeBounds(lo, hi) => + if ((lo eq hi) || (hi <:< lo)(ctx.fresh.setExploreTyperState)) inst(lo) else NoType + case tp: PolyParam => + var tvar1 = constraint.typeVarOfParam(tp) + if (tvar1.exists) tvar1 else tp + case _ => tp + } + tp match { + case tp: TypeVar if constraint.contains(tp) => inst(constraint.entry(tp.origin)) + case _ => tp + } + } + def adaptNoArgs(wtp: Type): Tree = wtp match { case wtp: ExprType => adaptInterpolated(tree.withType(wtp.resultType), pt, original) - case wtp: ImplicitMethodType if constrainResult(wtp, pt) => + case wtp: ImplicitMethodType if constrainResult(wtp, followAlias(pt)) => val constr = ctx.typerState.constraint def addImplicitArgs = { def implicitArgError(msg: => String): Tree = { diff --git a/tests/pos/implicitNums.scala b/tests/pos/implicitNums.scala new file mode 100644 index 000000000000..48c5302f7fb3 --- /dev/null +++ b/tests/pos/implicitNums.scala @@ -0,0 +1,15 @@ +object Test { + + trait Number + trait Zero extends Number + trait Succ[N <: Number](n: N) extends Number + + implicit def succ[N <: Number](implicit n: N): Succ[N] = new Succ[N](n) {} + implicit def zero: Zero = new Zero{} + + implicitly[Zero] + implicitly[Succ[Zero]] + implicitly[Succ[Succ[Zero]]] + implicitly[Succ[Succ[Succ[Zero]]]] + +} From 1e9ffd45ed3c7782ae2f21ed0361e776a122897b Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 25 Jun 2015 09:47:26 +0200 Subject: [PATCH 6/7] Add test from pending. --- tests/{pending => }/pos/depmet_implicit_chaining_zw.scala | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{pending => }/pos/depmet_implicit_chaining_zw.scala (100%) diff --git a/tests/pending/pos/depmet_implicit_chaining_zw.scala b/tests/pos/depmet_implicit_chaining_zw.scala similarity index 100% rename from tests/pending/pos/depmet_implicit_chaining_zw.scala rename to tests/pos/depmet_implicit_chaining_zw.scala From e984e232bb21c8b24f7f591239afd1672fcf504e Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 25 Jun 2015 22:38:20 +0200 Subject: [PATCH 7/7] Cleanups --- src/dotty/tools/dotc/typer/Typer.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 02b740dd30bc..479eedd380a4 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1292,11 +1292,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit def followAlias(tp: Type)(implicit ctx: Context): Type = { val constraint = ctx.typerState.constraint def inst(tp: Type): Type = tp match { - case TypeBounds(lo, hi) => - if ((lo eq hi) || (hi <:< lo)(ctx.fresh.setExploreTyperState)) inst(lo) else NoType + case TypeBounds(lo, hi) + if (lo eq hi) || (hi <:< lo)(ctx.fresh.setExploreTyperState) => + inst(lo) case tp: PolyParam => - var tvar1 = constraint.typeVarOfParam(tp) - if (tvar1.exists) tvar1 else tp + constraint.typeVarOfParam(tp).orElse(tp) case _ => tp } tp match {