Skip to content

Commit 70f18eb

Browse files
committed
Merge pull request #694 from dotty-staging/fix/dependent-methods
Fix/dependent methods
2 parents 6a36dd8 + e984e23 commit 70f18eb

File tree

6 files changed

+74
-17
lines changed

6 files changed

+74
-17
lines changed

src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ trait Implicits { self: Typer =>
421421
assert(!ctx.isAfterTyper,
422422
if (argument.isEmpty) i"missing implicit parameter of type $pt after typer"
423423
else i"type error: ${argument.tpe} does not conform to $pt${err.whyNoMatchStr(argument.tpe, pt)}")
424+
val prevConstr = ctx.typerState.constraint
424425
ctx.traceIndented(s"search implicit ${pt.show}, arg = ${argument.show}: ${argument.tpe.show}", implicits, show = true) {
425426
assert(!pt.isInstanceOf[ExprType])
426427
val isearch =
@@ -435,6 +436,7 @@ trait Implicits { self: Typer =>
435436
val deepPt = pt.deepenProto
436437
if (deepPt ne pt) inferImplicit(deepPt, argument, pos) else result
437438
case _ =>
439+
assert(prevConstr eq ctx.typerState.constraint)
438440
result
439441
}
440442
}
@@ -450,7 +452,7 @@ trait Implicits { self: Typer =>
450452
// Not clear whether we need to drop the `.widen` here. All tests pass with it in place, though.
451453

452454
assert(argument.isEmpty || argument.tpe.isValueType || argument.tpe.isInstanceOf[ExprType],
453-
d"found: ${argument.tpe}, expected: $pt")
455+
d"found: $argument: ${argument.tpe}, expected: $pt")
454456

455457
/** The expected type for the searched implicit */
456458
lazy val fullProto = implicitProto(pt, identity)
@@ -472,9 +474,11 @@ trait Implicits { self: Typer =>
472474

473475
/** Search a list of eligible implicit references */
474476
def searchImplicits(eligible: List[TermRef], contextual: Boolean): SearchResult = {
477+
val constr = ctx.typerState.constraint
475478

476479
/** Try to typecheck an implicit reference */
477480
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) {
481+
assert(constr eq ctx.typerState.constraint)
478482
var generated: Tree = tpd.ref(ref).withPos(pos)
479483
if (!argument.isEmpty)
480484
generated = typedUnadapted(
@@ -483,7 +487,7 @@ trait Implicits { self: Typer =>
483487
val generated1 = adapt(generated, pt)
484488
lazy val shadowing =
485489
typed(untpd.Ident(ref.name) withPos pos.toSynthetic, funProto)
486-
(nestedContext.addMode(Mode.ImplicitShadowing).setNewTyperState)
490+
(nestedContext.addMode(Mode.ImplicitShadowing).setExploreTyperState)
487491
def refMatches(shadowing: Tree): Boolean =
488492
ref.symbol == closureBody(shadowing).symbol || {
489493
shadowing match {

src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ object ProtoTypes {
341341

342342
/** The normalized form of a type
343343
* - unwraps polymorphic types, tracking their parameters in the current constraint
344-
* - skips implicit parameters
344+
* - skips implicit parameters; if result type depends on implicit parameter,
345+
* replace with Wildcard.
345346
* - converts non-dependent method types to the corresponding function types
346347
* - dereferences parameterless method types
347348
* - dereferences nullary method types provided the corresponding function type
@@ -356,17 +357,22 @@ object ProtoTypes {
356357
def normalize(tp: Type, pt: Type)(implicit ctx: Context): Type = Stats.track("normalize") {
357358
tp.widenSingleton match {
358359
case poly: PolyType => normalize(constrained(poly).resultType, pt)
359-
case mt: MethodType if !mt.isDependent /*&& !pt.isInstanceOf[ApplyingProto]*/ =>
360-
if (mt.isImplicit) mt.resultType
361-
else {
362-
val rt = normalize(mt.resultType, pt)
363-
if (pt.isInstanceOf[ApplyingProto])
364-
mt.derivedMethodType(mt.paramNames, mt.paramTypes, rt)
360+
case mt: MethodType =>
361+
if (mt.isImplicit)
362+
if (mt.isDependent)
363+
mt.resultType.substParams(mt, mt.paramTypes.map(Function.const(WildcardType)))
364+
else mt.resultType
365+
else
366+
if (mt.isDependent) tp
365367
else {
366-
val ft = defn.FunctionType(mt.paramTypes, rt)
367-
if (mt.paramTypes.nonEmpty || ft <:< pt) ft else rt
368+
val rt = normalize(mt.resultType, pt)
369+
if (pt.isInstanceOf[ApplyingProto])
370+
mt.derivedMethodType(mt.paramNames, mt.paramTypes, rt)
371+
else {
372+
val ft = defn.FunctionType(mt.paramTypes, rt)
373+
if (mt.paramTypes.nonEmpty || ft <:< pt) ft else rt
374+
}
368375
}
369-
}
370376
case et: ExprType => et.resultType
371377
case _ => tp
372378
}

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,10 +1279,37 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12791279
}
12801280
}
12811281

1282+
/** If `tp` is a TypeVar which is fully constrained (i.e. its upper bound `hi` conforms
1283+
* to its lower bound `lo`), replace `tp` by `hi`. This is necessary to
1284+
* keep the right constraints for some implicit search problems. The paradigmatic case
1285+
* is `implicitNums.scala`. Without the healing done in `followAlias`, we cannot infer
1286+
* implicitly[_3], where _2 is the typelevel number 3. The problem here is that if a
1287+
* prototype is, say, Succ[Succ[Zero]], we can infer that it's argument type is Succ[Zero].
1288+
* But if the prototype is N? >: Succ[Succ[Zero]] <: Succ[Succ[Zero]], the same
1289+
* decomposition does not work - we'd get a N?#M where M is the element type name of Succ
1290+
* instead.
1291+
*/
1292+
def followAlias(tp: Type)(implicit ctx: Context): Type = {
1293+
val constraint = ctx.typerState.constraint
1294+
def inst(tp: Type): Type = tp match {
1295+
case TypeBounds(lo, hi)
1296+
if (lo eq hi) || (hi <:< lo)(ctx.fresh.setExploreTyperState) =>
1297+
inst(lo)
1298+
case tp: PolyParam =>
1299+
constraint.typeVarOfParam(tp).orElse(tp)
1300+
case _ => tp
1301+
}
1302+
tp match {
1303+
case tp: TypeVar if constraint.contains(tp) => inst(constraint.entry(tp.origin))
1304+
case _ => tp
1305+
}
1306+
}
1307+
12821308
def adaptNoArgs(wtp: Type): Tree = wtp match {
12831309
case wtp: ExprType =>
12841310
adaptInterpolated(tree.withType(wtp.resultType), pt, original)
1285-
case wtp: ImplicitMethodType if constrainResult(wtp, pt) =>
1311+
case wtp: ImplicitMethodType if constrainResult(wtp, followAlias(pt)) =>
1312+
val constr = ctx.typerState.constraint
12861313
def addImplicitArgs = {
12871314
def implicitArgError(msg: => String): Tree = {
12881315
ctx.error(msg, tree.pos.endPos)
@@ -1292,14 +1319,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12921319
def where = d"parameter $pname of $methodStr"
12931320
inferImplicit(formal, EmptyTree, tree.pos.endPos) match {
12941321
case SearchSuccess(arg, _, _) =>
1295-
adapt(arg, formal)
1322+
arg
12961323
case ambi: AmbiguousImplicits =>
12971324
implicitArgError(s"ambiguous implicits: ${ambi.explanation} of $where")
12981325
case failure: SearchFailure =>
12991326
implicitArgError(d"no implicit argument of type $formal found for $where" + failure.postscript)
13001327
}
13011328
}
1302-
adapt(tpd.Apply(tree, args), pt)
1329+
if (args.exists(_.isEmpty)) { assert(constr eq ctx.typerState.constraint); tree }
1330+
else adapt(tpd.Apply(tree, args), pt)
13031331
}
13041332
if ((pt eq WildcardType) || original.isEmpty) addImplicitArgs
13051333
else

test/dotc/tests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class tests extends CompilerTest {
121121
@Test def neg_t1843_variances = compileFile(negDir, "t1843-variances", xerrors = 1)
122122
@Test def neg_t2660_ambi = compileFile(negDir, "t2660", xerrors = 2)
123123
@Test def neg_t2994 = compileFile(negDir, "t2994", xerrors = 2)
124-
@Test def neg_subtyping = compileFile(negDir, "subtyping", xerrors = 4)
124+
@Test def neg_subtyping = compileFile(negDir, "subtyping", xerrors = 5)
125125
@Test def neg_variances = compileFile(negDir, "variances", xerrors = 2)
126126
@Test def neg_badAuxConstr = compileFile(negDir, "badAuxConstr", xerrors = 2)
127127
@Test def neg_typetest = compileFile(negDir, "typetest", xerrors = 1)

tests/pending/pos/depmet_implicit_chaining_zw.scala renamed to tests/pos/depmet_implicit_chaining_zw.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ object ZipWith {
2222
// thus, I present ?: implicitly on steroids!
2323
def ?[T <: AnyRef](implicit w: T): w.type = w
2424

25+
type _0 = Zero
26+
type _1 = Succ[Zero]
2527
type _2 = Succ[Succ[Zero]]
26-
val zw = ?[ZipWith[_2, Int => String => Boolean]].x // : Stream[Int] => Stream[String] => Stream[Boolean]
28+
val zw = ?[ZipWith[_2, Int => String => Boolean]](
29+
SuccZipWith[_1, Int, String => Boolean](
30+
SuccZipWith[_0, String, Boolean])).x
2731
// val zw = implicitly[ZipWith[Succ[Succ[Zero]], Int => String => Boolean]{type T = Stream[Int] => Stream[String] => Stream[Boolean]}].x
2832
}

tests/pos/implicitNums.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Test {
2+
3+
trait Number
4+
trait Zero extends Number
5+
trait Succ[N <: Number](n: N) extends Number
6+
7+
implicit def succ[N <: Number](implicit n: N): Succ[N] = new Succ[N](n) {}
8+
implicit def zero: Zero = new Zero{}
9+
10+
implicitly[Zero]
11+
implicitly[Succ[Zero]]
12+
implicitly[Succ[Succ[Zero]]]
13+
implicitly[Succ[Succ[Succ[Zero]]]]
14+
15+
}

0 commit comments

Comments
 (0)