From 529346d241459f0d20e607fb5ea78b6091c8bc12 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 10 Apr 2017 16:14:50 +0200 Subject: [PATCH 1/5] Handle printing of term paramrefs These were not printed before, fell back to toString method. --- .../src/dotty/tools/dotc/printing/PlainPrinter.scala | 12 +++++++----- .../dotty/tools/dotc/printing/RefinedPrinter.scala | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala index d5014b54720a..c762bbeafbdd 100644 --- a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -134,7 +134,7 @@ class PlainPrinter(_ctx: Context) extends Printer { case tp: TypeType => toTextRHS(tp) case tp: TermRef - if !tp.denotationIsCurrent && !homogenizedView || // always print underyling when testing picklers + if !tp.denotationIsCurrent && !homogenizedView || // always print underlying when testing picklers tp.symbol.is(Module) || tp.symbol.name.isImportName => toTextRef(tp) ~ ".type" @@ -183,7 +183,9 @@ class PlainPrinter(_ctx: Context) extends Printer { toTextGlobal(tp.resultType) } case tp: TypeParamRef => - TypeParamRefNameString(tp) ~ lambdaHash(tp.binder) + ParamRefNameString(tp) ~ lambdaHash(tp.binder) + case tp: TermParamRef => + ParamRefNameString(tp) ~ ".type" case AnnotatedType(tpe, annot) => toTextLocal(tpe) ~ " " ~ toText(annot) case HKApply(tycon, args) => @@ -206,10 +208,10 @@ class PlainPrinter(_ctx: Context) extends Printer { } }.close - protected def TypeParamRefNameString(name: TypeName): String = name.toString + protected def ParamRefNameString(name: Name): String = name.toString - protected def TypeParamRefNameString(param: TypeParamRef): String = - TypeParamRefNameString(param.binder.paramNames(param.paramNum)) + protected def ParamRefNameString(param: ParamRef): String = + ParamRefNameString(param.binder.paramNames(param.paramNum)) /** The name of the symbol without a unique id. Under refined printing, * the decoded original name. diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 76bce7920ec0..0b683d90c66a 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -604,7 +604,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { def optText[T >: Untyped](tree: List[Tree[T]])(encl: Text => Text): Text = if (tree.exists(!_.isEmpty)) encl(blockText(tree)) else "" - override protected def TypeParamRefNameString(name: TypeName): String = + override protected def ParamRefNameString(name: Name): String = name.unexpandedName.toString override protected def treatAsTypeParam(sym: Symbol): Boolean = sym is TypeParam From b3d683a4088f3db894c026070449637ec74e92fd Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 10 Apr 2017 16:16:31 +0200 Subject: [PATCH 2/5] Explain skolem types Strictly speaking, all the info about a skolem type is printed, e.g. A(?2) But it's reassuring to have an explanation line like ?2 is an unknown value of type A --- .../src/dotty/tools/dotc/printing/Formatting.scala | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/Formatting.scala b/compiler/src/dotty/tools/dotc/printing/Formatting.scala index e8fa454039e6..aa25880c28d1 100644 --- a/compiler/src/dotty/tools/dotc/printing/Formatting.scala +++ b/compiler/src/dotty/tools/dotc/printing/Formatting.scala @@ -107,7 +107,7 @@ object Formatting { else nonSensicalStartTag + str + nonSensicalEndTag } - private type Recorded = AnyRef /*Symbol | TypeParamRef*/ + private type Recorded = AnyRef /*Symbol | TypeParamRef | SkolemType */ private class Seen extends mutable.HashMap[String, List[Recorded]] { @@ -135,8 +135,13 @@ object Formatting { if ((sym is ModuleClass) && sym.sourceModule.exists) simpleNameString(sym.sourceModule) else seen.record(super.simpleNameString(sym), sym) - override def TypeParamRefNameString(param: TypeParamRef): String = - seen.record(super.TypeParamRefNameString(param), param) + override def ParamRefNameString(param: ParamRef): String = + seen.record(super.ParamRefNameString(param), param) + + override def toTextRef(tp: SingletonType): Text = tp match { + case tp: SkolemType => seen.record(tp.repr, tp) + case _ => super.toTextRef(tp) + } } /** Create explanation for single `Recorded` type or symbol */ @@ -165,6 +170,8 @@ object Formatting { s"is a type variable${addendum("constraint", ctx.typeComparer.bounds(param))}" case sym: Symbol => s"is a ${ctx.printer.kindString(sym)}${sym.showExtendedLocation}${addendum("bounds", sym.info)}" + case tp: SkolemType => + s"is an unknown value of type ${tp.widen.show}" } } @@ -176,6 +183,7 @@ object Formatting { private def explanations(seen: Seen)(implicit ctx: Context): String = { def needsExplanation(entry: Recorded) = entry match { case param: TypeParamRef => ctx.typerState.constraint.contains(param) + case skolem: SkolemType => true case _ => false } From bb0faff0d45f873fff9054fd8baec92eabb78c3d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 10 Apr 2017 16:18:05 +0200 Subject: [PATCH 3/5] Skolemize arguments to dependent methods as necessary. This was missing before, led to errors not being detected. --- .../dotty/tools/dotc/typer/Applications.scala | 5 ++-- .../dotty/tools/dotc/typer/TypeAssigner.scala | 20 ++++++++++++- tests/neg/i2142.scala | 28 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 tests/neg/i2142.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 4e43e429be92..fcc1a3b72cf6 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -395,9 +395,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic => def addTyped(arg: Arg, formal: Type): Type => Type = { addArg(typedArg(arg, formal), formal) if (methodType.isParamDependent) - _.substParam(methodType.newParamRef(n), typeOfArg(arg)) - else - identity + substArgForParam(_, typeOfArg(arg), methodType.paramRefs(n)) + else identity } def missingArg(n: Int): Unit = { diff --git a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala index 2aa7036b40f7..d04b1645198a 100644 --- a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -315,10 +315,28 @@ trait TypeAssigner { } } + /** Substitute argument type `argType` for parameter `pref` in type `tp`, + * skolemizing the argument type if it is not stable and `pref` occurs in `tp`. + */ + def substArgForParam(tp: Type, argType: Type, pref: ParamRef)(implicit ctx: Context) = { + val tp1 = tp.substParam(pref, argType) + if ((tp1 eq tp) || argType.isStable) tp1 + else tp.substParam(pref, SkolemType(argType.widen)) + } + def assignType(tree: untpd.Apply, fn: Tree, args: List[Tree])(implicit ctx: Context) = { val ownType = fn.tpe.widen match { case fntpe: MethodType => - if (sameLength(fntpe.paramInfos, args) || ctx.phase.prev.relaxedTyping) fntpe.instantiate(args.tpes) + def substArgsForParams(tp: Type, args: List[Tree], params: List[ParamRef]): Type = params match { + case param :: params1 => + val tp1 = substArgForParam(tp, args.head.tpe, param) + substArgsForParams(tp1, args.tail, params1) + case Nil => + tp + } + if (sameLength(fntpe.paramInfos, args) || ctx.phase.prev.relaxedTyping) + if (fntpe.isDependent) substArgsForParams(fntpe.resultType, args, fntpe.paramRefs) + else fntpe.resultType else errorType(i"wrong number of arguments for $fntpe: ${fn.tpe}, expected: ${fntpe.paramInfos.length}, found: ${args.length}", tree.pos) case t => diff --git a/tests/neg/i2142.scala b/tests/neg/i2142.scala new file mode 100644 index 000000000000..7aeef95f0364 --- /dev/null +++ b/tests/neg/i2142.scala @@ -0,0 +1,28 @@ +object Foo { + +class A +val a1 = new A() +val a2 = new A() + +def f(x: A, y: x.type) = () +f(a1, a1) // ok +f(a1, a2) // error +f(new A(), new A()) // error +f(new A(), a1) // error + +def g(x: A)(y: x.type) = () +g(a1)(a1) // ok +g(a1)(a2) // error +g(new A())(new A()) // error +g(new A())(a1) // error + +val x0 = g(new A()) _ +x0 (new A()) // error + +class C[T] + +def h(x: A): C[x.type] = ??? +val x = h(a1) +val y = h(new A()) + +} From ab06e2d3f2749a6fd594971bee1ae0fa533fa0bd Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 10 Apr 2017 16:20:55 +0200 Subject: [PATCH 4/5] Generalize definition of Type#isStable We now consider a type also as stable if it refers to an ExprType whose result type is stable. The previous commit made pos/z1720.scala break, because it skolemized unstable argument types. This commit makes the test pass again. --- compiler/src/dotty/tools/dotc/core/Types.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index e7130ee2f2cd..4c69c94844fc 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -107,10 +107,11 @@ object Types { final def isValueTypeOrLambda: Boolean = isValueType || this.isInstanceOf[TypeLambda] /** Does this type denote a stable reference (i.e. singleton type)? */ - @tailrec final def isStable(implicit ctx: Context): Boolean = stripTypeVar match { - case tp: TermRef => tp.termSymbol.isStable && tp.prefix.isStable + final def isStable(implicit ctx: Context): Boolean = stripTypeVar match { + case tp: TermRef => tp.termSymbol.isStable && tp.prefix.isStable || tp.info.isStable case _: SingletonType | NoPrefix => true case tp: RefinedOrRecType => tp.parent.isStable + case tp: ExprType => tp.resultType.isStable case _ => false } From 0cf17c5f63b3ec37a05da920a81337067ac335db Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 10 Apr 2017 17:10:24 +0200 Subject: [PATCH 5/5] Align safe parameter substitution with other subst methods Change name and align order of parameters. --- compiler/src/dotty/tools/dotc/typer/Applications.scala | 2 +- compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index fcc1a3b72cf6..1fcebf4f02ff 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -395,7 +395,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic => def addTyped(arg: Arg, formal: Type): Type => Type = { addArg(typedArg(arg, formal), formal) if (methodType.isParamDependent) - substArgForParam(_, typeOfArg(arg), methodType.paramRefs(n)) + safeSubstParam(_, methodType.paramRefs(n), typeOfArg(arg)) else identity } diff --git a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala index d04b1645198a..ead4ad5cbd42 100644 --- a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -318,7 +318,7 @@ trait TypeAssigner { /** Substitute argument type `argType` for parameter `pref` in type `tp`, * skolemizing the argument type if it is not stable and `pref` occurs in `tp`. */ - def substArgForParam(tp: Type, argType: Type, pref: ParamRef)(implicit ctx: Context) = { + def safeSubstParam(tp: Type, pref: ParamRef, argType: Type)(implicit ctx: Context) = { val tp1 = tp.substParam(pref, argType) if ((tp1 eq tp) || argType.isStable) tp1 else tp.substParam(pref, SkolemType(argType.widen)) @@ -327,15 +327,15 @@ trait TypeAssigner { def assignType(tree: untpd.Apply, fn: Tree, args: List[Tree])(implicit ctx: Context) = { val ownType = fn.tpe.widen match { case fntpe: MethodType => - def substArgsForParams(tp: Type, args: List[Tree], params: List[ParamRef]): Type = params match { + def safeSubstParams(tp: Type, params: List[ParamRef], args: List[Tree]): Type = params match { case param :: params1 => - val tp1 = substArgForParam(tp, args.head.tpe, param) - substArgsForParams(tp1, args.tail, params1) + val tp1 = safeSubstParam(tp, param, args.head.tpe) + safeSubstParams(tp1, params1, args.tail) case Nil => tp } if (sameLength(fntpe.paramInfos, args) || ctx.phase.prev.relaxedTyping) - if (fntpe.isDependent) substArgsForParams(fntpe.resultType, args, fntpe.paramRefs) + if (fntpe.isDependent) safeSubstParams(fntpe.resultType, fntpe.paramRefs, args) else fntpe.resultType else errorType(i"wrong number of arguments for $fntpe: ${fn.tpe}, expected: ${fntpe.paramInfos.length}, found: ${args.length}", tree.pos)