diff options
-rw-r--r-- | src/dotty/tools/dotc/core/Definitions.scala | 4 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/transform/Erasure.scala | 17 | ||||
-rw-r--r-- | src/dotty/tools/dotc/transform/Erasure.scala | 25 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/TypeAssigner.scala | 2 | ||||
-rw-r--r-- | test/dotc/tests.scala | 54 | ||||
-rw-r--r-- | tests/pos/unions.scala | 14 |
6 files changed, 69 insertions, 47 deletions
diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala index 311abd8e1..46878d3ca 100644 --- a/src/dotty/tools/dotc/core/Definitions.scala +++ b/src/dotty/tools/dotc/core/Definitions.scala @@ -103,8 +103,8 @@ class Definitions { lazy val Object_!= = newMethod(ObjectClass, nme.NE, methOfAny(BooleanType), Final) lazy val Object_eq = newMethod(ObjectClass, nme.eq, methOfAnyRef(BooleanType), Final) lazy val Object_ne = newMethod(ObjectClass, nme.ne, methOfAnyRef(BooleanType), Final) - lazy val Object_isInstanceOf = newT1EmptyParamsMethod(ObjectClass, nme.isInstanceOf_Ob, _ => BooleanType, Final | Synthetic) - lazy val Object_asInstanceOf = newT1EmptyParamsMethod(ObjectClass, nme.asInstanceOf_Ob, PolyParam(_, 0), Final | Synthetic) + lazy val Object_isInstanceOf = newT1ParameterlessMethod(ObjectClass, nme.isInstanceOf_Ob, _ => BooleanType, Final | Synthetic) + lazy val Object_asInstanceOf = newT1ParameterlessMethod(ObjectClass, nme.asInstanceOf_Ob, PolyParam(_, 0), Final | Synthetic) lazy val Object_synchronized = newPolyMethod(ObjectClass, nme.synchronized_, 1, pt => MethodType(List(PolyParam(pt, 0)), PolyParam(pt, 0)), Final) diff --git a/src/dotty/tools/dotc/core/transform/Erasure.scala b/src/dotty/tools/dotc/core/transform/Erasure.scala index d59bdf1a9..842e2f81f 100644 --- a/src/dotty/tools/dotc/core/transform/Erasure.scala +++ b/src/dotty/tools/dotc/core/transform/Erasure.scala @@ -61,16 +61,11 @@ object Erasure { */ def transformInfo(sym: Symbol, tp: Type)(implicit ctx: Context): Type = { val erase = erasureFn(sym is JavaDefined, isSemi = true, sym.isConstructor, wildcardOK = false) - if ((sym eq defn.Object_asInstanceOf) || sym.isType && (sym.owner eq defn.ArrayClass)) - sym.info - else if ((sym eq defn.Object_isInstanceOf) || (sym eq defn.ArrayClass.primaryConstructor)) { - val tp @ PolyType(pnames) = sym.info - tp.derivedPolyType(pnames, TypeBounds.empty :: Nil, erase(tp.resultType)) - } - else if (sym.isAbstractType) - TypeAlias(WildcardType) - else - erase(tp) + if ((sym eq defn.Object_asInstanceOf) || + (sym eq defn.Object_isInstanceOf) || + (sym.owner eq defn.ArrayClass) && (sym.isType || sym.isConstructor)) sym.info + else if (sym.isAbstractType) TypeAlias(WildcardType) + else erase(tp) } def isUnboundedGeneric(tp: Type)(implicit ctx: Context) = !( @@ -120,7 +115,7 @@ class Erasure(isJava: Boolean, isSemi: Boolean, isConstructor: Boolean, wildcard case tp: TypeRef => val sym = tp.symbol if (!sym.isClass) - if (sym.owner eq defn.ArrayClass) tp else this(tp.info) + if (sym.exists && (sym.owner eq defn.ArrayClass)) tp else this(tp.info) //!!!! else if (sym.isDerivedValueClass) eraseDerivedValueClassRef(tp) else eraseNormalClassRef(tp) case tp: RefinedType => diff --git a/src/dotty/tools/dotc/transform/Erasure.scala b/src/dotty/tools/dotc/transform/Erasure.scala index f9b602f54..b403d4e66 100644 --- a/src/dotty/tools/dotc/transform/Erasure.scala +++ b/src/dotty/tools/dotc/transform/Erasure.scala @@ -130,7 +130,7 @@ object Erasure { // See SI-2386 for one example of when this might be necessary. cast(runtimeCall(nme.toObjectArray, tree :: Nil), pt) case _ => - println(s"casting from ${tree.showSummary}: ${tree.tpe.show} to ${pt.show}") + ctx.log(s"casting from ${tree.showSummary}: ${tree.tpe.show} to ${pt.show}") TypeApply(Select(tree, defn.Object_asInstanceOf), TypeTree(pt) :: Nil) } @@ -173,8 +173,10 @@ object Erasure { override def typedIdent(tree: untpd.Ident, pt: Type)(implicit ctx: Context): Tree = { val tree1 = promote(tree) - println(i"typed ident ${tree.name}: ${tree1.tpe} at phase ${ctx.phase}, history = ${tree1.symbol.history}") - tree1 + tree1.tpe match { + case ThisType(cls) => This(cls) withPos tree.pos + case _ => tree1 + } } /** Type check select nodes, applying the following rewritings exhaustively @@ -226,8 +228,16 @@ object Erasure { recur(typed(tree.qualifier, AnySelectionProto)) } - override def typedTypeApply(tree: untpd.TypeApply, pt: Type)(implicit ctx: Context) = - typedExpr(tree.fun, pt) + override def typedTypeApply(tree: untpd.TypeApply, pt: Type)(implicit ctx: Context) = { + val TypeApply(fun, args) = tree + val fun1 = typedExpr(fun, pt) + fun1.tpe.widen match { + case funTpe: PolyType => + val args1 = args.mapconserve(typedType(_)) + untpd.cpy.TypeApply(tree, fun1, args1).withType(funTpe.instantiate(args1.tpes)) + case _ => fun1 + } + } override def typedApply(tree: untpd.Apply, pt: Type)(implicit ctx: Context): Tree = { val Apply(fun, args) = tree @@ -268,9 +278,8 @@ object Erasure { */ override def typedNamed(tree: untpd.NameTree, pt: Type)(implicit ctx: Context): Tree = { if (tree eq untpd.EmptyValDef) return tpd.EmptyValDef - assert(tree.hasType, tree) + assert(tree.hasType, tree.show) val sym = tree.symbol - assert(sym.exists, tree) def localContext = ctx.fresh.setTree(tree).setOwner(sym) tree match { case tree: untpd.Ident => typedIdent(tree, pt) @@ -288,5 +297,7 @@ object Erasure { assert(ctx.phase == ctx.erasurePhase.next, ctx.phase) if (tree.isEmpty) tree else adaptToType(tree, pt) } + + override def index(trees: List[untpd.Tree])(implicit ctx: Context) = ctx } }
\ No newline at end of file diff --git a/src/dotty/tools/dotc/typer/TypeAssigner.scala b/src/dotty/tools/dotc/typer/TypeAssigner.scala index 3060f3951..5345b7396 100644 --- a/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -228,7 +228,7 @@ trait TypeAssigner { val ownType = fn.tpe.widen match { case pt: PolyType => val argTypes = args.tpes - if (sameLength(argTypes, pt.paramNames)) pt.instantiate(args.tpes) + if (sameLength(argTypes, pt.paramNames)) pt.instantiate(argTypes) else errorType(i"wrong number of type parameters for ${fn.tpe}; expected: ${pt.paramNames.length}", tree.pos) case _ => errorType(s"${err.exprStr(fn)} does not take type parameters", tree.pos) diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index ed6f3b5d9..dcd5c67c8 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -15,37 +15,39 @@ class tests extends CompilerTest { "-pagewidth", "160" ) val twice = List("#runs", "2") + val doErase = List("-Ystop-before:terminal") val posDir = "./tests/pos/" val negDir = "./tests/neg/" val newDir = "./tests/new/" val dotcDir = "./src/dotty/" -/* - @Test def pos_Coder() = compileFile(posDir, "Coder", twice) - @Test def pos_blockescapes() = compileFile(posDir, "blockescapes", twice) - @Test def pos_collections() = compileFile(posDir, "collections", twice) - @Test def pos_functions1() = compileFile(posDir, "functions1", twice) - @Test def pos_implicits1() = compileFile(posDir, "implicits1", twice) - @Test def pos_inferred() = compileFile(posDir, "inferred", twice) - @Test def pos_Patterns() = compileFile(posDir, "Patterns", twice) - @Test def pos_selftypes() = compileFile(posDir, "selftypes", twice) - @Test def pos_varargs() = compileFile(posDir, "varargs", twice) - @Test def pos_opassign() = compileFile(posDir, "opassign", twice) - @Test def pos_typedapply() = compileFile(posDir, "typedapply", twice) - @Test def pos_nameddefaults() = compileFile(posDir, "nameddefaults", twice) - @Test def pos_desugar() = compileFile(posDir, "desugar", twice) - @Test def pos_sigs() = compileFile(posDir, "sigs", twice) - @Test def pos_typers() = compileFile(posDir, "typers", twice) - @Test def pos_typedidents() = compileFile(posDir, "typedIdents", twice) - @Test def pos_assignments() = compileFile(posDir, "assignments", twice) - @Test def pos_packageobject() = compileFile(posDir, "packageobject", twice) - @Test def pos_overloaded() = compileFile(posDir, "overloaded", twice) - @Test def pos_templateParents() = compileFile(posDir, "templateParents", twice) - @Test def pos_structural() = compileFile(posDir, "structural", twice) - @Test def pos_i39 = compileFile(posDir, "i39", twice) - @Test def pos_overloadedAccess = compileFile(posDir, "overloadedAccess", twice) - @Test def pos_approximateUnion = compileFile(posDir, "approximateUnion", twice) -*/ + + @Test def pos_erasure = compileFile(posDir, "erasure", doErase) + @Test def pos_Coder() = compileFile(posDir, "Coder", doErase) + @Test def pos_blockescapes() = compileFile(posDir, "blockescapes", doErase) + @Test def pos_collections() = compileFile(posDir, "collections", doErase) + @Test def pos_functions1() = compileFile(posDir, "functions1", doErase) + @Test def pos_implicits1() = compileFile(posDir, "implicits1", doErase) + @Test def pos_inferred() = compileFile(posDir, "inferred", doErase) + @Test def pos_Patterns() = compileFile(posDir, "Patterns", doErase) + @Test def pos_selftypes() = compileFile(posDir, "selftypes", doErase) + @Test def pos_varargs() = compileFile(posDir, "varargs", doErase) + @Test def pos_opassign() = compileFile(posDir, "opassign", doErase) + @Test def pos_typedapply() = compileFile(posDir, "typedapply", doErase) + @Test def pos_nameddefaults() = compileFile(posDir, "nameddefaults", doErase) + @Test def pos_desugar() = compileFile(posDir, "desugar", doErase) + @Test def pos_sigs() = compileFile(posDir, "sigs", doErase) + @Test def pos_typers() = compileFile(posDir, "typers", doErase) + @Test def pos_typedidents() = compileFile(posDir, "typedIdents", doErase) + @Test def pos_assignments() = compileFile(posDir, "assignments", doErase) + @Test def pos_packageobject() = compileFile(posDir, "packageobject", doErase) + @Test def pos_overloaded() = compileFile(posDir, "overloaded", doErase) + @Test def pos_templateParents() = compileFile(posDir, "templateParents", doErase) + @Test def pos_structural() = compileFile(posDir, "structural", doErase) + @Test def pos_i39 = compileFile(posDir, "i39", doErase) + @Test def pos_overloadedAccess = compileFile(posDir, "overloadedAccess", doErase) + @Test def pos_approximateUnion = compileFile(posDir, "approximateUnion", doErase) + @Test def pos_all = compileFiles(posDir, twice) @Test def new_all = compileFiles(newDir, twice) diff --git a/tests/pos/unions.scala b/tests/pos/unions.scala new file mode 100644 index 000000000..779d1847e --- /dev/null +++ b/tests/pos/unions.scala @@ -0,0 +1,14 @@ +object unions { + + class A { + def f: String = "abc" + } + + class B { + def f: String = "bcd" + } + + val x: A | B = if (true) new A else new B + println(x.f) + +} |