diff options
38 files changed, 465 insertions, 762 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/Trees.scala b/src/compiler/scala/tools/nsc/ast/Trees.scala index 1d29e33c50..9a6d32be01 100644 --- a/src/compiler/scala/tools/nsc/ast/Trees.scala +++ b/src/compiler/scala/tools/nsc/ast/Trees.scala @@ -95,11 +95,12 @@ trait Trees extends reflect.internal.Trees { self: Global => val (edefs, rest) = body span treeInfo.isEarlyDef val (evdefs, etdefs) = edefs partition treeInfo.isEarlyValDef val gvdefs = evdefs map { - case vdef @ ValDef(_, _, tpt, _) => copyValDef(vdef)( - // !!! I know "atPos in case" wasn't intentionally planted to - // add an air of mystery to this file, but it is the sort of - // comment which only its author could love. - tpt = atPos(vdef.pos.focus)(TypeTree() setOriginal tpt setPos tpt.pos.focus), // atPos in case + case vdef @ ValDef(_, _, tpt, _) => + copyValDef(vdef)( + // atPos for the new tpt is necessary, since the original tpt might have no position + // (when missing type annotation for ValDef for example), so even though setOriginal modifies the + // position of TypeTree, it would still be NoPosition. That's what the author meant. + tpt = atPos(vdef.pos.focus)(TypeTree() setOriginal tpt setPos tpt.pos.focus), rhs = EmptyTree ) } @@ -125,7 +126,7 @@ trait Trees extends reflect.internal.Trees { self: Global => DefDef(constrMods, nme.CONSTRUCTOR, List(), vparamss1, TypeTree(), Block(lvdefs ::: List(superCall), Literal(Constant()))))) } } - constrs foreach (ensureNonOverlapping(_, parents ::: gvdefs)) + constrs foreach (ensureNonOverlapping(_, parents ::: gvdefs, focus=false)) // Field definitions for the class - remove defaults. val fieldDefs = vparamss.flatten map (vd => copyValDef(vd)(mods = vd.mods &~ DEFAULTPARAM, rhs = EmptyTree)) diff --git a/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala b/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala index 801b4ad22b..aab1c8fb7e 100644 --- a/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala +++ b/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala @@ -252,6 +252,19 @@ trait CompilerControl { self: Global => /** Asks for a computation to be done quickly on the presentation compiler thread */ def ask[A](op: () => A): A = if (self.onCompilerThread) op() else scheduler doQuickly op + /** Asks for a computation to be done on presentation compiler thread, returning + * a response with the result or an exception + */ + def askForResponse[A](op: () => A): Response[A] = { + val r = new Response[A] + val ir = scheduler askDoQuickly op + ir onComplete { + case Left(result) => r set result + case Right(exc) => r raise exc + } + r + } + def onCompilerThread = Thread.currentThread == compileRunner /** Info given for every member found by completion @@ -390,7 +403,7 @@ trait CompilerControl { self: Global => case _ => println("don't know what to do with this " + action.getClass) } } - + override def doQuickly[A](op: () => A): A = { throw new FailedInterrupt(new Exception("Posted a work item to a compiler that's shutting down")) } diff --git a/src/compiler/scala/tools/nsc/interactive/Global.scala b/src/compiler/scala/tools/nsc/interactive/Global.scala index 2a435aa6f6..27b6cae2a6 100644 --- a/src/compiler/scala/tools/nsc/interactive/Global.scala +++ b/src/compiler/scala/tools/nsc/interactive/Global.scala @@ -1030,11 +1030,15 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "") } } - def getInstrumented(source: SourceFile, line: Int, response: Response[(String, Array[Char])]) { - respond(response) { - instrument(source, line) + def getInstrumented(source: SourceFile, line: Int, response: Response[(String, Array[Char])]) = + try { + interruptsEnabled = false + respond(response) { + instrument(source, line) + } + } finally { + interruptsEnabled = true } - } // ---------------- Helper classes --------------------------- diff --git a/src/compiler/scala/tools/nsc/interactive/REPL.scala b/src/compiler/scala/tools/nsc/interactive/REPL.scala index 1dcc979255..6876ea14e0 100644 --- a/src/compiler/scala/tools/nsc/interactive/REPL.scala +++ b/src/compiler/scala/tools/nsc/interactive/REPL.scala @@ -11,7 +11,7 @@ import scala.tools.nsc.symtab._ import scala.tools.nsc.ast._ import scala.tools.nsc.reporters._ import scala.tools.nsc.io._ -import scala.tools.nsc.scratchpad.{Executor, SourceInserter} +import scala.tools.nsc.scratchpad.SourceInserter import scala.tools.nsc.interpreter.AbstractFileClassLoader import java.io.{File, FileWriter} @@ -138,7 +138,6 @@ object REPL { * @param arguments Further argumenrs to pass to the compiler * @return Optionallu, if no -d option is given, the virtual directory * contained the generated bytecode classes - */ def compileInstrumented(iSourceName: String, arguments: List[String]): Option[AbstractFile] = { println("compiling "+iSourceName) val command = new CompilerCommand(iSourceName :: arguments, reporter.error(scala.reflect.internal.util.NoPosition, _)) @@ -176,6 +175,7 @@ object REPL { println("done") si.currentContents } + */ /** The method for implementing worksheet functionality. * @param arguments a file name, followed by optional command line arguments that are passed @@ -186,17 +186,22 @@ object REPL { * and outputs in the right column, or None if the presentation compiler * does not respond to askInstrumented. */ - def instrument(arguments: List[String], line: Int): Option[Array[Char]] = { + def instrument(arguments: List[String], line: Int): Option[String] = { val source = toSourceFile(arguments.head) // strip right hand side comment column and any trailing spaces from all lines val strippedSource = new BatchSourceFile(source.file, SourceInserter.stripRight(source.content)) + println("stripped source = "+strippedSource) comp.askReload(List(strippedSource), reloadResult) comp.askInstrumented(strippedSource, line, instrumentedResult) using(instrumentedResult) { case (iFullName, iContents) => + println(s"instrumented source $iFullName = ${iContents.mkString}") val iSourceName = writeInstrumented(iFullName, iContents) - val vdirOpt = compileInstrumented(iSourceName, arguments.tail) + iSourceName +/* + * val vdirOpt = compileInstrumented(iSourceName, arguments.tail) runInstrumented(vdirOpt, iFullName, strippedSource.content) + */ } } diff --git a/src/compiler/scala/tools/nsc/interactive/RangePositions.scala b/src/compiler/scala/tools/nsc/interactive/RangePositions.scala index 06828f3a3a..b702d2787c 100644 --- a/src/compiler/scala/tools/nsc/interactive/RangePositions.scala +++ b/src/compiler/scala/tools/nsc/interactive/RangePositions.scala @@ -41,11 +41,11 @@ self: scala.tools.nsc.Global => /** A position that wraps a set of trees. * The point of the wrapping position is the point of the default position. * If some of the trees are ranges, returns a range position enclosing all ranges - * Otherwise returns default position. + * Otherwise returns default position that is either focused or not. */ - override def wrappingPos(default: Position, trees: List[Tree]): Position = { + override def wrappingPos(default: Position, trees: List[Tree], focus: Boolean): Position = { val ranged = trees filter (_.pos.isRange) - if (ranged.isEmpty) default.focus + if (ranged.isEmpty) if (focus) default.focus else default else new RangePosition(default.source, (ranged map (_.pos.start)).min, default.point, (ranged map (_.pos.end)).max) } @@ -59,13 +59,25 @@ self: scala.tools.nsc.Global => if (headpos.isDefined) wrappingPos(headpos, trees) else headpos } -/* - override def integratePos(tree: Tree, pos: Position) = - if (pos.isSynthetic && !tree.pos.isSynthetic) tree.syntheticDuplicate - else tree -*/ - // -------------- ensuring no overlaps ------------------------------- + + /** Ensure that given tree has no positions that overlap with + * any of the positions of `others`. This is done by + * shortening the range, assigning TransparentPositions + * to some of the nodes in `tree` or focusing on the position. + */ + override def ensureNonOverlapping(tree: Tree, others: List[Tree], focus: Boolean) { + def isOverlapping(pos: Position) = + pos.isRange && (others exists (pos overlaps _.pos)) + if (isOverlapping(tree.pos)) { + val children = tree.children + children foreach (ensureNonOverlapping(_, others, focus)) + if (tree.pos.isOpaqueRange) { + val wpos = wrappingPos(tree.pos, children, focus) + tree setPos (if (isOverlapping(wpos)) tree.pos.makeTransparent else wpos) + } + } + } def solidDescendants(tree: Tree): List[Tree] = if (tree.pos.isTransparent) tree.children flatMap solidDescendants @@ -106,24 +118,6 @@ self: scala.tools.nsc.Global => if (ts.head == t) replacement ::: ts.tail else ts.head :: replace(ts.tail, t, replacement) - /** Ensure that given tree has no positions that overlap with - * any of the positions of `others`. This is done by - * shortening the range or assigning TransparentPositions - * to some of the nodes in `tree`. - */ - override def ensureNonOverlapping(tree: Tree, others: List[Tree]) { - def isOverlapping(pos: Position) = - pos.isRange && (others exists (pos overlaps _.pos)) - if (isOverlapping(tree.pos)) { - val children = tree.children - children foreach (ensureNonOverlapping(_, others)) - if (tree.pos.isOpaqueRange) { - val wpos = wrappingPos(tree.pos.focus, children) - tree setPos (if (isOverlapping(wpos)) tree.pos.makeTransparent else wpos) - } - } - } - /** Does given list of trees have mutually non-overlapping positions? * pre: None of the trees is transparent */ diff --git a/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala b/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala index c79248e1c1..bd1869e1a4 100644 --- a/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala +++ b/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala @@ -30,8 +30,12 @@ trait ScratchPadMaker { self: Global => private def literal(str: String) = "\"\"\""+str+"\"\"\"" + private val prologue = "import scala.runtime.WorksheetSupport._; def main(args: Array[String])=$execute{" + + private val epilogue = "}" + private def applyPendingPatches(offset: Int) = { - if (skipped == 0) patches += Patch(offset, "import scala.tools.nsc.scratchpad.Executor._; ") + if (skipped == 0) patches += Patch(offset, prologue) for (msg <- toPrint) patches += Patch(offset, ";System.out.println("+msg+")") toPrint.clear() } @@ -92,10 +96,12 @@ trait ScratchPadMaker { self: Global => case PackageDef(_, _) => super.traverse(tree) case ModuleDef(_, name, Template(_, _, body)) => - if (objectName.length == 0) - objectName = tree.symbol.fullName + val topLevel = objectName.isEmpty + if (topLevel) objectName = tree.symbol.fullName body foreach traverseStat applyPendingPatches(skipped) + if (topLevel) + patches += Patch(skipped, epilogue) case _ => } diff --git a/src/compiler/scala/tools/nsc/scratchpad/Executor.scala b/src/compiler/scala/tools/nsc/scratchpad/Executor.scala deleted file mode 100644 index 89523df71e..0000000000 --- a/src/compiler/scala/tools/nsc/scratchpad/Executor.scala +++ /dev/null @@ -1,55 +0,0 @@ -package scala.tools.nsc.scratchpad - -import java.io.{PrintStream, OutputStreamWriter, Writer} - -import scala.runtime.ScalaRunTime.stringOf -import java.lang.reflect.InvocationTargetException -import scala.reflect.runtime.ReflectionUtils._ - -object Executor { - - println("exec started") - - private var currentWriter: CommentWriter = null - - /** Execute module with given name, redirecting all output to given - * source inserter. Catch all exceptions and print stacktrace of underlying causes. - */ - def execute(name: String, si: SourceInserter, classLoader: ClassLoader = getClass.getClassLoader) { - val oldSysOut = System.out - val oldSysErr = System.err - val oldConsOut = Console.out - val oldConsErr = Console.err - val oldCwr = currentWriter - currentWriter = new CommentWriter(si) - val newOut = new PrintStream(new CommentOutputStream(currentWriter)) - System.setOut(newOut) - System.setErr(newOut) - Console.setOut(newOut) - Console.setErr(newOut) - try { - singletonInstance(classLoader, name) - } catch { - case ex: Throwable => - unwrapThrowable(ex) match { - case _: StopException => ; - case cause => cause.printStackTrace() - } - } finally { - currentWriter.close() - System.setOut(oldSysOut) - System.setErr(oldSysErr) - Console.setOut(oldConsOut) - Console.setErr(oldConsErr) - currentWriter = oldCwr - } - } - - def $skip(n: Int) = currentWriter.skip(n) - - def $stop() = throw new StopException - - def $show(x: Any): String = stringOf(x, scala.Int.MaxValue) -} - -class StopException extends Exception diff --git a/src/compiler/scala/tools/nsc/scratchpad/Mixer.scala b/src/compiler/scala/tools/nsc/scratchpad/Mixer.scala new file mode 100644 index 0000000000..46ccc32097 --- /dev/null +++ b/src/compiler/scala/tools/nsc/scratchpad/Mixer.scala @@ -0,0 +1,98 @@ +package scala.tools.nsc.scratchpad + +import java.io.{FileInputStream, InputStreamReader, IOException} + +import scala.runtime.ScalaRunTime.stringOf +import java.lang.reflect.InvocationTargetException +import scala.reflect.runtime.ReflectionUtils._ +import collection.mutable.ArrayBuffer + +class Mixer { + + protected val stdSeparator = "//> " + protected val ctdSeparator = "//| " + protected val sepColumn = 50 + protected val tabInc = 8 + + type Comments = Seq[(Int, Array[Char])] + + def parseComments(comments: Array[Char]): Iterator[(Int, Array[Char])] = new Iterator[(Int, Array[Char])] { + var idx = 0 + def hasNext = idx < comments.length + def next() = { + val nextSpace = comments indexOf (' ', idx) + var nextNL = comments indexOf ('\n', nextSpace + 1) + if (nextNL < 0) nextNL = comments.length + val result = + (new String(comments.slice(idx, nextSpace)).toInt, comments.slice(nextSpace + 1, nextNL)) + idx = nextNL + 1 + result + } + } + + def mix(source: Array[Char], comments: Array[Char]): Array[Char] = { + val mixed = new ArrayBuffer[Char] + var written = 0 + def align() = { + var idx = mixed.lastIndexOf('\n') + 1 + var col = 0 + while (idx < mixed.length) { + col = + if (mixed(idx) == '\t') (col / tabInc) * tabInc + tabInc + else col + 1 + idx += 1 + } + if (col > sepColumn) { + mixed += '\n' + col = 0 + } + mixed ++= (" " * (sepColumn - col)) + } + for ((offset, cs) <- parseComments(comments)) { + val sep = + if (written < offset) { + for (i <- written until offset) mixed += source(i) + written = offset + stdSeparator + } else { + mixed += '\n' + ctdSeparator + } + align() + mixed ++= sep ++= cs + } + mixed ++= source.view(written, source.length) + mixed.toArray + } + +} + +object Mixer extends Mixer { + + def contents(name: String): Array[Char] = { + val page = new Array[Char](2 << 14) + val buf = new ArrayBuffer[Char] + val in = new FileInputStream(name) + val rdr = new InputStreamReader(in) + var nread = 0 + do { + nread = rdr.read(page, 0, page.length) + buf ++= (if (nread == page.length) page else page.take(nread)) + } while (nread >= 0) + buf.toArray + } + + def main(args: Array[String]) { + val mixer = new Mixer + try { + require(args.length == 2, "required arguments: file1 file2") + val source = contents(args(0)) + val comments = contents(args(1)) + val mixed = mixer.mix(source, comments) + println(mixed.mkString) + } catch { + case ex: IOException => + println("error: "+ ex.getMessage) + } + } +} diff --git a/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala b/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala index 9b4e793241..46c52e68a2 100644 --- a/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala +++ b/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala @@ -112,11 +112,23 @@ abstract class SymbolLoaders { enterClassAndModule(root, name, new SourcefileLoader(src)) } + /** The package objects of scala and scala.reflect should always + * be loaded in binary if classfiles are available, even if sourcefiles + * are newer. Late-compiling these objects from source leads to compilation + * order issues. + * Note: We do a name-base comparison here because the method is called before we even + * have ReflectPackage defined. + */ + def binaryOnly(owner: Symbol, name: String): Boolean = + name == "package" && + (owner.fullName == "scala" || owner.fullName == "scala.reflect") + /** Initialize toplevel class and module symbols in `owner` from class path representation `classRep` */ def initializeFromClassPath(owner: Symbol, classRep: ClassPath[platform.BinaryRepr]#ClassRep) { ((classRep.binary, classRep.source) : @unchecked) match { - case (Some(bin), Some(src)) if platform.needCompile(bin, src) => + case (Some(bin), Some(src)) + if platform.needCompile(bin, src) && !binaryOnly(owner, classRep.name) => if (settings.verbose.value) inform("[symloader] picked up newer source file for " + src.path) global.loaders.enterToplevelsFromSource(owner, classRep.name, src) case (None, Some(src)) => diff --git a/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala b/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala index 5f66cadbc9..e937589f54 100644 --- a/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala +++ b/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala @@ -78,13 +78,13 @@ abstract class ExtensionMethods extends Transform with TypingTransformers { } /** This method removes the `$this` argument from the parameter list a method. - * + * * A method may be a `PolyType`, in which case we tear out the `$this` and the class * type params from its nested `MethodType`. * It may be a `MethodType`, either with a curried parameter list in which the first argument * is a `$this` - we just return the rest of the list. * This means that the corresponding symbol was generated during `extmethods`. - * + * * It may also be a `MethodType` in which the `$this` does not appear in a curried parameter list. * The curried lists disappear during `uncurry`, and the methods may be duplicated afterwards, * for instance, during `specialize`. @@ -105,6 +105,14 @@ abstract class ExtensionMethods extends Transform with TypingTransformers { private val extensionDefs = mutable.Map[Symbol, mutable.ListBuffer[Tree]]() + def checkNonCyclic(pos: Position, seen: Set[Symbol], clazz: Symbol): Unit = + if (seen contains clazz) + unit.error(pos, "value class may not unbox to itself") + else { + val unboxed = erasure.underlyingOfValueClass(clazz).typeSymbol + if (unboxed.isDerivedValueClass) checkNonCyclic(pos, seen + clazz, unboxed) + } + def extensionMethInfo(extensionMeth: Symbol, origInfo: Type, clazz: Symbol): Type = { var newTypeParams = cloneSymbolsAtOwner(clazz.typeParams, extensionMeth) val thisParamType = appliedType(clazz.typeConstructor, newTypeParams map (_.tpeHK)) @@ -129,6 +137,7 @@ abstract class ExtensionMethods extends Transform with TypingTransformers { tree match { case Template(_, _, _) => if (currentOwner.isDerivedValueClass) { + checkNonCyclic(currentOwner.pos, Set(), currentOwner) extensionDefs(currentOwner.companionModule) = new mutable.ListBuffer[Tree] currentOwner.primaryConstructor.makeNotPrivate(NoSymbol) super.transform(tree) diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index b1c3249e35..e57cae00e0 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1401,6 +1401,15 @@ trait Typers extends Modes with Adaptations with Tags { unit.error(clazz.pos, "value class needs to have exactly one public val parameter") } } + body foreach { + case md: ModuleDef => + unit.error(md.pos, "value class may not have nested module definitions") + case cd: ClassDef => + unit.error(cd.pos, "value class may not have nested class definitions") + case md: DefDef if md.symbol.isConstructor && !md.symbol.isPrimaryConstructor => + unit.error(md.pos, "value class may not have secondary constructors") + case _ => + } for (tparam <- clazz.typeParams) if (tparam hasAnnotation definitions.SpecializedClass) unit.error(tparam.pos, "type parameter of value class may not be specialized") @@ -5019,7 +5028,9 @@ trait Typers extends Modes with Adaptations with Tags { if (isPatternMode) { val uncheckedTypeExtractor = extractorForUncheckedType(tpt.pos, tptTyped.tpe) val ownType = inferTypedPattern(tptTyped, tptTyped.tpe, pt, canRemedy = uncheckedTypeExtractor.nonEmpty) - treeTyped setType ownType + // println(s"Typed($expr, ${tpt.tpe}) : $pt --> $ownType (${isFullyDefined(ownType)}, ${makeFullyDefined(ownType)})") + // make fully defined to avoid bounded wildcard types that may be in pt from calling dropExistential (SI-2038) + treeTyped setType (if (isFullyDefined(ownType)) ownType else makeFullyDefined(ownType)) //ownType uncheckedTypeExtractor match { case None => treeTyped diff --git a/src/compiler/scala/tools/nsc/util/InterruptReq.scala b/src/compiler/scala/tools/nsc/util/InterruptReq.scala index 61aaa1bdcb..816d16f767 100644 --- a/src/compiler/scala/tools/nsc/util/InterruptReq.scala +++ b/src/compiler/scala/tools/nsc/util/InterruptReq.scala @@ -2,6 +2,7 @@ package scala.tools.nsc package util /** A class of work items to be used in interrupt requests. + * Todo: we should replace the Eithers by Futures or Try's. */ abstract class InterruptReq { /** The result type of the operation @@ -11,9 +12,14 @@ abstract class InterruptReq { /** The operation to be performed */ protected val todo: () => R + type Continuation = Either[R, Throwable] => Unit + /** The result provided */ private var result: Option[Either[R, Throwable]] = None + /** The continuations waiting asynchronously on a provided result */ + private var waiting: List[Continuation] = Nil + /** To be called from interrupted server to execute demanded task */ def execute(): Unit = synchronized { try { @@ -22,6 +28,7 @@ abstract class InterruptReq { case t: Throwable => result = Some(Right(t)) } finally { notify() + for (k <- waiting.reverse) k(result.get) } } @@ -38,6 +45,10 @@ abstract class InterruptReq { case Right(t) => throw new FailedInterrupt(t) } } + + def onComplete(k: Continuation) = synchronized { + waiting = k :: waiting + } } class FailedInterrupt(cause: Throwable) extends Exception("Compiler exception during call to 'ask'", cause) diff --git a/src/compiler/scala/tools/nsc/util/WorkScheduler.scala b/src/compiler/scala/tools/nsc/util/WorkScheduler.scala index 8c037cbda5..b1f4696d3e 100644 --- a/src/compiler/scala/tools/nsc/util/WorkScheduler.scala +++ b/src/compiler/scala/tools/nsc/util/WorkScheduler.scala @@ -54,6 +54,11 @@ class WorkScheduler { /** Called from client: have interrupt executed by server and return result */ def doQuickly[A](op: () => A): A = { + val ir = askDoQuickly(op) + ir.getResult() + } + + def askDoQuickly[A](op: () => A): InterruptReq { type R = A } = { val ir = new InterruptReq { type R = A val todo = op @@ -62,7 +67,7 @@ class WorkScheduler { interruptReqs enqueue ir notify() } - ir.getResult() + ir } /** Called from client: have action executed by server */ diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index f11dfb72ae..7d37fa4aa1 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -26,7 +26,7 @@ case class StringContext(parts: String*) { * @param `args` The arguments to be checked. * @throws An `IllegalArgumentException` if this is not the case. */ - def checkLengths(args: Any*): Unit = + def checkLengths(args: Seq[Any]): Unit = if (parts.length != args.length + 1) throw new IllegalArgumentException("wrong number of arguments for interpolated string") @@ -42,11 +42,27 @@ case class StringContext(parts: String*) { * @throws A `StringContext.InvalidEscapeException` if if a `parts` string contains a backslash (`\`) character * that does not start a valid escape sequence. */ - def s(args: Any*): String = { - checkLengths(args: _*) + def s(args: Any*): String = standardInterpolator(treatEscapes, args) + + /** The raw string interpolator. + * + * It inserts its arguments between corresponding parts of the string context. + * As opposed to the simple string interpolator `s`, this one does not treat + * standard escape sequences as defined in the Scala specification. + * @param `args` The arguments to be inserted into the resulting string. + * @throws An `IllegalArgumentException` + * if the number of `parts` in the enclosing `StringContext` does not exceed + * the number of arguments `arg` by exactly 1. + * @throws A `StringContext.InvalidEscapeException` if if a `parts` string contains a backslash (`\`) character + * that does not start a valid escape sequence. + */ + def raw(args: Any*): String = standardInterpolator(identity, args) + + def standardInterpolator(process: String => String, args: Seq[Any]): String = { + checkLengths(args) val pi = parts.iterator val ai = args.iterator - val bldr = new java.lang.StringBuilder(treatEscapes(pi.next())) + val bldr = new java.lang.StringBuilder(process(pi.next())) while (ai.hasNext) { bldr append ai.next bldr append treatEscapes(pi.next()) diff --git a/src/library/scala/runtime/WorksheetSupport.scala b/src/library/scala/runtime/WorksheetSupport.scala new file mode 100644 index 0000000000..db6d6359a3 --- /dev/null +++ b/src/library/scala/runtime/WorksheetSupport.scala @@ -0,0 +1,87 @@ +package scala.runtime +import java.io.{OutputStream, PrintStream} +import scala.runtime.ScalaRunTime.stringOf + +/** A utility object that's needed by the code that executes a worksheet. + */ +object WorksheetSupport { + + /** The offset in the source which should be printed */ + private var currentOffset = 0 + + /** A stream that flushes in regular intervals so that output can be captured + * in real time. The flush interval is determined by the field "flushInterval". + * By default it is 30ms. + */ + private class FlushedOutputStream(out: OutputStream) extends OutputStream { + private var lastFlush: Long = 0L + protected val flushInterval = 30000000L // 30ms + private var lastCh: Int = '\n' + override def write(b: Array[Byte], off: Int, len: Int) = { + for (idx <- off until (off + len min b.length)) writeOne(b(idx)) + flush() + } + override def write(c: Int) { + writeOne(c) + flush() + } + override def flush() { + val current = System.nanoTime + if (current - lastFlush >= flushInterval) { + out.flush() + lastFlush = current + } + } + def writeOne(c: Int) { + if (lastCh == '\n') { + lastCh = 0 + write((currentOffset+" ").getBytes) + } + out.write(c) + lastCh = c + } + def ensureNewLine() = if (lastCh != '\n') writeOne('\n') + } + + private val flushedOut = new FlushedOutputStream(System.out) + private val printOut = new PrintStream(flushedOut) + + private def redirected(op: => Unit) = { + val oldSysOut = System.out + val oldSysErr = System.err + val oldConsOut = Console.out + val oldConsErr = Console.err + System.setOut(printOut) + System.setErr(printOut) + Console.setOut(printOut) + Console.setErr(printOut) + try op + finally { + printOut.close() + System.setOut(oldSysOut) + System.setErr(oldSysErr) + Console.setOut(oldConsOut) + Console.setErr(oldConsErr) + } + } + + def $execute(op: => Unit) = redirected { + try op + catch { + case ex: StopException => ; + case ex => ex.printStackTrace() + } + } + + def $skip(n: Int) = { + flushedOut.ensureNewLine() + currentOffset += n + } + + def $stop() = throw new StopException + + def $show(x: Any): String = stringOf(x, scala.Int.MaxValue) +} + +class StopException extends Exception + diff --git a/src/reflect/scala/reflect/api/Symbols.scala b/src/reflect/scala/reflect/api/Symbols.scala index 1d2888961b..c94c796279 100644 --- a/src/reflect/scala/reflect/api/Symbols.scala +++ b/src/reflect/scala/reflect/api/Symbols.scala @@ -232,31 +232,6 @@ trait Symbols extends base.Symbols { self: Universe => /** The overloaded alternatives of this symbol */ def alternatives: List[Symbol] - /** Performs method overloading resolution. More precisely, resolves an overloaded TermSymbol - * to a single, non-overloaded TermSymbol that accepts the specified argument types. - * @param pre The prefix type, i.e. the type of the value the method is dispatched on. - * This is required when resolving references to type parameters of the type - * the method is declared in. For example if the method is declared in class `List[A]`, - * providing the prefix as `List[Int]` allows the overloading resolution to use - * `Int` instead of `A`. - * @param targs Type arguments that a candidate alternative must be able to accept. Candidates - * will be considered with these arguments substituted for their corresponding - * type parameters. - * @param posVargs Positional argument types that a candidate alternative must be able to accept. - * @param nameVargs Named argument types that a candidate alternative must be able to accept. - * Each element in the sequence should be a pair of a parameter name and an - * argument type. - * @param expected Return type that a candidate alternative has to be compatible with. - * @return Either a single, non-overloaded Symbol referring to the selected alternative - * or NoSymbol if no single member could be selected given the passed arguments. - */ - def resolveOverloaded( - pre: Type = NoPrefix, - targs: Seq[Type] = List(), - posVargs: Seq[Type] = List(), - nameVargs: Seq[(TermName, Type)] = List(), - expected: Type = NoType - ): Symbol } /** The API of type symbols */ diff --git a/src/reflect/scala/reflect/internal/Positions.scala b/src/reflect/scala/reflect/internal/Positions.scala index 6ae9b40fcb..faa161d6b1 100644 --- a/src/reflect/scala/reflect/internal/Positions.scala +++ b/src/reflect/scala/reflect/internal/Positions.scala @@ -10,23 +10,25 @@ trait Positions extends api.Positions { self: SymbolTable => /** A position that wraps a set of trees. * The point of the wrapping position is the point of the default position. * If some of the trees are ranges, returns a range position enclosing all ranges - * Otherwise returns default position. + * Otherwise returns default position that is either focused or not. */ - def wrappingPos(default: Position, trees: List[Tree]): Position = default + def wrappingPos(default: Position, trees: List[Tree]) = wrappingPos(default, trees, true) + def wrappingPos(default: Position, trees: List[Tree], focus: Boolean): Position = default /** A position that wraps the non-empty set of trees. * The point of the wrapping position is the point of the first trees' position. - * If all some the trees are non-synthetic, returns a range position enclosing the non-synthetic trees + * If some of the trees are non-synthetic, returns a range position enclosing the non-synthetic trees * Otherwise returns a synthetic offset position to point. */ def wrappingPos(trees: List[Tree]): Position = trees.head.pos /** Ensure that given tree has no positions that overlap with * any of the positions of `others`. This is done by - * shortening the range or assigning TransparentPositions - * to some of the nodes in `tree`. + * shortening the range, assigning TransparentPositions + * to some of the nodes in `tree` or focusing on the position. */ - def ensureNonOverlapping(tree: Tree, others: List[Tree]) {} + def ensureNonOverlapping(tree: Tree, others: List[Tree]){ ensureNonOverlapping(tree, others, true) } + def ensureNonOverlapping(tree: Tree, others: List[Tree], focus: Boolean) {} trait PosAssigner extends Traverser { var pos: Position diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala index 5c9999b3bd..e6a9cb46c6 100644 --- a/src/reflect/scala/reflect/internal/Symbols.scala +++ b/src/reflect/scala/reflect/internal/Symbols.scala @@ -84,281 +84,6 @@ trait Symbols extends api.Symbols { self: SymbolTable => def getAnnotations: List[AnnotationInfo] = { initialize; annotations } def setAnnotations(annots: AnnotationInfo*): this.type = { setAnnotations(annots.toList); this } - def resolveOverloaded( - pre: Type, - targs: Seq[Type], - posVargTypes: Seq[Type], - nameVargTypes: Seq[(TermName, Type)], - expected: Type - ): Symbol = { - - // Begin Correlation Helpers - - def isCompatible(tp: Type, pt: Type): Boolean = { - def isCompatibleByName(tp: Type, pt: Type): Boolean = pt match { - case TypeRef(_, ByNameParamClass, List(res)) if !definitions.isByNameParamType(tp) => - isCompatible(tp, res) - case _ => - false - } - (tp weak_<:< pt) || isCompatibleByName(tp, pt) - } - - def signatureAsSpecific(method1: MethodSymbol, method2: MethodSymbol): Boolean = { - (substituteTypeParams(method1), substituteTypeParams(method2)) match { - case (NullaryMethodType(r1), NullaryMethodType(r2)) => - r1 weak_<:< r2 - case (NullaryMethodType(_), MethodType(_, _)) => - true - case (MethodType(_, _), NullaryMethodType(_)) => - false - case (MethodType(p1, _), MethodType(p2, _)) => - val len = p1.length max p2.length - val sub = extend(p1 map (_.typeSignature), len) - val sup = extend(p2 map (_.typeSignature), len) - (sub corresponds sup)(isCompatible) - } - } - - def scopeMoreSpecific(method1: MethodSymbol, method2: MethodSymbol): Boolean = { - val o1 = method1.owner.asClassSymbol - val o2 = method2.owner.asClassSymbol - val c1 = if (o1.hasFlag(Flag.MODULE)) o1.companionSymbol else o1 - val c2 = if (o2.hasFlag(Flag.MODULE)) o2.companionSymbol else o2 - c1.typeSignature <:< c2.typeSignature - } - - def moreSpecific(method1: MethodSymbol, method2: MethodSymbol): Boolean = { - def points(m1: MethodSymbol, m2: MethodSymbol) = { - val p1 = if (signatureAsSpecific(m1, m2)) 1 else 0 - val p2 = if (scopeMoreSpecific(m1, m2)) 1 else 0 - p1 + p2 - } - points(method1, method2) > points(method2, method1) - } - - def combineInto ( - variadic: Boolean - )( - positional: Seq[Type], - named: Seq[(TermName, Type)] - )( - target: Seq[TermName], - defaults: Map[Int, Type] - ): Option[Seq[Type]] = { - - val offset = positional.length - val unfilled = target.zipWithIndex drop offset - val canAcceptAllNameVargs = named forall { case (argName, _) => - unfilled exists (_._1 == argName) - } - - val paramNamesUnique = { - named.length == named.map(_._1).distinct.length - } - - if (canAcceptAllNameVargs && paramNamesUnique) { - - val rest = unfilled map { case (paramName, paramIndex) => - val passedIn = named.collect { - case (argName, argType) if argName == paramName => argType - }.headOption - - passedIn orElse defaults.get(paramIndex).map(_.asInstanceOf[Type]) - } - - val rest1 = { - if (variadic && !rest.isEmpty && !rest.last.isDefined) rest.init - else rest - } - - - if (rest1 forall (_.isDefined)) { - val joined = positional ++ rest1.map(_.get) - val repeatedCollapsed = { - if (variadic) { - val (normal, repeated) = joined.splitAt(target.length - 1) - if (repeated.forall(_ =:= repeated.head)) Some(normal ++ repeated.headOption) - else None - } - else Some(joined) - } - if (repeatedCollapsed.exists(_.length == target.length)) - repeatedCollapsed - else if (variadic && repeatedCollapsed.exists(_.length == target.length - 1)) - repeatedCollapsed - else None - } else None - - } else None - } - - // Begin Reflection Helpers - - // Replaces a repeated parameter type at the end of the parameter list - // with a number of non-repeated parameter types in order to pad the - // list to be nargs in length - def extend(types: Seq[Type], nargs: Int): Seq[Type] = { - if (isVarArgTypes(types)) { - val repeatedType = types.last.normalize.typeArgs.head - types.init ++ Seq.fill(nargs - (types.length - 1))(repeatedType) - } else types - } - - // Replaces by-name parameters with their result type and - // TypeRefs with the thing they reference - def unwrap(paramType: Type): Type = paramType match { - case TypeRef(_, IntClass, _) => typeOf[Int] - case TypeRef(_, LongClass, _) => typeOf[Long] - case TypeRef(_, ShortClass, _) => typeOf[Short] - case TypeRef(_, ByteClass, _) => typeOf[Byte] - case TypeRef(_, CharClass, _) => typeOf[Char] - case TypeRef(_, FloatClass, _) => typeOf[Float] - case TypeRef(_, DoubleClass, _) => typeOf[Double] - case TypeRef(_, BooleanClass, _) => typeOf[Boolean] - case TypeRef(_, UnitClass, _) => typeOf[Unit] - case TypeRef(_, NullClass, _) => typeOf[Null] - case TypeRef(_, AnyClass, _) => typeOf[Any] - case TypeRef(_, NothingClass, _) => typeOf[Nothing] - case TypeRef(_, AnyRefClass, _) => typeOf[AnyRef] - case TypeRef(_, ByNameParamClass, List(resultType)) => unwrap(resultType) - case t: Type => t - } - - // Gives the names of the parameters to a method - def paramNames(signature: Type): Seq[TermName] = signature match { - case PolyType(_, resultType) => paramNames(resultType) - case MethodType(params, _) => params.map(_.name.asInstanceOf[TermName]) - case NullaryMethodType(_) => Seq.empty - } - - def valParams(signature: Type): Seq[TermSymbol] = signature match { - case PolyType(_, resultType) => valParams(resultType) - case MethodType(params, _) => params.map(_.asTermSymbol) - case NullaryMethodType(_) => Seq.empty - } - - // Returns a map from parameter index to default argument type - def defaultTypes(method: MethodSymbol): Map[Int, Type] = { - val typeSig = substituteTypeParams(method) - val owner = method.owner - valParams(typeSig).zipWithIndex.filter(_._1.hasFlag(Flag.DEFAULTPARAM)).map { case(_, index) => - val name = nme.defaultGetterName(method.name.decodedName, index + 1) - val default = owner.asType member name - index -> default.typeSignature.asInstanceOf[NullaryMethodType].resultType - }.toMap - } - - // True if any of method's parameters have default values. False otherwise. - def usesDefault(method: MethodSymbol): Boolean = valParams(method.typeSignature) drop(posVargTypes).length exists { param => - (param hasFlag Flag.DEFAULTPARAM) && nameVargTypes.forall { case (argName, _) => - param.name != argName - } - } - - // The number of type parameters that the method takes - def numTypeParams(x: MethodSymbol): Int = { - x.typeSignature.typeParams.length - } - - def substituteTypeParams(m: MethodSymbol): Type = { - (pre memberType m) match { - case m: MethodType => m - case n: NullaryMethodType => n - case PolyType(tparams, rest) => rest.substituteTypes(tparams, targs.toList) - } - } - - // Begin Selection Helpers - - def select( - alternatives: Seq[MethodSymbol], - filters: Seq[Seq[MethodSymbol] => Seq[MethodSymbol]] - ): Seq[MethodSymbol] = - filters.foldLeft(alternatives)((a, f) => { - if (a.size > 1) f(a) else a - }) - - // Drop arguments that take the wrong number of type - // arguments. - val posTargLength: Seq[MethodSymbol] => Seq[MethodSymbol] = _.filter { alt => - numTypeParams(alt) == targs.length - } - - // Drop methods that are not applicable to the arguments - val applicable: Seq[MethodSymbol] => Seq[MethodSymbol] = _.filter { alt => - // Note: combine returns None if a is not applicable and - // None.exists(_ => true) == false - val paramTypes = - valParams(substituteTypeParams(alt)).map(p => unwrap(p.typeSignature)) - val variadic = isVarArgTypes(paramTypes) - val maybeArgTypes = - combineInto(variadic)(posVargTypes, nameVargTypes)(paramNames(alt.typeSignature), defaultTypes(alt)) - maybeArgTypes exists { argTypes => - if (isVarArgTypes(argTypes) && !isVarArgTypes(paramTypes)) false - else { - val a = argTypes - val p = extend(paramTypes, argTypes.length) - (a corresponds p)(_ weak_<:< _) - } - } - } - - // Always prefer methods that don't need to use default - // arguments over those that do. - // e.g. when resolving foo(1), prefer def foo(x: Int) over - // def foo(x: Int, y: Int = 4) - val noDefaults: Seq[MethodSymbol] => Seq[MethodSymbol] = - _ filterNot usesDefault - - // Try to select the most specific method. If that's not possible, - // return all of the candidates (this will likely cause an error - // higher up in the call stack) - val mostSpecific: Seq[MethodSymbol] => Seq[MethodSymbol] = { alts => - val sorted = alts.sortWith(moreSpecific) - val mostSpecific = sorted.head - val agreeTest: MethodSymbol => Boolean = - moreSpecific(mostSpecific, _) - val disagreeTest: MethodSymbol => Boolean = - moreSpecific(_, mostSpecific) - if (!sorted.tail.forall(agreeTest)) { - mostSpecific +: sorted.tail.filterNot(agreeTest) - } else if (sorted.tail.exists(disagreeTest)) { - mostSpecific +: sorted.tail.filter(disagreeTest) - } else { - Seq(mostSpecific) - } - } - - def finalResult(t: Type): Type = t match { - case PolyType(_, rest) => finalResult(rest) - case MethodType(_, result) => finalResult(result) - case NullaryMethodType(result) => finalResult(result) - case t: Type => t - } - - // If a result type is given, drop alternatives that don't meet it - val resultType: Seq[MethodSymbol] => Seq[MethodSymbol] = - if (expected == NoType) identity - else _.filter { alt => - finalResult(substituteTypeParams(alt)) <:< expected - } - - def defaultFilteringOps = - Seq(posTargLength, resultType, applicable, noDefaults, mostSpecific) - - // Begin Method Proper - - - val alts = alternatives.map(_.asMethodSymbol) - - val selection = select(alts, defaultFilteringOps) - - val knownApplicable = applicable(selection) - - if (knownApplicable.size == 1) knownApplicable.head - else NoSymbol - } } /** The class for all symbols */ diff --git a/test/files/neg/t5799.check b/test/files/neg/t5799.check new file mode 100644 index 0000000000..10e2658d56 --- /dev/null +++ b/test/files/neg/t5799.check @@ -0,0 +1,4 @@ +t5799.scala:2: error: value class may not have secondary constructors + def this(s: String) = this(s.toDouble) + ^ +one error found diff --git a/test/files/neg/t5799.scala b/test/files/neg/t5799.scala new file mode 100644 index 0000000000..9bd6ab7dd1 --- /dev/null +++ b/test/files/neg/t5799.scala @@ -0,0 +1,8 @@ +class Foo(val bar: Double) extends AnyVal { + def this(s: String) = this(s.toDouble) +} +object Test { + def main(args: Array[String]): Unit = + new Foo("") + } + diff --git a/test/files/neg/t5878.check b/test/files/neg/t5878.check new file mode 100644 index 0000000000..50dba0d272 --- /dev/null +++ b/test/files/neg/t5878.check @@ -0,0 +1,13 @@ +t5878.scala:1: error: value class may not unbox to itself +case class Foo(x: Bar) extends AnyVal + ^ +t5878.scala:2: error: value class may not unbox to itself +case class Bar(x: Foo) extends AnyVal + ^ +t5878.scala:4: error: value class may not unbox to itself +class Foo1(val x: Bar1) extends AnyVal + ^ +t5878.scala:5: error: value class may not unbox to itself +class Bar1(val x: Foo1) extends AnyVal + ^ +four errors found diff --git a/test/files/neg/t5878.scala b/test/files/neg/t5878.scala new file mode 100644 index 0000000000..b4e33627ef --- /dev/null +++ b/test/files/neg/t5878.scala @@ -0,0 +1,6 @@ +case class Foo(x: Bar) extends AnyVal +case class Bar(x: Foo) extends AnyVal + +class Foo1(val x: Bar1) extends AnyVal +class Bar1(val x: Foo1) extends AnyVal + diff --git a/test/files/neg/t5882.check b/test/files/neg/t5882.check new file mode 100644 index 0000000000..df01c7bc0a --- /dev/null +++ b/test/files/neg/t5882.check @@ -0,0 +1,15 @@ +t5882.scala:2: warning: case classes without a parameter list have been deprecated; +use either case objects or case classes with `()' as parameter list. + case class Scope + ^ +t5882.scala:2: error: value class may not have nested class definitions + case class Scope + ^ +t5882.scala:3: error: value class may not have nested class definitions + class Foo + ^ +t5882.scala:4: error: value class may not have nested module definitions + object Bar + ^ +one warning found +three errors found diff --git a/test/files/neg/t5882.scala b/test/files/neg/t5882.scala new file mode 100644 index 0000000000..1233eb636f --- /dev/null +++ b/test/files/neg/t5882.scala @@ -0,0 +1,5 @@ +class NodeOps(val n: Any) extends AnyVal { + case class Scope + class Foo + object Bar +} diff --git a/test/files/pos/t2038.scala b/test/files/pos/t2038.scala new file mode 100644 index 0000000000..17b1a702dd --- /dev/null +++ b/test/files/pos/t2038.scala @@ -0,0 +1,5 @@ +class Test { + List(Some(classOf[java.lang.Integer]), Some(classOf[Int])).map { + case Some(f: Class[_]) => f.cast(???) + } +}
\ No newline at end of file diff --git a/test/files/run/rawstrings.check b/test/files/run/rawstrings.check new file mode 100644 index 0000000000..36e63594df --- /dev/null +++ b/test/files/run/rawstrings.check @@ -0,0 +1 @@ +[\n\t'"$] diff --git a/test/files/run/rawstrings.scala b/test/files/run/rawstrings.scala new file mode 100644 index 0000000000..9df64f6625 --- /dev/null +++ b/test/files/run/rawstrings.scala @@ -0,0 +1,3 @@ +object Test extends App { + println(raw"[\n\t'${'"'}$$]") +} diff --git a/test/files/run/reflect-resolveoverload-bynameparam.scala b/test/files/run/reflect-resolveoverload-bynameparam.scala deleted file mode 100644 index 7fb8c82ab8..0000000000 --- a/test/files/run/reflect-resolveoverload-bynameparam.scala +++ /dev/null @@ -1,32 +0,0 @@ - -class A -class B extends A - -class C { - def foo(x: => Int)(y: String) = x - def foo(x: String)(y: List[_]) = x - def foo(x: => A)(y: Array[_]) = 1 - def foo(x: A)(y: Seq[_]) = 2 - def foo(x: B)(y: Map[_, _]) = 4 -} - -object Test extends App { - val cm = reflect.runtime.currentMirror - val u = cm.universe - val c = new C - val im = cm.reflect(c) - val t = u.typeOf[C] member u.newTermName("foo") asTermSymbol - val f1 = t.resolveOverloaded(posVargs = List(u.typeOf[Int])) asMethodSymbol - val f2 = t.resolveOverloaded(posVargs = List(u.typeOf[String])) asMethodSymbol - val f3 = t.resolveOverloaded(posVargs = List(u.typeOf[A])) asMethodSymbol - val f4 = t.resolveOverloaded(posVargs = List(u.typeOf[B])) asMethodSymbol - val m1 = im.reflectMethod(f1) - val m2 = im.reflectMethod(f2) - val m3 = im.reflectMethod(f3) - val m4 = im.reflectMethod(f4) - assert(m1(() => 1, null) == c.foo(1)(null)) - assert(m2("a", null) == c.foo("a")(null)) - assert(m3(new A, null) == c.foo(new A)(null)) - assert(m4(new B, null) == c.foo(new B)(null)) -} - diff --git a/test/files/run/reflect-resolveoverload-expected.scala b/test/files/run/reflect-resolveoverload-expected.scala deleted file mode 100644 index 1378090309..0000000000 --- a/test/files/run/reflect-resolveoverload-expected.scala +++ /dev/null @@ -1,43 +0,0 @@ - -class A { - override def equals(x: Any) = { - x.isInstanceOf[A] && !x.isInstanceOf[B] - } -} -class B extends A { - override def equals(x: Any) = { - x.isInstanceOf[B] - } -} - -class C { - def a(x: String) = 1 - def a(x: Array[_]) = "a" - def b(x: String) = new A - def b(x: Array[_]) = new B - def c(x: String) = new B - def c(x: Array[_]) = "a" -} - -object Test extends App { - val cm = reflect.runtime.currentMirror - val u = cm.universe - val c = new C - val im = cm.reflect(c) - def invoke(s: String, expectedType: u.Type, expectedResult: Any) { - val ol = (u.typeOf[C] member u.newTermName(s)).asTermSymbol - val methodSym = ol.resolveOverloaded(posVargs = List(u.typeOf[Null]), expected = expectedType).asMethodSymbol - val sig = methodSym.typeSignature.asInstanceOf[u.MethodType] - val method = im.reflectMethod(methodSym) - assert(method(null) == expectedResult) - } - - invoke("a", u.typeOf[Int], c.a(null): Int) - invoke("a", u.typeOf[String], c.a(null): String) - invoke("b", u.typeOf[B], c.b(null): B) - invoke("c", u.typeOf[A], c.c(null): A) - invoke("c", u.typeOf[A], c.c(null): A) - invoke("c", u.typeOf[B], c.c(null): B) - invoke("c", u.typeOf[String], c.c(null): String) - -} diff --git a/test/files/run/reflect-resolveoverload-invalid.scala b/test/files/run/reflect-resolveoverload-invalid.scala deleted file mode 100644 index 8c5dc9f94b..0000000000 --- a/test/files/run/reflect-resolveoverload-invalid.scala +++ /dev/null @@ -1,43 +0,0 @@ - -class A -class B extends A - -class C { - def a(x: Int) = 1 - def a(x: String) = 2 - def b(x: B) = 3 - def c(x: A, y: B) = 4 - def c(x: B, y: A) = 5 - def d[T](x: Int) = 6 - def d(x: String) = 7 - def e(x: A) = 8 - def e(x: =>B) = 9 -} - -object Test extends App { - val cm = reflect.runtime.currentMirror - val u = cm.universe - - val x = new C - val t = u.typeOf[C] - - val a = t member u.newTermName("a") asTermSymbol - val b = t member u.newTermName("b") asTermSymbol - val c = t member u.newTermName("c") asTermSymbol - val d = t member u.newTermName("d") asTermSymbol - val e = t member u.newTermName("e") asTermSymbol - - val n1 = a.resolveOverloaded(posVargs = List(u.typeOf[Long])) - val n2 = b.resolveOverloaded(posVargs = List(u.typeOf[A])) - val n3 = c.resolveOverloaded(posVargs = List(u.typeOf[B], u.typeOf[B])) - val n4 = d.resolveOverloaded(targs = List(u.typeOf[Int])) - val n5 = d.resolveOverloaded() - val n6 = e.resolveOverloaded(posVargs = List(u.typeOf[B])) - - assert(n1 == u.NoSymbol) - assert(n2 == u.NoSymbol) - assert(n3 == u.NoSymbol) - assert(n4 == u.NoSymbol) - assert(n5 == u.NoSymbol) - assert(n6 == u.NoSymbol) -} diff --git a/test/files/run/reflect-resolveoverload-named.scala b/test/files/run/reflect-resolveoverload-named.scala deleted file mode 100644 index 017ec85c0d..0000000000 --- a/test/files/run/reflect-resolveoverload-named.scala +++ /dev/null @@ -1,26 +0,0 @@ - -class A { - def foo(x: String, y: Int) = 1 - def foo(x: Int, y: String) = 2 -} - -object Test extends App { - val cm = reflect.runtime.currentMirror - val u = cm.universe - val a = new A - val im = cm.reflect(a) - val tpe = u.typeOf[A] - val overloaded = tpe member u.newTermName("foo") asTermSymbol - val ms1 = - overloaded resolveOverloaded(nameVargs = Seq((u.newTermName("x"), u.typeOf[String]), (u.newTermName("y"), u.typeOf[Int]))) - val ms2 = - overloaded resolveOverloaded(nameVargs = Seq((u.newTermName("y"), u.typeOf[Int]), (u.newTermName("x"), u.typeOf[String]))) - val ms3 = - overloaded resolveOverloaded(nameVargs = Seq((u.newTermName("x"), u.typeOf[Int]), (u.newTermName("y"), u.typeOf[String]))) - val ms4 = - overloaded resolveOverloaded(nameVargs = Seq((u.newTermName("y"), u.typeOf[String]), (u.newTermName("x"), u.typeOf[Int]))) - assert(im.reflectMethod(ms1 asMethodSymbol)("A", 1) == 1) - assert(im.reflectMethod(ms2 asMethodSymbol)("A", 1) == 1) - assert(im.reflectMethod(ms3 asMethodSymbol)(1, "A") == 2) - assert(im.reflectMethod(ms4 asMethodSymbol)(1, "A") == 2) -} diff --git a/test/files/run/reflect-resolveoverload-targs.scala b/test/files/run/reflect-resolveoverload-targs.scala deleted file mode 100644 index 888b2f0c15..0000000000 --- a/test/files/run/reflect-resolveoverload-targs.scala +++ /dev/null @@ -1,29 +0,0 @@ - -import reflect.runtime.{universe=>u} -import scala.reflect.runtime.{currentMirror => cm} - -class C { - def foo[T: u.TypeTag](x: String) = 1 - def foo[T: u.TypeTag, S: u.TypeTag](x: String) = 2 -} - -object Test extends App { - val c = new C - val im = cm.reflect(c) - val foo = u.typeOf[C] member u.newTermName("foo") asTermSymbol - val f1 = foo.resolveOverloaded( - targs = Seq(u.typeOf[Int]), - posVargs = Seq(u.typeOf[String]) - ) - - val f2 = foo.resolveOverloaded( - targs = Seq(u.typeOf[Int], - u.typeOf[Int]), posVargs = Seq(u.typeOf[String]) - ) - - val m1 = im.reflectMethod(f1 asMethodSymbol) - val m2 = im.reflectMethod(f2 asMethodSymbol) - - assert(m1("a", u.typeTag[Int]) == c.foo[Int]("a")) - assert(m2("a", u.typeTag[Int], u.typeTag[Int]) == c.foo[Int, Int]("a")) -} diff --git a/test/files/run/reflect-resolveoverload-tparm-substitute.scala b/test/files/run/reflect-resolveoverload-tparm-substitute.scala deleted file mode 100644 index 22e7bcd40a..0000000000 --- a/test/files/run/reflect-resolveoverload-tparm-substitute.scala +++ /dev/null @@ -1,77 +0,0 @@ - -class A -class B extends A - -class C { - def foo[T](x: T) = x - def foo(x: Int) = "a" - def foo(x: A) = x -} - -object Test extends App { - val cm = reflect.runtime.currentMirror - val u = cm.universe - val c = new C - val im = cm.reflect(c) - val term = u.typeOf[C] member u.newTermName("foo") asTermSymbol - - val f1 = term.resolveOverloaded( - posVargs = List(u.typeOf[Int]), - expected = u.typeOf[String] - ) - - val f2 = term.resolveOverloaded( - targs = List(u.typeOf[String]), - posVargs = List(u.typeOf[String]), - expected = u.typeOf[String] - ) - - val f3 = term.resolveOverloaded( - posVargs = List(u.typeOf[A]), - expected = u.typeOf[A] - ) - - val f4 = term.resolveOverloaded( - targs = List(u.typeOf[A]), - posVargs = List(u.typeOf[A]), - expected = u.typeOf[A] - ) - - val f5 = term.resolveOverloaded( - targs = List(u.typeOf[B]), - posVargs = List(u.typeOf[B]), - expected = u.typeOf[B] - ) - - val f6 = term.resolveOverloaded( - targs = List(u.typeOf[B]), - posVargs = List(u.typeOf[B]), - expected = u.typeOf[A] - ) - - val f7 = term.resolveOverloaded( - targs = List(u.typeOf[A]), - posVargs = List(u.typeOf[B]), - expected = u.typeOf[A] - ) - - val m1 = im.reflectMethod(f1 asMethodSymbol) - val m2 = im.reflectMethod(f2 asMethodSymbol) - val m3 = im.reflectMethod(f3 asMethodSymbol) - val m4 = im.reflectMethod(f4 asMethodSymbol) - val m5 = im.reflectMethod(f5 asMethodSymbol) - val m6 = im.reflectMethod(f6 asMethodSymbol) - val m7 = im.reflectMethod(f7 asMethodSymbol) - - val a = new A - val b = new B - assert(m1(2) == (c.foo(2): String)) - assert(m2("xyz") == (c.foo[String]("xyz"): String)) - assert(m3(a) == (c.foo(a): A)) - assert(m4(a) == (c.foo[A](a): A)) - assert(m5(b) == (c.foo[B](b): B)) - assert(m6(b) == (c.foo[B](b): A)) - assert(m7(b) == (c.foo[A](b): A)) - - -} diff --git a/test/files/run/reflect-resolveoverload-variadic.scala b/test/files/run/reflect-resolveoverload-variadic.scala deleted file mode 100644 index 8e2e15600f..0000000000 --- a/test/files/run/reflect-resolveoverload-variadic.scala +++ /dev/null @@ -1,27 +0,0 @@ - -class C { - def foo(x: Int*) = 1 + x.sum - def foo(x: String) = 2 -} - -object Test extends App { - val cm = reflect.runtime.currentMirror - val u = cm.universe - val c = new C - val im = cm.reflect(c) - val foo = u.typeOf[C] member u.newTermName("foo") asTermSymbol - val f0 = foo.resolveOverloaded() - val f1 = foo.resolveOverloaded(posVargs = Seq(u.typeOf[Int])) - val f2 = foo.resolveOverloaded(posVargs = Seq(u.typeOf[Int], u.typeOf[Int])) - val f3 = foo.resolveOverloaded(posVargs = Seq(u.typeOf[String])) - - val m0 = im.reflectMethod(f0 asMethodSymbol) - val m1 = im.reflectMethod(f1 asMethodSymbol) - val m2 = im.reflectMethod(f2 asMethodSymbol) - val m3 = im.reflectMethod(f3 asMethodSymbol) - - assert(m0(Seq()) == c.foo()) - assert(m1(Seq(1)) == c.foo(1)) - assert(m2(Seq(4, 9)) == c.foo(4, 9)) - assert(m3("abc") == c.foo("abc")) -} diff --git a/test/files/run/reflect-resolveoverload1.scala b/test/files/run/reflect-resolveoverload1.scala deleted file mode 100644 index a859a0ec4e..0000000000 --- a/test/files/run/reflect-resolveoverload1.scala +++ /dev/null @@ -1,19 +0,0 @@ -import scala.reflect.runtime.universe._ -import scala.reflect.runtime.{currentMirror => cm} - -object Test extends App { - - val s = "hello world" - val m = cm.reflect(s) - val sc = m.symbol - val st = sc.asType - val meth = (st member newTermName("indexOf")).asTermSymbol - val IntType = definitions.IntClass.asType - val indexOf = (meth resolveOverloaded(posVargs = List(IntType))).asMethodSymbol - assert(m.reflectMethod(indexOf)('w') == 6) - assert((m.reflectMethod(indexOf)('w') match { case x: Int => x }) == 6) - - val meth2 = (st member newTermName("substring")).asTermSymbol - val substring = (meth2 resolveOverloaded(posVargs = List(IntType, IntType))).asMethodSymbol - assert(m.reflectMethod(substring)(2, 6) == "llo ") -} diff --git a/test/files/run/reflect-resolveoverload2.scala b/test/files/run/reflect-resolveoverload2.scala deleted file mode 100644 index a800a3e92c..0000000000 --- a/test/files/run/reflect-resolveoverload2.scala +++ /dev/null @@ -1,51 +0,0 @@ -class A -class B extends A - -class C { - def a(x: Int) = 1 - def a(x: String) = 2 - //def b(x: => Int)(s: String) = 1 - //def b(x: => String)(a: Array[_]) = 2 - def c(x: A) = 1 - def c(x: B) = 2 - //def d(x: => A)(s: String) = 1 - //def d(x: => B)(a: Array[_]) = 2 - def e(x: A) = 1 - def e(x: B = new B) = 2 - def f(x: Int) = 1 - def f(x: String) = 2 - def f(x: Long) = 3 - def f(x: Double) = 4 -} - -object Test extends App { - val cm = reflect.runtime.currentMirror - val u = cm.universe - val c = new C - val im = cm.reflect(c) - def invoke(s: String, arg: Any, argType: u.Type): Int = { - val ol = u.typeOf[C] member u.newTermName(s) asTermSymbol - val methodSym = ol.resolveOverloaded(posVargs = List(argType)) asMethodSymbol - val sig = methodSym.typeSignature.asInstanceOf[u.MethodType] - val method = im.reflectMethod(methodSym) - if (sig.resultType.kind == "MethodType") method(arg, null).asInstanceOf[Int] - else method(arg).asInstanceOf[Int] - } - assert(c.a(1) == invoke("a", 1, u.typeOf[Int])) - assert(c.a("a") == invoke("a", "a", u.typeOf[String])) - assert(c.a('a') == invoke("a", 'a', u.typeOf[Char])) - assert(c.a(3: Byte) == invoke("a", 3: Byte, u.typeOf[Byte])) - //assert(c.b(1)(null) == invoke("b", 1, u.typeOf[Int])) - //assert(c.b("a")(null) == invoke("b", "a", u.typeOf[String])) - assert(c.c(new A) == invoke("c", new A, u.typeOf[A])) - assert(c.c(new B) == invoke("c", new B, u.typeOf[B])) - //assert(c.d(new A)(null) == invoke("d", new A, u.typeOf[A])) - //assert(c.d(new B)(null) == invoke("d", new B, u.typeOf[B])) - assert(c.e(new A) == invoke("e", new A, u.typeOf[A])) - assert(c.e(new B) == invoke("e", new B, u.typeOf[B])) - assert(c.f(1: Short) == invoke("f", 1: Short, u.typeOf[Short])) - assert(c.f(2) == invoke("f", 2, u.typeOf[Int])) - assert(c.f(3L) == invoke("f", 3L, u.typeOf[Long])) - assert(c.f(4f) == invoke("f", 4f, u.typeOf[Float])) - assert(c.f(5d) == invoke("f", 5d, u.typeOf[Double])) -} diff --git a/test/files/run/t5603.check b/test/files/run/t5603.check new file mode 100644 index 0000000000..5127d3c1c7 --- /dev/null +++ b/test/files/run/t5603.check @@ -0,0 +1,29 @@ +[[syntax trees at end of parser]] // newSource1 +[0:241]package [0:0]<empty> { + [0:82]abstract trait Greeting extends [15:82][83]scala.AnyRef { + [15]def $init$() = [15]{ + [15]() + }; + [23:39]val name: [33:39]String; + [46:76]val msg = [56:76][56:72][56:71]"How are you, ".$plus([72:76]name) + }; + [87:209]class C extends [94:209][151:159]Greeting { + [119:139]val nameElse = _; + [95:101]<paramaccessor> private[this] val i: [98:101]Int = _; + <119:139>def <init>([95]i: [98]Int) = <119:139>{ + <119:139>val nameElse = <134:139>"Bob"; + [94][94][94]super.<init>(); + [94]() + }; + [168:184]val name = [179:184]"avc"; + [191:203][191:198]println([199:202]msg) + }; + [215:241]object Test extends [227:241][235:238]App { + [227]def <init>() = [227]{ + [227][227][227]super.<init>(); + [227]() + }; + [NoPosition]<empty> + } +} + diff --git a/test/files/run/t5603.scala b/test/files/run/t5603.scala new file mode 100644 index 0000000000..60dfd01fee --- /dev/null +++ b/test/files/run/t5603.scala @@ -0,0 +1,42 @@ +import scala.tools.partest._ +import java.io._ +import scala.tools.nsc._ +import scala.tools.nsc.util.CommandLineParser +import scala.tools.nsc.{Global, Settings, CompilerCommand} +import scala.tools.nsc.reporters.ConsoleReporter + +object Test extends DirectTest { + + override def extraSettings: String = "-usejavacp -Xprint:parser -Ystop-after:parser -d " + testOutput.path + + override def code = """ + trait Greeting { + val name: String + val msg = "How are you, "+name + } + class C(i: Int) extends { + val nameElse = "Bob" + } with Greeting { + val name = "avc" + println(msg) + } + + object Test extends App {} + """.trim + + override def show(): Unit = { + // redirect err to out, for logging + val prevErr = System.err + System.setErr(System.out) + compile() + System.setErr(prevErr) + } + + override def newCompiler(args: String*): Global = { + + val settings = new Settings() + settings.Xprintpos.value = true + val command = new CompilerCommand((CommandLineParser tokenize extraSettings) ++ args.toList, settings) + new Global(command.settings, new ConsoleReporter(settings)) with interactive.RangePositions + } +} |