From 67651e220a6a4d1d1ee1004766d5b1e33fd46531 Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Mon, 7 Jul 2014 14:53:22 +0200 Subject: Simplify (ambiguous) error issuing. The two functional differences are: - always add the diagnostics string - check erroneousness in `issueAmbiguousTypeErrorUnlessErroneous`, before even constructing the error message. Consider this nugget: ``` - def issueAmbiguousError(pre: Type, sym1: Symbol, sym2: Symbol, err: AbsTypeError) { - issueCommon(err) { case _ if ambiguousErrors => - if (!pre.isErroneous && !sym1.isErroneous && !sym2.isErroneous) ``` I'd like to state for the record that the if-erroneous in the case of the partial function looked super-dodgy: it meant that, when `ambiguousErrors`, `issueCommon` would not get to the `else` branches that buffer or throw, and if the erroneous condition was met, nothing would be issued/buffered/thrown. This refactoring checks this condition up front. --- test/files/neg/t3909.check | 1 + 1 file changed, 1 insertion(+) (limited to 'test/files') diff --git a/test/files/neg/t3909.check b/test/files/neg/t3909.check index 7da0195607..052b49f855 100644 --- a/test/files/neg/t3909.check +++ b/test/files/neg/t3909.check @@ -1,4 +1,5 @@ t3909.scala:1: error: in object DO, multiple overloaded alternatives of m1 define default arguments +Error occurred in an application involving default arguments. object DO { ^ one error found -- cgit v1.2.3 From 725c5c907ba9ecdec4ee343bd0aa0ff438b4c20c Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Thu, 17 Jul 2014 12:29:49 +0200 Subject: Encapsulate reporting mode as class of reportBuffer. Reporting mode used to be governed by contextMode. This logic is left in place by this commit, and the consistency of the new and the old is checked. Will be removed in follow-up commit. The main difference is that we no longer throw TypeErrors in buffering mode. There was one instance of context.error in implicit search the exploited the fact that implicit search runs in buffering (silent) mode and thus calls to error(pos,msg) used to throw new TypeError(pos, msg) -- made this explicit, and removed throwing behavior from the buffering context reporter. --- .../scala/tools/nsc/typechecker/Contexts.scala | 145 ++++++++++++++------- .../scala/tools/nsc/typechecker/Implicits.scala | 11 +- test/files/neg/macro-basic-mamdmi.check | 10 +- 3 files changed, 113 insertions(+), 53 deletions(-) (limited to 'test/files') diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index 9a6647a83a..37eff40dd8 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -9,6 +9,7 @@ package typechecker import scala.collection.{ immutable, mutable } import scala.annotation.tailrec import scala.reflect.internal.util.shortClassOfInstance +import scala.tools.nsc.reporters.Reporter /** * @author Martin Odersky @@ -325,9 +326,22 @@ trait Contexts { self: Analyzer => // Error reporting policies and buffer. // - private var _reportBuffer: ReportBuffer = new ReportBuffer + // TODO: start out with a regular reporter? + private var _reportBuffer: ContextReporter = new ThrowingReporter + /** A buffer for errors and warnings, used with `this.bufferErrors == true` */ - def reportBuffer = _reportBuffer + def reportBuffer = { + assert( + (bufferErrors == _reportBuffer.isInstanceOf[BufferingReporter]) + && (throwErrors == _reportBuffer.isInstanceOf[ThrowingReporter]) + && (checking == _reportBuffer.isInstanceOf[CheckingReporter]) + && (reportErrors != (_reportBuffer.isInstanceOf[BufferingReporter] + || _reportBuffer.isInstanceOf[ThrowingReporter] + || _reportBuffer.isInstanceOf[CheckingReporter])), + s"INCONSISTENCY $contextMode != ${_reportBuffer.getClass}") + + _reportBuffer + } def reportErrors = contextMode.inNone(ThrowErrors | BufferErrors) def bufferErrors = this(BufferErrors) @@ -349,7 +363,9 @@ trait Contexts { self: Analyzer => def apply(): Unit = if (implicitsEnabled) { val savedContextMode = contextMode + val savedReporter = _reportBuffer var fallback = false + _reportBuffer = (new BufferingReporter).sharingBuffersWith(reportBuffer) setBufferErrors() setAmbiguousErrors(false) // We cache the current buffer because it is impossible to @@ -365,6 +381,7 @@ trait Contexts { self: Analyzer => if (reportBuffer.hasErrors) { fallback = true contextMode = savedContextMode + _reportBuffer = savedReporter reportBuffer.clearAllErrors() tryOnce(true) } @@ -372,9 +389,11 @@ trait Contexts { self: Analyzer => case ex: CyclicReference => throw ex case ex: TypeError => // recoverable cyclic references contextMode = savedContextMode + _reportBuffer = savedReporter if (!fallback) tryOnce(true) else () } finally { contextMode = savedContextMode + _reportBuffer = savedReporter reportBuffer ++= errorsToRestore } } @@ -421,11 +440,16 @@ trait Contexts { self: Analyzer => /** @return true if the `expr` evaluates to true within a silent Context that incurs no errors */ @inline final def inSilentMode(expr: => Boolean): Boolean = { + val savedReporter = _reportBuffer withMode() { // withMode with no arguments to restore the mode mutated by `setBufferErrors`. + _reportBuffer = (new BufferingReporter).sharingBuffersWith(reportBuffer) setBufferErrors() setAmbiguousErrors(false) try expr && !reportBuffer.hasErrors - finally reportBuffer.clearAll() + finally { + _reportBuffer = savedReporter + _reportBuffer.clearAll() // TODO: ??? + } } } @@ -473,7 +497,7 @@ trait Contexts { self: Analyzer => c.diagUsedDefaults = diagUsedDefaults c.openImplicits = openImplicits c.contextMode = contextMode // note: ConstructorSuffix, a bit within `mode`, is conditionally overwritten below. - c._reportBuffer = reportBuffer + c._reportBuffer = _reportBuffer // Fields that may take on a different value in the child c.prefix = prefixInChild @@ -490,9 +514,10 @@ trait Contexts { self: Analyzer => /** Use reporter (possibly buffered) for errors/warnings and enable implicit conversion **/ def initRootContext(throwing: Boolean = false, checking: Boolean = false): Unit = { - if (checking) this.checking = true - else if (throwing) setThrowErrors() - else setReportErrors() + _reportBuffer = + if (checking) {this.checking = true ; new CheckingReporter} + else if (throwing) {setThrowErrors(); new ThrowingReporter} + else {setReportErrors() ; new ContextReporter} // TODO: this is likely redundant setAmbiguousErrors(!throwing) this(EnrichmentEnabled | ImplicitsEnabled) = !throwing @@ -500,7 +525,7 @@ trait Contexts { self: Analyzer => def make(tree: Tree, owner: Symbol, scope: Scope): Context = // TODO SI-7345 Moving this optimization into the main overload of `make` causes all tests to fail. - // even if it is extened to check that `unit == this.unit`. Why is this? + // even if it is extended to check that `unit == this.unit`. Why is this? if (tree == this.tree && owner == this.owner && scope == this.scope) this else make(tree, owner, scope, unit) @@ -508,17 +533,20 @@ trait Contexts { self: Analyzer => def makeNewScope(tree: Tree, owner: Symbol): Context = make(tree, owner, newNestedScope(scope)) + /** Make a child context that buffers errors and warnings into a fresh report buffer. */ def makeSilent(reportAmbiguousErrors: Boolean = ambiguousErrors, newtree: Tree = tree): Context = { val c = make(newtree) c.setBufferErrors() c.setAmbiguousErrors(reportAmbiguousErrors) - c._reportBuffer = new ReportBuffer // A fresh buffer so as not to leak errors/warnings into `this`. + // A fresh buffer so as not to leak errors/warnings into `this`. + c._reportBuffer = new BufferingReporter c } def makeNonSilent(newtree: Tree): Context = { val c = make(newtree) + c._reportBuffer = (new ContextReporter).sharingBuffersWith(reportBuffer) c.setReportErrors() c.setAmbiguousErrors(true) c @@ -545,6 +573,7 @@ trait Contexts { self: Analyzer => val baseContext = enclClass.outer.nextEnclosing(!_.tree.isInstanceOf[Template]) val argContext = baseContext.makeNewScope(tree, owner) argContext.contextMode = contextMode + argContext._reportBuffer = _reportBuffer // caught by neg/t3649 TODO: make sure _reportBuffer is propagated wherever contextMode is argContext.inSelfSuperCall = true def enterElems(c: Context) { def enterLocalElems(e: ScopeEntry) { @@ -567,46 +596,17 @@ trait Contexts { self: Analyzer => // // Error and warning issuance // - private def addDiagString(msg: String) = { - val diagUsedDefaultsMsg = "Error occurred in an application involving default arguments." - if (diagUsedDefaults && !(msg endsWith diagUsedDefaultsMsg)) msg + "\n" + diagUsedDefaultsMsg - else msg - } - - private def unitError(pos: Position, msg: String): Unit = - if (checking) onTreeCheckerError(pos, msg) - else reporter.error(pos, msg) - - @inline private def issueCommon(err: AbsTypeError, reportError: Boolean) { - // TODO: are errors allowed to have pos == NoPosition?? - // if not, Jason suggests doing: val pos = err.errPos.orElse( { devWarning("Que?"); context.tree.pos }) - if (settings.Yissuedebug) { - log("issue error: " + err.errMsg) - (new Exception).printStackTrace() - } - if (reportError) unitError(err.errPos, addDiagString(err.errMsg)) - else if (bufferErrors) { reportBuffer += err } - else throw new TypeError(err.errPos, err.errMsg) - } /** Issue/buffer/throw the given type error according to the current mode for error reporting. */ - private[typechecker] def issue(err: AbsTypeError) = issueCommon(err, reportErrors) - + private[typechecker] def issue(err: AbsTypeError) = reportBuffer.issue(err)(this) /** Issue/buffer/throw the given implicit ambiguity error according to the current mode for error reporting. */ - private[typechecker] def issueAmbiguousError(err: AbsTypeError) = issueCommon(err, ambiguousErrors) - + private[typechecker] def issueAmbiguousError(err: AbsAmbiguousTypeError) = reportBuffer.issueAmbiguousError(err)(this) /** Issue/throw the given error message according to the current mode for error reporting. */ - def error(pos: Position, msg: String) = { - val msg1 = addDiagString(msg) - if (reportErrors) unitError(pos, msg1) - else throw new TypeError(pos, msg1) - } - + def error(pos: Position, msg: String) = reportBuffer.error(pos, msg) /** Issue/throw the given error message according to the current mode for error reporting. */ - def warning(pos: Position, msg: String) { - if (reportErrors) reporter.warning(pos, msg) - else if (bufferErrors) reportBuffer += (pos -> msg) - } + def warning(pos: Position, msg: String) = reportBuffer.warning(pos, msg) + def echo(pos: Position, msg: String) = reportBuffer.echo(pos, msg) + def deprecationWarning(pos: Position, sym: Symbol, msg: String): Unit = currentRun.reporting.deprecationWarning(pos, sym, msg) @@ -616,7 +616,6 @@ trait Contexts { self: Analyzer => def featureWarning(pos: Position, featureName: String, featureDesc: String, featureTrait: Symbol, construct: => String = "", required: Boolean): Unit = currentRun.reporting.featureWarning(pos, featureName, featureDesc, featureTrait, construct, required) - def echo(pos: Position, msg: String): Unit = reporter.echo(pos, msg) // nextOuter determines which context is searched next for implicits // (after `this`, which contributes `newImplicits` below.) In @@ -1254,7 +1253,7 @@ trait Contexts { self: Analyzer => } /** A buffer for warnings and errors that are accumulated during speculative type checking. */ - final class ReportBuffer { + class ContextReporter extends Reporter { type Error = AbsTypeError type Warning = (Position, String) @@ -1270,6 +1269,13 @@ trait Contexts { self: Analyzer => private def warningBuffer = {if (_warningBuffer == null) _warningBuffer = newBuffer; _warningBuffer} def warnings: immutable.Seq[Warning] = warningBuffer.toVector + def sharingBuffersWith(other: ContextReporter): this.type = { + // go through other's accessor so that its buffer will be created + _errorBuffer = other.errorBuffer + _warningBuffer = other.warningBuffer + this + } + def +=(error: AbsTypeError): this.type = { errorBuffer += error this @@ -1296,7 +1302,8 @@ trait Contexts { self: Analyzer => this } - def hasErrors = errorBuffer.nonEmpty + // TODO + override def hasErrors = super.hasErrors || errorBuffer.nonEmpty def firstError = errorBuffer.headOption // have to pass in context because multiple contexts may share the same ReportBuffer @@ -1323,8 +1330,52 @@ trait Contexts { self: Analyzer => case err: DivergentImplicitTypeError => err ne saved case _ => false } + + def isBuffering = false + + protected def addDiagString(msg: String)(implicit context: Context): String = { + val diagUsedDefaultsMsg = "Error occurred in an application involving default arguments." + if (context.diagUsedDefaults && !(msg endsWith diagUsedDefaultsMsg)) msg + "\n" + diagUsedDefaultsMsg + else msg + } + + def issue(err: AbsTypeError)(implicit context: Context): Unit = error(err.errPos, addDiagString(err.errMsg)) + + def issueAmbiguousError(err: AbsAmbiguousTypeError)(implicit context: Context): Unit = + if (context.ambiguousErrors) error(err.errPos, addDiagString(err.errMsg)) + + protected final def info0(pos: Position, msg: String, severity: Severity, force: Boolean): Unit = ??? + override def echo(pos: Position, msg: String): Unit = reporter.echo(pos, msg) + override def warning(pos: Position, msg: String): Unit = reporter.warning(pos, msg) + override def error(pos: Position, msg: String): Unit = reporter.error(pos, msg) } + private class BufferingReporter extends ContextReporter { + override def isBuffering = true + + override def issue(err: AbsTypeError)(implicit context: Context): Unit = this += err + override def issueAmbiguousError(err: AbsAmbiguousTypeError)(implicit context: Context): Unit = + if (context.ambiguousErrors) reporter.error(err.errPos, addDiagString(err.errMsg)) + else this += err + + override def warning(pos: Position, msg: String): Unit = this += (pos -> msg) + + // this used to throw new TypeError(pos, msg) -- buffering lets us report more errors (test/files/neg/macro-basic-mamdmi) + // the old throwing behavior was relied on by diagnostics in manifestOfType + override def error(pos: Position, msg: String): Unit = this += TypeErrorWrapper(new TypeError(pos, msg)) + } + + // TODO: remove (specialization relies on TypeError being thrown, among other post-typer phases) + private class ThrowingReporter extends ContextReporter { + override def error(pos: Position, msg: String): Unit = throw new TypeError(pos, msg) + } + + /** Used during a run of [[scala.tools.nsc.typechecker.TreeCheckers]]? */ + private class CheckingReporter extends ContextReporter { + override def error(pos: Position, msg: String): Unit = onTreeCheckerError(pos, msg) + } + + class ImportInfo(val tree: Import, val depth: Int) { def pos = tree.pos def posOf(sel: ImportSelector) = tree.pos withPoint sel.namePos diff --git a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala index ed773f0178..0a5521aff4 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala @@ -1276,19 +1276,20 @@ trait Implicits { if (tagInScope.isEmpty) mot(tp, Nil, Nil) else { if (ReflectRuntimeUniverse == NoSymbol) { - // todo. write a test for this - context.error(pos, + // TODO: write a test for this (the next error message is already checked by neg/interop_typetags_without_classtags_arenot_manifests.scala) + // TODO: this was using context.error, and implicit search always runs in silent mode, thus it was actually throwing a TypeError + // with the new strategy-based reporting, a BufferingReporter buffers instead of throwing + // it would be good to rework this logic to fit into the regular context.error mechanism + throw new TypeError(pos, sm"""to create a manifest here, it is necessary to interoperate with the type tag `$tagInScope` in scope. |however typetag -> manifest conversion requires Scala reflection, which is not present on the classpath. |to proceed put scala-reflect.jar on your compilation classpath and recompile.""") - return SearchFailure } if (resolveClassTag(pos, tp, allowMaterialization = true) == EmptyTree) { - context.error(pos, + throw new TypeError(pos, sm"""to create a manifest here, it is necessary to interoperate with the type tag `$tagInScope` in scope. |however typetag -> manifest conversion requires a class tag for the corresponding type to be present. |to proceed add a class tag to the type `$tp` (e.g. by introducing a context bound) and recompile.""") - return SearchFailure } val cm = typed(Ident(ReflectRuntimeCurrentMirror)) val internal = gen.mkAttributedSelect(gen.mkAttributedRef(ReflectRuntimeUniverse), UniverseInternal) diff --git a/test/files/neg/macro-basic-mamdmi.check b/test/files/neg/macro-basic-mamdmi.check index 61df5131cc..54743d4936 100644 --- a/test/files/neg/macro-basic-mamdmi.check +++ b/test/files/neg/macro-basic-mamdmi.check @@ -1,5 +1,13 @@ +Impls_Macros_Test_1.scala:33: error: macro implementation not found: foo +(the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them) + println(foo(2) + Macros.bar(2) * new Macros().quux(4)) + ^ +Impls_Macros_Test_1.scala:33: error: macro implementation not found: bar +(the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them) + println(foo(2) + Macros.bar(2) * new Macros().quux(4)) + ^ Impls_Macros_Test_1.scala:33: error: macro implementation not found: quux (the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them) println(foo(2) + Macros.bar(2) * new Macros().quux(4)) ^ -one error found +three errors found -- cgit v1.2.3 From abb58dcef1b4758f4e3af26c030e1f0630b8d145 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sun, 27 Apr 2014 00:08:54 -0700 Subject: SI-6476 Improve error on escapement Behavior of escape processing under string interpolation can be confusing. This commit improves the exception message so you know at least what sort of escapes are handled. This came up on SO in the form `s"\d".r`, where it may not be obvious what is throwing and how to work around it. ``` scala> s"\d".r scala.StringContext$InvalidEscapeException: invalid escape '\d' not one of [\b, \t, \n, \f, \r, \\, \", \'] at index 0 in "\d". Use \\ for literal \. scala> s"a\" scala.StringContext$InvalidEscapeException: invalid escape at terminal index 1 in "a\". Use \\ for literal \. ``` Referencing SI-6476 because that has become the magnet ticket for "escape processing under string interpolation, huh?" This doesn't address `$"` and doesn't handle the more interesting parse error `s"a\"b"`. --- src/library/scala/StringContext.scala | 9 +++++++-- test/files/neg/t8266-invalid-interp.check | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'test/files') diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index c0468e8b02..0b08182102 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -175,8 +175,13 @@ object StringContext { * @param str The offending string * @param idx The index of the offending backslash character in `str`. */ - class InvalidEscapeException(str: String, @deprecatedName('idx) val index: Int) - extends IllegalArgumentException("invalid escape character at index "+index+" in \""+str+"\"") + class InvalidEscapeException(str: String, @deprecatedName('idx) val index: Int) extends IllegalArgumentException( + s"""invalid escape ${ + require(index >= 0 && index < str.length) + val ok = """[\b, \t, \n, \f, \r, \\, \", \']""" + if (index == str.length - 1) "at terminal" else s"'\\${str(index + 1)}' not one of $ok at" + } index $index in "$str". Use \\\\ for literal \\.""" + ) /** Expands standard Scala escape sequences in a string. * Escape sequences are: diff --git a/test/files/neg/t8266-invalid-interp.check b/test/files/neg/t8266-invalid-interp.check index 70dd4081b0..bb2d44a80c 100644 --- a/test/files/neg/t8266-invalid-interp.check +++ b/test/files/neg/t8266-invalid-interp.check @@ -1,10 +1,10 @@ t8266-invalid-interp.scala:4: error: Trailing '\' escapes nothing. f"a\", ^ -t8266-invalid-interp.scala:5: error: invalid escape character at index 1 in "a\xc" +t8266-invalid-interp.scala:5: error: invalid escape '\x' not one of [\b, \t, \n, \f, \r, \\, \", \'] at index 1 in "a\xc". Use \\ for literal \. f"a\xc", ^ -t8266-invalid-interp.scala:7: error: invalid escape character at index 1 in "a\vc" +t8266-invalid-interp.scala:7: error: invalid escape '\v' not one of [\b, \t, \n, \f, \r, \\, \", \'] at index 1 in "a\vc". Use \\ for literal \. f"a\vc" ^ three errors found -- cgit v1.2.3 From b4f506749189b337bd25633d521b6b917108ee15 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Fri, 18 Jul 2014 22:34:01 -0700 Subject: SI-6476 Documentation And adjust the test. --- src/compiler/scala/tools/nsc/ast/parser/Parsers.scala | 2 ++ test/files/run/t6631.scala | 18 ------------------ test/junit/scala/StringContextTest.scala | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 18 deletions(-) delete mode 100644 test/files/run/t6631.scala (limited to 'test/files') diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 883fd31dbc..90bc53721d 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -1254,6 +1254,8 @@ self => } if (in.token == STRINGLIT) partsBuf += literal() + // Documenting that it is intentional that the ident is not rooted for purposes of virtualization + //val t1 = atPos(o2p(start)) { Select(Select (Ident(nme.ROOTPKG), nme.scala_), nme.StringContext) } val t1 = atPos(o2p(start)) { Ident(nme.StringContext) } val t2 = atPos(start) { Apply(t1, partsBuf.toList) } t2 setPos t2.pos.makeTransparent diff --git a/test/files/run/t6631.scala b/test/files/run/t6631.scala deleted file mode 100644 index e472b83d50..0000000000 --- a/test/files/run/t6631.scala +++ /dev/null @@ -1,18 +0,0 @@ -import reflect.ClassTag - -object Test extends App { - def intercept[T <: Throwable : ClassTag](act: => Any) = try { - act - } catch { - case x: Throwable => - val cls = implicitly[ClassTag[T]].runtimeClass - assert(cls.isInstance(x), (x.getClass, x, cls).toString) - } - assert(s"""\f\r\n\t""" == "\f\r\n\t") - - import StringContext.InvalidEscapeException - intercept[InvalidEscapeException](s"""\""") - intercept[InvalidEscapeException](s"""\x""") - intercept[InvalidEscapeException](s"\") - -} diff --git a/test/junit/scala/StringContextTest.scala b/test/junit/scala/StringContextTest.scala index 5abfe90cd1..bb0e8c4252 100644 --- a/test/junit/scala/StringContextTest.scala +++ b/test/junit/scala/StringContextTest.scala @@ -48,4 +48,18 @@ class StringContextTest { val res = processEscapes(s) assertEquals("Scala", res) } + + @Test def t6631_baseline() = assertEquals("\f\r\n\t", s"""\f\r\n\t""") + + @Test def t6631_badEscape() = assertThrows[InvalidEscapeException] { + s"""\x""" + } + + // verifying that the standard interpolators can be supplanted + @Test def antiHijack_?() = { + object AllYourStringsAreBelongToMe { case class StringContext(args: Any*) { def s(args: Any) = "!!!!" } } + import AllYourStringsAreBelongToMe._ + //assertEquals("????", s"????") + assertEquals("!!!!", s"????") // OK to hijack core interpolator ids + } } -- cgit v1.2.3 From ed9dfee181e56bb83afa0598523786bee5572068 Mon Sep 17 00:00:00 2001 From: Antoine Gourlay Date: Tue, 29 Jul 2014 18:12:42 +0200 Subject: SI-4563 friendlier behavior for Ctrl+D in the REPL Closing the REPL with Ctrl+D does not issue a newline, so the user's prompt displays on the same line as the `scala>` prompt. This is bad. We now force a newline before closing the interpreter, and display `:quit` while we're at it so that people know how to exit the REPL (since `exit` doesn't exist anymore). The tricky part was to only add a newline when the console is interrupted, and *not* when it is closed by a command (like `:quit`), since commands are processed after their text (including newline) has been sent to the console. --- src/compiler/scala/tools/nsc/Properties.scala | 1 + src/repl/scala/tools/nsc/interpreter/ILoop.scala | 9 ++++++++- test/files/jvm/interpreter.check | 2 +- test/files/jvm/throws-annot-from-java.check | 2 +- test/files/jvm/xml05.check | 2 +- test/files/run/class-symbol-contravariant.check | 2 +- test/files/run/constant-type.check | 2 +- test/files/run/constrained-types.check | 2 +- test/files/run/kind-repl-command.check | 2 +- test/files/run/lub-visibility.check | 2 +- test/files/run/macro-bundle-repl.check | 2 +- test/files/run/macro-repl-basic.check | 2 +- test/files/run/macro-repl-dontexpand.check | 2 +- test/files/run/macro-system-properties.check | 2 +- test/files/run/reflection-equality.check | 2 +- test/files/run/reflection-magicsymbols-repl.check | 2 +- test/files/run/reflection-repl-classes.check | 2 +- test/files/run/reflection-repl-elementary.check | 2 +- test/files/run/reify-repl-fail-gracefully.check | 2 +- test/files/run/reify_newimpl_22.check | 2 +- test/files/run/reify_newimpl_23.check | 2 +- test/files/run/reify_newimpl_25.check | 2 +- test/files/run/reify_newimpl_26.check | 2 +- test/files/run/reify_newimpl_35.check | 2 +- test/files/run/repl-assign.check | 2 +- test/files/run/repl-bare-expr.check | 2 +- test/files/run/repl-colon-type.check | 2 +- test/files/run/repl-empty-package.check | 2 +- test/files/run/repl-javap-app.check | 6 +++--- test/files/run/repl-out-dir.check | 2 +- test/files/run/repl-parens.check | 2 +- test/files/run/repl-paste-2.check | 2 +- test/files/run/repl-paste-3.check | 2 +- test/files/run/repl-paste-4.scala | 2 +- test/files/run/repl-paste-raw.scala | 2 +- test/files/run/repl-paste.check | 2 +- test/files/run/repl-power.check | 2 +- test/files/run/repl-reset.check | 2 +- test/files/run/repl-save.scala | 2 +- test/files/run/repl-term-macros.check | 2 +- test/files/run/repl-transcript.check | 2 +- test/files/run/repl-trim-stack-trace.scala | 2 +- test/files/run/repl-type-verbose.check | 2 +- test/files/run/t3376.check | 2 +- test/files/run/t4025.check | 2 +- test/files/run/t4172.check | 2 +- test/files/run/t4216.check | 2 +- test/files/run/t4285.check | 2 +- test/files/run/t4542.check | 2 +- test/files/run/t4594-repl-settings.scala | 2 +- test/files/run/t4671.check | 2 +- test/files/run/t4710.check | 2 +- test/files/run/t5072.check | 2 +- test/files/run/t5256d.check | 2 +- test/files/run/t5535.check | 2 +- test/files/run/t5537.check | 2 +- test/files/run/t5583.check | 2 +- test/files/run/t5655.check | 2 +- test/files/run/t5789.check | 2 +- test/files/run/t6086-repl.check | 2 +- test/files/run/t6146b.check | 2 +- test/files/run/t6187.check | 2 +- test/files/run/t6273.check | 2 +- test/files/run/t6320.check | 2 +- test/files/run/t6329_repl.check | 2 +- test/files/run/t6329_repl_bug.check | 2 +- test/files/run/t6381.check | 2 +- test/files/run/t6434.check | 2 +- test/files/run/t6439.check | 2 +- test/files/run/t6507.check | 2 +- test/files/run/t6549.check | 2 +- test/files/run/t6937.check | 2 +- test/files/run/t7185.check | 2 +- test/files/run/t7319.check | 2 +- test/files/run/t7482a.check | 2 +- test/files/run/t7634.check | 2 +- test/files/run/t7747-repl.check | 2 +- test/files/run/t7801.check | 2 +- test/files/run/t7805-repl-i.check | 2 +- test/files/run/tpeCache-tyconCache.check | 2 +- 80 files changed, 89 insertions(+), 81 deletions(-) (limited to 'test/files') diff --git a/src/compiler/scala/tools/nsc/Properties.scala b/src/compiler/scala/tools/nsc/Properties.scala index ed5fda9c3f..59fefba954 100644 --- a/src/compiler/scala/tools/nsc/Properties.scala +++ b/src/compiler/scala/tools/nsc/Properties.scala @@ -14,6 +14,7 @@ object Properties extends scala.util.PropertiesTrait { // settings based on jar properties def residentPromptString = scalaPropOrElse("resident.prompt", "\nnsc> ") def shellPromptString = scalaPropOrElse("shell.prompt", "\nscala> ") + def shellInterruptedString = scalaPropOrElse("shell.interrupted", ":quit\n") // derived values def isEmacsShell = propOrEmpty("env.emacs") != "" diff --git a/src/repl/scala/tools/nsc/interpreter/ILoop.scala b/src/repl/scala/tools/nsc/interpreter/ILoop.scala index ce0eadc04f..50c89f7442 100644 --- a/src/repl/scala/tools/nsc/interpreter/ILoop.scala +++ b/src/repl/scala/tools/nsc/interpreter/ILoop.scala @@ -430,7 +430,14 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) import scala.concurrent.duration._ Await.ready(globalFuture, 60.seconds) - (line ne null) && (command(line) match { + if (line eq null) { + // SI-4563: this means the console was properly interrupted (Ctrl+D usually) + // so we display the output message (which by default ends with + // a newline so as not to break the user's terminal) + if (in.interactive) out.print(Properties.shellInterruptedString) + + false + } else (command(line) match { case Result(false, _) => false case Result(_, Some(line)) => addReplay(line) ; true case _ => true diff --git a/test/files/jvm/interpreter.check b/test/files/jvm/interpreter.check index d124794e72..d03edb638c 100644 --- a/test/files/jvm/interpreter.check +++ b/test/files/jvm/interpreter.check @@ -361,7 +361,7 @@ It would fail on the following inputs: Exp(), Term() ^ f: (e: Exp)Int -scala> +scala> :quit plusOne: (x: Int)Int res0: Int = 6 res0: String = after reset diff --git a/test/files/jvm/throws-annot-from-java.check b/test/files/jvm/throws-annot-from-java.check index be3ba412f8..c541b26fcc 100644 --- a/test/files/jvm/throws-annot-from-java.check +++ b/test/files/jvm/throws-annot-from-java.check @@ -44,4 +44,4 @@ bar tp.typeParams.isEmpty: true throws[test.PolymorphicException[_]](classOf[test.PolymorphicException]) -scala> +scala> :quit diff --git a/test/files/jvm/xml05.check b/test/files/jvm/xml05.check index 92ea995350..cad907525d 100644 --- a/test/files/jvm/xml05.check +++ b/test/files/jvm/xml05.check @@ -4,4 +4,4 @@ Type :help for more information. scala> res0: scala.xml.Elem = -scala> +scala> :quit diff --git a/test/files/run/class-symbol-contravariant.check b/test/files/run/class-symbol-contravariant.check index 987f215bca..cbb90b52c2 100644 --- a/test/files/run/class-symbol-contravariant.check +++ b/test/files/run/class-symbol-contravariant.check @@ -33,4 +33,4 @@ res2: Boolean = true scala> sym.isContravariant // was true res3: Boolean = false -scala> +scala> :quit diff --git a/test/files/run/constant-type.check b/test/files/run/constant-type.check index 77bdf618e6..a7ba5a46c2 100644 --- a/test/files/run/constant-type.check +++ b/test/files/run/constant-type.check @@ -23,4 +23,4 @@ Class[String](classOf[java.lang.String]) scala> { ConstantType(Constant(s)); exitingPhase(currentRun.erasurePhase)(println(ConstantType(Constant(s)))); } Class(classOf[java.lang.String]) -scala> +scala> :quit diff --git a/test/files/run/constrained-types.check b/test/files/run/constrained-types.check index a3cd59b9fb..89a08d5ccb 100644 --- a/test/files/run/constrained-types.check +++ b/test/files/run/constrained-types.check @@ -148,4 +148,4 @@ scala> val x = 3 : Int @Annot(e+f+g+h) // should have a graceful error message val x = 3 : Int @Annot(e+f+g+h) // should have a graceful error message ^ -scala> +scala> :quit diff --git a/test/files/run/kind-repl-command.check b/test/files/run/kind-repl-command.check index 1c292572e6..586b2710e1 100644 --- a/test/files/run/kind-repl-command.check +++ b/test/files/run/kind-repl-command.check @@ -25,4 +25,4 @@ scala> :k Nonexisting Nonexisting ^ -scala> +scala> :quit diff --git a/test/files/run/lub-visibility.check b/test/files/run/lub-visibility.check index f76579412e..70734966f0 100644 --- a/test/files/run/lub-visibility.check +++ b/test/files/run/lub-visibility.check @@ -8,4 +8,4 @@ scala> // but reverted that for SI-5534. scala> val x = List(List(), Vector()) x: List[scala.collection.immutable.Seq[Nothing] with scala.collection.AbstractSeq[Nothing] with java.io.Serializable] = List(List(), Vector()) -scala> +scala> :quit diff --git a/test/files/run/macro-bundle-repl.check b/test/files/run/macro-bundle-repl.check index 4a0b421606..75c5c2adda 100644 --- a/test/files/run/macro-bundle-repl.check +++ b/test/files/run/macro-bundle-repl.check @@ -21,4 +21,4 @@ defined term macro foo: Unit scala> foo -scala> +scala> :quit diff --git a/test/files/run/macro-repl-basic.check b/test/files/run/macro-repl-basic.check index 86b4d472ed..fab03d1558 100644 --- a/test/files/run/macro-repl-basic.check +++ b/test/files/run/macro-repl-basic.check @@ -49,4 +49,4 @@ import Macros.Shmacros._ scala> println(foo(2) + Macros.bar(2) * new Macros().quux(4)) 31 -scala> +scala> :quit diff --git a/test/files/run/macro-repl-dontexpand.check b/test/files/run/macro-repl-dontexpand.check index 20d3b2d702..6ecc9245fa 100644 --- a/test/files/run/macro-repl-dontexpand.check +++ b/test/files/run/macro-repl-dontexpand.check @@ -13,4 +13,4 @@ bar2: (c: scala.reflect.macros.whitebox.Context)Nothing scala> def foo2 = macro bar2 defined term macro foo2: Nothing -scala> +scala> :quit diff --git a/test/files/run/macro-system-properties.check b/test/files/run/macro-system-properties.check index ffbd5a8aa8..e2e2bd32b9 100644 --- a/test/files/run/macro-system-properties.check +++ b/test/files/run/macro-system-properties.check @@ -19,4 +19,4 @@ defined object Test scala> object Test { class C(implicit a: Any) { GrabContext.grab } } defined object Test -scala> +scala> :quit diff --git a/test/files/run/reflection-equality.check b/test/files/run/reflection-equality.check index 682326bc18..d60d861a90 100644 --- a/test/files/run/reflection-equality.check +++ b/test/files/run/reflection-equality.check @@ -48,4 +48,4 @@ res2: Boolean = true scala> t2 <:< t1 res3: Boolean = true -scala> +scala> :quit diff --git a/test/files/run/reflection-magicsymbols-repl.check b/test/files/run/reflection-magicsymbols-repl.check index 72aef1d3be..ca8857ada4 100644 --- a/test/files/run/reflection-magicsymbols-repl.check +++ b/test/files/run/reflection-magicsymbols-repl.check @@ -34,4 +34,4 @@ scala.Null scala.Nothing scala.Singleton -scala> +scala> :quit diff --git a/test/files/run/reflection-repl-classes.check b/test/files/run/reflection-repl-classes.check index 03a6aef2b5..5ebf993a87 100644 --- a/test/files/run/reflection-repl-classes.check +++ b/test/files/run/reflection-repl-classes.check @@ -30,4 +30,4 @@ scala> scala> mm(new A) res0: Any = 1 -scala> +scala> :quit diff --git a/test/files/run/reflection-repl-elementary.check b/test/files/run/reflection-repl-elementary.check index 4a223e8a24..e948c9fd61 100644 --- a/test/files/run/reflection-repl-elementary.check +++ b/test/files/run/reflection-repl-elementary.check @@ -4,4 +4,4 @@ Type :help for more information. scala> scala.reflect.runtime.universe.typeOf[List[Nothing]] res0: reflect.runtime.universe.Type = scala.List[Nothing] -scala> +scala> :quit diff --git a/test/files/run/reify-repl-fail-gracefully.check b/test/files/run/reify-repl-fail-gracefully.check index 29ccee3cc6..c9e69744d6 100644 --- a/test/files/run/reify-repl-fail-gracefully.check +++ b/test/files/run/reify-repl-fail-gracefully.check @@ -14,4 +14,4 @@ scala> reify reify ^ -scala> +scala> :quit diff --git a/test/files/run/reify_newimpl_22.check b/test/files/run/reify_newimpl_22.check index 1432d10127..952f384a1c 100644 --- a/test/files/run/reify_newimpl_22.check +++ b/test/files/run/reify_newimpl_22.check @@ -22,4 +22,4 @@ scala> { ^ 2 -scala> +scala> :quit diff --git a/test/files/run/reify_newimpl_23.check b/test/files/run/reify_newimpl_23.check index 217f0a98c7..b7e9bfdfbc 100644 --- a/test/files/run/reify_newimpl_23.check +++ b/test/files/run/reify_newimpl_23.check @@ -21,4 +21,4 @@ scala> def foo[T]{ ^ foo: [T]=> Unit -scala> +scala> :quit diff --git a/test/files/run/reify_newimpl_25.check b/test/files/run/reify_newimpl_25.check index 93ad69defa..4f36ba10ee 100644 --- a/test/files/run/reify_newimpl_25.check +++ b/test/files/run/reify_newimpl_25.check @@ -12,4 +12,4 @@ scala> { ^ TypeTag[x.type] -scala> +scala> :quit diff --git a/test/files/run/reify_newimpl_26.check b/test/files/run/reify_newimpl_26.check index 8e0ad87bf2..681b862795 100644 --- a/test/files/run/reify_newimpl_26.check +++ b/test/files/run/reify_newimpl_26.check @@ -14,4 +14,4 @@ foo: [T]=> Unit scala> foo[Int] WeakTypeTag[scala.List[T]] -scala> +scala> :quit diff --git a/test/files/run/reify_newimpl_35.check b/test/files/run/reify_newimpl_35.check index f884d2c0d0..bd9b3a2fb1 100644 --- a/test/files/run/reify_newimpl_35.check +++ b/test/files/run/reify_newimpl_35.check @@ -10,4 +10,4 @@ foo: [T](implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtim scala> println(foo) Expr[List[Nothing]](Nil) -scala> +scala> :quit diff --git a/test/files/run/repl-assign.check b/test/files/run/repl-assign.check index bdc7793c37..faa8a93244 100644 --- a/test/files/run/repl-assign.check +++ b/test/files/run/repl-assign.check @@ -13,4 +13,4 @@ x: Int = 12 scala> y = 13 y: Int = 13 -scala> +scala> :quit diff --git a/test/files/run/repl-bare-expr.check b/test/files/run/repl-bare-expr.check index 97ae208ff4..07cf23412f 100644 --- a/test/files/run/repl-bare-expr.check +++ b/test/files/run/repl-bare-expr.check @@ -47,4 +47,4 @@ Bovine.x: List[Any] = List(Ruminant(5), Cow, Moooooo) scala> Bovine.x res4: List[Any] = List(Ruminant(5), Cow, Moooooo) -scala> +scala> :quit diff --git a/test/files/run/repl-colon-type.check b/test/files/run/repl-colon-type.check index 1f6d3e2b39..9898027c1d 100644 --- a/test/files/run/repl-colon-type.check +++ b/test/files/run/repl-colon-type.check @@ -218,4 +218,4 @@ Unit scala> :type println("side effect!") Unit -scala> +scala> :quit diff --git a/test/files/run/repl-empty-package.check b/test/files/run/repl-empty-package.check index ecf79c2c6d..d3b75f685e 100644 --- a/test/files/run/repl-empty-package.check +++ b/test/files/run/repl-empty-package.check @@ -4,4 +4,4 @@ Type :help for more information. scala> println(Bippy.bippy) bippy! -scala> +scala> :quit diff --git a/test/files/run/repl-javap-app.check b/test/files/run/repl-javap-app.check index 1136b415d7..eb3718f44b 100644 --- a/test/files/run/repl-javap-app.check +++ b/test/files/run/repl-javap-app.check @@ -15,7 +15,7 @@ public final void delayedEndpoint$MyApp$1(); Start Length Slot Name Signature 0 9 0 this LMyApp$; -scala> +scala> :quit #partest java7 Welcome to Scala Type in expressions to have them evaluated. @@ -37,7 +37,7 @@ scala> :javap -app MyApp$ line 5: 0 } -scala> +scala> :quit #partest java8 Welcome to Scala Type in expressions to have them evaluated. @@ -60,4 +60,4 @@ scala> :javap -app MyApp$ line 5: 0 } -scala> +scala> :quit diff --git a/test/files/run/repl-out-dir.check b/test/files/run/repl-out-dir.check index 3e51c63155..c354492898 100644 --- a/test/files/run/repl-out-dir.check +++ b/test/files/run/repl-out-dir.check @@ -46,4 +46,4 @@ repl-out-dir-run.obj Test$.class Test.class -scala> +scala> :quit diff --git a/test/files/run/repl-parens.check b/test/files/run/repl-parens.check index 15f4b4524a..74d15ff93c 100644 --- a/test/files/run/repl-parens.check +++ b/test/files/run/repl-parens.check @@ -81,4 +81,4 @@ scala> scala> List(1) ++ List('a') res16: List[AnyVal] = List(1, a) -scala> +scala> :quit diff --git a/test/files/run/repl-paste-2.check b/test/files/run/repl-paste-2.check index ab3809a2e0..6ea8e2f419 100644 --- a/test/files/run/repl-paste-2.check +++ b/test/files/run/repl-paste-2.check @@ -58,4 +58,4 @@ scala> x.length + res5 res3: Int = 129 -scala> +scala> :quit diff --git a/test/files/run/repl-paste-3.check b/test/files/run/repl-paste-3.check index 8fae61792e..23e402852f 100644 --- a/test/files/run/repl-paste-3.check +++ b/test/files/run/repl-paste-3.check @@ -7,4 +7,4 @@ scala> println(3) scala> List(1,2) res1: List[Int] = List(1, 2) -scala> +scala> :quit diff --git a/test/files/run/repl-paste-4.scala b/test/files/run/repl-paste-4.scala index 0060dc1ff6..cb0a6aa768 100644 --- a/test/files/run/repl-paste-4.scala +++ b/test/files/run/repl-paste-4.scala @@ -14,7 +14,7 @@ s"""|Type in expressions to have them evaluated. |scala> Foo(new Foo) |res0: Int = 7 | - |scala> """ + |scala> :quit""" def pastie = testPath changeExtension "pastie" } diff --git a/test/files/run/repl-paste-raw.scala b/test/files/run/repl-paste-raw.scala index 2953796f99..3b41254e96 100644 --- a/test/files/run/repl-paste-raw.scala +++ b/test/files/run/repl-paste-raw.scala @@ -15,6 +15,6 @@ s"""|Type in expressions to have them evaluated. |scala> favoriteThing.hasString |res0: Boolean = true | - |scala> """ + |scala> :quit""" def pastie = testPath changeExtension "pastie" } diff --git a/test/files/run/repl-paste.check b/test/files/run/repl-paste.check index 97f177ddc4..171447214f 100644 --- a/test/files/run/repl-paste.check +++ b/test/files/run/repl-paste.check @@ -23,4 +23,4 @@ defined class Dingus defined object Dingus x: Int = 110 -scala> +scala> :quit diff --git a/test/files/run/repl-power.check b/test/files/run/repl-power.check index 8a8ca46012..e2318c93f2 100644 --- a/test/files/run/repl-power.check +++ b/test/files/run/repl-power.check @@ -27,4 +27,4 @@ m: $r.treedsl.global.Literal = 10 scala> typed(m).tpe // typed is in scope res2: $r.treedsl.global.Type = Int(10) -scala> +scala> :quit diff --git a/test/files/run/repl-reset.check b/test/files/run/repl-reset.check index ed95c7b8ff..cd7893bbc3 100644 --- a/test/files/run/repl-reset.check +++ b/test/files/run/repl-reset.check @@ -54,4 +54,4 @@ defined class BippyBungus scala> { new BippyBungus ; x1 } res2: Int = 4 -scala> +scala> :quit diff --git a/test/files/run/repl-save.scala b/test/files/run/repl-save.scala index 4539790b1a..c98e6aebc3 100644 --- a/test/files/run/repl-save.scala +++ b/test/files/run/repl-save.scala @@ -16,7 +16,7 @@ s"""|Type in expressions to have them evaluated. | |scala> :save $saveto | - |scala> """ + |scala> :quit""" def saveto = testOutput / "session.repl" override def show() = { super.show() diff --git a/test/files/run/repl-term-macros.check b/test/files/run/repl-term-macros.check index 3580bfe1f1..2cd0b93cd0 100644 --- a/test/files/run/repl-term-macros.check +++ b/test/files/run/repl-term-macros.check @@ -37,4 +37,4 @@ defined term macro foo3: (x: Int)(y: Int)Unit scala> foo3(2)(3) -scala> +scala> :quit diff --git a/test/files/run/repl-transcript.check b/test/files/run/repl-transcript.check index 49891af900..b0f106387b 100644 --- a/test/files/run/repl-transcript.check +++ b/test/files/run/repl-transcript.check @@ -35,4 +35,4 @@ scala> res6.sum + res5 res0: Int = 5273 -scala> +scala> :quit diff --git a/test/files/run/repl-trim-stack-trace.scala b/test/files/run/repl-trim-stack-trace.scala index 483659146a..a53ce3b3e4 100644 --- a/test/files/run/repl-trim-stack-trace.scala +++ b/test/files/run/repl-trim-stack-trace.scala @@ -32,7 +32,7 @@ java.lang.Exception at .f(:7) ... 69 elided -scala> """ +scala> :quit""" // normalize the "elided" lines because the frame count depends on test context lazy val elided = """(\s+\.{3} )\d+( elided)""".r diff --git a/test/files/run/repl-type-verbose.check b/test/files/run/repl-type-verbose.check index e37754a060..6f6b47b86d 100644 --- a/test/files/run/repl-type-verbose.check +++ b/test/files/run/repl-type-verbose.check @@ -187,4 +187,4 @@ PolyType( ) ) -scala> +scala> :quit diff --git a/test/files/run/t3376.check b/test/files/run/t3376.check index cc6949d326..b8fd2843f6 100644 --- a/test/files/run/t3376.check +++ b/test/files/run/t3376.check @@ -13,4 +13,4 @@ m2: M[Float] = mmm scala> val m3 = new M[String]() m3: M[String] = mmm -scala> +scala> :quit diff --git a/test/files/run/t4025.check b/test/files/run/t4025.check index 2d4f644c5a..e8c6851236 100644 --- a/test/files/run/t4025.check +++ b/test/files/run/t4025.check @@ -14,4 +14,4 @@ scala> scala> def f(c: Any) = c match { case Red(_) => () } f: (c: Any)Unit -scala> +scala> :quit diff --git a/test/files/run/t4172.check b/test/files/run/t4172.check index a748430e2e..315c1c9dbd 100644 --- a/test/files/run/t4172.check +++ b/test/files/run/t4172.check @@ -5,4 +5,4 @@ scala> val c = { class C { override def toString = "C" }; ((new C, new C { def f warning: there was one feature warning; re-run with -feature for details c: (C, C{def f: Int}) forSome { type C <: AnyRef } = (C,C) -scala> +scala> :quit diff --git a/test/files/run/t4216.check b/test/files/run/t4216.check index 091e55a0c7..e4610e87d3 100644 --- a/test/files/run/t4216.check +++ b/test/files/run/t4216.check @@ -34,4 +34,4 @@ res4: java.util.List[V] = [V@0] scala> o(new V(0)) res5: java.util.List[Any] = [V@0] -scala> +scala> :quit diff --git a/test/files/run/t4285.check b/test/files/run/t4285.check index 314c8e5a35..b952cb8e1b 100644 --- a/test/files/run/t4285.check +++ b/test/files/run/t4285.check @@ -10,4 +10,4 @@ y: scala.collection.mutable.WrappedArray[Int] = WrappedArray(2, 4, 6, 8, 10, 12, scala> println(y.sum) 56 -scala> +scala> :quit diff --git a/test/files/run/t4542.check b/test/files/run/t4542.check index a53f31a3c7..f7716dc2f0 100644 --- a/test/files/run/t4542.check +++ b/test/files/run/t4542.check @@ -12,4 +12,4 @@ scala> val f = new Foo ^ f: Foo = Bippy -scala> +scala> :quit diff --git a/test/files/run/t4594-repl-settings.scala b/test/files/run/t4594-repl-settings.scala index 8b8b2e3746..4202991607 100644 --- a/test/files/run/t4594-repl-settings.scala +++ b/test/files/run/t4594-repl-settings.scala @@ -22,5 +22,5 @@ object Test extends SessionTest { | ^ |b: String | - |scala> """ + |scala> :quit""" } diff --git a/test/files/run/t4671.check b/test/files/run/t4671.check index 0c36083759..1640dac8e4 100644 --- a/test/files/run/t4671.check +++ b/test/files/run/t4671.check @@ -43,4 +43,4 @@ println(s.mkString("")) } -scala> +scala> :quit diff --git a/test/files/run/t4710.check b/test/files/run/t4710.check index 6ee7198b4b..0dd49dfbd3 100644 --- a/test/files/run/t4710.check +++ b/test/files/run/t4710.check @@ -5,4 +5,4 @@ scala> def method : String = { implicit def f(s: Symbol) = "" ; 'symbol } warning: there was one feature warning; re-run with -feature for details method: String -scala> +scala> :quit diff --git a/test/files/run/t5072.check b/test/files/run/t5072.check index ddd49c71cb..ab34e49869 100644 --- a/test/files/run/t5072.check +++ b/test/files/run/t5072.check @@ -7,4 +7,4 @@ defined class C scala> Thread.currentThread.getContextClassLoader.loadClass(classOf[C].getName) res0: Class[_] = class C -scala> +scala> :quit diff --git a/test/files/run/t5256d.check b/test/files/run/t5256d.check index d42d234386..c2b49989ab 100644 --- a/test/files/run/t5256d.check +++ b/test/files/run/t5256d.check @@ -25,4 +25,4 @@ scala.AnyRef { def foo: scala.Nothing } -scala> +scala> :quit diff --git a/test/files/run/t5535.check b/test/files/run/t5535.check index a0c87a47f4..84097ccea9 100644 --- a/test/files/run/t5535.check +++ b/test/files/run/t5535.check @@ -13,4 +13,4 @@ f: Int => Int = scala> println(f(10)) 11 -scala> +scala> :quit diff --git a/test/files/run/t5537.check b/test/files/run/t5537.check index b9d521f301..98265ccc92 100644 --- a/test/files/run/t5537.check +++ b/test/files/run/t5537.check @@ -13,4 +13,4 @@ res2: List[scala.collection.immutable.List.type] = List() scala> List[Set.type]() res3: List[Set.type] = List() -scala> +scala> :quit diff --git a/test/files/run/t5583.check b/test/files/run/t5583.check index af96405bdd..32d285cbb3 100644 --- a/test/files/run/t5583.check +++ b/test/files/run/t5583.check @@ -13,4 +13,4 @@ scala> for (i <- 1 to 10) {s += i} scala> println(s) 165 -scala> +scala> :quit diff --git a/test/files/run/t5655.check b/test/files/run/t5655.check index 06c6b32599..4bbc54b641 100644 --- a/test/files/run/t5655.check +++ b/test/files/run/t5655.check @@ -23,4 +23,4 @@ and import x x ^ -scala> +scala> :quit diff --git a/test/files/run/t5789.check b/test/files/run/t5789.check index bcb2382559..193abfaff0 100644 --- a/test/files/run/t5789.check +++ b/test/files/run/t5789.check @@ -7,4 +7,4 @@ n: Int = 2 scala> () => n res0: () => Int = -scala> +scala> :quit diff --git a/test/files/run/t6086-repl.check b/test/files/run/t6086-repl.check index 115eff5f85..b904f118e8 100644 --- a/test/files/run/t6086-repl.check +++ b/test/files/run/t6086-repl.check @@ -7,4 +7,4 @@ defined class X scala> scala.reflect.runtime.universe.typeOf[X] res0: reflect.runtime.universe.Type = X -scala> +scala> :quit diff --git a/test/files/run/t6146b.check b/test/files/run/t6146b.check index a3b09efcd9..6998873fb7 100644 --- a/test/files/run/t6146b.check +++ b/test/files/run/t6146b.check @@ -60,4 +60,4 @@ res2: u.Type = O.S3 scala> memType(S4, fTpe) res3: u.Type = S4 -scala> +scala> :quit diff --git a/test/files/run/t6187.check b/test/files/run/t6187.check index 0180125809..9a9e266ec6 100644 --- a/test/files/run/t6187.check +++ b/test/files/run/t6187.check @@ -29,4 +29,4 @@ res1: List[Int] = List(1) scala> List("") collect { case x => x } res2: List[String] = List("") -scala> +scala> :quit diff --git a/test/files/run/t6273.check b/test/files/run/t6273.check index bef0b227d2..3b682800df 100644 --- a/test/files/run/t6273.check +++ b/test/files/run/t6273.check @@ -12,4 +12,4 @@ x: String = y = 55 " -scala> +scala> :quit diff --git a/test/files/run/t6320.check b/test/files/run/t6320.check index 013acc1c54..af7c865690 100644 --- a/test/files/run/t6320.check +++ b/test/files/run/t6320.check @@ -10,4 +10,4 @@ defined class Dyn scala> new Dyn(Map("foo" -> 10)).foo[Int] res0: Int = 10 -scala> +scala> :quit diff --git a/test/files/run/t6329_repl.check b/test/files/run/t6329_repl.check index ad0bb46e5b..ebb1aace7c 100644 --- a/test/files/run/t6329_repl.check +++ b/test/files/run/t6329_repl.check @@ -32,4 +32,4 @@ res6: scala.reflect.ClassTag[scala.collection.immutable.Set[_]] = scala.collecti scala> classTag[scala.collection.immutable.Set[_]] res7: scala.reflect.ClassTag[scala.collection.immutable.Set[_]] = scala.collection.immutable.Set -scala> +scala> :quit diff --git a/test/files/run/t6329_repl_bug.check b/test/files/run/t6329_repl_bug.check index 38a8de5606..84297a629f 100644 --- a/test/files/run/t6329_repl_bug.check +++ b/test/files/run/t6329_repl_bug.check @@ -14,4 +14,4 @@ res0: scala.reflect.ClassTag[List[_]] = scala.collection.immutable.List[] scala> scala.reflect.classTag[List[_]] res1: scala.reflect.ClassTag[List[_]] = scala.collection.immutable.List -scala> +scala> :quit diff --git a/test/files/run/t6381.check b/test/files/run/t6381.check index 4ed11d59ff..49c6a784ad 100644 --- a/test/files/run/t6381.check +++ b/test/files/run/t6381.check @@ -16,4 +16,4 @@ defined term macro pos: String scala> pos res0: String = class scala.reflect.internal.util.RangePosition -scala> +scala> :quit diff --git a/test/files/run/t6434.check b/test/files/run/t6434.check index f898b6b781..0a75ae2bd5 100644 --- a/test/files/run/t6434.check +++ b/test/files/run/t6434.check @@ -7,4 +7,4 @@ f: (x: => Int)Int scala> f _ res0: (=> Int) => Int = -scala> +scala> :quit diff --git a/test/files/run/t6439.check b/test/files/run/t6439.check index f8d5b3a8cd..c4b7591069 100644 --- a/test/files/run/t6439.check +++ b/test/files/run/t6439.check @@ -70,4 +70,4 @@ defined object lookup scala> lookup("F") // this now works as a result of changing .typeSymbol to .typeSymbolDirect in IMain#Request#definedSymbols res0: $r.intp.global.Symbol = type F -scala> +scala> :quit diff --git a/test/files/run/t6507.check b/test/files/run/t6507.check index 3536c42381..5da4aa3a24 100644 --- a/test/files/run/t6507.check +++ b/test/files/run/t6507.check @@ -21,4 +21,4 @@ scala> res0 ! res1: A = A -scala> +scala> :quit diff --git a/test/files/run/t6549.check b/test/files/run/t6549.check index d5dfc5ebe8..be3445927e 100644 --- a/test/files/run/t6549.check +++ b/test/files/run/t6549.check @@ -25,4 +25,4 @@ m(scala.Symbol("s")).xxx: Any = 's scala> val `"` = 0 ": Int = 0 -scala> +scala> :quit diff --git a/test/files/run/t6937.check b/test/files/run/t6937.check index 4729dc7006..5c5d4485b6 100644 --- a/test/files/run/t6937.check +++ b/test/files/run/t6937.check @@ -19,4 +19,4 @@ apiru: scala.reflect.api.Universe = scala> apiru.typeTag[A].in(cm) res0: reflect.runtime.universe.TypeTag[A] = TypeTag[A] -scala> +scala> :quit diff --git a/test/files/run/t7185.check b/test/files/run/t7185.check index ebf85b731f..e4f80a8ff9 100644 --- a/test/files/run/t7185.check +++ b/test/files/run/t7185.check @@ -29,4 +29,4 @@ res0: Any = } } -scala> +scala> :quit diff --git a/test/files/run/t7319.check b/test/files/run/t7319.check index 2ac4142098..e35cfc90c0 100644 --- a/test/files/run/t7319.check +++ b/test/files/run/t7319.check @@ -40,4 +40,4 @@ scala> Range(1,2).toArray: Seq[_] scala> 0 res2: Int = 0 -scala> +scala> :quit diff --git a/test/files/run/t7482a.check b/test/files/run/t7482a.check index 943538f352..a21ef7b68f 100644 --- a/test/files/run/t7482a.check +++ b/test/files/run/t7482a.check @@ -7,4 +7,4 @@ v: java.util.ArrayList[String] = [] scala> val v: java.util.ArrayList[String] = new java.util.ArrayList[String](5) v: java.util.ArrayList[String] = [] -scala> +scala> :quit diff --git a/test/files/run/t7634.check b/test/files/run/t7634.check index aea3b94da5..9c6b8b47dd 100644 --- a/test/files/run/t7634.check +++ b/test/files/run/t7634.check @@ -5,4 +5,4 @@ Type :help for more information. scala> .lines res1: List[String] = List(shello, world.) -scala> +scala> :quit diff --git a/test/files/run/t7747-repl.check b/test/files/run/t7747-repl.check index ad924f482c..105b238d01 100644 --- a/test/files/run/t7747-repl.check +++ b/test/files/run/t7747-repl.check @@ -283,4 +283,4 @@ object $read extends $read { } res3: List[Product with Serializable] = List(BippyBups(), PuppyPups(), Bingo()) -scala> +scala> :quit diff --git a/test/files/run/t7801.check b/test/files/run/t7801.check index d72060c684..e0b656b784 100644 --- a/test/files/run/t7801.check +++ b/test/files/run/t7801.check @@ -8,4 +8,4 @@ import g.abort scala> class C(val a: Any) extends AnyVal defined class C -scala> +scala> :quit diff --git a/test/files/run/t7805-repl-i.check b/test/files/run/t7805-repl-i.check index eecfff079a..7f66c06a11 100644 --- a/test/files/run/t7805-repl-i.check +++ b/test/files/run/t7805-repl-i.check @@ -8,4 +8,4 @@ Type :help for more information. scala> Console println Try(8) Success(8) -scala> +scala> :quit diff --git a/test/files/run/tpeCache-tyconCache.check b/test/files/run/tpeCache-tyconCache.check index a892f5477a..ff604819e0 100644 --- a/test/files/run/tpeCache-tyconCache.check +++ b/test/files/run/tpeCache-tyconCache.check @@ -16,4 +16,4 @@ res0: Boolean = true scala> AnyRefClass.tpe eq AnyRefClass.typeConstructor res1: Boolean = true -scala> +scala> :quit -- cgit v1.2.3 From 0270972b9f70d107dff393081dc48c877be1f161 Mon Sep 17 00:00:00 2001 From: Lukas Rytz Date: Tue, 12 Aug 2014 10:57:42 +0200 Subject: test for InnerClass and EnclosingMethod attributes Some parts of the test assert (current) buggy behavior. This is marked in the test file with TODO. It will be fixed in later work on the backend. --- test/files/jvm/innerClassAttribute/Classes_1.scala | 99 ++++++++++ .../files/jvm/innerClassAttribute/JavaAnnot_1.java | 3 + test/files/jvm/innerClassAttribute/Test.scala | 208 +++++++++++++++++++++ 3 files changed, 310 insertions(+) create mode 100644 test/files/jvm/innerClassAttribute/Classes_1.scala create mode 100644 test/files/jvm/innerClassAttribute/JavaAnnot_1.java create mode 100644 test/files/jvm/innerClassAttribute/Test.scala (limited to 'test/files') diff --git a/test/files/jvm/innerClassAttribute/Classes_1.scala b/test/files/jvm/innerClassAttribute/Classes_1.scala new file mode 100644 index 0000000000..0875d9160c --- /dev/null +++ b/test/files/jvm/innerClassAttribute/Classes_1.scala @@ -0,0 +1,99 @@ +class A1 { + class B +} + +class A2 { + object B +} + +object A3 { + class B1 + object B2 +} + +class A4 { + def f(l: List[Int]): List[Int] = { + l map (_ + 1) + } +} + +class A5 { + def f(): Object = { + object B + B + } +} + +trait A6 { + def hui = -6 + trait TT +} + +class A7 extends A6 + +abstract class A8 extends A6 { + def fish: TT +} + +class A9 { + class brick extends annotation.StaticAnnotation +} + +class A10 { + val a9 = new A9() + // there's no reference to brick in the bytecode (only in the pickle), so there's no InnerClass attribute for it. + @a9.brick def f = -7 +} + +class A11 { + @JavaAnnot_1.Ann def f = -8 +} + +object A12 { + object B { + class C + } +} + +class A13 { + def oak: A12.B.C = new A12.B.C +} + +class A14 { + def f = { + val x: Object = { + class K + new K + } + x + } + def g = { + val x: Object = new A6 { } + } +} + +object A15 { + def f = { + class B { // static (does not have an outer pointer) + class C // non-static + } + } +} + +class A16 { + val x: A6 = { + class U extends A6 + new A6 { } + } + + { + class V extends A6 + new A6 { } + } +} + +class A17 { + object B { + class C // not static, has an outer pointer. + } +} diff --git a/test/files/jvm/innerClassAttribute/JavaAnnot_1.java b/test/files/jvm/innerClassAttribute/JavaAnnot_1.java new file mode 100644 index 0000000000..27c4d4e5d3 --- /dev/null +++ b/test/files/jvm/innerClassAttribute/JavaAnnot_1.java @@ -0,0 +1,3 @@ +public class JavaAnnot_1 { + public static @interface Ann {} +} diff --git a/test/files/jvm/innerClassAttribute/Test.scala b/test/files/jvm/innerClassAttribute/Test.scala new file mode 100644 index 0000000000..6cf60ab92d --- /dev/null +++ b/test/files/jvm/innerClassAttribute/Test.scala @@ -0,0 +1,208 @@ +import scala.tools.partest.BytecodeTest +import scala.tools.asm +import asm.tree.{ClassNode, InnerClassNode} +import asm.{Opcodes => Flags} +import scala.collection.JavaConverters._ + +object Test extends BytecodeTest { + def assertSame(a: Any, b: Any) = { + assert(a == b, s"\na: $a\nb: $b") + } + + val publicStatic = Flags.ACC_PUBLIC | Flags.ACC_STATIC + val publicAbstractInterface = Flags.ACC_PUBLIC | Flags.ACC_ABSTRACT | Flags.ACC_INTERFACE + + def innerClassNodes(className: String): List[InnerClassNode] = { + loadClassNode(className).innerClasses.asScala.toList.sortBy(_.name) + } + + final case class EnclosingMethod(name: String, descriptor: String, outerClass: String) + def enclosingMethod(className: String) = { + val n = loadClassNode(className) + EnclosingMethod(n.outerMethod, n.outerMethodDesc, n.outerClass) + } + + def assertMember(node: InnerClassNode, outer: String, inner: String, name: Option[String] = None, flags: Int = Flags.ACC_PUBLIC) = { + assertSame(node.name, name.getOrElse(s"$outer$$$inner")) + assertSame(node.outerName, outer) + assertSame(node.innerName, inner) + assertSame(node.access, flags) + } + + def assertAnonymous(node: InnerClassNode, name: String, flags: Int = Flags.ACC_PUBLIC | Flags.ACC_FINAL) = { + assertSame(node.name, name) + assertSame(node.outerName, null) + assertSame(node.innerName, null) + assertSame(node.access, flags) + } + + def assertLocal(node: InnerClassNode, name: String, inner: String, flags: Int = Flags.ACC_PUBLIC) = { + assertSame(node.name, name) + assertSame(node.outerName, null) + assertSame(node.innerName, inner) + assertSame(node.access, flags) + } + + def assertEnclosingMethod(enclosingMethod: EnclosingMethod, outerClass: String, name: String, descriptor: String) = { + assertSame(enclosingMethod.outerClass, outerClass) + assertSame(enclosingMethod.name, name) + assertSame(enclosingMethod.descriptor, descriptor) + } + + def testA1() = { + val List(b1) = innerClassNodes("A1") + assertMember(b1, "A1", "B") + val List(b2) = innerClassNodes("A1$B") + assertMember(b2, "A1", "B") + } + + def testA2() = { + val List(b1) = innerClassNodes("A2") + assertMember(b1, "A2", "B$") + val List(b2) = innerClassNodes("A2$B$") + assertMember(b2, "A2", "B$") + } + + def testA3() = { + def t(c: String) = { + val List(b1, b2) = innerClassNodes(c) + // the outer class for classes nested inside top-level modules is not the module class, but the mirror class. + // this is a hack for java interop, handled in the backend. see BTypes.scala, comment on "Java Compatibility". + assertMember(b1, "A3", "B1", flags = publicStatic) + assertMember(b2, "A3", "B2$", flags = publicStatic) + } + t("A3$") + // the mirror class has the same inner class attributes as the module + // class (added when the mirror is created in the backend) + t("A3") + } + + def testA4() = { + val List(an1) = innerClassNodes("A4") + assertAnonymous(an1, "A4$$anonfun$f$1") + val List(an2) = innerClassNodes("A4$$anonfun$f$1") + assertAnonymous(an2, "A4$$anonfun$f$1") + assertEnclosingMethod( + enclosingMethod("A4$$anonfun$f$1"), + "A4", "f", "(Lscala/collection/immutable/List;)Lscala/collection/immutable/List;") + } + + def testA5() = { + val List(b1) = innerClassNodes("A5") + assertLocal(b1, "A5$B$2$", "B$2$") + val List(b2) = innerClassNodes("A5$B$2$") + assertLocal(b2, "A5$B$2$", "B$2$") + assertEnclosingMethod( + enclosingMethod("A5$B$2$"), + "A5", "f", "()Ljava/lang/Object;") + } + + def testA6() = { + val List(tt1) = innerClassNodes("A6") + assertMember(tt1, "A6", "TT", flags = publicAbstractInterface) + val List() = innerClassNodes("A6$class") + val List(tt2) = innerClassNodes("A6$TT") + assertMember(tt2, "A6", "TT", flags = publicAbstractInterface) + } + + def testA7() = { + val List() = innerClassNodes("A7") + } + + def testA8() = { + val List(tt) = innerClassNodes("A8") + assertMember(tt, "A6", "TT", flags = publicAbstractInterface) + } + + def testA10() = { + val List() = innerClassNodes("A10") + } + + def testA11() = { + val List(ann) = innerClassNodes("A11") + // in the java class file, the INNERCLASS attribute has more flags (public | static | abstract | interface | annotation) + // the scala compiler has its own interpretation of java annotations ant their flags.. it only emits publicStatic. + assertMember(ann, "JavaAnnot_1", "Ann", flags = publicStatic) + } + + def testA13() = { + val List(b, c) = innerClassNodes("A13") + assertMember(b, "A12", "B$", flags = publicStatic) + assertMember(c, "A12$B$", "C", name = Some("A12$B$C"), flags = publicStatic) + } + + def testA14() = { + val List(anon, k) = innerClassNodes("A14") + + assertLocal(k, "A14$K$1", "K$1") + assertEnclosingMethod( + enclosingMethod("A14$K$1"), + "A14", "f", "()Ljava/lang/Object;") + + assertAnonymous(anon, "A14$$anon$1") + assertEnclosingMethod( + enclosingMethod("A14$$anon$1"), + "A14", "g", "()V") + } + + def testA15() = { + val List(b) = innerClassNodes("A15") + assertLocal(b, "A15$B$3", "B$3", flags = publicStatic) + + val List(_, c) = innerClassNodes("A15$B$3") + // TODO this is a bug in the backend, C should be a member. Instead, its outerClass is null + // assertMember(c, "A15$B$3", "C") + assertLocal(c, "A15$B$3$C", "C") + } + + def testA16() = { + val List(anon1, anon2, u, v) = innerClassNodes("A16") + // TODO there's a bug in the backend: anon$2 has outerClass A16, but anonymous classes should have outerClass null + // assertAnonymous(anon1, "A16$$anon$2") + assertMember(anon1, "A16", null, name = Some("A16$$anon$2"), flags = Flags.ACC_PUBLIC | Flags.ACC_FINAL) + assertAnonymous(anon2, "A16$$anon$3") + // TODO this is a bug in the backend, U should not be a member, its outerClass should be null + // assertLocal(u, "A16$U$1", "U$1") + assertMember(u, "A16", "U$1") + assertLocal(v, "A16$V$1", "V$1") + + assertEnclosingMethod( + enclosingMethod("A16$$anon$2"), + "A16", "", "()V") + assertEnclosingMethod( + enclosingMethod("A16$$anon$3"), + "A16", "", "()V") + // TODO this is a bug, there should be an enclosingMethod attribute in U + // assertEnclosingMethod( + // enclosingMethod("A16$U$1"), + // "A16", "", "()V") + assertEnclosingMethod( + enclosingMethod("A16$V$1"), + "A16", "", "()V") + } + + def testA17() = { + val List(b, c) = innerClassNodes("A17$B$") + assertMember(b, "A17", "B$") + // TODO this is a bug, should not be static. + assertMember(c, "A17$B$", "C", name = Some("A17$B$C"), flags = publicStatic) // (should be) not static, has an outer pointer. + } + + def show(): Unit = { + testA1() + testA2() + testA3() + testA4() + testA5() + testA6() + testA7() + testA8() + testA10() + testA11() + testA13() + testA14() + testA15() + testA16() + testA17() + } +} -- cgit v1.2.3 From 2e3583b3644f3acd67933b54b9b61e6d60e9b6bb Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sat, 2 Aug 2014 14:24:54 -0700 Subject: SI-8512 Infer a type for f"$args" The f-interpolator gets a type param that better be Any to avoid unfortunate widenings. Hey, it worked! Unfortunately, when `Any` is inferred, `-Xlint:infer-any` takes notice. This is probably a greater problem for the f-interpolator than for quasiquotes, which are a more specialized tool. --- src/library/scala/StringContext.scala | 2 +- test/files/neg/t7848-interp-warn.flags | 2 +- test/junit/scala/StringContextTest.scala | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) (limited to 'test/files') diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index 2d2601c6fb..20a328ec8f 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -163,7 +163,7 @@ case class StringContext(parts: String*) { */ // The implementation is hardwired to `scala.tools.reflect.MacroImplementations.macro_StringInterpolation_f` // Using the mechanism implemented in `scala.tools.reflect.FastTrack` - def f(args: Any*): String = macro ??? + def f[A >: Any](args: A*): String = macro ??? } object StringContext { diff --git a/test/files/neg/t7848-interp-warn.flags b/test/files/neg/t7848-interp-warn.flags index 7949c2afa2..b0d7bc25cb 100644 --- a/test/files/neg/t7848-interp-warn.flags +++ b/test/files/neg/t7848-interp-warn.flags @@ -1 +1 @@ --Xlint -Xfatal-warnings +-Xlint:missing-interpolator -Xfatal-warnings diff --git a/test/junit/scala/StringContextTest.scala b/test/junit/scala/StringContextTest.scala index bb0e8c4252..608b82bd96 100644 --- a/test/junit/scala/StringContextTest.scala +++ b/test/junit/scala/StringContextTest.scala @@ -62,4 +62,17 @@ class StringContextTest { //assertEquals("????", s"????") assertEquals("!!!!", s"????") // OK to hijack core interpolator ids } + + @Test def fIf() = { + val res = f"${if (true) 2.5 else 2.5}%.2f" + assertEquals("2.50", res) + } + @Test def fIfNot() = { + val res = f"${if (false) 2.5 else 3.5}%.2f" + assertEquals("3.50", res) + } + @Test def fHeteroArgs() = { + val res = f"${3.14}%.2f rounds to ${3}%d" + assertEquals("3.14 rounds to 3", res) + } } -- cgit v1.2.3 From 606a553e12dbc1bda25939dbda3e7fcaaaa678b9 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Tue, 5 Aug 2014 23:03:41 -0700 Subject: SI-8512 Infer Any for the q Avoid the widening bug for q. This resolution also suffers from the inference of Any, which can trigger a warning and an anxiety attack. But that's still better than doing the wrong thing. Right? --- src/reflect/scala/reflect/api/Quasiquotes.scala | 2 +- test/files/pos/t8013.flags | 2 +- test/junit/scala/reflect/QTest.scala | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/junit/scala/reflect/QTest.scala (limited to 'test/files') diff --git a/src/reflect/scala/reflect/api/Quasiquotes.scala b/src/reflect/scala/reflect/api/Quasiquotes.scala index e905aa4153..eaae05bed5 100644 --- a/src/reflect/scala/reflect/api/Quasiquotes.scala +++ b/src/reflect/scala/reflect/api/Quasiquotes.scala @@ -13,7 +13,7 @@ trait Quasiquotes { self: Universe => protected trait api { // implementation is hardwired to `dispatch` method of `scala.tools.reflect.quasiquotes.Quasiquotes` // using the mechanism implemented in `scala.tools.reflect.FastTrack` - def apply[T](args: T*): Tree = macro ??? + def apply[A >: Any](args: A*): Tree = macro ??? def unapply(scrutinee: Any): Any = macro ??? } object q extends api diff --git a/test/files/pos/t8013.flags b/test/files/pos/t8013.flags index 954eaba352..3955bb6710 100644 --- a/test/files/pos/t8013.flags +++ b/test/files/pos/t8013.flags @@ -1 +1 @@ --Xfatal-warnings -Xlint +-Xfatal-warnings -Xlint:-infer-any,_ diff --git a/test/junit/scala/reflect/QTest.scala b/test/junit/scala/reflect/QTest.scala new file mode 100644 index 0000000000..24c35dc401 --- /dev/null +++ b/test/junit/scala/reflect/QTest.scala @@ -0,0 +1,23 @@ + +package scala.reflect + +import org.junit.Test +import org.junit.Assert._ +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.AssertUtil._ + +@RunWith(classOf[JUnit4]) +class QTest { + + import reflect.runtime._ + import universe._ + @Test def qConstantsNotHomogenized() = { + //Apply(Select(Literal(Constant(1.0)), TermName("$plus")), List(Literal(Constant(1.0)))) + val t = q"${1} + ${1.0}" + val Apply(Select(Literal(Constant(i)), TermName("$plus")), List(Literal(Constant(j)))) = t + assertEquals(1, i) + assertEquals(1.0, j) + } +} -- cgit v1.2.3 From a16a635b647bb1cdb0759ab4ba1e794ec56d8081 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Fri, 15 Aug 2014 16:59:59 +1000 Subject: SI-8793 Fix patmat regression with extractors, existentials In the same vein as SI-8128 / 3e9e2c65a, revert to the 2.10.x style of determining the types of the product elements of an extractor when using `TupleN`. I believe we can discard the special casing for Option/Tuple/Seq altogether with judicious application of `repackExistential` in `unapplyMethodTypes`. That ought to also fix fix SI-8149. But I'll target that work at 2.12.x. --- src/reflect/scala/reflect/internal/Definitions.scala | 10 ++++++++-- test/files/pos/t8793.scala | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/files/pos/t8793.scala (limited to 'test/files') diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index bf560a21e5..02578e2038 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -836,12 +836,18 @@ trait Definitions extends api.StandardDefinitions { def typeOfMemberNamedHead(tp: Type) = typeArgOfBaseTypeOr(tp, SeqClass)(resultOfMatchingMethod(tp, nme.head)()) def typeOfMemberNamedApply(tp: Type) = typeArgOfBaseTypeOr(tp, SeqClass)(resultOfMatchingMethod(tp, nme.apply)(IntTpe)) def typeOfMemberNamedDrop(tp: Type) = typeArgOfBaseTypeOr(tp, SeqClass)(resultOfMatchingMethod(tp, nme.drop)(IntTpe)) - def typesOfSelectors(tp: Type) = getterMemberTypes(tp, productSelectors(tp)) + def typesOfSelectors(tp: Type) = + if (isTupleType(tp)) tp.typeArgs + else getterMemberTypes(tp, productSelectors(tp)) + // SI-8128 Still using the type argument of the base type at Seq/Option if this is an old-style (2.10 compatible) // extractor to limit exposure to regressions like the reported problem with existentials. // TODO fix the existential problem in the general case, see test/pending/pos/t8128.scala private def typeArgOfBaseTypeOr(tp: Type, baseClass: Symbol)(or: => Type): Type = (tp baseType baseClass).typeArgs match { - case x :: Nil => x + case x :: Nil => + val x1 = x + val x2 = repackExistential(x1) + x2 case _ => or } diff --git a/test/files/pos/t8793.scala b/test/files/pos/t8793.scala new file mode 100644 index 0000000000..1276155675 --- /dev/null +++ b/test/files/pos/t8793.scala @@ -0,0 +1,15 @@ +package regr + +trait F[A] + +class G(val a: F[_], val b: F[_]) + +object G { + def unapply(g: G) = Option((g.a, g.b)) +} + +object H { + def unapply(g: G) = g match { + case G(a, _) => Option(a) + } +} -- cgit v1.2.3