aboutsummaryrefslogtreecommitdiff
path: root/compiler/src
diff options
context:
space:
mode:
authorGuillaume Martres <smarter@ubuntu.com>2016-12-21 22:40:36 +0100
committerGitHub <noreply@github.com>2016-12-21 22:40:36 +0100
commit404ce763d0f9ef82dae52e6aff3ec20950c67e29 (patch)
treee1c1b4ac13d26c214816b2f0582e573298c741f4 /compiler/src
parentee4f4a109b3ec3190886c75bb6d140087c33287d (diff)
parent9568eba6de7e881c6003dbfc95e46e6f83fa2e7b (diff)
downloaddotty-404ce763d0f9ef82dae52e6aff3ec20950c67e29.tar.gz
dotty-404ce763d0f9ef82dae52e6aff3ec20950c67e29.tar.bz2
dotty-404ce763d0f9ef82dae52e6aff3ec20950c67e29.zip
Merge pull request #1826 from dotty-staging/fix-compile-stdlib
Make more parts of stdlib compile
Diffstat (limited to 'compiler/src')
-rw-r--r--compiler/src/dotty/tools/dotc/ast/Desugar.scala30
-rw-r--r--compiler/src/dotty/tools/dotc/core/TypeComparer.scala10
-rw-r--r--compiler/src/dotty/tools/dotc/core/TypeOps.scala2
-rw-r--r--compiler/src/dotty/tools/dotc/core/Types.scala8
-rw-r--r--compiler/src/dotty/tools/dotc/transform/ElimByName.scala2
-rw-r--r--compiler/src/dotty/tools/dotc/transform/LiftTry.scala2
-rw-r--r--compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala33
-rw-r--r--compiler/src/dotty/tools/dotc/typer/Applications.scala26
-rw-r--r--compiler/src/dotty/tools/dotc/typer/FrontEnd.scala15
-rw-r--r--compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala20
-rw-r--r--compiler/src/dotty/tools/dotc/typer/RefChecks.scala7
-rw-r--r--compiler/src/dotty/tools/dotc/typer/Typer.scala23
12 files changed, 131 insertions, 47 deletions
diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala
index 13ddff08c..211683c0a 100644
--- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala
+++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala
@@ -7,6 +7,7 @@ import util.Positions._, Types._, Contexts._, Constants._, Names._, NameOps._, F
import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._
import Decorators._
import language.higherKinds
+import typer.FrontEnd
import collection.mutable.ListBuffer
import util.Property
import reporting.diagnostic.messages._
@@ -363,7 +364,7 @@ object desugar {
if (mods.is(Abstract) || hasRepeatedParam) Nil // cannot have default arguments for repeated parameters, hence copy method is not issued
else {
def copyDefault(vparam: ValDef) =
- makeAnnotated(defn.UncheckedVarianceAnnot, refOfDef(vparam))
+ makeAnnotated("scala.annotation.unchecked.uncheckedVariance", refOfDef(vparam))
val copyFirstParams = derivedVparamss.head.map(vparam =>
cpy.ValDef(vparam)(rhs = copyDefault(vparam)))
val copyRestParamss = derivedVparamss.tail.nestedMap(vparam =>
@@ -559,7 +560,7 @@ object desugar {
case VarPattern(named, tpt) =>
derivedValDef(original, named, tpt, rhs, mods)
case _ =>
- val rhsUnchecked = makeAnnotated(defn.UncheckedAnnot, rhs)
+ val rhsUnchecked = makeAnnotated("scala.unchecked", rhs)
val vars = getVariables(pat)
val isMatchingTuple: Tree => Boolean = {
case Tuple(es) => es.length == vars.length
@@ -688,11 +689,28 @@ object desugar {
new ImplicitFunction(params, body)
}
- /** Add annotation with class `cls` to tree:
- * tree @cls
+ /** Add annotation to tree:
+ * tree @fullName
+ *
+ * The annotation is usually represented as a TypeTree referring to the class
+ * with the given name `fullName`. However, if the annotation matches a file name
+ * that is still to be entered, the annotation is represented as a cascade of `Selects`
+ * following `fullName`. This is necessary so that we avoid reading an annotation from
+ * the classpath that is also compiled from source.
*/
- def makeAnnotated(cls: Symbol, tree: Tree)(implicit ctx: Context) =
- Annotated(tree, untpd.New(untpd.TypeTree(cls.typeRef), Nil))
+ def makeAnnotated(fullName: String, tree: Tree)(implicit ctx: Context) = {
+ val parts = fullName.split('.')
+ val ttree = ctx.typerPhase match {
+ case phase: FrontEnd if phase.stillToBeEntered(parts.last) =>
+ val prefix =
+ ((Ident(nme.ROOTPKG): Tree) /: parts.init)((qual, name) =>
+ Select(qual, name.toTermName))
+ Select(prefix, parts.last.toTypeName)
+ case _ =>
+ TypeTree(ctx.requiredClass(fullName).typeRef)
+ }
+ Annotated(tree, untpd.New(ttree, Nil))
+ }
private def derivedValDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit ctx: Context) = {
val vdef = ValDef(named.name.asTermName, tpt, rhs)
diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala
index 334306f19..8930983f3 100644
--- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala
+++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala
@@ -757,8 +757,14 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
if (args1.isEmpty) args2.isEmpty
else args2.nonEmpty && {
val v = tparams.head.paramVariance
- (v > 0 || isSubType(args2.head, args1.head)) &&
- (v < 0 || isSubType(args1.head, args2.head))
+ def isSub(tp1: Type, tp2: Type) = tp2 match {
+ case tp2: TypeBounds =>
+ tp2.contains(tp1)
+ case _ =>
+ (v > 0 || isSubType(tp2, tp1)) &&
+ (v < 0 || isSubType(tp1, tp2))
+ }
+ isSub(args1.head, args2.head)
} && isSubArgs(args1.tail, args2.tail, tparams)
/** Test whether `tp1` has a base type of the form `B[T1, ..., Tn]` where
diff --git a/compiler/src/dotty/tools/dotc/core/TypeOps.scala b/compiler/src/dotty/tools/dotc/core/TypeOps.scala
index db73daaa2..c2a7d7ea6 100644
--- a/compiler/src/dotty/tools/dotc/core/TypeOps.scala
+++ b/compiler/src/dotty/tools/dotc/core/TypeOps.scala
@@ -552,7 +552,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
def dynamicsEnabled =
featureEnabled(defn.LanguageModuleClass, nme.dynamics)
- def testScala2Mode(msg: String, pos: Position) = {
+ def testScala2Mode(msg: => String, pos: Position) = {
if (scala2Mode) migrationWarning(msg, pos)
scala2Mode
}
diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala
index 64fa48071..636204f64 100644
--- a/compiler/src/dotty/tools/dotc/core/Types.scala
+++ b/compiler/src/dotty/tools/dotc/core/Types.scala
@@ -217,6 +217,14 @@ object Types {
case _ => false
}
+ /** Is this the type of a method with a leading empty parameter list?
+ */
+ def isNullaryMethod(implicit ctx: Context): Boolean = this match {
+ case MethodType(Nil, _) => true
+ case tp: PolyType => tp.resultType.isNullaryMethod
+ case _ => false
+ }
+
/** Is this an alias TypeBounds? */
def isAlias: Boolean = this.isInstanceOf[TypeAlias]
diff --git a/compiler/src/dotty/tools/dotc/transform/ElimByName.scala b/compiler/src/dotty/tools/dotc/transform/ElimByName.scala
index 192227261..71ced3175 100644
--- a/compiler/src/dotty/tools/dotc/transform/ElimByName.scala
+++ b/compiler/src/dotty/tools/dotc/transform/ElimByName.scala
@@ -71,7 +71,7 @@ class ElimByName extends MiniPhaseTransform with InfoTransformer { thisTransform
def transformArg(arg: Tree, formal: Type): Tree = formal.dealias match {
case formalExpr: ExprType =>
- val argType = arg.tpe.widen
+ val argType = arg.tpe.widenIfUnstable
val argFun = arg match {
case Apply(Select(qual, nme.apply), Nil)
if qual.tpe.derivesFrom(defn.FunctionClass(0)) && isPureExpr(qual) =>
diff --git a/compiler/src/dotty/tools/dotc/transform/LiftTry.scala b/compiler/src/dotty/tools/dotc/transform/LiftTry.scala
index 6a273b91e..d01195614 100644
--- a/compiler/src/dotty/tools/dotc/transform/LiftTry.scala
+++ b/compiler/src/dotty/tools/dotc/transform/LiftTry.scala
@@ -57,7 +57,7 @@ class LiftTry extends MiniPhase with IdentityDenotTransformer { thisTransform =>
ctx.debuglog(i"lifting tree at ${tree.pos}, current owner = ${ctx.owner}")
val fn = ctx.newSymbol(
ctx.owner, ctx.freshName("liftedTree").toTermName, Synthetic | Method,
- MethodType(Nil, tree.tpe), coord = tree.pos)
+ MethodType(Nil, tree.tpe.widenIfUnstable), coord = tree.pos)
tree.changeOwnerAfter(ctx.owner, fn, thisTransform)
Block(DefDef(fn, tree) :: Nil, ref(fn).appliedToNone)
}
diff --git a/compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala b/compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala
index fea478c9b..3c11827fc 100644
--- a/compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala
+++ b/compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala
@@ -71,21 +71,24 @@ class SuperAccessors(thisTransformer: DenotTransformer) {
val Select(qual, name) = sel
val sym = sel.symbol
val clazz = qual.symbol.asClass
- var supername = name.superName
- if (clazz is Trait) supername = supername.expandedName(clazz)
-
- val superAcc = clazz.info.decl(supername).suchThat(_.signature == sym.signature).symbol orElse {
- ctx.debuglog(s"add super acc ${sym.showLocated} to $clazz")
- val deferredOrPrivate = if (clazz is Trait) Deferred | ExpandedName else Private
- val acc = ctx.newSymbol(
- clazz, supername, SuperAccessor | Artifact | Method | deferredOrPrivate,
- sel.tpe.widenSingleton.ensureMethodic, coord = sym.coord).enteredAfter(thisTransformer)
- // Diagnostic for SI-7091
- if (!accDefs.contains(clazz))
- ctx.error(s"Internal error: unable to store accessor definition in ${clazz}. clazz.hasPackageFlag=${clazz is Package}. Accessor required for ${sel} (${sel.show})", sel.pos)
- else accDefs(clazz) += DefDef(acc, EmptyTree)
- acc
- }
+ var superName = name.superName
+ if (clazz is Trait) superName = superName.expandedName(clazz)
+ val superInfo = sel.tpe.widenSingleton.ensureMethodic
+
+ val superAcc = clazz.info.decl(superName)
+ .suchThat(_.signature == superInfo.signature).symbol
+ .orElse {
+ ctx.debuglog(s"add super acc ${sym.showLocated} to $clazz")
+ val deferredOrPrivate = if (clazz is Trait) Deferred | ExpandedName else Private
+ val acc = ctx.newSymbol(
+ clazz, superName, SuperAccessor | Artifact | Method | deferredOrPrivate,
+ superInfo, coord = sym.coord).enteredAfter(thisTransformer)
+ // Diagnostic for SI-7091
+ if (!accDefs.contains(clazz))
+ ctx.error(s"Internal error: unable to store accessor definition in ${clazz}. clazz.hasPackageFlag=${clazz is Package}. Accessor required for ${sel} (${sel.show})", sel.pos)
+ else accDefs(clazz) += DefDef(acc, EmptyTree)
+ acc
+ }
This(clazz).select(superAcc).withPos(sel.pos)
}
diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala
index 8a18e63c0..42c24ffb7 100644
--- a/compiler/src/dotty/tools/dotc/typer/Applications.scala
+++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala
@@ -657,18 +657,20 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
case err: ErrorType => untpd.cpy.Apply(tree)(fun1, tree.args).withType(err)
case TryDynamicCallType => typedDynamicApply(tree, pt)
case _ =>
- tryEither {
- implicit ctx => simpleApply(fun1, proto)
- } {
- (failedVal, failedState) =>
- def fail = { failedState.commit(); failedVal }
- // Try once with original prototype and once (if different) with tupled one.
- // The reason we need to try both is that the decision whether to use tupled
- // or not was already taken but might have to be revised when an implicit
- // is inserted on the qualifier.
- tryWithImplicitOnQualifier(fun1, originalProto).getOrElse(
- if (proto eq originalProto) fail
- else tryWithImplicitOnQualifier(fun1, proto).getOrElse(fail))
+ if (originalProto.isDropped) fun1
+ else
+ tryEither {
+ implicit ctx => simpleApply(fun1, proto)
+ } {
+ (failedVal, failedState) =>
+ def fail = { failedState.commit(); failedVal }
+ // Try once with original prototype and once (if different) with tupled one.
+ // The reason we need to try both is that the decision whether to use tupled
+ // or not was already taken but might have to be revised when an implicit
+ // is inserted on the qualifier.
+ tryWithImplicitOnQualifier(fun1, originalProto).getOrElse(
+ if (proto eq originalProto) fail
+ else tryWithImplicitOnQualifier(fun1, proto).getOrElse(fail))
}
}
}
diff --git a/compiler/src/dotty/tools/dotc/typer/FrontEnd.scala b/compiler/src/dotty/tools/dotc/typer/FrontEnd.scala
index cd374e32c..6eff63e2b 100644
--- a/compiler/src/dotty/tools/dotc/typer/FrontEnd.scala
+++ b/compiler/src/dotty/tools/dotc/typer/FrontEnd.scala
@@ -19,6 +19,15 @@ class FrontEnd extends Phase {
override def isTyper = true
import ast.tpd
+ /** The contexts for compilation units that are parsed but not yet entered */
+ private var remaining: List[Context] = Nil
+
+ /** Does a source file ending with `<name>.scala` belong to a compilation unit
+ * that is parsed but not yet entered?
+ */
+ def stillToBeEntered(name: String): Boolean =
+ remaining.exists(_.compilationUnit.toString.endsWith(name + ".scala"))
+
def monitor(doing: String)(body: => Unit)(implicit ctx: Context) =
try body
catch {
@@ -75,7 +84,11 @@ class FrontEnd extends Phase {
}
unitContexts foreach (parse(_))
record("parsedTrees", ast.Trees.ntrees)
- unitContexts.foreach(enterSyms(_))
+ remaining = unitContexts
+ while (remaining.nonEmpty) {
+ enterSyms(remaining.head)
+ remaining = remaining.tail
+ }
unitContexts.foreach(enterAnnotations(_))
unitContexts.foreach(typeCheck(_))
record("total trees after typer", ast.Trees.ntrees)
diff --git a/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala b/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
index ed6b95c3b..eb46a131f 100644
--- a/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
+++ b/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
@@ -40,7 +40,9 @@ object ProtoTypes {
/** Test compatibility after normalization in a fresh typerstate. */
def normalizedCompatible(tp: Type, pt: Type)(implicit ctx: Context) = {
val nestedCtx = ctx.fresh.setExploreTyperState
- isCompatible(normalize(tp, pt)(nestedCtx), pt)(nestedCtx)
+ val normTp = normalize(tp, pt)(nestedCtx)
+ isCompatible(normTp, pt)(nestedCtx) ||
+ pt.isRef(defn.UnitClass) && normTp.isParameterless
}
private def disregardProto(pt: Type)(implicit ctx: Context): Boolean = pt.dealias match {
@@ -250,6 +252,22 @@ object ProtoTypes {
/** Somebody called the `tupled` method of this prototype */
def isTupled: Boolean = myTupled.isInstanceOf[FunProto]
+ /** If true, the application of this prototype was canceled. */
+ private var toDrop: Boolean = false
+
+ /** Cancel the application of this prototype. This can happen for a nullary
+ * application `f()` if `f` refers to a symbol that exists both in parameterless
+ * form `def f` and nullary method form `def f()`. A common example for such
+ * a method is `toString`. If in that case the type in the denotation is
+ * parameterless, we compensate by dropping the application.
+ */
+ def markAsDropped() = {
+ assert(args.isEmpty)
+ toDrop = true
+ }
+
+ def isDropped: Boolean = toDrop
+
override def toString = s"FunProto(${args mkString ","} => $resultType)"
def map(tm: TypeMap)(implicit ctx: Context): FunProto =
diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala
index dcbd444f9..3192546cd 100644
--- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala
+++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala
@@ -299,7 +299,9 @@ object RefChecks {
!member.isAnyOverride) {
// (*) Exclusion for default getters, fixes SI-5178. We cannot assign the Override flag to
// the default getter: one default getter might sometimes override, sometimes not. Example in comment on ticket.
- if (autoOverride(member))
+ // Also excluded under Scala2 mode are overrides of default methods of Java traits.
+ if (autoOverride(member) ||
+ other.owner.is(JavaTrait) && ctx.testScala2Mode("`override' modifier required when a Java 8 default method is re-implemented", member.pos))
member.setFlag(Override)
else if (member.owner != clazz && other.owner != clazz && !(other.owner derivesFrom member.owner))
emitOverrideError(
@@ -326,7 +328,8 @@ object RefChecks {
overrideError("needs to be a stable, immutable value")
} else if (member.is(ModuleVal) && !other.isRealMethod && !other.is(Deferred | Lazy)) {
overrideError("may not override a concrete non-lazy value")
- } else if (member.is(Lazy, butNot = Module) && !other.isRealMethod && !other.is(Lazy)) {
+ } else if (member.is(Lazy, butNot = Module) && !other.isRealMethod && !other.is(Lazy) &&
+ !ctx.testScala2Mode("may not override a non-lazy value", member.pos)) {
overrideError("may not override a non-lazy value")
} else if (other.is(Lazy) && !other.isRealMethod && !member.is(Lazy)) {
overrideError("must be declared lazy to override a lazy value")
diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala
index 07a27a498..fe158dfe2 100644
--- a/compiler/src/dotty/tools/dotc/typer/Typer.scala
+++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala
@@ -1640,13 +1640,19 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
case _ => false
}
- /** Add apply node or implicit conversions. Two strategies are tried, and the first
- * that is successful is picked. If neither of the strategies are successful, continues with
- * `fallBack`.
+ /** Potentially add apply node or implicit conversions. Before trying either,
+ * if the function is applied to an empty parameter list (), we try
+ *
+ * 0th strategy: If `tree` overrides a nullary method, mark the prototype
+ * so that the argument is dropped and return `tree` itself.
+ *
+ * After that, two strategies are tried, and the first that is successful is picked.
+ * If neither of the strategies are successful, continues with`fallBack`.
*
* 1st strategy: Try to insert `.apply` so that the result conforms to prototype `pt`.
* This strategy is not tried if the prototype represents already
* another `.apply` or `.apply()` selection.
+ *
* 2nd strategy: If tree is a select `qual.name`, try to insert an implicit conversion
* around the qualifier part `qual` so that the result conforms to the expected type
* with wildcard result type.
@@ -1661,8 +1667,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def tryImplicit =
tryInsertImplicitOnQualifier(tree, pt).getOrElse(fallBack)
- if (isApplyProto(pt)) tryImplicit
- else tryEither(tryApply(_))((_, _) => tryImplicit)
+ pt match {
+ case pt @ FunProto(Nil, _, _)
+ if tree.symbol.allOverriddenSymbols.exists(_.info.isNullaryMethod) =>
+ pt.markAsDropped()
+ tree
+ case _ =>
+ if (isApplyProto(pt)) tryImplicit
+ else tryEither(tryApply(_))((_, _) => tryImplicit)
+ }
}
/** If this tree is a select node `qual.name`, try to insert an implicit conversion