diff options
110 files changed, 2514 insertions, 765 deletions
diff --git a/project/Build.scala b/project/Build.scala index ea0e84f0cc..d3be8cd810 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -156,6 +156,8 @@ object ScalaBuild extends Build with Layers { def settingOverrides: Seq[Setting[_]] = publishSettings ++ Seq( crossPaths := false, autoScalaLibrary := false, + // Work around a bug where scala-library (and forkjoin) is put on classpath for analysis. + classpathOptions := ClasspathOptions.manual, publishArtifact in packageDoc := false, publishArtifact in packageSrc := false, target <<= (baseDirectory, name) apply (_ / "target" / _), @@ -168,8 +170,8 @@ object ScalaBuild extends Build with Layers { // Most libs in the compiler use this order to build. compileOrder in Compile := CompileOrder.JavaThenScala, lockFile <<= target(_ / "compile.lock"), - skip in Compile <<= lockFile.map(_ exists), - lock <<= lockFile map { f => IO.touch(f) }, + skip in Compile <<= lockFile map (_.exists), + lock <<= lockFile map (f => IO.touch(f)), unlock <<= lockFile map IO.delete ) diff --git a/src/compiler/scala/reflect/internal/Constants.scala b/src/compiler/scala/reflect/internal/Constants.scala index 135d18d5ad..861bc870a7 100644 --- a/src/compiler/scala/reflect/internal/Constants.scala +++ b/src/compiler/scala/reflect/internal/Constants.scala @@ -224,6 +224,7 @@ trait Constants extends api.Constants { case ClazzTag => "classOf[" + signature(typeValue) + "]" case CharTag => "'" + escapedChar(charValue) + "'" case LongTag => longValue.toString() + "L" + case EnumTag => symbolValue.name.toString() case _ => String.valueOf(value) } } diff --git a/src/compiler/scala/reflect/internal/Definitions.scala b/src/compiler/scala/reflect/internal/Definitions.scala index 0612dcdfd4..0cdef9e79a 100644 --- a/src/compiler/scala/reflect/internal/Definitions.scala +++ b/src/compiler/scala/reflect/internal/Definitions.scala @@ -404,6 +404,7 @@ trait Definitions extends reflect.api.StandardDefinitions { lazy val JavaSerializableClass = requiredClass[java.io.Serializable] modifyInfo fixupAsAnyTrait lazy val ComparableClass = requiredClass[java.lang.Comparable[_]] modifyInfo fixupAsAnyTrait lazy val JavaCloneableClass = requiredClass[java.lang.Cloneable] + lazy val JavaNumberClass = requiredClass[java.lang.Number] lazy val RemoteInterfaceClass = requiredClass[java.rmi.Remote] lazy val RemoteExceptionClass = requiredClass[java.rmi.RemoteException] @@ -609,6 +610,7 @@ trait Definitions extends reflect.api.StandardDefinitions { def isTupleTypeOrSubtype(tp: Type) = isTupleType(tp) def tupleField(n: Int, j: Int) = getMember(TupleClass(n), nme.productAccessorName(j)) + // NOTE: returns true for NoSymbol since it's included in the TupleClass array -- is this intensional? def isTupleSymbol(sym: Symbol) = TupleClass contains unspecializedSymbol(sym) def isProductNClass(sym: Symbol) = ProductClass contains sym diff --git a/src/compiler/scala/tools/nsc/backend/icode/Members.scala b/src/compiler/scala/tools/nsc/backend/icode/Members.scala index 71795a02aa..019e887c4e 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/Members.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/Members.scala @@ -257,11 +257,23 @@ trait Members { var succ = bb do { succ = nextBlock(succ); - bb.removeLastInstruction - succ.toList foreach { i => bb.emit(i, i.pos) } - code.removeBlock(succ) + val lastInstr = bb.lastInstruction + /* Ticket SI-5672 + * Besides removing the control-flow instruction at the end of `bb` (usually a JUMP), we have to pop any values it pushes. + * Examples: + * `SWITCH` consisting of just the default case, or + * `CJUMP(targetBlock, targetBlock, _, _)` ie where success and failure targets coincide (this one consumes two stack values). + */ + val oldTKs = lastInstr.consumedTypes + assert(lastInstr.consumed == oldTKs.size, "Someone forgot to override consumedTypes() in " + lastInstr) + + bb.removeLastInstruction + for(tk <- oldTKs.reverse) { bb.emit(DROP(tk), lastInstr.pos) } + succ.toList foreach { i => bb.emit(i, i.pos) } + code.removeBlock(succ) + exh foreach { e => e.covered = e.covered - succ } + nextBlock -= bb - exh foreach { e => e.covered = e.covered - succ } } while (nextBlock.isDefinedAt(succ)) bb.close } else diff --git a/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala b/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala index 576cc72f82..3179fc5c56 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala @@ -436,6 +436,8 @@ trait Opcodes { self: ICodes => override def consumed = 1 override def produced = 0 + override val consumedTypes = List(INT) + def flatTagsCount: Int = { var acc = 0; var rest = tags; while(rest.nonEmpty) { acc += rest.head.length; rest = rest.tail }; acc } // a one-liner } @@ -470,6 +472,8 @@ trait Opcodes { self: ICodes => override def consumed = 2 override def produced = 0 + + override val consumedTypes = List(kind, kind) } /** This class represents a CZJUMP instruction @@ -489,6 +493,8 @@ trait Opcodes { self: ICodes => override def consumed = 1 override def produced = 0 + + override val consumedTypes = List(kind) } @@ -499,6 +505,8 @@ trait Opcodes { self: ICodes => case class RETURN(kind: TypeKind) extends Instruction { override def consumed = if (kind == UNIT) 0 else 1 override def produced = 0 + + // TODO override val consumedTypes = List(kind) } /** This class represents a THROW instruction diff --git a/src/compiler/scala/tools/nsc/doc/html/page/Index.scala b/src/compiler/scala/tools/nsc/doc/html/page/Index.scala index 346780147e..8ed13e0da2 100644 --- a/src/compiler/scala/tools/nsc/doc/html/page/Index.scala +++ b/src/compiler/scala/tools/nsc/doc/html/page/Index.scala @@ -44,7 +44,7 @@ class Index(universe: doc.Universe, index: doc.Index) extends HtmlPage { </div> { browser } <div id="content" class="ui-layout-center"> - <iframe name="template" src={ relativeLinkTo{List("package.html")} }/> + <iframe id="template" name="template" src={ relativeLinkTo{List("package.html")} }/> </div> </body> diff --git a/src/compiler/scala/tools/nsc/doc/html/page/Template.scala b/src/compiler/scala/tools/nsc/doc/html/page/Template.scala index 220321d225..49cd17c176 100644 --- a/src/compiler/scala/tools/nsc/doc/html/page/Template.scala +++ b/src/compiler/scala/tools/nsc/doc/html/page/Template.scala @@ -87,7 +87,7 @@ class Template(universe: doc.Universe, tpl: DocTemplateEntity) extends HtmlPage { memberToCommentHtml(tpl, true) } <div id="mbrsel"> - <div id='textfilter'><span class='pre'/><span class='input'><input type='text' accesskey='/'/></span><span class='post'/></div> + <div id='textfilter'><span class='pre'/><span class='input'><input id='mbrsel-input' type='text' accesskey='/'/></span><span class='post'/></div> { if (tpl.linearizationTemplates.isEmpty && tpl.conversions.isEmpty) NodeSeq.Empty else <div id="order"> <span class="filtertype">Ordering</span> diff --git a/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.css b/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.css index c6136c508e..2a8f9b570a 100644 --- a/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.css +++ b/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.css @@ -1,23 +1,32 @@ * { - color: inherit; - font-size: 10pt; - text-decoration: none; + color: inherit; + font-size: 10pt; + text-decoration: none; font-family: Arial, sans-serif; - border-width: 0px; - padding: 0px; - margin: 0px; + border-width: 0px; + padding: 0px; + margin: 0px; } a { - cursor: pointer; + cursor: pointer; } a:hover { - text-decoration: underline; + text-decoration: underline; } h1 { - display: none; + display: none; +} + +.selected { + -moz-box-shadow: inset 0px 5px 10px rgba(58, 88, 97, .36); + -webkit-box-shadow: inset 0px 5px 10px rgba(58, 88, 97, .36); + border-top: solid 1px rgba(119, 138, 153, 0.8); + border-bottom: solid 1px rgba(151, 173, 191, 0.4); + background-color: #ced2d9; + margin: -1px 0px; } /*.letters { @@ -33,81 +42,81 @@ h1 { } #browser { - top: 0px; - left: 0px; - bottom: 0px; - width: 100%; - display: block; - position: fixed; + top: 0px; + left: 0px; + bottom: 0px; + width: 100%; + display: block; + position: fixed; } #filter { - position: absolute; - display: block; -/* padding: 5px;*/ - right: 0; - left: 0; - top: 0; - background-image:url('filterbg.gif'); - background-repeat:repeat-x; - background-color: #ededee; /* light gray */ - /*background-color: #DADADA;*/ - border:1px solid #bbbbbb; - border-top:0; - border-left:0; - border-right:0; + position: absolute; + display: block; +/* padding: 5px;*/ + right: 0; + left: 0; + top: 0; + background-image:url('filterbg.gif'); + background-repeat:repeat-x; + background-color: #ededee; /* light gray */ + /*background-color: #DADADA;*/ + border:1px solid #bbbbbb; + border-top:0; + border-left:0; + border-right:0; } #textfilter { - position: relative; - display: block; - height: 20px; - margin-top: 5px; - margin-bottom: 5px; + position: relative; + display: block; + height: 20px; + margin-top: 5px; + margin-bottom: 5px; } #textfilter > .pre { - display: block; - position: absolute; - top: 0; - left: 0; - height: 23px; - width: 21px; - background: url("filter_box_left.png"); + display: block; + position: absolute; + top: 0; + left: 0; + height: 23px; + width: 21px; + background: url("filter_box_left.png"); } #textfilter > .input { - display: block; - position: absolute; - top: 0; - right: 20px; - left: 20px; + display: block; + position: absolute; + top: 0; + right: 20px; + left: 20px; } #textfilter > .input > input { - height: 20px; - padding: 1px; - font-weight: bold; - color: #000000; - background: #ffffff url("filterboxbarbg.png") repeat-x bottom left; - width: 100%; + height: 20px; + padding: 1px; + font-weight: bold; + color: #000000; + background: #ffffff url("filterboxbarbg.png") repeat-x bottom left; + width: 100%; } #textfilter > .post { - display: block; - position: absolute; - top: 0; - right: 0; - height: 23px; - width: 21px; - background: url("filter_box_right.png"); + display: block; + position: absolute; + top: 0; + right: 0; + height: 23px; + width: 21px; + background: url("filter_box_right.png"); } /*#textfilter { - position: relative; - display: block; + position: relative; + display: block; height: 20px; - margin-bottom: 5px; + margin-bottom: 5px; } #textfilter > .pre { @@ -121,7 +130,7 @@ h1 { } #textfilter > .input { - display: block; + display: block; position: absolute; top: 0; right: 20px; @@ -129,11 +138,11 @@ h1 { } #textfilter > .input > input { - height: 16px; - padding: 2px; - font-weight: bold; - color: darkblue; - background-color: white; + height: 16px; + padding: 2px; + font-weight: bold; + color: darkblue; + background-color: white; width: 100%; } @@ -148,22 +157,22 @@ h1 { }*/ #focusfilter { - position: relative; - text-align: center; - display: block; - padding: 5px; - background-color: #fffebd; /* light yellow*/ - text-shadow: #ffffff 0 1px 0; + position: relative; + text-align: center; + display: block; + padding: 5px; + background-color: #fffebd; /* light yellow*/ + text-shadow: #ffffff 0 1px 0; } #focusfilter .focuscoll { - font-weight: bold; - text-shadow: #ffffff 0 1px 0; + font-weight: bold; + text-shadow: #ffffff 0 1px 0; } #focusfilter img { - bottom: -2px; - position: relative; + bottom: -2px; + position: relative; } #kindfilter { @@ -182,10 +191,9 @@ h1 { } #kindfilter > a:hover { - color: #4C4C4C; - text-decoration: none; - text-shadow: #ffffff 0 1px 0; - + color: #4C4C4C; + text-decoration: none; + text-shadow: #ffffff 0 1px 0; } #letters { @@ -208,117 +216,117 @@ h1 { } #tpl { - display: block; - position: fixed; - overflow: auto; - right: 0; - left: 0; - bottom: 0; - top: 5px; - position: absolute; - display: block; + display: block; + position: fixed; + overflow: auto; + right: 0; + left: 0; + bottom: 0; + top: 5px; + position: absolute; + display: block; } #tpl .packhide { - display: block; - float: right; - font-weight: normal; - color: white; + display: block; + float: right; + font-weight: normal; + color: white; } #tpl .packfocus { - display: block; - float: right; - font-weight: normal; - color: white; + display: block; + float: right; + font-weight: normal; + color: white; } #tpl .packages > ol { - background-color: #dadfe6; - /*margin-bottom: 5px;*/ + background-color: #dadfe6; + /*margin-bottom: 5px;*/ } /*#tpl .packages > ol > li { - margin-bottom: 1px; + margin-bottom: 1px; }*/ #tpl .packages > li > a { - padding: 0px 5px; + padding: 0px 5px; } #tpl .packages > li > a.tplshow { - display: block; - color: white; - font-weight: bold; - display: block; - text-shadow: #000000 0 1px 0; + display: block; + color: white; + font-weight: bold; + display: block; + text-shadow: #000000 0 1px 0; } #tpl ol > li.pack { - padding: 3px 5px; - background: url("packagesbg.gif"); - background-repeat:repeat-x; - min-height: 14px; - background-color: #6e808e; + padding: 3px 5px; + background: url("packagesbg.gif"); + background-repeat:repeat-x; + min-height: 14px; + background-color: #6e808e; } #tpl ol > li { - display: block; + display: block; } #tpl .templates > li { - padding-left: 5px; - min-height: 18px; + padding-left: 5px; + min-height: 18px; } #tpl ol > li .icon { - padding-right: 5px; - bottom: -2px; - position: relative; + padding-right: 5px; + bottom: -2px; + position: relative; } #tpl .templates div.placeholder { - padding-right: 5px; - width: 13px; - display: inline-block; + padding-right: 5px; + width: 13px; + display: inline-block; } #tpl .templates span.tplLink { - padding-left: 5px; + padding-left: 5px; } #content { - border-left-width: 1px; - border-left-color: black; - border-left-style: white; - right: 0px; - left: 0px; - bottom: 0px; - top: 0px; - position: fixed; - margin-left: 300px; - display: block; + border-left-width: 1px; + border-left-color: black; + border-left-style: white; + right: 0px; + left: 0px; + bottom: 0px; + top: 0px; + position: fixed; + margin-left: 300px; + display: block; } #content > iframe { - display: block; - height: 100%; - width: 100%; + display: block; + height: 100%; + width: 100%; } .ui-layout-pane { - background: #FFF; - overflow: auto; + background: #FFF; + overflow: auto; } .ui-layout-resizer { - background-image:url('filterbg.gif'); - background-repeat:repeat-x; - background-color: #ededee; /* light gray */ - border:1px solid #bbbbbb; - border-top:0; - border-bottom:0; - border-left: 0; + background-image:url('filterbg.gif'); + background-repeat:repeat-x; + background-color: #ededee; /* light gray */ + border:1px solid #bbbbbb; + border-top:0; + border-bottom:0; + border-left: 0; } .ui-layout-toggler { diff --git a/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js b/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js index b767722b8c..eb7f752440 100644 --- a/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js +++ b/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js @@ -15,10 +15,10 @@ var lastHash = ""; $(document).ready(function() { $('body').layout({ west__size: '20%' }); - $('#browser').layout({ - center__paneSelector: ".ui-west-center" + $('#browser').layout({ + center__paneSelector: ".ui-west-center" //,center__initClosed:true - ,north__paneSelector: ".ui-west-north" + ,north__paneSelector: ".ui-west-north" }); $('iframe').bind("load", function(){ var subtitle = $(this).contents().find('title').text(); @@ -260,18 +260,95 @@ function prepareEntityList() { .prepend("<a class='packfocus'>focus</a>"); } +/* Handles all key presses while scrolling around with keyboard shortcuts in left panel */ +function keyboardScrolldownLeftPane() { + scheduler.add("init", function() { + $("#textfilter input").blur(); + var $items = $("#tpl li"); + $items.first().addClass('selected'); + + $(window).bind("keydown", function(e) { + var $old = $items.filter('.selected'), + $new; + + switch ( e.keyCode ) { + + case 9: // tab + $old.removeClass('selected'); + break; + + case 13: // enter + $old.removeClass('selected'); + var $url = $old.children().filter('a:last').attr('href'); + $("#template").attr("src",$url); + break; + + case 27: // escape + $old.removeClass('selected'); + $(window).unbind(e); + $("#textfilter input").focus(); + + break; + + case 38: // up + $new = $old.prev(); + + if (!$new.length) { + $new = $old.parent().prev(); + } + + if ($new.is('ol') && $new.children(':last').is('ol')) { + $new = $new.children().children(':last'); + } else if ($new.is('ol')) { + $new = $new.children(':last'); + } + + break; + + case 40: // down + $new = $old.next(); + if (!$new.length) { + $new = $old.parent().parent().next(); + } + if ($new.is('ol')) { + $new = $new.children(':first'); + } + break; + } + + if ($new.is('li')) { + $old.removeClass('selected'); + $new.addClass('selected'); + } else if (e.keyCode == 38) { + $(window).unbind(e); + $("#textfilter input").focus(); + } + }); + }); +} + /* Configures the text filter */ function configureTextFilter() { scheduler.add("init", function() { - $("#filter").append("<div id='textfilter'><span class='pre'/><span class='input'><input type='text' accesskey='/'/></span><span class='post'/></div>"); + $("#filter").append("<div id='textfilter'><span class='pre'/><span class='input'><input id='index-input' type='text' accesskey='/'/></span><span class='post'/></div>"); printAlphabet(); var input = $("#textfilter input"); resizeFilterBlock(); - input.bind("keyup", function(event) { + input.bind("keydown", function(event) { if (event.keyCode == 27) { // escape input.attr("value", ""); } - textFilter(); + if (event.keyCode == 9) { // tab + $("#template").contents().find("#mbrsel-input").focus(); + input.attr("value", ""); + return false; + } + if (event.keyCode == 40) { // down arrow + $(window).unbind("keydown"); + keyboardScrolldownLeftPane(); + return false; + } + textFilter(); }); input.focus(function(event) { input.select(); }); }); diff --git a/src/compiler/scala/tools/nsc/doc/html/resource/lib/template.js b/src/compiler/scala/tools/nsc/doc/html/resource/lib/template.js index fd5a981cb0..33fbd83bee 100644 --- a/src/compiler/scala/tools/nsc/doc/html/resource/lib/template.js +++ b/src/compiler/scala/tools/nsc/doc/html/resource/lib/template.js @@ -25,9 +25,37 @@ $(document).ready(function(){ // Member filter box var input = $("#textfilter input"); input.bind("keyup", function(event) { - if (event.keyCode == 27) - input.val(""); // escape key - filter(true); + + switch ( event.keyCode ) { + + case 27: // escape key + input.val(""); + filter(true); + break; + + case 38: // up + input.val(""); + filter(false); + window.scrollTo(0, $("body").offset().top); + input.focus(); + break; + + case 33: //page up + input.val(""); + filter(false); + break; + + case 34: //page down + input.val(""); + filter(false); + break; + + default: + window.scrollTo(0, $("#mbrsel").offset().top); + filter(true); + break; + + } }); input.focus(function(event) { input.select(); @@ -37,13 +65,13 @@ $(document).ready(function(){ filter(); }); $(document).keydown(function(event) { - if(!event.altKey && !event.ctrlKey && - (event.keyCode == 27 || (event.keyCode >= 48 && event.keyCode <= 90)) && - document.activeElement != $("#textfilter input")[0]) { - $("#textfilter input").focus(); + + if (event.keyCode == 9) { // tab + $("#index-input", window.parent.document).focus(); + input.attr("value", ""); + return false; } }); - $("#textfilter input").focus(); $("#linearization li").click(function(){ if ($(this).hasClass("in")) { @@ -251,7 +279,8 @@ function initInherit() { }); }; -function filter(scrollToMember) { +/* filter used to take boolean scrollToMember */ +function filter() { var query = $.trim($("#textfilter input").val()).toLowerCase(); query = query.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&").replace(/\s+/g, "|"); var queryRegExp = new RegExp(query, "i"); @@ -327,10 +356,6 @@ function filter(scrollToMember) { members.hide(); }; - if (scrollToMember) { - window.scrollTo(0, $("#mbrsel").offset().top); - } - return false; }; diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala index 7c61ec032e..7373a610d7 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala @@ -615,12 +615,11 @@ abstract class ClassfileParser { // sealed java enums (experimental) if (isEnum && opt.experimental) { - // need to give singleton type - sym setInfo info.narrow - if (!sym.superClass.isSealed) - sym.superClass setFlag SEALED + val enumClass = sym.owner.linkedClassOfClass + if (!enumClass.isSealed) + enumClass setFlag (SEALED | ABSTRACT) - sym.superClass addChild sym + enumClass addChild sym } } } diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala index 7f220992a3..8af12f3f10 100644 --- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala +++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala @@ -77,9 +77,17 @@ abstract class UnCurry extends InfoTransform private var inConstructorFlag = 0L private val byNameArgs = mutable.HashSet[Tree]() private val noApply = mutable.HashSet[Tree]() - private val newMembers = mutable.ArrayBuffer[Tree]() + private val newMembers = mutable.Map[Symbol, mutable.Buffer[Tree]]() private val repeatedParams = mutable.Map[Symbol, List[ValDef]]() + /** Add a new synthetic member for `currentOwner` */ + private def addNewMember(t: Tree): Unit = + newMembers.getOrElseUpdate(currentOwner, mutable.Buffer()) += t + + /** Process synthetic members for `owner`. They are removed form the `newMembers` as a side-effect. */ + @inline private def useNewMembers[T](owner: Symbol)(f: List[Tree] => T): T = + f(newMembers.remove(owner).getOrElse(Nil).toList) + @inline private def withInPattern[T](value: Boolean)(body: => T): T = { inPattern = value try body @@ -568,7 +576,12 @@ abstract class UnCurry extends InfoTransform if ((sym ne null) && (sym.elisionLevel.exists (_ < settings.elidebelow.value || settings.noassertions.value))) replaceElidableTree(tree) else translateSynchronized(tree) match { - case dd @ DefDef(mods, name, tparams, vparamss, tpt, rhs) => + case dd @ DefDef(mods, name, tparams, _, tpt, rhs) => + // Remove default argument trees from parameter ValDefs, SI-4812 + val vparamssNoRhs = dd.vparamss mapConserve (_ mapConserve {p => + treeCopy.ValDef(p, p.mods, p.name, p.tpt, EmptyTree) + }) + if (dd.symbol hasAnnotation VarargsClass) saveRepeatedParams(dd) withNeedLift(false) { @@ -586,10 +599,10 @@ abstract class UnCurry extends InfoTransform } treeCopy.DefDef( dd, mods, name, transformTypeDefs(tparams), - transformValDefss(vparamss), transform(tpt), rhs1) + transformValDefss(vparamssNoRhs), transform(tpt), rhs1) } } else { - super.transform(dd) + super.transform(treeCopy.DefDef(dd, mods, name, tparams, vparamssNoRhs, tpt, rhs)) } } case ValDef(_, _, _, rhs) => @@ -675,9 +688,8 @@ abstract class UnCurry extends InfoTransform tree match { /* Some uncurry post transformations add members to templates. - * When inside a template, the following sequence is available: - * - newMembers - * Any entry in this sequence will be added into the template + * + * Members registered by `addMembers` for the current template are added * once the template transformation has finished. * * In particular, this case will add: @@ -685,8 +697,10 @@ abstract class UnCurry extends InfoTransform */ case Template(_, _, _) => localTyper = typer.atOwner(tree, currentClass) - try deriveTemplate(tree)(transformTrees(newMembers.toList) ::: _) - finally newMembers.clear() + useNewMembers(currentClass) { + newMembers => + deriveTemplate(tree)(transformTrees(newMembers) ::: _) + } case dd @ DefDef(_, _, _, vparamss0, _, rhs0) => val flatdd = copyDefDef(dd)( @@ -758,7 +772,7 @@ abstract class UnCurry extends InfoTransform /* Called during post transform, after the method argument lists have been flattened. * It looks for the method in the `repeatedParams` map, and generates a Java-style - * varargs forwarder. It then adds the forwarder to the `newMembers` sequence. + * varargs forwarder. */ private def addJavaVarargsForwarders(dd: DefDef, flatdd: DefDef): DefDef = { if (!dd.symbol.hasAnnotation(VarargsClass) || !repeatedParams.contains(dd.symbol)) @@ -835,8 +849,7 @@ abstract class UnCurry extends InfoTransform case None => // enter symbol into scope currentClass.info.decls enter forwsym - // add the method to `newMembers` - newMembers += forwtree + addNewMember(forwtree) } flatdd diff --git a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala index df0258832c..affa9cd63b 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala @@ -690,34 +690,44 @@ trait ContextErrors { setError(tree) } - def NoBestMethodAlternativeError(tree: Tree, argtpes: List[Type], pt: Type) = { + // side-effect on the tree, break the overloaded type cycle in infer + @inline + private def setErrorOnLastTry(lastTry: Boolean, tree: Tree) = if (lastTry) setError(tree) + + def NoBestMethodAlternativeError(tree: Tree, argtpes: List[Type], pt: Type, lastTry: Boolean) = { issueNormalTypeError(tree, applyErrorMsg(tree, " cannot be applied to ", argtpes, pt)) // since inferMethodAlternative modifies the state of the tree // we have to set the type of tree to ErrorType only in the very last - // fallback action that is done in the inference (tracking it manually is error prone). + // fallback action that is done in the inference. // This avoids entering infinite loop in doTypeApply. - if (implicitly[Context].reportErrors) setError(tree) + setErrorOnLastTry(lastTry, tree) } def AmbiguousMethodAlternativeError(tree: Tree, pre: Type, best: Symbol, - firstCompeting: Symbol, argtpes: List[Type], pt: Type) = { - val msg0 = - "argument types " + argtpes.mkString("(", ",", ")") + - (if (pt == WildcardType) "" else " and expected result type " + pt) - val (pos, msg) = ambiguousErrorMsgPos(tree.pos, pre, best, firstCompeting, msg0) - // discover last attempt in a similar way as for NoBestMethodAlternativeError - if (implicitly[Context].ambiguousErrors) setError(tree) - issueAmbiguousTypeError(pre, best, firstCompeting, AmbiguousTypeError(tree, pos, msg)) + firstCompeting: Symbol, argtpes: List[Type], pt: Type, lastTry: Boolean) = { + + if (!(argtpes exists (_.isErroneous)) && !pt.isErroneous) { + val msg0 = + "argument types " + argtpes.mkString("(", ",", ")") + + (if (pt == WildcardType) "" else " and expected result type " + pt) + val (pos, msg) = ambiguousErrorMsgPos(tree.pos, pre, best, firstCompeting, msg0) + issueAmbiguousTypeError(pre, best, firstCompeting, AmbiguousTypeError(tree, pos, msg)) + setErrorOnLastTry(lastTry, tree) + } else setError(tree) // do not even try further attempts because they should all fail + // even if this is not the last attempt (because of the SO's possibility on the horizon) + } - def NoBestExprAlternativeError(tree: Tree, pt: Type) = + def NoBestExprAlternativeError(tree: Tree, pt: Type, lastTry: Boolean) = { issueNormalTypeError(tree, withAddendum(tree.pos)(typeErrorMsg(tree.symbol.tpe, pt, isPossiblyMissingArgs(tree.symbol.tpe, pt)))) + setErrorOnLastTry(lastTry, tree) + } - def AmbiguousExprAlternativeError(tree: Tree, pre: Type, best: Symbol, firstCompeting: Symbol, pt: Type) = { + def AmbiguousExprAlternativeError(tree: Tree, pre: Type, best: Symbol, firstCompeting: Symbol, pt: Type, lastTry: Boolean) = { val (pos, msg) = ambiguousErrorMsgPos(tree.pos, pre, best, firstCompeting, "expected type " + pt) - setError(tree) issueAmbiguousTypeError(pre, best, firstCompeting, AmbiguousTypeError(tree, pos, msg)) + setErrorOnLastTry(lastTry, tree) } // checkBounds @@ -874,8 +884,12 @@ trait ContextErrors { val s1 = if (prevSym.isModule) "case class companion " else "" val s2 = if (prevSym.isSynthetic) "(compiler-generated) " + s1 else "" val s3 = if (prevSym.isCase) "case class " + prevSym.name else "" + prevSym + val where = if (currentSym.owner.isPackageClass != prevSym.owner.isPackageClass) { + val inOrOut = if (prevSym.owner.isPackageClass) "outside of" else "in" + " %s package object %s".format(inOrOut, ""+prevSym.effectiveOwner.name) + } else "" - issueSymbolTypeError(currentSym, prevSym.name + " is already defined as " + s2 + s3) + issueSymbolTypeError(currentSym, prevSym.name + " is already defined as " + s2 + s3 + where) } def MaxParametersCaseClassError(tree: Tree) = @@ -1034,6 +1048,12 @@ trait ContextErrors { setError(arg) } else arg } + + def WarnAfterNonSilentRecursiveInference(param: Symbol, arg: Tree)(implicit context: Context) = { + val note = "type-checking the invocation of "+ param.owner +" checks if the named argument expression '"+ param.name + " = ...' is a valid assignment\n"+ + "in the current scope. The resulting type inference error (see above) can be fixed by providing an explicit type in the local definition for "+ param.name +"." + context.warning(arg.pos, note) + } def UnknownParameterNameNamesDefaultError(arg: Tree, name: Name)(implicit context: Context) = { issueNormalTypeError(arg, "unknown parameter name: " + name) diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index 0924789948..f4f081252f 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -438,8 +438,8 @@ trait Contexts { self: Analyzer => def enclosingContextChain: List[Context] = this :: outer.enclosingContextChain - override def toString = "Context(%s@%s unit=%s scope=%s errors=%b)".format( - owner.fullName, tree.shortClass, unit, scope.##, hasErrors + override def toString = "Context(%s@%s unit=%s scope=%s errors=%b, reportErrors=%b, throwErrors=%b)".format( + owner.fullName, tree.shortClass, unit, scope.##, hasErrors, reportErrors, throwErrors ) /** Is `sub` a subclass of `base` or a companion object of such a subclass? */ @@ -598,16 +598,16 @@ trait Contexts { self: Analyzer => * it is accessible, and if it is imported there is not already a local symbol * with the same names. Local symbols override imported ones. This fixes #2866. */ - private def isQualifyingImplicit(sym: Symbol, pre: Type, imported: Boolean) = + private def isQualifyingImplicit(name: Name, sym: Symbol, pre: Type, imported: Boolean) = sym.isImplicit && isAccessible(sym, pre) && !(imported && { - val e = scope.lookupEntry(sym.name) + val e = scope.lookupEntry(name) (e ne null) && (e.owner == scope) }) private def collectImplicits(syms: List[Symbol], pre: Type, imported: Boolean = false): List[ImplicitInfo] = - for (sym <- syms if isQualifyingImplicit(sym, pre, imported)) yield + for (sym <- syms if isQualifyingImplicit(sym.name, sym, pre, imported)) yield new ImplicitInfo(sym.name, pre, sym) private def collectImplicitImports(imp: ImportInfo): List[ImplicitInfo] = { @@ -621,7 +621,7 @@ trait Contexts { self: Analyzer => var impls = collect(sels1) filter (info => info.name != from) if (to != nme.WILDCARD) { for (sym <- imp.importedSymbol(to).alternatives) - if (isQualifyingImplicit(sym, pre, imported = true)) + if (isQualifyingImplicit(to, sym, pre, imported = true)) impls = new ImplicitInfo(to, pre, sym) :: impls } impls diff --git a/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala b/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala index 2574a1d241..b7a6ea677e 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala @@ -143,8 +143,8 @@ abstract class Duplicators extends Analyzer { else sym - private def invalidate(tree: Tree) { - debuglog("attempting to invalidate " + tree.symbol + ", owner - " + (if (tree.symbol ne null) tree.symbol.owner else "<NULL>")) + private def invalidate(tree: Tree, owner: Symbol = NoSymbol) { + debuglog("attempting to invalidate " + tree.symbol) if (tree.isDef && tree.symbol != NoSymbol) { debuglog("invalid " + tree.symbol) invalidSyms(tree.symbol) = tree @@ -158,18 +158,20 @@ abstract class Duplicators extends Analyzer { newsym.setInfo(fixType(ldef.symbol.info)) ldef.symbol = newsym debuglog("newsym: " + newsym + " info: " + newsym.info) - + case vdef @ ValDef(mods, name, _, rhs) if mods.hasFlag(Flags.LAZY) => debuglog("ValDef " + name + " sym.info: " + vdef.symbol.info) invalidSyms(vdef.symbol) = vdef - val newsym = vdef.symbol.cloneSymbol(context.owner) + val newowner = if (owner != NoSymbol) owner else context.owner + val newsym = vdef.symbol.cloneSymbol(newowner) newsym.setInfo(fixType(vdef.symbol.info)) vdef.symbol = newsym - debuglog("newsym: " + newsym + " info: " + newsym.info) - + debuglog("newsym: " + newsym + " info: " + newsym.info + ", owner: " + newsym.owner + ", " + newsym.owner.isClass) + if (newsym.owner.isClass) newsym.owner.info.decls enter newsym + case DefDef(_, name, tparams, vparamss, _, rhs) => // invalidate parameters - invalidate(tparams ::: vparamss.flatten) + invalidateAll(tparams ::: vparamss.flatten) tree.symbol = NoSymbol case _ => @@ -178,14 +180,14 @@ abstract class Duplicators extends Analyzer { } } - private def invalidate(stats: List[Tree]) { - stats foreach invalidate + private def invalidateAll(stats: List[Tree], owner: Symbol = NoSymbol) { + stats.foreach(invalidate(_, owner)) } def retypedMethod(ddef: DefDef, oldThis: Symbol, newThis: Symbol): Tree = { oldClassOwner = oldThis newClassOwner = newThis - invalidate(ddef.tparams) + invalidateAll(ddef.tparams) mforeach(ddef.vparamss) { vdef => invalidate(vdef) vdef.tpe = null @@ -239,15 +241,15 @@ abstract class Duplicators extends Analyzer { case Block(stats, res) => debuglog("invalidating block") - invalidate(stats) + invalidateAll(stats) invalidate(res) tree.tpe = null super.typed(tree, mode, pt) case ClassDef(_, _, _, tmpl @ Template(parents, _, stats)) => - // log("invalidating classdef " + tree.tpe) + // log("invalidating classdef " + tree) tmpl.symbol = tree.symbol.newLocalDummy(tree.pos) - invalidate(stats) + invalidateAll(stats, tree.symbol) tree.tpe = null super.typed(tree, mode, pt) diff --git a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala index 8f025336bb..a671b8d6b5 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala @@ -554,7 +554,11 @@ trait Implicits { val itree = atPos(pos.focus) { if (info.pre == NoPrefix) Ident(info.name) - else Select(gen.mkAttributedQualifier(info.pre), info.name) + else { + // SI-2405 Not info.name, which might be an aliased import + val implicitMemberName = info.sym.name + Select(gen.mkAttributedQualifier(info.pre), implicitMemberName) + } } printTyping("typedImplicit1 %s, pt=%s, from implicit %s:%s".format( typeDebug.ptTree(itree), wildPt, info.name, info.tpe) @@ -929,7 +933,7 @@ trait Implicits { } case None => if (pre.isStable) { - val companion = sym.companionModule + val companion = companionSymbolOf(sym, context) companion.moduleClass match { case mc: ModuleClassSymbol => val infos = diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala index 5534cd179c..839cdee301 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala @@ -617,7 +617,8 @@ trait Infer { } else if (argPos.contains(pos)) { // parameter specified twice namesOK = false } else { - positionalAllowed = false + if (index != pos) + positionalAllowed = false argPos(index) = pos } index += 1 @@ -1448,10 +1449,10 @@ trait Infer { * If no alternative matches `pt`, take the parameterless one anyway. */ def inferExprAlternative(tree: Tree, pt: Type) = tree.tpe match { - case OverloadedType(pre, alts) => tryTwice { - val alts0 = alts filter (alt => isWeaklyCompatible(pre.memberType(alt), pt)) - val secondTry = alts0.isEmpty - val alts1 = if (secondTry) alts else alts0 + case OverloadedType(pre, alts) => tryTwice { isSecondTry => + val alts0 = alts filter (alt => isWeaklyCompatible(pre.memberType(alt), pt)) + val noAlternatives = alts0.isEmpty + val alts1 = if (noAlternatives) alts else alts0 //println("trying "+alts1+(alts1 map (_.tpe))+(alts1 map (_.locationString))+" for "+pt) def improves(sym1: Symbol, sym2: Symbol): Boolean = @@ -1479,10 +1480,10 @@ trait Infer { } } // todo: missing test case - NoBestExprAlternativeError(tree, pt) + NoBestExprAlternativeError(tree, pt, isSecondTry) } else if (!competing.isEmpty) { - if (secondTry) { NoBestExprAlternativeError(tree, pt); setError(tree) } - else if (!pt.isErroneous) AmbiguousExprAlternativeError(tree, pre, best, competing.head, pt) + if (noAlternatives) NoBestExprAlternativeError(tree, pt, isSecondTry) + else if (!pt.isErroneous) AmbiguousExprAlternativeError(tree, pre, best, competing.head, pt, isSecondTry) } else { // val applicable = alts1 filter (alt => // global.typer.infer.isWeaklyCompatible(pre.memberType(alt), pt)) @@ -1561,10 +1562,10 @@ trait Infer { * assignment expression. */ def inferMethodAlternative(tree: Tree, undetparams: List[Symbol], - argtpes: List[Type], pt0: Type, varArgsOnly: Boolean = false): Unit = tree.tpe match { + argtpes: List[Type], pt0: Type, varArgsOnly: Boolean = false, lastInferAttempt: Boolean = true): Unit = tree.tpe match { case OverloadedType(pre, alts) => val pt = if (pt0.typeSymbol == UnitClass) WildcardType else pt0 - tryTwice { + tryTwice { isSecondTry => debuglog("infer method alt "+ tree.symbol +" with alternatives "+ (alts map pre.memberType) +", argtpes = "+ argtpes +", pt = "+ pt) @@ -1586,13 +1587,10 @@ trait Infer { if (improves(alt, best)) alt else best) val competing = applicable.dropWhile(alt => best == alt || improves(best, alt)) if (best == NoSymbol) { - if (pt == WildcardType) NoBestMethodAlternativeError(tree, argtpes, pt) - else inferMethodAlternative(tree, undetparams, argtpes, WildcardType) + if (pt == WildcardType) NoBestMethodAlternativeError(tree, argtpes, pt, isSecondTry && lastInferAttempt) + else inferMethodAlternative(tree, undetparams, argtpes, WildcardType, lastInferAttempt = isSecondTry) } else if (!competing.isEmpty) { - if (!(argtpes exists (_.isErroneous)) && !pt.isErroneous) - AmbiguousMethodAlternativeError(tree, pre, best, competing.head, argtpes, pt) - else setError(tree) - () + AmbiguousMethodAlternativeError(tree, pre, best, competing.head, argtpes, pt, isSecondTry && lastInferAttempt) } else { // checkNotShadowed(tree.pos, pre, best, applicable) tree.setSymbol(best).setType(pre.memberType(best)) @@ -1606,29 +1604,28 @@ trait Infer { * * @param infer ... */ - def tryTwice(infer: => Unit): Unit = { + def tryTwice(infer: Boolean => Unit): Unit = { if (context.implicitsEnabled) { val saved = context.state var fallback = false context.setBufferErrors() - val res = try { - context.withImplicitsDisabled(infer) + try { + context.withImplicitsDisabled(infer(false)) if (context.hasErrors) { fallback = true context.restoreState(saved) context.flushBuffer() - infer + infer(true) } } catch { case ex: CyclicReference => throw ex case ex: TypeError => // recoverable cyclic references context.restoreState(saved) - if (!fallback) infer else () + if (!fallback) infer(true) else () } context.restoreState(saved) - res } - else infer + else infer(true) } /** Assign <code>tree</code> the type of all polymorphic alternatives diff --git a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala index 9e6b9617a5..d1f319311e 100644 --- a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala +++ b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala @@ -478,7 +478,18 @@ trait NamesDefaults { self: Analyzer => // instead of arg, but can't do that because eventually setType(ErrorType) // is called, and EmptyTree can only be typed NoType. Thus we need to // disable conforms as a view... - try typer.silent(_.typed(arg, subst(paramtpe))) match { + val errsBefore = reporter.ERROR.count + try typer.silent { tpr => + val res = tpr.typed(arg, subst(paramtpe)) + // better warning for SI-5044: if `silent` was not actually silent give a hint to the user + // [H]: the reason why `silent` is not silent is because the cyclic reference exception is + // thrown in a context completely different from `context` here. The exception happens while + // completing the type, and TypeCompleter is created/run with a non-silent Namer `context` + // and there is at the moment no way to connect the two unless we go through some global state. + if (errsBefore < reporter.ERROR.count) + WarnAfterNonSilentRecursiveInference(param, arg)(context) + res + } match { case SilentResultValue(t) => !t.isErroneous // #4041 case _ => false } @@ -487,7 +498,7 @@ trait NamesDefaults { self: Analyzer => // CyclicReferences. Fix for #3685 case cr @ CyclicReference(sym, _) => (sym.name == param.name) && sym.accessedOrSelf.isVariable && { - NameClashError(sym, arg)(typer.context) + NameClashError(sym, arg)(context) true } } diff --git a/src/compiler/scala/tools/nsc/typechecker/PatternMatching.scala b/src/compiler/scala/tools/nsc/typechecker/PatternMatching.scala index 61e02edaff..80d40011ad 100644 --- a/src/compiler/scala/tools/nsc/typechecker/PatternMatching.scala +++ b/src/compiler/scala/tools/nsc/typechecker/PatternMatching.scala @@ -12,6 +12,8 @@ import Flags.{MUTABLE, METHOD, LABEL, SYNTHETIC} import language.postfixOps import scala.tools.nsc.transform.TypingTransformers import scala.tools.nsc.transform.Transform +import scala.collection.mutable.HashSet +import scala.collection.mutable.HashMap /** Translate pattern matching. * @@ -306,7 +308,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // it tests the type, checks the outer pointer and casts to the expected type // TODO: the outer check is mandated by the spec for case classes, but we do it for user-defined unapplies as well [SPEC] // (the prefix of the argument passed to the unapply must equal the prefix of the type of the binder) - val treeMaker = TypeTestTreeMaker(patBinder, extractor.paramType, pos) + val treeMaker = TypeTestTreeMaker(patBinder, patBinder, extractor.paramType, extractor.paramType)(pos, extractorArgTypeTest = true) (List(treeMaker), treeMaker.nextBinder) } else (Nil, patBinder) @@ -364,7 +366,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // must treat Typed and Bind together -- we need to know the patBinder of the Bind pattern to get at the actual type case MaybeBoundTyped(subPatBinder, pt) => // a typed pattern never has any subtrees - noFurtherSubPats(TypeAndEqualityTestTreeMaker(subPatBinder, patBinder, pt, pos)) + noFurtherSubPats(TypeTestTreeMaker(subPatBinder, patBinder, pt, glb(List(patBinder.info.widen, pt)).normalize)(pos)) /** A pattern binder x@p consists of a pattern variable x and a pattern p. The type of the variable x is the static type T of the pattern p. @@ -562,8 +564,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL protected def lengthGuard(binder: Symbol): Option[Tree] = // no need to check unless it's an unapplySeq and the minimal length is non-trivially satisfied - if (!isSeq || (expectedLength < minLenToCheck)) None - else { import CODE._ + checkedLength map { expectedLength => import CODE._ // `binder.lengthCompare(expectedLength)` def checkExpectedLength = (seqTree(binder) DOT seqLenCmp)(LIT(expectedLength)) @@ -575,8 +576,14 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL else _ INT_== _ // `if (binder != null && $checkExpectedLength [== | >=] 0) then else zero` - Some((seqTree(binder) ANY_!= NULL) AND compareOp(checkExpectedLength, ZERO)) + (seqTree(binder) ANY_!= NULL) AND compareOp(checkExpectedLength, ZERO) } + + def checkedLength: Option[Int] = + // no need to check unless it's an unapplySeq and the minimal length is non-trivially satisfied + if (!isSeq || (expectedLength < minLenToCheck)) None + else Some(expectedLength) + } // TODO: to be called when there's a def unapplyProd(x: T): U @@ -650,7 +657,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // the extractor call (applied to the binder bound by the flatMap corresponding to the previous (i.e., enclosing/outer) pattern) val extractorApply = atPos(pos)(spliceApply(patBinderOrCasted)) val binder = freshSym(pos, pureType(resultInMonad)) // can't simplify this when subPatBinders.isEmpty, since UnitClass.tpe is definitely wrong when isSeq, and resultInMonad should always be correct since it comes directly from the extractor's result type - ExtractorTreeMaker(extractorApply, lengthGuard(binder), binder, Substitution(subPatBinders, subPatRefs(binder)))(resultType.typeSymbol == BooleanClass) + ExtractorTreeMaker(extractorApply, lengthGuard(binder), binder, Substitution(subPatBinders, subPatRefs(binder)))(resultType.typeSymbol == BooleanClass, checkedLength, patBinderOrCasted) } override protected def seqTree(binder: Symbol): Tree = @@ -762,7 +769,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL val (fromFiltered, toFiltered) = (from, to).zipped filter { (f, t) => !other.from.contains(f) } new Substitution(other.from ++ fromFiltered, other.to.map(apply) ++ toFiltered) // a quick benchmarking run indicates the `.map(apply)` is not too costly } - override def toString = (from zip to) mkString("Substitution(", ", ", ")") + override def toString = (from.map(_.name) zip to) mkString("Substitution(", ", ", ")") } object EmptySubstitution extends Substitution(Nil, Nil) { @@ -775,7 +782,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // the making of the trees /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// trait TreeMakers extends TypedSubstitution { self: CodegenCore => - def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) = + def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, unchecked: Boolean): (List[List[TreeMaker]], List[Tree]) = (cases, Nil) def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Tree => Tree]): Option[Tree] = @@ -819,11 +826,13 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL case class BodyTreeMaker(body: Tree, matchPt: Type) extends TreeMaker with NoNewBinders { def chainBefore(next: Tree)(casegen: Casegen): Tree = // assert(next eq EmptyTree) atPos(body.pos)(casegen.one(substitution(body))) // since SubstOnly treemakers are dropped, need to do it here + override def toString = "B"+(body, matchPt) } case class SubstOnlyTreeMaker(prevBinder: Symbol, nextBinder: Symbol) extends TreeMaker { val localSubstitution = Substitution(prevBinder, CODE.REF(nextBinder)) def chainBefore(next: Tree)(casegen: Casegen): Tree = substitution(next) + override def toString = "S"+ localSubstitution } abstract class FunTreeMaker extends TreeMaker { @@ -851,7 +860,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL * the function's body is determined by the next TreeMaker * in this function's body, and all the subsequent ones, references to the symbols in `from` will be replaced by the corresponding tree in `to` */ - case class ExtractorTreeMaker(extractor: Tree, extraCond: Option[Tree], nextBinder: Symbol, localSubstitution: Substitution)(extractorReturnsBoolean: Boolean) extends FunTreeMaker { + case class ExtractorTreeMaker(extractor: Tree, extraCond: Option[Tree], nextBinder: Symbol, localSubstitution: Substitution)(extractorReturnsBoolean: Boolean, val checkedLength: Option[Int], val prevBinder: Symbol) extends FunTreeMaker { def chainBefore(next: Tree)(casegen: Casegen): Tree = { val condAndNext = extraCond map (casegen.ifThenElseZero(_, next)) getOrElse next atPos(extractor.pos)( @@ -860,136 +869,157 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL ) } - override def toString = "X"+(extractor, nextBinder) + override def toString = "X"+(extractor, nextBinder.name) } // TODO: allow user-defined unapplyProduct - case class ProductExtractorTreeMaker(prevBinder: Symbol, extraCond: Option[Tree], localSubstitution: Substitution) extends TreeMaker { import CODE._ + case class ProductExtractorTreeMaker(prevBinder: Symbol, extraCond: Option[Tree], localSubstitution: Substitution) extends FunTreeMaker { import CODE._ + val nextBinder = prevBinder // just passing through def chainBefore(next: Tree)(casegen: Casegen): Tree = { val nullCheck = REF(prevBinder) OBJ_NE NULL val cond = extraCond map (nullCheck AND _) getOrElse nullCheck casegen.ifThenElseZero(cond, substitution(next)) } - override def toString = "P"+(prevBinder, extraCond getOrElse "", localSubstitution) + override def toString = "P"+(prevBinder.name, extraCond getOrElse "", localSubstitution) } - // tack an outer test onto `cond` if binder.info and expectedType warrant it - def maybeWithOuterCheck(binder: Symbol, expectedTp: Type)(cond: Tree): Tree = { import CODE._ - if ( !((expectedTp.prefix eq NoPrefix) || expectedTp.prefix.typeSymbol.isPackageClass) - && needsOuterTest(expectedTp, binder.info, matchOwner)) { - val expectedPrefix = expectedTp.prefix match { - case ThisType(clazz) => THIS(clazz) - case pre => REF(pre.prefix, pre.termSymbol) - } + // typetag-based tests are inserted by the type checker + def needsTypeTest(tp: Type, pt: Type): Boolean = !(tp <:< pt) + + object TypeTestTreeMaker { + // factored out so that we can consistently generate other representations of the tree that implements the test + // (e.g. propositions for exhaustivity and friends, boolean for isPureTypeTest) + trait TypeTestCondStrategy { + type Result + + def outerTest(testedBinder: Symbol, expectedTp: Type): Result + // TODO: can probably always widen + def typeTest(testedBinder: Symbol, expectedTp: Type): Result + def nonNullTest(testedBinder: Symbol): Result + def equalsTest(pat: Tree, testedBinder: Symbol): Result + def eqTest(pat: Tree, testedBinder: Symbol): Result + def and(a: Result, b: Result): Result + } + + object treeCondStrategy extends TypeTestCondStrategy { import CODE._ + type Result = Tree - // ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` - // if there's an outer accessor, otherwise the condition becomes `true` -- TODO: can we improve needsOuterTest so there's always an outerAccessor? - val outer = expectedTp.typeSymbol.newMethod(vpmName.outer) setInfo expectedTp.prefix setFlag SYNTHETIC - val outerCheck = (Select(codegen._asInstanceOf(binder, expectedTp), outer)) OBJ_EQ expectedPrefix - - // first check cond, since that should ensure we're not selecting outer on null - codegen.and(cond, outerCheck) - } - else - cond - } - - // containsUnchecked: also need to test when erasing pt loses crucial information (maybe we can recover it using a TypeTag) - def needsTypeTest(tp: Type, pt: Type): Boolean = !(tp <:< pt) // || containsUnchecked(pt) - // TODO: try to find the TypeTag for the binder's type and the expected type, and if they exists, - // check that the TypeTag of the binder's type conforms to the TypeTag of the expected type - private def typeTest(binderToTest: Symbol, expectedTp: Type, disableOuterCheck: Boolean = false, dynamic: Boolean = false): Tree = { import CODE._ - // def coreTest = - if (disableOuterCheck) codegen._isInstanceOf(binderToTest, expectedTp) else maybeWithOuterCheck(binderToTest, expectedTp)(codegen._isInstanceOf(binderToTest, expectedTp)) - // [Eugene to Adriaan] use `resolveErasureTag` instead of `findManifest`. please, provide a meaningful position - // if (opt.experimental && containsUnchecked(expectedTp)) { - // if (dynamic) { - // val expectedTpTagTree = findManifest(expectedTp, true) - // if (!expectedTpTagTree.isEmpty) - // ((expectedTpTagTree DOT "erasure".toTermName) DOT "isAssignableFrom".toTermName)(REF(binderToTest) DOT nme.getClass_) - // else - // coreTest - // } else { - // val expectedTpTagTree = findManifest(expectedTp, true) - // val binderTpTagTree = findManifest(binderToTest.info, true) - // if(!(expectedTpTagTree.isEmpty || binderTpTagTree.isEmpty)) - // coreTest AND (binderTpTagTree DOT nme.CONFORMS)(expectedTpTagTree) - // else - // coreTest - // } - // } else coreTest - } - - // need to substitute since binder may be used outside of the next extractor call (say, in the body of the case) - case class TypeTestTreeMaker(prevBinder: Symbol, nextBinderTp: Type, pos: Position) extends CondTreeMaker { - val cond = typeTest(prevBinder, nextBinderTp, dynamic = true) - val res = codegen._asInstanceOf(prevBinder, nextBinderTp) - override def toString = "TT"+(prevBinder, nextBinderTp) - } - - // implements the run-time aspects of (§8.2) (typedPattern has already done the necessary type transformations) - // TODO: normalize construction, which yields a combination of a EqualityTestTreeMaker (when necessary) and a TypeTestTreeMaker - case class TypeAndEqualityTestTreeMaker(prevBinder: Symbol, patBinder: Symbol, pt: Type, pos: Position) extends CondTreeMaker { - val nextBinderTp = glb(List(patBinder.info.widen, pt)).normalize - - /** Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms: - - A reference to a class C, p.C, or T#C. - This type pattern matches any non-null instance of the given class. - Note that the prefix of the class, if it is given, is relevant for determining class instances. - For instance, the pattern p.C matches only instances of classes C which were created with the path p as prefix. - The bottom types scala.Nothing and scala.Null cannot be used as type patterns, because they would match nothing in any case. - - - A singleton type p.type. - This type pattern matches only the value denoted by the path p - (that is, a pattern match involved a comparison of the matched value with p using method eq in class AnyRef). // TODO: the actual pattern matcher uses ==, so that's what I'm using for now - // https://issues.scala-lang.org/browse/SI-4577 "pattern matcher, still disappointing us at equality time" - - - A compound type pattern T1 with ... with Tn where each Ti is a type pat- tern. - This type pattern matches all values that are matched by each of the type patterns Ti. - - - A parameterized type pattern T[a1,...,an], where the ai are type variable patterns or wildcards _. - This type pattern matches all values which match T for some arbitrary instantiation of the type variables and wildcards. - The bounds or alias type of these type variable are determined as described in (§8.3). - - - A parameterized type pattern scala.Array[T1], where T1 is a type pattern. // TODO - This type pattern matches any non-null instance of type scala.Array[U1], where U1 is a type matched by T1. - **/ - - // generate the tree for the run-time test that follows from the fact that - // a `scrut` of known type `scrutTp` is expected to have type `expectedTp` - // uses maybeWithOuterCheck to check the type's prefix - private def typeAndEqualityTest(patBinder: Symbol, pt: Type): Tree = { import CODE._ - // TODO: `null match { x : T }` will yield a check that (indirectly) tests whether `null ne null` - // don't bother (so that we don't end up with the warning "comparing values of types Null and Null using `ne' will always yield false") - def genEqualsAndInstanceOf(sym: Symbol): Tree - = codegen._equals(REF(sym), patBinder) AND typeTest(patBinder, pt.widen, disableOuterCheck = true) - - def isRefTp(tp: Type) = tp <:< AnyRefClass.tpe - - val patBinderTp = patBinder.info.widen - def isMatchUnlessNull = isRefTp(pt) && !needsTypeTest(patBinderTp, pt) - - // TODO: [SPEC] type test for Array - // TODO: use TypeTags to improve tests (for erased types we can do better when we have a TypeTag) - pt match { - case SingleType(_, sym) /*this implies sym.isStable*/ => genEqualsAndInstanceOf(sym) // TODO: [SPEC] the spec requires `eq` instead of `==` here - case ThisType(sym) if sym.isModule => genEqualsAndInstanceOf(sym) // must use == to support e.g. List() == Nil - case ThisType(sym) => REF(patBinder) OBJ_EQ This(sym) - case ConstantType(Constant(null)) if isRefTp(patBinderTp) => REF(patBinder) OBJ_EQ NULL - case ConstantType(const) => codegen._equals(Literal(const), patBinder) - case _ if isMatchUnlessNull => maybeWithOuterCheck(patBinder, pt)(REF(patBinder) OBJ_NE NULL) - case _ => typeTest(patBinder, pt) + def and(a: Result, b: Result): Result = a AND b + def typeTest(testedBinder: Symbol, expectedTp: Type) = codegen._isInstanceOf(testedBinder, expectedTp) + def nonNullTest(testedBinder: Symbol) = REF(testedBinder) OBJ_NE NULL + def equalsTest(pat: Tree, testedBinder: Symbol) = codegen._equals(pat, testedBinder) + def eqTest(pat: Tree, testedBinder: Symbol) = REF(testedBinder) OBJ_EQ pat + + def outerTest(testedBinder: Symbol, expectedTp: Type): Tree = { + val expectedOuter = expectedTp.prefix match { + case ThisType(clazz) => THIS(clazz) + case pre => REF(pre.prefix, pre.termSymbol) } + + // ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` + // if there's an outer accessor, otherwise the condition becomes `true` -- TODO: can we improve needsOuterTest so there's always an outerAccessor? + val outer = expectedTp.typeSymbol.newMethod(vpmName.outer) setInfo expectedTp.prefix setFlag SYNTHETIC + + (Select(codegen._asInstanceOf(testedBinder, expectedTp), outer)) OBJ_EQ expectedOuter + } } - val cond = typeAndEqualityTest(patBinder, pt) - val res = codegen._asInstanceOf(patBinder, nextBinderTp) + object pureTypeTestChecker extends TypeTestCondStrategy { + type Result = Boolean - // TODO: remove this - def isStraightTypeTest = cond match { case TypeApply(_, _) => cond.symbol == Any_isInstanceOf case _ => false } + def typeTest(testedBinder: Symbol, expectedTp: Type): Result = true - override def toString = "TET"+(patBinder, pt) + def outerTest(testedBinder: Symbol, expectedTp: Type): Result = false + def nonNullTest(testedBinder: Symbol): Result = false + def equalsTest(pat: Tree, testedBinder: Symbol): Result = false + def eqTest(pat: Tree, testedBinder: Symbol): Result = false + def and(a: Result, b: Result): Result = false // we don't and type tests, so the conjunction must include at least one false + } + } + + /** implements the run-time aspects of (§8.2) (typedPattern has already done the necessary type transformations) + * + * Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms: + - A reference to a class C, p.C, or T#C. + This type pattern matches any non-null instance of the given class. + Note that the prefix of the class, if it is given, is relevant for determining class instances. + For instance, the pattern p.C matches only instances of classes C which were created with the path p as prefix. + The bottom types scala.Nothing and scala.Null cannot be used as type patterns, because they would match nothing in any case. + + - A singleton type p.type. + This type pattern matches only the value denoted by the path p + (that is, a pattern match involved a comparison of the matched value with p using method eq in class AnyRef). // TODO: the actual pattern matcher uses ==, so that's what I'm using for now + // https://issues.scala-lang.org/browse/SI-4577 "pattern matcher, still disappointing us at equality time" + + - A compound type pattern T1 with ... with Tn where each Ti is a type pat- tern. + This type pattern matches all values that are matched by each of the type patterns Ti. + + - A parameterized type pattern T[a1,...,an], where the ai are type variable patterns or wildcards _. + This type pattern matches all values which match T for some arbitrary instantiation of the type variables and wildcards. + The bounds or alias type of these type variable are determined as described in (§8.3). + + - A parameterized type pattern scala.Array[T1], where T1 is a type pattern. // TODO + This type pattern matches any non-null instance of type scala.Array[U1], where U1 is a type matched by T1. + **/ + case class TypeTestTreeMaker(prevBinder: Symbol, testedBinder: Symbol, expectedTp: Type, nextBinderTp: Type)(_pos: Position, extractorArgTypeTest: Boolean = false) extends CondTreeMaker { + val pos = _pos + + import TypeTestTreeMaker._ + // println("TTTM"+(prevBinder, extractorArgTypeTest, testedBinder, expectedTp, nextBinderTp)) + + lazy val outerTestNeeded = ( + !((expectedTp.prefix eq NoPrefix) || expectedTp.prefix.typeSymbol.isPackageClass) + && needsOuterTest(expectedTp, testedBinder.info, matchOwner)) + + // the logic to generate the run-time test that follows from the fact that + // a `prevBinder` is expected to have type `expectedTp` + // the actual tree-generation logic is factored out, since the analyses generate Cond(ition)s rather than Trees + // TODO: `null match { x : T }` will yield a check that (indirectly) tests whether `null ne null` + // don't bother (so that we don't end up with the warning "comparing values of types Null and Null using `ne' will always yield false") + def renderCondition(cs: TypeTestCondStrategy): cs.Result = { + import cs._ + + def default = + // do type test first to ensure we won't select outer on null + if (outerTestNeeded) and(typeTest(testedBinder, expectedTp), outerTest(testedBinder, expectedTp)) + else typeTest(testedBinder, expectedTp) + + // true when called to type-test the argument to an extractor + // don't do any fancy equality checking, just test the type + if (extractorArgTypeTest) default + else expectedTp match { + // TODO: [SPEC] the spec requires `eq` instead of `==` for singleton types + // this implies sym.isStable + case SingleType(_, sym) => and(equalsTest(CODE.REF(sym), testedBinder), typeTest(testedBinder, expectedTp.widen)) + // must use == to support e.g. List() == Nil + case ThisType(sym) if sym.isModule => and(equalsTest(CODE.REF(sym), testedBinder), typeTest(testedBinder, expectedTp.widen)) + case ConstantType(const) => equalsTest(Literal(const), testedBinder) + + case ThisType(sym) => eqTest(This(sym), testedBinder) + case ConstantType(Constant(null)) if testedBinder.info.widen <:< AnyRefClass.tpe + => eqTest(CODE.NULL, testedBinder) + + // TODO: verify that we don't need to special-case Array + // I think it's okay: + // - the isInstanceOf test includes a test for the element type + // - Scala's arrays are invariant (so we don't drop type tests unsoundly) + case _ if (expectedTp <:< AnyRefClass.tpe) && !needsTypeTest(testedBinder.info.widen, expectedTp) => + // do non-null check first to ensure we won't select outer on null + if (outerTestNeeded) and(nonNullTest(testedBinder), outerTest(testedBinder, expectedTp)) + else nonNullTest(testedBinder) + + case _ => default + } + } + + val cond = renderCondition(treeCondStrategy) + val res = codegen._asInstanceOf(testedBinder, nextBinderTp) + + // is this purely a type test, e.g. no outer check, no equality tests (used in switch emission) + def isPureTypeTest = renderCondition(pureTypeTestChecker) + + override def toString = "TT"+(expectedTp, testedBinder.name, nextBinderTp) } // need to substitute to deal with existential types -- TODO: deal with existentials better, don't substitute (see RichClass during quick.comp) @@ -1000,7 +1030,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // equals need not be well-behaved, so don't intersect with pattern's (stabilized) type (unlike MaybeBoundTyped's accumType, where it's required) val cond = codegen._equals(patTree, prevBinder) val res = CODE.REF(prevBinder) - override def toString = "ET"+(prevBinder, patTree) + override def toString = "ET"+(prevBinder.name, patTree) } case class AlternativesTreeMaker(prevBinder: Symbol, var altss: List[List[TreeMaker]], pos: Position) extends TreeMaker with NoNewBinders { @@ -1062,7 +1092,21 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL def matchFailGen = (matchFailGenOverride orElse Some(CODE.MATCHERROR(_: Tree))) // println("combining cases: "+ (casesNoSubstOnly.map(_.mkString(" >> ")).mkString("{", "\n", "}"))) + def isSwitchAnnotation(tpe: Type) = tpe hasAnnotation SwitchClass + def isUncheckedAnnotation(tpe: Type) = tpe hasAnnotation UncheckedClass + + val (unchecked, requireSwitch) = scrut match { + case Typed(_, tpt) => + (isUncheckedAnnotation(tpt.tpe), + // matches with two or fewer cases need not apply for switchiness (if-then-else will do) + isSwitchAnnotation(tpt.tpe) && casesNoSubstOnly.lengthCompare(2) > 0) + case _ => + (false, false) + } + emitSwitch(scrut, scrutSym, casesNoSubstOnly, pt, matchFailGenOverride).getOrElse{ + if (requireSwitch) typer.context.unit.error(scrut.pos, "could not emit switch for @switch annotated match") + if (casesNoSubstOnly nonEmpty) { // before optimizing, check casesNoSubstOnly for presence of a default case, // since DCE will eliminate trivial cases like `case _ =>`, even if they're the last one @@ -1077,7 +1121,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL }) None else matchFailGen - val (cases, toHoist) = optimizeCases(scrutSym, casesNoSubstOnly, pt) + val (cases, toHoist) = optimizeCases(scrutSym, casesNoSubstOnly, pt, unchecked) val matchRes = codegen.matcher(scrut, scrutSym, pt)(cases map combineExtractors, synthCatchAll) @@ -1262,7 +1306,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // decisions, decisions /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - trait TreeMakerApproximation extends TreeMakers { self: CodegenCore => + trait TreeMakerApproximation extends TreeMakers with Prettification{ self: CodegenCore => object Test { var currId = 0 } @@ -1281,9 +1325,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL val id = { Test.currId += 1; Test.currId} override def toString = - if (cond eq Top) "T" - else if(cond eq Havoc) "!?" - else "T"+ id + (if(reusedBy nonEmpty) "!["+ treeMaker +"]" else (if(reuses.isEmpty) "["+ treeMaker +"]" else " cf. T"+reuses.get.id)) + "T"+ id + "C("+ cond +")" //+ (reuses map ("== T"+_.id) getOrElse (if(reusedBy.isEmpty) treeMaker else reusedBy mkString (treeMaker+ " -->(", ", ",")"))) } object Cond { @@ -1304,10 +1346,11 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL } // does not contribute any knowledge - case object Top extends Cond + case object Top extends Cond {override def toString = "T"} + // takes away knowledge. e.g., a user-defined guard - case object Havoc extends Cond + case object Havoc extends Cond {override def toString = "_|_"} // we know everything! everything! // this either means the case is unreachable, @@ -1315,11 +1358,15 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // case object Bottom extends Cond + case class AndCond(a: Cond, b: Cond) extends Cond {override def toString = a +"/\\"+ b} + case class OrCond(a: Cond, b: Cond) extends Cond {override def toString = "("+a+") \\/ ("+ b +")"} + object EqualityCond { private val uniques = new collection.mutable.HashMap[(Tree, Tree), EqualityCond] def apply(testedPath: Tree, rhs: Tree): EqualityCond = uniques getOrElseUpdate((testedPath, rhs), new EqualityCond(testedPath, rhs)) + def unapply(c: EqualityCond) = Some(c.testedPath, c.rhs) } - class EqualityCond(testedPath: Tree, rhs: Tree) extends Cond { + class EqualityCond(val testedPath: Tree, val rhs: Tree) extends Cond { // def negation = TopCond // inequality doesn't teach us anything // do simplification when we know enough about the tree statically: // - collapse equal trees @@ -1329,103 +1376,873 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL override def toString = testedPath +" == "+ rhs +"#"+ id } - object TypeCond { - private val uniques = new collection.mutable.HashMap[(Tree, Type), TypeCond] - def apply(testedPath: Tree, pt: Type): TypeCond = uniques getOrElseUpdate((testedPath, pt), new TypeCond(testedPath, pt)) + object NonNullCond { + private val uniques = new collection.mutable.HashMap[Tree, NonNullCond] + def apply(testedPath: Tree): NonNullCond = uniques getOrElseUpdate(testedPath, new NonNullCond(testedPath)) + def unapply(c: NonNullCond) = Some(c.testedPath) } - class TypeCond(testedPath: Tree, pt: Type) extends Cond { - // def negation = TopCond // inequality doesn't teach us anything - // do simplification when we know enough about the tree statically: - // - collapse equal trees - // - accumulate tests when (in)equality not known statically - // - become bottom when we statically know this can never match - override def toString = testedPath +" <: "+ pt +"#"+ id + class NonNullCond(val testedPath: Tree) extends Cond { + override def toString = testedPath +" ne null " +"#"+ id } - object TypeAndEqualityCond { - private val uniques = new collection.mutable.HashMap[(Tree, Type), TypeAndEqualityCond] - def apply(testedPath: Tree, pt: Type): TypeAndEqualityCond = uniques getOrElseUpdate((testedPath, pt), new TypeAndEqualityCond(testedPath, pt)) + object TypeCond { + private val uniques = new collection.mutable.HashMap[(Tree, Type), TypeCond] + def apply(testedPath: Tree, pt: Type): TypeCond = uniques getOrElseUpdate((testedPath, pt), new TypeCond(testedPath, pt)) + def unapply(c: TypeCond) = Some(c.testedPath, c.pt) } - class TypeAndEqualityCond(testedPath: Tree, pt: Type) extends Cond { + class TypeCond(val testedPath: Tree, val pt: Type) extends Cond { // def negation = TopCond // inequality doesn't teach us anything // do simplification when we know enough about the tree statically: // - collapse equal trees // - accumulate tests when (in)equality not known statically // - become bottom when we statically know this can never match - override def toString = testedPath +" (<: && ==) "+ pt +"#"+ id + override def toString = testedPath +" : "+ pt +"#"+ id } - def approximateMatch(root: Symbol, cases: List[List[TreeMaker]]): List[List[Test]] = { +// class OuterEqCond(val testedPath: Tree, val expectedType: Type) extends Cond { +// val expectedOuter = expectedTp.prefix match { +// case ThisType(clazz) => THIS(clazz) +// case pre => REF(pre.prefix, pre.termSymbol) +// } +// +// // ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` +// // if there's an outer accessor, otherwise the condition becomes `true` -- TODO: can we improve needsOuterTest so there's always an outerAccessor? +// val outer = expectedTp.typeSymbol.newMethod(vpmName.outer) setInfo expectedTp.prefix setFlag SYNTHETIC +// +// (Select(codegen._asInstanceOf(testedBinder, expectedTp), outer)) OBJ_EQ expectedOuter +// } + + + // returns (tree, tests), where `tree` will be used to refer to `root` in `tests` + abstract class TreeMakersToConds(val root: Symbol, val cases: List[List[TreeMaker]]) { // a variable in this set should never be replaced by a tree that "does not consist of a selection on a variable in this set" (intuitively) - val pointsToBound = collection.mutable.HashSet(root) + private val pointsToBound = collection.mutable.HashSet(root) // the substitution that renames variables to variables in pointsToBound - var normalize: Substitution = EmptySubstitution + private var normalize: Substitution = EmptySubstitution // replaces a variable (in pointsToBound) by a selection on another variable in pointsToBound // TODO check: // pointsToBound -- accumSubst.from == Set(root) && (accumSubst.from.toSet -- pointsToBound) isEmpty - var accumSubst: Substitution = EmptySubstitution + private var accumSubst: Substitution = EmptySubstitution + + private val trees = new collection.mutable.HashSet[Tree] - val trees = new collection.mutable.HashSet[Tree] + // TODO: improve, e.g., for constants + def sameValue(a: Tree, b: Tree): Boolean = (a eq b) || ((a, b) match { + case (_ : Ident, _ : Ident) => a.symbol eq b.symbol + case _ => false + }) + + // hashconsing trees (modulo value-equality) + def unique(t: Tree, tpOverride: Type = NoType): Tree = + trees find (a => a.equalsStructure0(t)(sameValue)) match { + case Some(orig) => orig // println("unique: "+ (t eq orig, orig)); + case _ => + trees += t + if (tpOverride != NoType) t setType tpOverride + else t + } - def approximateTreeMaker(tm: TreeMaker): Test = { - val subst = tm.substitution + def uniqueTp(tp: Type): Type = tp match { + // typerefs etc are already hashconsed + case _ : UniqueType => tp + case tp@RefinedType(parents, EmptyScope) => tp.memo(tp: Type)(identity) // TODO: does this help? + case _ => tp + } + // produce the unique tree used to refer to this binder + // the type of the binder passed to the first invocation + // determines the type of the tree that'll be returned for that binder as of then + def binderToUniqueTree(b: Symbol) = + unique(accumSubst(normalize(CODE.REF(b))), b.tpe) + + // note that the sequencing of operations is important: must visit in same order as match execution + // binderToUniqueTree uses the type of the first symbol that was encountered as the type for all future binders + def treeMakerToCond(tm: TreeMaker): Cond = { + updateSubstitution(tm.substitution) + + tm match { + case ttm@TypeTestTreeMaker(prevBinder, testedBinder, pt, _) => + object condStrategy extends TypeTestTreeMaker.TypeTestCondStrategy { + type Result = Cond + def and(a: Result, b: Result) = AndCond(a, b) + def outerTest(testedBinder: Symbol, expectedTp: Type) = Top // TODO OuterEqCond(testedBinder, expectedType) + def typeTest(b: Symbol, pt: Type) = TypeCond(binderToUniqueTree(b), uniqueTp(pt)) + def nonNullTest(testedBinder: Symbol) = NonNullCond(binderToUniqueTree(testedBinder)) + def equalsTest(pat: Tree, testedBinder: Symbol) = EqualityCond(binderToUniqueTree(testedBinder), unique(pat)) + def eqTest(pat: Tree, testedBinder: Symbol) = EqualityCond(binderToUniqueTree(testedBinder), unique(pat)) // TODO: eq, not == + } + ttm.renderCondition(condStrategy) + case EqualityTestTreeMaker(prevBinder, patTree, _) => EqualityCond(binderToUniqueTree(prevBinder), unique(patTree)) + case AlternativesTreeMaker(_, altss, _) => altss map (_ map treeMakerToCond reduceLeft AndCond) reduceLeft OrCond + case ProductExtractorTreeMaker(testedBinder, None, subst) => NonNullCond(binderToUniqueTree(testedBinder)) + case ExtractorTreeMaker(_, _, _, _) + | GuardTreeMaker(_) + | ProductExtractorTreeMaker(_, Some(_), _) + | BodyTreeMaker(_, _) => Havoc + case SubstOnlyTreeMaker(_, _) => Top + } + } + + private def updateSubstitution(subst: Substitution) = { // find part of substitution that replaces bound symbols by new symbols, and reverse that part // so that we don't introduce new aliases for existing symbols, thus keeping the set of bound symbols minimal val (boundSubst, unboundSubst) = (subst.from zip subst.to) partition {case (f, t) => t.isInstanceOf[Ident] && (t.symbol ne NoSymbol) && pointsToBound(f) } val (boundFrom, boundTo) = boundSubst.unzip + val (unboundFrom, unboundTo) = unboundSubst.unzip + + // reverse substitution that would otherwise replace a variable we already encountered by a new variable + // NOTE: this forgets the more precise we have for these later variables, but that's probably okay normalize >>= Substitution(boundTo map (_.symbol), boundFrom map (CODE.REF(_))) // println("normalize: "+ normalize) - val (unboundFrom, unboundTo) = unboundSubst unzip val okSubst = Substitution(unboundFrom, unboundTo map (normalize(_))) // it's important substitution does not duplicate trees here -- it helps to keep hash consing simple, anyway pointsToBound ++= ((okSubst.from, okSubst.to).zipped filter { (f, t) => pointsToBound exists (sym => t.exists(_.symbol == sym)) })._1 // println("pointsToBound: "+ pointsToBound) accumSubst >>= okSubst // println("accumSubst: "+ accumSubst) + } + + def approximateTreeMaker(tm: TreeMaker): Test = + Test(treeMakerToCond(tm), tm) + + def approximateMatch: List[List[Test]] = cases.map { _ map approximateTreeMaker } + } + + def approximateMatch(root: Symbol, cases: List[List[TreeMaker]]): List[List[Test]] = { + object approximator extends TreeMakersToConds(root, cases) + approximator.approximateMatch + } + + def showTreeMakers(cases: List[List[TreeMaker]]) = { + println("treeMakers:") + println(alignAcrossRows(cases, ">>")) + } - // TODO: improve, e.g., for constants - def sameValue(a: Tree, b: Tree): Boolean = (a eq b) || ((a, b) match { - case (_ : Ident, _ : Ident) => a.symbol eq b.symbol - case _ => false - }) - - // hashconsing trees (modulo value-equality) - def unique(t: Tree): Tree = - trees find (a => a.equalsStructure0(t)(sameValue)) match { - case Some(orig) => orig // println("unique: "+ (t eq orig, orig)); - case _ => trees += t; t + def showTests(testss: List[List[Test]]) = { + println("tests: ") + println(alignAcrossRows(testss, "&")) + } + } + + trait Prettification { + private def max(xs: Seq[Int]) = if (xs isEmpty) 0 else xs max + + def alignedColumns(cols: Seq[AnyRef]): Seq[String] = { + def toString(x: AnyRef) = if (x eq null) "" else x.toString + if (cols.isEmpty || cols.tails.isEmpty) cols map toString + else { + val (colStrs, colLens) = cols map {c => val s = toString(c); (s, s.length)} unzip + val maxLen = max(colLens) + val avgLen = colLens.sum/colLens.length + val goalLen = maxLen min avgLen*2 + def pad(s: String) = { + val toAdd = ((goalLen - s.length) max 0) + 2 + (" " * (toAdd/2)) + s + (" " * (toAdd/2 + (toAdd%2))) + } + cols map (x => pad(toString(x))) + } + } + def alignAcrossRows(xss: List[List[AnyRef]], sep: String, lineSep: String = "\n"): String = { + val maxLen = max(xss map (_.length)) + val padded = xss map (xs => xs ++ List.fill(maxLen - xs.length)(null)) + padded.transpose.map(alignedColumns).transpose map (_.mkString(sep)) mkString(lineSep) + } + } + + // http://www.cis.upenn.edu/~cis510/tcl/chap3.pdf + // http://users.encs.concordia.ca/~ta_ahmed/ms_thesis.pdf + trait Logic extends Prettification { + class Prop + case class Eq(p: Var, q: Const) extends Prop + + type Const + type Var <: AbsVar + + trait AbsVar { + // if the domain is enumerable, at least one assignment must be true + def domainEnumerable: Boolean + def domain: Option[Set[Const]] + + // for this var, call it V, turn V = C into the equivalent proposition in boolean logic + def propForEqualsTo(c: Const): Prop + + def equalitySyms: Set[Sym] + } + + // would be nice to statically check whether a prop is equational or pure, + // but that requires typing relations like And(x: Tx, y: Ty) : (if(Tx == PureProp && Ty == PureProp) PureProp else Prop) + case class And(a: Prop, b: Prop) extends Prop + case class Or(a: Prop, b: Prop) extends Prop + case class Not(a: Prop) extends Prop + + case object True extends Prop + case object False extends Prop + + private def nextSymId = {_symId += 1; _symId}; private var _symId = 0 + + // symbols are propositions + case class Sym(val variable: Var, val const: Const) extends Prop { + override val toString = variable +"="+ const +"#"+ nextSymId + } + + trait PropTraverser { + def apply(x: Prop): Unit = x match { + case And(a, b) => apply(a); apply(b) + case Or(a, b) => apply(a); apply(b) + case Not(a) => apply(a) + case Eq(a, b) => applyVar(a); applyConst(b) + case _ => + } + def applyVar(x: Var): Unit = {} + def applyConst(x: Const): Unit = {} + } + + trait PropMap { + def apply(x: Prop): Prop = x match { // TODO: mapConserve + case And(a, b) => And(apply(a), apply(b)) + case Or(a, b) => Or(apply(a), apply(b)) + case Not(a) => Not(apply(a)) + case p => p + } + } + + // plan: (aka TODO) + + // convert finite domain propositional logic to boolean propositional logic + // for all c in C, there is a corresponding (meta-indexed) proposition Qv(c) that represents V = c, + // the only property of equality that is encoded is that a variable can at most be equal to one of the c in C: + // thus, for each distinct c, c', c'',... in C, a clause `not (Qv(c) /\ (Qv(c') \/ ... \/ Qv(c'')))` is added + def removeVarEq(prop: Prop): Prop = { + val vars = new collection.mutable.HashSet[Var] + + object dropEquational extends PropMap { + override def apply(p: Prop) = p match { + case Eq(v, c) => vars += v; v.propForEqualsTo(c) + case _ => super.apply(p) + } + } + + // dropEquational populates vars, and for each var in vars. var.equalitySyms + val pure = dropEquational(prop) + + // X = C is translated to P_X=C + // X = C' is translated to P_X=C' + // need to enforce X cannot simultaneously equal C and C' + // thus, all equality syms are mutually exclusive + // X = A, B, C, D --> Not(And(A, B)) /\ Not(And(A, C)) /\ Not(And(A, D)) + // /\ Not(And(B, C)) /\ Not(And(B, D)) + // /\ Not(And(C, D)) + // equivalently Or(Not(A), Not(B)) /\ Or(...) + + var eqAxioms: Prop = True + def mutex(a: Sym)(b: Sym) = + eqAxioms = And(eqAxioms, Or(Not(a), Not(b))) + + // at least one assignment from the domain must be true + def assigned(dom: Set[Sym]) = + eqAxioms = And(eqAxioms, dom.reduceLeft(Or)) + + // println("vars: "+ vars) + vars.foreach { v => + // is the domain enumerable? then create equality syms for all elements in the domain and + // assert at least one of them must be selected + // if v.domain.isEmpty, we must consider the domain to be infinite + v.domain foreach { dom => + // get the Syms for the constants in the domain (making fresh ones for those not encountered in the formula) + val domProps = dom map {c => v.propForEqualsTo(c)} + val domSyms = new collection.mutable.HashSet[Sym]() + object collectSyms extends PropTraverser { + override def apply(p: Prop) = p match { + case domSym: Sym => domSyms += domSym + case _ => super.apply(p) + } } + domProps foreach collectSyms.apply - def uniqueTp(tp: Type): Type = tp match { - // typerefs etc are already hashconsed - case _ : UniqueType => tp - case tp@RefinedType(parents, EmptyScope) => tp.memo(tp: Type)(identity) // TODO: does this help? - case _ => tp + // TODO: an empty domain means involved type tests can never be true --> always non-exhaustive? + if (domSyms.nonEmpty) assigned(domSyms.toSet) } - def binderToUniqueTree(b: Symbol) = unique(accumSubst(normalize(CODE.REF(b)))) + // recover mutual-exclusivity (a variable can never be assigned two different constants) + var syms = v.equalitySyms.toList + while (syms.nonEmpty) { + syms.tail.foreach(mutex(syms.head)) + syms = syms.tail + } + } - Test(tm match { - case ProductExtractorTreeMaker(pb, None, subst) => Top // TODO: NotNullTest(prevBinder) - case tm@TypeTestTreeMaker(prevBinder, nextBinderTp, _) => TypeCond(binderToUniqueTree(prevBinder), uniqueTp(nextBinderTp)) - case tm@TypeAndEqualityTestTreeMaker(_, patBinder, pt, _) => TypeAndEqualityCond(binderToUniqueTree(patBinder), uniqueTp(pt)) - case tm@EqualityTestTreeMaker(prevBinder, patTree, _) => EqualityCond(binderToUniqueTree(prevBinder), unique(patTree)) - case ExtractorTreeMaker(_, _, _, _) - | GuardTreeMaker(_) - | ProductExtractorTreeMaker(_, Some(_), _) => Havoc - case AlternativesTreeMaker(_, _, _) => Havoc // TODO: can do better here - case SubstOnlyTreeMaker(_, _) => Top - case BodyTreeMaker(_, _) => Havoc - }, tm) + // println("eqAxioms:\n"+ cnfString(conjunctiveNormalForm(negationNormalForm(eqAxioms)))) + // println("pure:\n"+ cnfString(conjunctiveNormalForm(negationNormalForm(pure)))) + + And(eqAxioms, pure) + } + + // convert propositional logic formula to CNF + // http://www.dcs.warwick.ac.uk/people/academic/Ranko.Lazic/fsv/fsv6.pdf + def negationNormalForm(p: Prop): Prop = p match { + case And(a, b) => And(negationNormalForm(a), negationNormalForm(b)) + case Or(a, b) => Or(negationNormalForm(a), negationNormalForm(b)) + case Not(And(a, b)) => negationNormalForm(Or(Not(a), Not(b))) + case Not(Or(a, b)) => negationNormalForm(And(Not(a), Not(b))) + case Not(Not(p)) => negationNormalForm(p) + case Not(True) => False + case Not(False) => True + case True + | False + | (_ : Sym) + | Not(_ : Sym) => p + } + + // CNF: a formula is a conjunction of clauses + type Formula = List[Clause] ; def formula(c: Clause*): Formula = c.toList + + // a clause is a disjunction of distinct literals + type Clause = Set[Lit] ; def clause(l: Lit*): Clause = l.toSet + + // a literal is a (possibly negated) variable + case class Lit(sym: Sym, pos: Boolean = true) { + override def toString = if (!pos) "-"+ sym.toString else sym.toString + def unary_- = Lit(sym, !pos) + } + + val TrueF = formula() + val FalseF = formula(clause()) + def lit(s: Sym) = formula(clause(Lit(s))) + def negLit(s: Sym) = formula(clause(Lit(s, false))) + + def conjunctiveNormalForm(p: Prop): Formula = { + def distribute(a: Formula, b: Formula): Formula = (a, b) match { + // true \/ _ = true + case (TrueF, _) => TrueF + // _ \/ true = true + case (_, TrueF) => TrueF + // lit \/ lit + case (List(a), List(b)) => formula(a ++ b) + // (c1 /\ ... /\ cn) \/ d = ((c1 \/ d) /\ ... /\ (cn \/ d)) + case (cs, d) if cs.tail nonEmpty => cs flatMap (c => distribute(formula(c), d)) + // d \/ (c1 /\ ... /\ cn) = ((d \/ c1) /\ ... /\ (d \/ cn)) + case (d, cs) if cs.tail nonEmpty => cs flatMap (c => distribute(d, formula(c))) } - cases.map { _ map approximateTreeMaker } + p match { + case True => TrueF + case False => FalseF + case s: Sym => lit(s) + case Not(s: Sym) => negLit(s) + case And(a, b) => conjunctiveNormalForm(a) ++ conjunctiveNormalForm(b) + case Or(a, b) => distribute(conjunctiveNormalForm(a), conjunctiveNormalForm(b)) + } + } + + def normalize(p: Prop) = conjunctiveNormalForm(negationNormalForm(removeVarEq(p))) + def cnfString(f: Formula) = alignAcrossRows(f map (_.toList) toList, "\\/", " /\\\n") + + // adapted from http://lara.epfl.ch/w/sav10:simple_sat_solver (original by Hossein Hojjat) + type Model = Map[Sym, Boolean] + val EmptyModel = Map.empty[Sym, Boolean] + + // returns all solutions, if any (TODO: better infinite recursion backstop -- detect fixpoint??) + def fullDPLL(f: Formula): List[Model] = { + // the negation of a model -(S1=True/False /\ ... /\ SN=True/False) = clause(S1=False/True, ...., SN=False/True) + def negateModel(m: Model) = clause(m.toSeq.map{ case (sym, pos) => Lit(sym, !pos) } : _*) + + def findAllModels(f: Formula, models: List[Model], recursionDepthAllowed: Int = 20): List[Model]= + if (recursionDepthAllowed == 0) models + else { + val (ok, model) = DPLL(f) + // if we found a solution, conjunct the formula with the model's negation and recurse + if (ok) findAllModels(f :+ negateModel(model), model :: models, recursionDepthAllowed - 1) + else models + } + + findAllModels(f, Nil) + } + + def DPLL(f: Formula): (Boolean, Model) = { + @inline def withLit(res: (Boolean, Model), l: Lit) = (res._1, res._2 + (l.sym -> l.pos)) + @inline def orElse(a: (Boolean, Model), b: => (Boolean, Model)) = if (a._1) a else b + +// println("dpll\n"+ cnfString(f)) + + if (f isEmpty) (true, EmptyModel) + else if(f exists (_.isEmpty)) (false, EmptyModel) + else f.find(_.size == 1) map { unitClause => + val unitLit = unitClause.head +// println("unit: "+ unitLit) + val negated = -unitLit + // drop entire clauses that are trivially true + // (i.e., disjunctions that contain the literal we're making true in the returned model), + // and simplify clauses by dropping the negation of the literal we're making true + // (since False \/ X == X) + val simplified = f.filterNot(_.contains(unitLit)).map(_ - negated) + withLit(DPLL(simplified), unitLit) + } getOrElse { + // partition symbols according to whether they appear in positive and/or negative literals + val pos = new HashSet[Sym]() + val neg = new HashSet[Sym]() + f.foreach{_.foreach{ lit => + if (lit.pos) pos += lit.sym else neg += lit.sym + }} + // appearing in both positive and negative + val impures = pos intersect neg + // appearing only in either positive/negative positions + val pures = (pos ++ neg) -- impures + + if (pures nonEmpty) { + val pureSym = pures.head + // turn it back into a literal + // (since equality on literals is in terms of equality + // of the underlying symbol and its positivity, simply construct a new Lit) + val pureLit = Lit(pureSym, pos(pureSym)) +// println("pure: "+ pureLit +" pures: "+ pures +" impures: "+ impures) + val simplified = f.filterNot(_.contains(pureLit)) + withLit(DPLL(simplified), pureLit) + } else { + val split = f.head.head +// println("split: "+ split) + orElse(DPLL(f :+ clause(split)), DPLL(f :+ clause(-split))) + } + } + } + } + + /** + * Represent a match as a formula in propositional logic that encodes whether the match matches (abstractly: we only consider types) + * + */ + trait SymbolicMatchAnalysis extends TreeMakerApproximation with Logic { self: CodegenCore => + object Var { + private var _nextId = 0 + def nextId = {_nextId += 1; _nextId} + + private val uniques = new collection.mutable.HashMap[Tree, Var] + def apply(x: Tree): Var = uniques getOrElseUpdate(x, new Var(x, x.tpe)) + } + class Var(val path: Tree, fullTp: Type, checked: Boolean = true) extends AbsVar { + // when looking at the domain, we only care about types we can check at run time + val domainTp: Type = checkableType(fullTp) + + // case None => domain is unknown, + // case Some(List(tps: _*)) => domain is exactly tps + // we enumerate the subtypes of the full type, as that allows us to filter out more types statically, + // once we go to run-time checks (on Const's), convert them to checkable types + // TODO: there seems to be bug for singleton domains (variable does not show up in model) + val domain = if (checked) enumerateSubtypes(fullTp).map(_.map(Const).toSet) else None + + def describe = toString + ": "+ fullTp + domain.map(_.map(_.tp).mkString(" ::= ", " | ", "")).getOrElse(" ::= ??") +" // = "+ path + def domainEnumerable = domain.nonEmpty + + private val domMap = new collection.mutable.HashMap[Const, Sym] + private def symForEqualsTo(c: Const) = { + domMap getOrElseUpdate(c, { + // println("creating symbol for equality "+ this +" = "+ c) + Sym(this, c) + }) + } + + // for this var, call it V, turn V = C into the equivalent proposition in boolean logic + // over all executions of this method on the same Var object, + def propForEqualsTo(c: Const): Prop = { + domain match { + case None => symForEqualsTo(c) + case Some(domainConsts) => + val domainTps = domainConsts map (_.tp) + val checkedTp = c.tp + // find all the domain types that could make the type test true + // if the checked type is a supertype of the lub of the domain, + // we'll end up \/'ing the whole domain together, + // but must not simplify to True, as the type test may also fail... + val matches = domainTps.filter(_ <:< checkedTp).map{ tp => symForEqualsTo(Const(tp)) } + // println("type-equals-prop for "+ this +" = "+ c +": "+ (checkedTp, domainTp, domainTps) +" matches: "+ matches) + + if (matches isEmpty) False else matches.reduceLeft(Or) + } + } + + def equalitySyms: Set[Sym] = domMap.values.toSet + + private[this] val id: Int = Var.nextId + override def toString = "V"+ id + } + + // all our variables range over types + // a literal constant becomes ConstantType(Constant(v)) when the type allows it (roughly, anyval + string + null) + // equality between variables: SingleType(x) (note that pattern variables cannot relate to each other -- it's always patternVar == nonPatternVar) + case class Const(tp: Type) { + override def toString = tp.toString + + def toValueString = tp match { + case ConstantType(c) => c.escapedStringValue + case _ => tp.toString + } + } + + // make sure it's not a primitive, else (5: Byte) match { case 5 => ... } sees no Byte + // TODO: domain of feasibly enumerable built-in types (enums, char?) + def enumerateSubtypes(tp: Type): Option[List[Type]] = + tp.typeSymbol match { + case BooleanClass => + // println("enum bool "+ tp) + Some(List(ConstantType(Constant(true)), ConstantType(Constant(false)))) + // TODO case _ if tp.isTupleType => // recurse into component types + case sym if !sym.isSealed || isPrimitiveValueClass(sym) => + // println("enum unsealed "+ (tp, sym, sym.isSealed, isPrimitiveValueClass(sym))) + None + case sym => + val subclasses = ( + sym.sealedDescendants.toList sortBy (_.sealedSortName) + // symbols which are both sealed and abstract need not be covered themselves, because + // all of their children must be and they cannot otherwise be created. + filterNot (x => x.isSealed && x.isAbstractClass && !isPrimitiveValueClass(x))) + // println("subclasses "+ (sym, subclasses)) + + val tpApprox = typer.infer.approximateAbstracts(tp) + val pre = tpApprox.prefix + // valid subtypes are turned into checkable types, as we are entering the realm of the dynamic + val validSubTypes = (subclasses flatMap {sym => + // have to filter out children which cannot match: see ticket #3683 for an example + // compare to the fully known type `tp` (modulo abstract types), + // so that we can rule out stuff like: sealed trait X[T]; class XInt extends X[Int] --> XInt not valid when enumerating X[String] + // however, must approximate abstract types in + val subTp = appliedType(pre.memberType(sym), sym.typeParams.map(_ => WildcardType)) + val subTpApprox = typer.infer.approximateAbstracts(subTp) // TODO: needed? + // println("subtp"+(subTpApprox <:< tpApprox, subTpApprox, tpApprox)) + if (subTpApprox <:< tpApprox) Some(checkableType(subTp)) + else None + }) + // println("enum sealed "+ (tp, tpApprox) + " as "+ validSubTypes) + Some(validSubTypes) + } + + def narrowTypeOf(p: Tree) = p match { + case Literal(c) => ConstantType(c) + case Ident(_) if p.symbol.isStable => singleType(p.tpe.prefix, p.symbol) + case x => x.tpe.narrow + } + + // approximate a type to the static type that is fully checkable at run time, + // hiding statically known but dynamically uncheckable information using existential quantification + // TODO: this is subject to the availability of TypeTags (since an abstract type with a type tag is checkable at run time) + def checkableType(tp: Type): Type = { + // TODO: this is extremely rough... + object toCheckable extends TypeMap { + def apply(tp: Type) = tp match { + case TypeRef(pre, sym, a :: as) if sym ne ArrayClass => + // replace type args by existentials, since they can't be checked + // TODO: when type tags are available, we will check -- when this is implemented, can we take that into account here? + // TODO: don't reuse sym.typeParams, they have bounds (and those must not be considered) + newExistentialType(sym.typeParams, sym.tpe).asSeenFrom(pre, sym.owner) + case _ => mapOver(tp) + } + } + val res = toCheckable(tp) + // println("checkable "+(tp, res)) + res + } + + // a type is "uncheckable" (for exhaustivity) if we don't statically know its subtypes (i.e., it's unsealed) + // we consider tuple types with at least one component of a checkable type as a checkable type + def uncheckableType(tp: Type): Boolean = { + @inline def tupleComponents(tp: Type) = tp.normalize.typeArgs + val checkable = ( + (isTupleType(tp) && tupleComponents(tp).exists(tp => !uncheckableType(tp))) + || enumerateSubtypes(tp).nonEmpty) + // if (!checkable) println("deemed uncheckable: "+ tp) + !checkable + } + + def exhaustive(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): List[String] = if (uncheckableType(prevBinder.info)) Nil else { + // customize TreeMakersToConds (which turns a tree of tree makers into a more abstract DAG of tests) + // - approximate the pattern `List()` (unapplySeq on List with empty length) as `Nil`, + // otherwise the common (xs: List[Any]) match { case List() => case x :: xs => } is deemed unexhaustive + // - back off (to avoid crying exhaustive too often) when: + // - there are guards --> + // - there are extractor calls (that we can't secretly/soundly) rewrite + var backoff = false + object exhaustivityApproximation extends TreeMakersToConds(prevBinder, cases) { + override def treeMakerToCond(tm: TreeMaker): Cond = tm match { + case p@ExtractorTreeMaker(extractor, Some(lenCheck), testedBinder, _) => + p.checkedLength match { + // pattern: `List()` (interpret as `Nil`) + // TODO: make it more general List(1, 2) => 1 :: 2 :: Nil + case Some(0) if testedBinder.tpe.typeSymbol == ListClass => // extractor.symbol.owner == SeqFactory + EqualityCond(binderToUniqueTree(p.prevBinder), unique(Ident(NilModule) setType NilModule.tpe)) + case _ => + super.treeMakerToCond(tm) + } + case ExtractorTreeMaker(_, _, _, _) => +// println("backing off due to "+ tm) + backoff = true + super.treeMakerToCond(tm) + case GuardTreeMaker(guard) => + guard.tpe match { + case ConstantType(Constant(true)) => Top + case ConstantType(Constant(false)) => Havoc + case _ => +// println("can't statically interpret guard: "+(guard, guard.tpe)) + backoff = true + Havoc + } + case _ => + super.treeMakerToCond(tm) + } + } + + def symbolic(t: Cond): Prop = t match { + case AndCond(a, b) => And(symbolic(a), symbolic(b)) + case OrCond(a, b) => Or(symbolic(a), symbolic(b)) + case Top => True + case Havoc => False + case TypeCond(p, pt) => Eq(Var(p), Const(checkableType(pt))) + case EqualityCond(p, q) => Eq(Var(p), Const(narrowTypeOf(q))) + case NonNullCond(p) => True // ignoring nullness because it generates too much noise Not(Eq(Var(p), Const(NullClass.tpe))) + } + + def symbolicCase(tests: List[Test]) = { + val testsBeforeBody = tests.takeWhile(t => !t.treeMaker.isInstanceOf[BodyTreeMaker]) + testsBeforeBody.map(t => symbolic(t.cond)).foldLeft(True: Prop)(And) + } + + val tests = exhaustivityApproximation.approximateMatch + + if (backoff) Nil else { + val symbolicCases = tests map symbolicCase + + val prevBinderTree = exhaustivityApproximation.binderToUniqueTree(prevBinder) + + // TODO: null tests generate too much noise, so disabled them -- is there any way to bring them back? + // assuming we're matching on a non-null scrutinee (prevBinder), when does the match fail? + // val nonNullScrutineeCond = + // assume non-null for all the components of the tuple we're matching on (if we're matching on a tuple) + // if (isTupleType(prevBinder.tpe)) + // prevBinder.tpe.typeArgs.mapWithIndex{case (_, i) => NonNullCond(codegen.tupleSel(prevBinderTree)(i))}.reduceLeft(AndCond) + // else + // NonNullCond(prevBinderTree) + // val matchFails = And(symbolic(nonNullScrutineeCond), Not(symbolicCases reduceLeft (Or(_, _)))) + + // when does the match fail? + val matchFails = Not(symbolicCases reduceLeft (Or(_, _))) + + + // debug output: + // println("analysing:") + // showTreeMakers(cases) + // showTests(tests) + // + // def gatherVariables(p: Prop): Set[Var] = { + // val vars = new HashSet[Var]() + // (new PropTraverser { + // override def applyVar(v: Var) = vars += v + // })(p) + // vars.toSet + // } + // val vars = gatherVariables(matchFails) + // println("\nvars:\n"+ (vars map (_.describe) mkString ("\n"))) + // + // println("\nmatchFails as CNF:\n"+ cnfString(normalize(matchFails))) + + // find the models (under which the match fails) + val matchFailModels = fullDPLL(normalize(matchFails)) + + val scrutVar = Var(prevBinderTree) + val counterExamples = matchFailModels.map(modelToCounterExample(scrutVar)) + + CounterExample.prune(counterExamples).map(_.toString).sorted + } + } + + object CounterExample { + def prune(examples: List[CounterExample]): List[CounterExample] = { + val distinct = examples.filterNot(_ == NoExample).toSet + distinct.filterNot(ce => distinct.exists(other => (ce ne other) && ce.coveredBy(other))).toList + } + } + + // a way to construct a value that will make the match fail: a constructor invocation, a constant, an object of some type) + class CounterExample { + protected[SymbolicMatchAnalysis] def flattenConsArgs: List[CounterExample] = Nil + def coveredBy(other: CounterExample): Boolean = this == other || other == WildcardExample + } + case class ValueExample(c: Const) extends CounterExample { override def toString = c.toValueString } + case class TypeExample(c: Const) extends CounterExample { override def toString = "(_ : "+ c +")" } + case class NegativeExample(nonTrivialNonEqualTo: List[Const]) extends CounterExample { + override def toString = { + val negation = + if (nonTrivialNonEqualTo.tail.isEmpty) nonTrivialNonEqualTo.head.toValueString + else nonTrivialNonEqualTo.map(_.toValueString).sorted.mkString("in (", ", ", ")") + "<not "+ negation +">" + } + } + case class ListExample(ctorArgs: List[CounterExample]) extends CounterExample { + protected[SymbolicMatchAnalysis] override def flattenConsArgs: List[CounterExample] = ctorArgs match { + case hd :: tl :: Nil => hd :: tl.flattenConsArgs + case _ => Nil + } + protected[SymbolicMatchAnalysis] lazy val elems = flattenConsArgs + + override def coveredBy(other: CounterExample): Boolean = + other match { + case other@ListExample(_) => + this == other || ((elems.length == other.elems.length) && (elems zip other.elems).forall{case (a, b) => a coveredBy b}) + case _ => super.coveredBy(other) + } + + override def toString = elems.mkString("List(", ", ", ")") + } + case class TupleExample(ctorArgs: List[CounterExample]) extends CounterExample { + override def toString = ctorArgs.mkString("(", ", ", ")") + + override def coveredBy(other: CounterExample): Boolean = + other match { + case TupleExample(otherArgs) => + this == other || ((ctorArgs.length == otherArgs.length) && (ctorArgs zip otherArgs).forall{case (a, b) => a coveredBy b}) + case _ => super.coveredBy(other) + } + } + case class ConstructorExample(cls: Symbol, ctorArgs: List[CounterExample]) extends CounterExample { + override def toString = cls.decodedName + (if (cls.isModuleClass) "" else ctorArgs.mkString("(", ", ", ")")) + } + + case object WildcardExample extends CounterExample { override def toString = "_" } + case object NoExample extends CounterExample { override def toString = "??" } + + // return constructor call when the model is a true counter example + // (the variables don't take into account type information derived from other variables, + // so, naively, you might try to construct a counter example like _ :: Nil(_ :: _, _ :: _), + // since we didn't realize the tail of the outer cons was a Nil) + def modelToCounterExample(scrutVar: Var)(model: Model): CounterExample = { + // x1 = ... + // x1.hd = ... + // x1.tl = ... + // x1.hd.hd = ... + // ... + val varAssignment = model.toSeq.groupBy{f => f match {case (sym, value) => sym.variable} }.mapValues{ xs => + val (trues, falses) = xs.partition(_._2) + (trues map (_._1.const), falses map (_._1.const)) + // should never be more than one value in trues... + } + + // println("var assignment:\n"+ + // varAssignment.toSeq.sortBy(_._1.toString).map { case (v, (trues, falses)) => + // val assignment = "== "+ (trues mkString("(", ", ", ")")) +" != ("+ (falses mkString(", ")) +")" + // v +"(="+ v.path +": "+ v.domainTp +") "+ assignment + // }.mkString("\n")) + + + // chop a path into a list of symbols + def chop(path: Tree): List[Symbol] = path match { + case Ident(_) => List(path.symbol) + case Select(pre, name) => chop(pre) :+ path.symbol + case _ => println("don't know how to chop "+ path); Nil + } + + // turn the variable assignments into a tree + // the root is the scrutinee (x1), edges are labelled by the fields that are assigned + // a node is a variable example (which is later turned into a counter example) + object VariableAssignment { + private def findVar(path: List[Symbol]) = path match { + case List(root) if root == scrutVar.path.symbol => Some(scrutVar) + case _ => varAssignment.find{case (v, a) => chop(v.path) == path}.map(_._1) + } + + private val uniques = new collection.mutable.HashMap[Var, VariableAssignment] + private def unique(variable: Var): VariableAssignment = + uniques.getOrElseUpdate(variable, { + val (eqTo, neqTo) = varAssignment.getOrElse(variable, (Nil, Nil)) // TODO + VariableAssignment(variable, eqTo.toList, neqTo.toList, HashMap.empty) + }) + + def apply(variable: Var): VariableAssignment = { + val path = chop(variable.path) + val pre = path.init + val field = path.last + + val newCtor = unique(variable) + + if (pre.isEmpty) newCtor + else { + findVar(pre) foreach { preVar => + val outerCtor = this(preVar) + outerCtor.fields(field) = newCtor + } + newCtor + } + } + } + + // node in the tree that describes how to construct a counter-example + case class VariableAssignment(variable: Var, equalTo: List[Const], notEqualTo: List[Const], fields: collection.mutable.Map[Symbol, VariableAssignment]) { + private lazy val ctor = (equalTo match { case List(Const(tp)) => tp case _ => variable.domainTp }).typeSymbol.primaryConstructor + private lazy val ctorParams = if (ctor == NoSymbol || ctor.paramss.isEmpty) Nil else ctor.paramss.head + private lazy val cls = if (ctor == NoSymbol) NoSymbol else ctor.owner + private lazy val caseFieldAccs = if (cls == NoSymbol) Nil else cls.caseFieldAccessors + + + def allFieldAssignmentsLegal: Boolean = + (fields.keySet subsetOf caseFieldAccs.toSet) && fields.values.forall(_.allFieldAssignmentsLegal) + + private lazy val nonTrivialNonEqualTo = notEqualTo.filterNot{c => val sym = c.tp.typeSymbol; sym == AnyClass } // sym == NullClass || + + // NoExample if the constructor call is ill-typed + // (thus statically impossible -- can we incorporate this into the formula?) + // beBrief is used to suppress negative information nested in tuples -- it tends to get too noisy + def toCounterExample(beBrief: Boolean = false): CounterExample = + if (!allFieldAssignmentsLegal) NoExample + else { +// println("describing "+ (variable, equalTo, notEqualTo, fields, cls, allFieldAssignmentsLegal)) + val res = equalTo match { + // a definite assignment to a value + case List(eq@Const(_: ConstantType)) if fields.isEmpty => ValueExample(eq) + + // constructor call + // or we did not gather any information about equality but we have information about the fields + // --> typical example is when the scrutinee is a tuple and all the cases first unwrap that tuple and only then test something interesting + case _ if cls != NoSymbol && + ( equalTo.nonEmpty + || (fields.nonEmpty && !isPrimitiveValueClass(cls) && equalTo.isEmpty && notEqualTo.isEmpty)) => + + @inline def args(brevity: Boolean = beBrief) = { + // figure out the constructor arguments from the field assignment + val argLen = (caseFieldAccs.length min ctorParams.length) + + (0 until argLen).map(i => fields.get(caseFieldAccs(i)).map(_.toCounterExample(brevity)) getOrElse WildcardExample).toList + } + + cls match { + case ConsClass => ListExample(args()) + case _ if isTupleSymbol(cls) => TupleExample(args(true)) + case _ => ConstructorExample(cls, args()) + } + + // a definite assignment to a type + case List(eq) if fields.isEmpty => TypeExample(eq) + + // negative information + case Nil if nonTrivialNonEqualTo.nonEmpty => + // negation tends to get pretty verbose + if (beBrief) WildcardExample else NegativeExample(nonTrivialNonEqualTo) + + // not a valid counter-example, possibly since we have a definite type but there was a field mismatch + // TODO: improve reasoning -- in the mean time, a false negative is better than an annoying false positive + case _ => NoExample + } +// println("described as: "+ res) + res + } + + override def toString = toCounterExample().toString + } + + // slurp in information from other variables + varAssignment.keys.foreach{ v => if (v != scrutVar) VariableAssignment(v) } + + // this is the variable we want a counter example for + VariableAssignment(scrutVar).toCounterExample() } } @@ -1446,28 +2263,49 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // interpret: val dependencies = new collection.mutable.LinkedHashMap[Test, Set[Cond]] val tested = new collection.mutable.HashSet[Cond] - testss foreach { tests => - tested.clear() - tests dropWhile { test => - val cond = test.cond - if ((cond eq Havoc) || (cond eq Top)) (cond eq Top) // stop when we encounter a havoc, skip top - else { - tested += cond + + def storeDependencies(test: Test) = { + val cond = test.cond + + def simplify(c: Cond): Set[Cond] = c match { + case AndCond(a, b) => simplify(a) ++ simplify(b) + case OrCond(_, _) => Set(Havoc) // TODO: supremum? + case NonNullCond(_) => Set(Top) // not worth remembering + case _ => Set(c) + } + val conds = simplify(cond) + + if (conds(Havoc)) false // stop when we encounter a havoc + else { + val nonTrivial = conds filterNot (_ == Top) + if (nonTrivial nonEmpty) { + tested ++= nonTrivial // is there an earlier test that checks our condition and whose dependencies are implied by ours? - dependencies find { case (priorTest, deps) => - ((priorTest.cond eq cond) || (deps contains cond)) && (deps subsetOf tested) - } foreach { case (priorTest, deps) => - // if so, note the dependency in both tests - priorTest registerReuseBy test + dependencies find { + case (priorTest, deps) => + ((simplify(priorTest.cond) == nonTrivial) || // our conditions are implied by priorTest if it checks the same thing directly + (nonTrivial subsetOf deps) // or if it depends on a superset of our conditions + ) && (deps subsetOf tested) // the conditions we've tested when we are here in the match satisfy the prior test, and hence what it tested + } foreach { + case (priorTest, _) => + // if so, note the dependency in both tests + priorTest registerReuseBy test } dependencies(test) = tested.toSet // copies - true } + true } } + + testss foreach { tests => + tested.clear() + tests dropWhile storeDependencies + } + // println("dependencies: "+ dependencies) + // find longest prefix of tests that reuse a prior test, and whose dependent conditions monotonically increase // then, collapse these contiguous sequences of reusing tests // store the result of the final test and the intermediate results in hoisted mutable variables (TODO: optimize: don't store intermediate results that aren't used) @@ -1476,7 +2314,12 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL var okToCall = false val reusedOrOrig = (tm: TreeMaker) => {assert(okToCall); reused.getOrElse(tm, tm)} - val res = testss map { tests => + // maybe collapse: replace shared prefix of tree makers by a ReusingCondTreeMaker + // once this has been computed, we'll know which tree makers are reused, + // and we'll replace those by the ReusedCondTreeMakers we've constructed (and stored in the reused map) + val collapsed = testss map { tests => + // map tests to the equivalent list of treemakers, replacing shared prefixes by a reusing treemaker + // if there's no sharing, simply map to the tree makers corresponding to the tests var currDeps = Set[Cond]() val (sharedPrefix, suffix) = tests span { test => (test.cond eq Top) || (for( @@ -1487,23 +2330,33 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL yield diff).nonEmpty } - val collapsedTreeMakers = if (sharedPrefix.nonEmpty) { // even sharing prefixes of length 1 brings some benefit (overhead-percentage for compiler: 26->24%, lib: 19->16%) - for (test <- sharedPrefix; reusedTest <- test.reuses) reusedTest.treeMaker match { - case reusedCTM: CondTreeMaker => reused(reusedCTM) = ReusedCondTreeMaker(reusedCTM) - case _ => - } + val collapsedTreeMakers = + if (sharedPrefix.isEmpty) None + else { // even sharing prefixes of length 1 brings some benefit (overhead-percentage for compiler: 26->24%, lib: 19->16%) + for (test <- sharedPrefix; reusedTest <- test.reuses) reusedTest.treeMaker match { + case reusedCTM: CondTreeMaker => reused(reusedCTM) = ReusedCondTreeMaker(reusedCTM) + case _ => + } - // println("sharedPrefix: "+ sharedPrefix) - for (lastShared <- sharedPrefix.reverse.dropWhile(_.cond eq Top).headOption; - lastReused <- lastShared.reuses) - yield ReusingCondTreeMaker(sharedPrefix, reusedOrOrig) :: suffix.map(_.treeMaker) - } else None + // println("sharedPrefix: "+ sharedPrefix) + // if the shared prefix contains interesting conditions (!= Top) + // and the last of such interesting shared conditions reuses another treemaker's test + // replace the whole sharedPrefix by a ReusingCondTreeMaker + for (lastShared <- sharedPrefix.reverse.dropWhile(_.cond eq Top).headOption; + lastReused <- lastShared.reuses) + yield ReusingCondTreeMaker(sharedPrefix, reusedOrOrig) :: suffix.map(_.treeMaker) + } collapsedTreeMakers getOrElse tests.map(_.treeMaker) // sharedPrefix need not be empty (but it only contains Top-tests, which are dropped above) } okToCall = true // TODO: remove (debugging) - res mapConserve (_ mapConserve reusedOrOrig) + // replace original treemakers that are reused (as determined when computing collapsed), + // by ReusedCondTreeMakers + val reusedMakers = collapsed mapConserve (_ mapConserve reusedOrOrig) +// println("after CSE:") +// showTreeMakers(reusedMakers) + reusedMakers } object ReusedCondTreeMaker { @@ -1520,28 +2373,45 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // TODO: finer-grained duplication def chainBefore(next: Tree)(casegen: Casegen): Tree = // assert(codegen eq optimizedCodegen) atPos(pos)(casegen.asInstanceOf[optimizedCodegen.OptimizedCasegen].flatMapCondStored(cond, storedCond, res, nextBinder, substitution(next).duplicate)) + + override def toString = "Memo"+(nextBinder.name, storedCond.name, cond, res, substitution) } case class ReusingCondTreeMaker(sharedPrefix: List[Test], toReused: TreeMaker => TreeMaker) extends TreeMaker { import CODE._ - lazy val dropped_priors = sharedPrefix map (t => (toReused(t.treeMaker), t.reuses map (test => toReused(test.treeMaker)))) lazy val localSubstitution = { - val (from, to) = dropped_priors.collect { - case (dropped: CondTreeMaker, Some(prior: ReusedCondTreeMaker)) => - (dropped.nextBinder, REF(prior.nextBinder)) - }.unzip - val oldSubs = dropped_priors.collect { - case (dropped: TreeMaker, _) => - dropped.substitution + // replace binder of each dropped treemaker by corresponding binder bound by the most recent reused treemaker + var mostRecentReusedMaker: ReusedCondTreeMaker = null + def mapToStored(droppedBinder: Symbol) = if (mostRecentReusedMaker eq null) Nil else List((droppedBinder, REF(mostRecentReusedMaker.nextBinder))) + val (from, to) = sharedPrefix.flatMap { dropped => + dropped.reuses.map(test => toReused(test.treeMaker)).foreach { + case reusedMaker: ReusedCondTreeMaker => + mostRecentReusedMaker = reusedMaker + case _ => } - oldSubs.foldLeft(Substitution(from, to))(_ >> _) + + // TODO: have super-trait for retrieving the variable that's operated on by a tree maker + // and thus assumed in scope, either because it binds it or because it refers to it + dropped.treeMaker match { + case dropped: FunTreeMaker => + mapToStored(dropped.nextBinder) + case _ => Nil + } + }.unzip + val rerouteToReusedBinders = Substitution(from, to) + + val collapsedDroppedSubst = sharedPrefix map (t => (toReused(t.treeMaker).substitution)) + + collapsedDroppedSubst.foldLeft(rerouteToReusedBinders)(_ >> _) } - def chainBefore(next: Tree)(casegen: Casegen): Tree = { - val cond = REF(dropped_priors.reverse.collectFirst{case (_, Some(ctm: ReusedCondTreeMaker)) => ctm}.get.storedCond) + lazy val lastReusedTreeMaker = sharedPrefix.reverse.flatMap(tm => tm.reuses map (test => toReused(test.treeMaker))).collectFirst{case x: ReusedCondTreeMaker => x}.head - // TODO: finer-grained duplication -- MUST duplicate though, or we'll get VerifyErrors since sharing trees confuses lambdalift, and its confusion it emits illegal casts (diagnosed by Grzegorz: checkcast T ; invokevirtual S.m, where T not a subtype of S) - casegen.ifThenElseZero(cond, substitution(next).duplicate) + def chainBefore(next: Tree)(casegen: Casegen): Tree = { + // TODO: finer-grained duplication -- MUST duplicate though, or we'll get VerifyErrors since sharing trees confuses lambdalift, + // and in its confusion it emits illegal casts (diagnosed by Grzegorz: checkcast T ; invokevirtual S.m, where T not a subtype of S) + casegen.ifThenElseZero(REF(lastReusedTreeMaker.storedCond), substitution(next).duplicate) } + override def toString = "R"+(lastReusedTreeMaker.storedCond.name, substitution) } } @@ -1646,7 +2516,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // TODO: if patterns allow switch but the type of the scrutinee doesn't, cast (type-test) the scrutinee to the corresponding switchable type and switch on the result if (regularSwitchMaker.switchableTpe(scrutSym.tpe)) { val caseDefsWithDefault = regularSwitchMaker(cases map {c => (scrutSym, c)}, pt) - if (caseDefsWithDefault.length <= 2) None // not worth emitting a switch... also, the optimizer has trouble digesting tiny switches, apparently, so let's be nice and not generate them + if (caseDefsWithDefault isEmpty) None // not worth emitting a switch. else { // match on scrutSym -- converted to an int if necessary -- not on scrut directly (to avoid duplicating scrut) val scrutToInt: Tree = @@ -1669,10 +2539,8 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL // analyze the result of approximateTreeMaker rather than the TreeMaker itself object SwitchableTreeMaker extends SwitchableTreeMakerExtractor { def unapply(x: TreeMaker): Option[Tree] = x match { - case tm@TypeTestTreeMaker(_, _, _) => - Some(Bind(tm.nextBinder, Typed(Ident(nme.WILDCARD), TypeTree(tm.nextBinderTp)) /* not used by back-end */)) // -- TODO: use this if binder does not occur in the body - case tm@TypeAndEqualityTestTreeMaker(_, patBinder, pt, _) if tm.isStraightTypeTest => - Some(Bind(tm.nextBinder, Typed(Ident(nme.WILDCARD), TypeTree(tm.nextBinderTp)) /* not used by back-end */)) + case tm@TypeTestTreeMaker(_, _, pt, _) if tm.isPureTypeTest => // -- TODO: use this if binder does not occur in the body + Some(Bind(tm.nextBinder, Typed(Ident(nme.WILDCARD), TypeTree(pt)) /* not used by back-end */)) case _ => None } @@ -1823,8 +2691,18 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL trait MatchOptimizations extends CommonSubconditionElimination with DeadCodeElimination with SwitchEmission - with OptimizedCodegen { self: TreeMakers => - override def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) = { + with OptimizedCodegen + with SymbolicMatchAnalysis { self: TreeMakers => + override def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, unchecked: Boolean): (List[List[TreeMaker]], List[Tree]) = { + val counterExamples = if (unchecked) Nil else exhaustive(prevBinder, cases, pt) + if (counterExamples.nonEmpty) { + val ceString = + if (counterExamples.tail.isEmpty) "input: " + counterExamples.head + else "inputs: " + counterExamples.mkString(", ") + + typer.context.unit.warning(prevBinder.pos, "match may not be exhaustive.\nIt would fail on the following "+ ceString) + } + val optCases = doCSE(prevBinder, doDCE(prevBinder, cases, pt), pt) val toHoist = ( for (treeMakers <- optCases) diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 4e578e3f0d..3373878beb 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -1084,8 +1084,16 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R def isEitherNullable = (NullClass.tpe <:< receiver.info) || (NullClass.tpe <:< actual.info) def isBoolean(s: Symbol) = unboxedValueClass(s) == BooleanClass def isUnit(s: Symbol) = unboxedValueClass(s) == UnitClass - def isNumeric(s: Symbol) = isNumericValueClass(unboxedValueClass(s)) || (s isSubClass ScalaNumberClass) - def isSpecial(s: Symbol) = isPrimitiveValueClass(unboxedValueClass(s)) || (s isSubClass ScalaNumberClass) || isMaybeValue(s) + def isNumeric(s: Symbol) = isNumericValueClass(unboxedValueClass(s)) || isAnyNumber(s) + def isScalaNumber(s: Symbol) = s isSubClass ScalaNumberClass + // test is behind a platform guard + def isJavaNumber(s: Symbol) = !forMSIL && (s isSubClass JavaNumberClass) + // includes java.lang.Number if appropriate [SI-5779] + def isAnyNumber(s: Symbol) = isScalaNumber(s) || isJavaNumber(s) + def isMaybeAnyValue(s: Symbol) = isPrimitiveValueClass(unboxedValueClass(s)) || isMaybeValue(s) + // used to short-circuit unrelatedTypes check if both sides are special + def isSpecial(s: Symbol) = isMaybeAnyValue(s) || isAnyNumber(s) + // unused def possibleNumericCount = onSyms(_ filter (x => isNumeric(x) || isMaybeValue(x)) size) val nullCount = onSyms(_ filter (_ == NullClass) size) @@ -1155,7 +1163,7 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R if (isCaseEquals) { def thisCase = receiver.info.member(nme.equals_).owner actual.info.baseClasses.find(_.isCase) match { - case Some(p) if (p != thisCase) => nonSensible("case class ", false) + case Some(p) if p != thisCase => nonSensible("case class ", false) case None => // stronger message on (Some(1) == None) //if (receiver.isCase && receiver.isEffectivelyFinal && !(receiver isSubClass actual)) nonSensiblyNeq() diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index b5e58efaff..349bd1912b 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -114,6 +114,8 @@ trait Typers extends Modes with Adaptations with Taggings { case MethodType(params, _) => val argResultsBuff = new ListBuffer[SearchResult]() val argBuff = new ListBuffer[Tree]() + // paramFailed cannot be initialized with params.exists(_.tpe.isError) because that would + // hide some valid errors for params preceding the erroneous one. var paramFailed = false def mkPositionalArg(argTree: Tree, paramName: Name) = argTree @@ -129,7 +131,7 @@ trait Typers extends Modes with Adaptations with Taggings { for(ar <- argResultsBuff) paramTp = paramTp.subst(ar.subst.from, ar.subst.to) - val res = if (paramFailed) SearchFailure else inferImplicit(fun, paramTp, context.reportErrors, false, context) + val res = if (paramFailed || (paramTp.isError && {paramFailed = true; true})) SearchFailure else inferImplicit(fun, paramTp, context.reportErrors, false, context) argResultsBuff += res if (res != SearchFailure) { @@ -2325,7 +2327,7 @@ trait Typers extends Modes with Adaptations with Taggings { val methodBodyTyper = newTyper(context.makeNewScope(context.tree, methodSym)) // should use the DefDef for the context's tree, but it doesn't exist yet (we need the typer we're creating to create it) paramSyms foreach (methodBodyTyper.context.scope enter _) - val match_ = methodBodyTyper.typedMatch(selector, cases, mode, ptRes) + val match_ = methodBodyTyper.typedMatch(gen.mkUnchecked(selector), cases, mode, ptRes) val resTp = match_.tpe val methFormals = paramSyms map (_.tpe) @@ -2365,7 +2367,7 @@ trait Typers extends Modes with Adaptations with Taggings { val methodBodyTyper = newTyper(context.makeNewScope(context.tree, methodSym)) // should use the DefDef for the context's tree, but it doesn't exist yet (we need the typer we're creating to create it) paramSyms foreach (methodBodyTyper.context.scope enter _) - val match_ = methodBodyTyper.typedMatch(selector, cases, mode, ptRes) + val match_ = methodBodyTyper.typedMatch(gen.mkUnchecked(selector), cases, mode, ptRes) val resTp = match_.tpe anonClass setInfo ClassInfoType(parentsPartial(List(argTp, resTp)), newScope, anonClass) @@ -2392,7 +2394,7 @@ trait Typers extends Modes with Adaptations with Taggings { paramSyms foreach (methodBodyTyper.context.scope enter _) methodSym setInfoAndEnter MethodType(paramSyms, BooleanClass.tpe) - val match_ = methodBodyTyper.typedMatch(selector, casesTrue, mode, BooleanClass.tpe) + val match_ = methodBodyTyper.typedMatch(gen.mkUnchecked(selector), casesTrue, mode, BooleanClass.tpe) val body = methodBodyTyper.virtualizedMatch(match_ withAttachment DefaultOverrideMatchAttachment(FALSE_typed), mode, BooleanClass.tpe) DefDef(methodSym, body) @@ -2508,10 +2510,7 @@ trait Typers extends Modes with Adaptations with Taggings { namer.enterSyms(stats) // need to delay rest of typedRefinement to avoid cyclic reference errors unit.toCheck += { () => - // go to next outer context which is not silent, see #3614 - var c = context - while (c.bufferErrors) c = c.outer - val stats1 = newTyper(c).typedStats(stats, NoSymbol) + val stats1 = typedStats(stats, NoSymbol) for (stat <- stats1 if stat.isDef) { val member = stat.symbol if (!(context.owner.ancestors forall @@ -3246,7 +3245,9 @@ trait Typers extends Modes with Adaptations with Taggings { reportAnnotationError(NestedAnnotationError(ann, annType)) } else { val typedAnn = if (selfsym == NoSymbol) { - typed(ann, mode, annClass.tpe) + // local dummy fixes SI-5544 + val localTyper = newTyper(context.make(ann, context.owner.newLocalDummy(ann.pos))) + localTyper.typed(ann, mode, annClass.tpe) } else { // Since a selfsym is supplied, the annotation should have // an extra "self" identifier in scope for type checking. diff --git a/src/library/scala/collection/mutable/FlatHashTable.scala b/src/library/scala/collection/mutable/FlatHashTable.scala index ee6d4d1d22..4070174902 100644 --- a/src/library/scala/collection/mutable/FlatHashTable.scala +++ b/src/library/scala/collection/mutable/FlatHashTable.scala @@ -50,6 +50,10 @@ trait FlatHashTable[A] extends FlatHashTable.HashUtils[A] { protected def capacity(expectedSize: Int) = if (expectedSize == 0) 1 else powerOfTwo(expectedSize) + /** The initial size of the hash table. + */ + def initialSize: Int = 32 + private def initialCapacity = capacity(initialSize) protected def randomSeed = seedGenerator.get.nextInt() @@ -361,10 +365,6 @@ private[collection] object FlatHashTable { def defaultLoadFactor: Int = 450 final def loadFactorDenum = 1000 - /** The initial size of the hash table. - */ - def initialSize: Int = 32 - def sizeForThreshold(size: Int, _loadFactor: Int) = math.max(32, (size.toLong * loadFactorDenum / _loadFactor).toInt) def newThreshold(_loadFactor: Int, size: Int) = { diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala index a2f78dbde9..c307e6dcab 100644 --- a/src/library/scala/collection/mutable/HashTable.scala +++ b/src/library/scala/collection/mutable/HashTable.scala @@ -56,7 +56,15 @@ trait HashTable[A, Entry >: Null <: HashEntry[A, Entry]] extends HashTable.HashU protected def tableSizeSeed = Integer.bitCount(table.length - 1) - protected def initialSize: Int = HashTable.initialSize + /** The initial size of the hash table. + */ + protected def initialSize: Int = 16 + + /** The initial threshold. + */ + private def initialThreshold(_loadFactor: Int): Int = newThreshold(_loadFactor, initialCapacity) + + private def initialCapacity = capacity(initialSize) private def lastPopulatedIndex = { var idx = table.length - 1 @@ -354,16 +362,6 @@ private[collection] object HashTable { private[collection] final def defaultLoadFactor: Int = 750 // corresponds to 75% private[collection] final def loadFactorDenum = 1000; - /** The initial size of the hash table. - */ - private[collection] final def initialSize: Int = 16 - - /** The initial threshold. - */ - private[collection] final def initialThreshold(_loadFactor: Int): Int = newThreshold(_loadFactor, initialCapacity) - - private[collection] final def initialCapacity = capacity(initialSize) - private[collection] final def newThreshold(_loadFactor: Int, size: Int) = ((size.toLong * _loadFactor) / loadFactorDenum).toInt private[collection] final def sizeForThreshold(_loadFactor: Int, thr: Int) = ((thr.toLong * loadFactorDenum) / _loadFactor).toInt diff --git a/src/library/scala/collection/mutable/ObservableBuffer.scala b/src/library/scala/collection/mutable/ObservableBuffer.scala index a619edf281..6b5079e402 100644 --- a/src/library/scala/collection/mutable/ObservableBuffer.scala +++ b/src/library/scala/collection/mutable/ObservableBuffer.scala @@ -70,4 +70,18 @@ trait ObservableBuffer[A] extends Buffer[A] with Publisher[Message[A] with Undoa def undo() { throw new UnsupportedOperationException("cannot undo") } }) } + + abstract override def insertAll(n: Int, elems: collection.Traversable[A]) { + super.insertAll(n, elems) + var curr = n - 1 + val msg = elems.foldLeft(new Script[A]() with Undoable { + def undo() { throw new UnsupportedOperationException("cannot undo") } + }) { + case (msg, elem) => + curr += 1 + msg += Include(Index(curr), elem) + } + publish(msg) + } + } diff --git a/src/library/scala/collection/parallel/Tasks.scala b/src/library/scala/collection/parallel/Tasks.scala index 4a581f219e..7a0116b3b3 100644 --- a/src/library/scala/collection/parallel/Tasks.scala +++ b/src/library/scala/collection/parallel/Tasks.scala @@ -541,7 +541,7 @@ trait ExecutionContextTasks extends Tasks { // this part is a hack which allows switching val driver: Tasks = executionContext match { - case eci: scala.concurrent.impl.ExecutionContextImpl => eci.executorService match { + case eci: scala.concurrent.impl.ExecutionContextImpl => eci.executor match { case fjp: ForkJoinPool => new ForkJoinTaskSupport(fjp) case tpe: ThreadPoolExecutor => new ThreadPoolTaskSupport(tpe) case _ => ??? diff --git a/src/library/scala/concurrent/ConcurrentPackageObject.scala b/src/library/scala/concurrent/ConcurrentPackageObject.scala index c3c329121c..330a2f0e25 100644 --- a/src/library/scala/concurrent/ConcurrentPackageObject.scala +++ b/src/library/scala/concurrent/ConcurrentPackageObject.scala @@ -8,7 +8,7 @@ package scala.concurrent -import java.util.concurrent.{ Executors, ExecutorService, ThreadFactory } +import java.util.concurrent.{ Executors, Executor, ThreadFactory } import scala.concurrent.forkjoin.{ ForkJoinPool, ForkJoinWorkerThread } import scala.concurrent.util.Duration import language.implicitConversions @@ -19,7 +19,7 @@ import language.implicitConversions abstract class ConcurrentPackageObject { /** A global execution environment for executing lightweight tasks. */ - lazy val defaultExecutionContext = new impl.ExecutionContextImpl(null) + lazy val defaultExecutionContext: ExecutionContext with Executor = impl.ExecutionContextImpl.fromExecutor(null: Executor) val currentExecutionContext = new ThreadLocal[ExecutionContext] diff --git a/src/library/scala/concurrent/ExecutionContext.scala b/src/library/scala/concurrent/ExecutionContext.scala index d2a2d5e8a8..436a17a33b 100644 --- a/src/library/scala/concurrent/ExecutionContext.scala +++ b/src/library/scala/concurrent/ExecutionContext.scala @@ -47,11 +47,13 @@ object ExecutionContext { /** Creates an `ExecutionContext` from the given `ExecutorService`. */ - def fromExecutorService(e: ExecutorService, reporter: Throwable => Unit = defaultReporter): ExecutionContext with Executor = new impl.ExecutionContextImpl(e, reporter) + def fromExecutorService(e: ExecutorService, reporter: Throwable => Unit = defaultReporter): ExecutionContext with ExecutorService = + impl.ExecutionContextImpl.fromExecutorService(e, reporter) /** Creates an `ExecutionContext` from the given `Executor`. */ - def fromExecutor(e: Executor, reporter: Throwable => Unit = defaultReporter): ExecutionContext with Executor = new impl.ExecutionContextImpl(e, reporter) + def fromExecutor(e: Executor, reporter: Throwable => Unit = defaultReporter): ExecutionContext with Executor = + impl.ExecutionContextImpl.fromExecutor(e, reporter) def defaultReporter: Throwable => Unit = { // re-throwing `Error`s here causes an exception handling test to fail. diff --git a/src/library/scala/concurrent/Future.scala b/src/library/scala/concurrent/Future.scala index 2f90afe056..4df2bb63af 100644 --- a/src/library/scala/concurrent/Future.scala +++ b/src/library/scala/concurrent/Future.scala @@ -96,9 +96,9 @@ trait Future[+T] extends Awaitable[T] { * * $multipleCallbacks */ - def onSuccess[U](pf: PartialFunction[T, U]): this.type = onComplete { - case Left(t) => // do nothing - case Right(v) => if (pf isDefinedAt v) pf(v) else { /*do nothing*/ } + def onSuccess[U](pf: PartialFunction[T, U]): Unit = onComplete { + case Right(v) if pf isDefinedAt v => pf(v) + case _ => } /** When this future is completed with a failure (i.e. with a throwable), @@ -113,9 +113,9 @@ trait Future[+T] extends Awaitable[T] { * * $multipleCallbacks */ - def onFailure[U](callback: PartialFunction[Throwable, U]): this.type = onComplete { - case Left(t) => if (isFutureThrowable(t) && callback.isDefinedAt(t)) callback(t) else { /*do nothing*/ } - case Right(v) => // do nothing + def onFailure[U](callback: PartialFunction[Throwable, U]): Unit = onComplete { + case Left(t) if (isFutureThrowable(t) && callback.isDefinedAt(t)) => callback(t) + case _ => } /** When this future is completed, either through an exception, or a value, @@ -126,7 +126,7 @@ trait Future[+T] extends Awaitable[T] { * * $multipleCallbacks */ - def onComplete[U](func: Either[Throwable, T] => U): this.type + def onComplete[U](func: Either[Throwable, T] => U): Unit /* Miscellaneous */ @@ -169,7 +169,7 @@ trait Future[+T] extends Awaitable[T] { onComplete { case Left(t) => p success t - case Right(v) => p failure (new NoSuchElementException("Future.failed not completed with a throwable. Instead completed with: " + v)) + case Right(v) => p failure (new NoSuchElementException("Future.failed not completed with a throwable.")) } p.future @@ -184,7 +184,36 @@ trait Future[+T] extends Awaitable[T] { */ def foreach[U](f: T => U): Unit = onComplete { case Right(r) => f(r) - case Left(_) => // do nothing + case _ => // do nothing + } + + /** Creates a new future by applying the 's' function to the successful result of + * this future, or the 'f' function to the failed result. If there is any non-fatal + * exception thrown when 's' or 'f' is applied, that exception will be propagated + * to the resulting future. + * + * @param s function that transforms a successful result of the receiver into a + * successful result of the returned future + * @param f function that transforms a failure of the receiver into a failure of + * the returned future + * @return a future that will be completed with the transformed value + */ + def transform[S](s: T => S, f: Throwable => Throwable): Future[S] = { + val p = Promise[S]() + + onComplete { + case result => + try { + result match { + case Left(t) => p failure f(t) + case Right(r) => p success s(r) + } + } catch { + case NonFatal(t) => p failure t + } + } + + p.future } /** Creates a new future by applying a function to the successful result of @@ -193,14 +222,17 @@ trait Future[+T] extends Awaitable[T] { * * $forComprehensionExamples */ - def map[S](f: T => S): Future[S] = { + def map[S](f: T => S): Future[S] = { // transform(f, identity) val p = Promise[S]() onComplete { - case Left(t) => p failure t - case Right(v) => - try p success f(v) - catch { + case result => + try { + result match { + case Right(r) => p success f(r) + case l: Left[_, _] => p complete l.asInstanceOf[Left[Throwable, S]] + } + } catch { case NonFatal(t) => p failure t } } @@ -219,11 +251,11 @@ trait Future[+T] extends Awaitable[T] { val p = Promise[S]() onComplete { - case Left(t) => p failure t + case l: Left[_, _] => p complete l.asInstanceOf[Left[Throwable, S]] case Right(v) => try { f(v) onComplete { - case Left(t) => p failure t + case l: Left[_, _] => p complete l.asInstanceOf[Left[Throwable, S]] case Right(v) => p success v } } catch { @@ -254,7 +286,7 @@ trait Future[+T] extends Awaitable[T] { val p = Promise[T]() onComplete { - case Left(t) => p failure t + case l: Left[_, _] => p complete l.asInstanceOf[Left[Throwable, T]] case Right(v) => try { if (pred(v)) p success v @@ -303,7 +335,7 @@ trait Future[+T] extends Awaitable[T] { val p = Promise[S]() onComplete { - case Left(t) => p failure t + case l: Left[_, _] => p complete l.asInstanceOf[Left[Throwable, S]] case Right(v) => try { if (pf.isDefinedAt(v)) p success pf(v) @@ -384,7 +416,7 @@ trait Future[+T] extends Awaitable[T] { val p = Promise[(T, U)]() this onComplete { - case Left(t) => p failure t + case l: Left[_, _] => p complete l.asInstanceOf[Left[Throwable, (T, U)]] case Right(r) => that onSuccess { case r2 => p success ((r, r2)) @@ -431,7 +463,7 @@ trait Future[+T] extends Awaitable[T] { val p = Promise[S]() onComplete { - case l: Left[Throwable, _] => p complete l.asInstanceOf[Either[Throwable, S]] + case l: Left[_, _] => p complete l.asInstanceOf[Left[Throwable, S]] case Right(t) => p complete (try { Right(boxedType(tag.erasure).cast(t).asInstanceOf[S]) @@ -470,9 +502,7 @@ trait Future[+T] extends Awaitable[T] { val p = Promise[T]() onComplete { - case r => - try if (pf isDefinedAt r) pf(r) - finally p complete r + case r => try if (pf isDefinedAt r) pf(r) finally p complete r } p.future @@ -493,11 +523,7 @@ trait Future[+T] extends Awaitable[T] { */ def either[U >: T](that: Future[U]): Future[U] = { val p = Promise[U]() - - val completePromise: PartialFunction[Either[Throwable, U], _] = { - case Left(t) => p tryFailure t - case Right(v) => p trySuccess v - } + val completePromise: PartialFunction[Either[Throwable, U], _] = { case result => p tryComplete result } this onComplete completePromise that onComplete completePromise diff --git a/src/library/scala/concurrent/impl/ExecutionContextImpl.scala b/src/library/scala/concurrent/impl/ExecutionContextImpl.scala index 3ed960c7ab..1083a93439 100644 --- a/src/library/scala/concurrent/impl/ExecutionContextImpl.scala +++ b/src/library/scala/concurrent/impl/ExecutionContextImpl.scala @@ -10,18 +10,20 @@ package scala.concurrent.impl -import java.util.concurrent.{ Callable, Executor, ExecutorService, Executors, ThreadFactory } +import java.util.concurrent.{ Callable, Executor, ExecutorService, Executors, ThreadFactory, TimeUnit } +import java.util.Collection import scala.concurrent.forkjoin._ import scala.concurrent.{ ExecutionContext, Awaitable } import scala.concurrent.util.Duration -private[scala] class ExecutionContextImpl(es: AnyRef, reporter: Throwable => Unit = ExecutionContext.defaultReporter) -extends ExecutionContext with Executor { - import ExecutionContextImpl._ - - val executorService: AnyRef = if (es eq null) getExecutorService else es +private[scala] class ExecutionContextImpl private[impl] (es: Executor, reporter: Throwable => Unit) extends ExecutionContext with Executor { + + val executor: Executor = es match { + case null => createExecutorService + case some => some + } // to ensure that the current execution context thread local is properly set def executorsThreadFactory = new ThreadFactory { @@ -42,51 +44,46 @@ extends ExecutionContext with Executor { } } - def getExecutorService: AnyRef = - if (scala.util.Properties.isJavaAtLeast("1.6")) { - val vendor = scala.util.Properties.javaVmVendor - if ((vendor contains "Oracle") || (vendor contains "Sun") || (vendor contains "Apple")) - new ForkJoinPool( - Runtime.getRuntime.availableProcessors(), + def createExecutorService: ExecutorService = try { new ForkJoinPool( + Runtime.getRuntime.availableProcessors(), //FIXME from config forkJoinPoolThreadFactory, - null, - false) - else - Executors.newCachedThreadPool(executorsThreadFactory) - } else Executors.newCachedThreadPool(executorsThreadFactory) + null, //FIXME we should have an UncaughtExceptionHandler, see what Akka does + true) //FIXME I really think this should be async... + } catch { + case NonFatal(t) => + System.err.println("Failed to create ForkJoinPool for the default ExecutionContext, falling back to Executors.newCachedThreadPool") + t.printStackTrace(System.err) + Executors.newCachedThreadPool(executorsThreadFactory) + } - def execute(runnable: Runnable): Unit = executorService match { + def execute(runnable: Runnable): Unit = executor match { case fj: ForkJoinPool => Thread.currentThread match { case fjw: ForkJoinWorkerThread if fjw.getPool eq fj => - val fjtask = runnable match { + (runnable match { case fjt: ForkJoinTask[_] => fjt case _ => ForkJoinTask.adapt(runnable) - } - fjtask.fork - case _ => - fj.execute(runnable) + }).fork + case _ => fj.execute(runnable) } - case executor: Executor => - executor execute runnable + case generic => generic execute runnable } def internalBlockingCall[T](awaitable: Awaitable[T], atMost: Duration): T = { Future.releaseStack(this) - executorService match { + executor match { case fj: ForkJoinPool => var result: T = null.asInstanceOf[T] - val managedBlocker = new ForkJoinPool.ManagedBlocker { + ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker { @volatile var isdone = false - def block() = { - result = awaitable.result(atMost)(scala.concurrent.Await.canAwaitEvidence) + def block(): Boolean = { + result = awaitable.result(atMost)(scala.concurrent.Await.canAwaitEvidence) // FIXME what happens if there's an exception thrown here? isdone = true true } def isReleasable = isdone - } - ForkJoinPool.managedBlock(managedBlocker) + }) result case _ => awaitable.result(atMost)(scala.concurrent.Await.canAwaitEvidence) @@ -94,12 +91,29 @@ extends ExecutionContext with Executor { } def reportFailure(t: Throwable) = reporter(t) - } private[concurrent] object ExecutionContextImpl { - + + def fromExecutor(e: Executor, reporter: Throwable => Unit = ExecutionContext.defaultReporter): ExecutionContextImpl = new ExecutionContextImpl(e, reporter) + def fromExecutorService(es: ExecutorService, reporter: Throwable => Unit = ExecutionContext.defaultReporter): ExecutionContextImpl with ExecutorService = + new ExecutionContextImpl(es, reporter) with ExecutorService { + final def asExecutorService: ExecutorService = executor.asInstanceOf[ExecutorService] + override def execute(command: Runnable) = executor.execute(command) + override def shutdown() { asExecutorService.shutdown() } + override def shutdownNow() = asExecutorService.shutdownNow() + override def isShutdown = asExecutorService.isShutdown + override def isTerminated = asExecutorService.isTerminated + override def awaitTermination(l: Long, timeUnit: TimeUnit) = asExecutorService.awaitTermination(l, timeUnit) + override def submit[T](callable: Callable[T]) = asExecutorService.submit(callable) + override def submit[T](runnable: Runnable, t: T) = asExecutorService.submit(runnable, t) + override def submit(runnable: Runnable) = asExecutorService.submit(runnable) + override def invokeAll[T](callables: Collection[_ <: Callable[T]]) = asExecutorService.invokeAll(callables) + override def invokeAll[T](callables: Collection[_ <: Callable[T]], l: Long, timeUnit: TimeUnit) = asExecutorService.invokeAll(callables, l, timeUnit) + override def invokeAny[T](callables: Collection[_ <: Callable[T]]) = asExecutorService.invokeAny(callables) + override def invokeAny[T](callables: Collection[_ <: Callable[T]], l: Long, timeUnit: TimeUnit) = asExecutorService.invokeAny(callables, l, timeUnit) + } } diff --git a/src/library/scala/concurrent/impl/Future.scala b/src/library/scala/concurrent/impl/Future.scala index bf136b6195..a54e81bd05 100644 --- a/src/library/scala/concurrent/impl/Future.scala +++ b/src/library/scala/concurrent/impl/Future.scala @@ -65,6 +65,8 @@ private[concurrent] object Future { promise.future } + private[impl] val throwableId: Throwable => Throwable = identity _ + // an optimization for batching futures // TODO we should replace this with a public queue, // so that it can be stolen from diff --git a/src/library/scala/concurrent/impl/Promise.scala b/src/library/scala/concurrent/impl/Promise.scala index 5a5b893f16..78053f5117 100644 --- a/src/library/scala/concurrent/impl/Promise.scala +++ b/src/library/scala/concurrent/impl/Promise.scala @@ -112,7 +112,7 @@ object Promise { } } - def onComplete[U](func: Either[Throwable, T] => U): this.type = { + def onComplete[U](func: Either[Throwable, T] => U): Unit = { @tailrec //Tries to add the callback, if already completed, it dispatches the callback to be executed def dispatchOrAddCallback(): Unit = getState match { @@ -120,7 +120,6 @@ object Promise { case listeners: List[_] => if (updateState(listeners, func :: listeners)) () else dispatchOrAddCallback() } dispatchOrAddCallback() - this } private final def notifyCompleted(func: Either[Throwable, T] => Any, result: Either[Throwable, T]) { @@ -144,10 +143,9 @@ object Promise { def tryComplete(value: Either[Throwable, T]): Boolean = false - def onComplete[U](func: Either[Throwable, T] => U): this.type = { - val completedAs = value.get + def onComplete[U](func: Either[Throwable, T] => U): Unit = { + val completedAs = value.get // Avoid closing over "this" Future.dispatchFuture(executor, () => func(completedAs)) - this } def ready(atMost: Duration)(implicit permit: CanAwait): this.type = this diff --git a/src/library/scala/util/parsing/combinator/JavaTokenParsers.scala b/src/library/scala/util/parsing/combinator/JavaTokenParsers.scala index bc71391bdb..06567ea348 100644 --- a/src/library/scala/util/parsing/combinator/JavaTokenParsers.scala +++ b/src/library/scala/util/parsing/combinator/JavaTokenParsers.scala @@ -9,6 +9,8 @@ package scala.util.parsing.combinator +import annotation.migration + /** `JavaTokenParsers` differs from [[scala.util.parsing.combinator.RegexParsers]] * by adding the following definitions: * @@ -39,12 +41,13 @@ trait JavaTokenParsers extends RegexParsers { /** Double quotes (`"`) enclosing a sequence of: * * - Any character except double quotes, control characters or backslash (`\`) - * - A backslash followed by a slash, another backslash, or one of the letters - * `b`, `f`, `n`, `r` or `t`. + * - A backslash followed by another backslash, a single or double quote, or one + * of the letters `b`, `f`, `n`, `r` or `t` * - `\` followed by `u` followed by four hexadecimal digits */ + @migration("`stringLiteral` allows escaping single and double quotes, but not forward slashes any longer.", "2.10.0") def stringLiteral: Parser[String] = - ("\""+"""([^"\p{Cntrl}\\]|\\[\\/bfnrt]|\\u[a-fA-F0-9]{4})*"""+"\"").r + ("\""+"""([^"\p{Cntrl}\\]|\\[\\'"bfnrt]|\\u[a-fA-F0-9]{4})*"""+"\"").r /** A number following the rules of `decimalNumber`, with the following * optional additions: * diff --git a/test/files/jvm/future-spec/FutureTests.scala b/test/files/jvm/future-spec/FutureTests.scala index 9a9cf951bb..e5e01a5954 100644 --- a/test/files/jvm/future-spec/FutureTests.scala +++ b/test/files/jvm/future-spec/FutureTests.scala @@ -19,7 +19,7 @@ object FutureTests extends MinimalScalaTest { case "NoReply" => Promise[String]().future } - val defaultTimeout = Inf + val defaultTimeout = 5 seconds /* future specification */ diff --git a/test/files/jvm/future-spec/PromiseTests.scala b/test/files/jvm/future-spec/PromiseTests.scala index 6016746a23..bf9d9b39d7 100644 --- a/test/files/jvm/future-spec/PromiseTests.scala +++ b/test/files/jvm/future-spec/PromiseTests.scala @@ -133,7 +133,7 @@ object PromiseTests extends MinimalScalaTest { (future, result) => intercept[NoSuchElementException] { Await.result(future.failed, defaultTimeout) - }.getMessage mustBe ("Future.failed not completed with a throwable. Instead completed with: " + result) + }.getMessage mustBe ("Future.failed not completed with a throwable.") } } diff --git a/test/files/jvm/scala-concurrent-tck.scala b/test/files/jvm/scala-concurrent-tck.scala index fce1a25bb6..86655ad89c 100644 --- a/test/files/jvm/scala-concurrent-tck.scala +++ b/test/files/jvm/scala-concurrent-tck.scala @@ -182,6 +182,72 @@ trait FutureCombinators extends TestBase { } } + def testMapSuccessPF(): Unit = once { + done => + val f = future { 5 } + val g = f map { case r => "result: " + r } + g onSuccess { + case s => + done() + assert(s == "result: 5") + } + g onFailure { + case _ => + done() + assert(false) + } + } + + def testTransformSuccess(): Unit = once { + done => + val f = future { 5 } + val g = f.transform(r => "result: " + r, identity) + g onSuccess { + case s => + done() + assert(s == "result: 5") + } + g onFailure { + case _ => + done() + assert(false) + } + } + + def testTransformSuccessPF(): Unit = once { + done => + val f = future { 5 } + val g = f.transform( { case r => "result: " + r }, identity) + g onSuccess { + case s => + done() + assert(s == "result: 5") + } + g onFailure { + case _ => + done() + assert(false) + } + } + + def testFoldFailure(): Unit = once { + done => + val f = future { + throw new Exception("exception message") + } + val g = f.transform(r => "result: " + r, identity) + g onSuccess { + case _ => + done() + assert(false) + } + g onFailure { + case t => + done() + assert(t.getMessage() == "exception message") + } + } + def testFlatMapSuccess(): Unit = once { done => val f = future { 5 } @@ -340,14 +406,17 @@ trait FutureCombinators extends TestBase { } recover { case re: RuntimeException => "recovered" - } onSuccess { + } + f onSuccess { case x => done() assert(x == "recovered") - } onFailure { case any => + } + f onFailure { case any => done() assert(false) } + f } def testRecoverFailure(): Unit = once { @@ -357,11 +426,13 @@ trait FutureCombinators extends TestBase { throw cause } recover { case te: TimeoutException => "timeout" - } onSuccess { + } + f onSuccess { case x => done() assert(false) - } onFailure { case any => + } + f onFailure { case any => done() assert(any == cause) } @@ -375,11 +446,13 @@ trait FutureCombinators extends TestBase { } recoverWith { case re: RuntimeException => future { "recovered" } - } onSuccess { + } + f onSuccess { case x => done() assert(x == "recovered") - } onFailure { case any => + } + f onFailure { case any => done() assert(false) } @@ -393,11 +466,13 @@ trait FutureCombinators extends TestBase { } recoverWith { case te: TimeoutException => future { "timeout" } - } onSuccess { + } + f onSuccess { case x => done() assert(false) - } onFailure { case any => + } + f onFailure { case any => done() assert(any == cause) } @@ -635,11 +710,12 @@ trait Promises extends TestBase { val p = promise[Int]() val f = p.future - f.onSuccess { + f onSuccess { case x => done() assert(x == 5) - } onFailure { + } + f onFailure { case any => done() assert(false) diff --git a/test/files/neg/exhausting.check b/test/files/neg/exhausting.check index 0bef21e077..7140b99428 100644 --- a/test/files/neg/exhausting.check +++ b/test/files/neg/exhausting.check @@ -1,29 +1,25 @@ -exhausting.scala:20: error: match is not exhaustive! -missing combination * Nil - +exhausting.scala:21: error: match may not be exhaustive. +It would fail on the following input: List(_, _, _) def fail1[T](xs: List[T]) = xs match { ^ -exhausting.scala:24: error: match is not exhaustive! -missing combination Nil - +exhausting.scala:27: error: match may not be exhaustive. +It would fail on the following input: Nil def fail2[T](xs: List[T]) = xs match { ^ -exhausting.scala:27: error: match is not exhaustive! -missing combination Bar3 - +exhausting.scala:32: error: match may not be exhaustive. +It would fail on the following input: List(<not in (1, 2)>) + def fail3a(xs: List[Int]) = xs match { + ^ +exhausting.scala:39: error: match may not be exhaustive. +It would fail on the following input: Bar3 def fail3[T](x: Foo[T]) = x match { ^ -exhausting.scala:31: error: match is not exhaustive! -missing combination Bar2 Bar2 - +exhausting.scala:47: error: match may not be exhaustive. +It would fail on the following inputs: (Bar1, Bar2), (Bar1, Bar3), (Bar2, Bar1), (Bar2, Bar2) def fail4[T <: AnyRef](xx: (Foo[T], Foo[T])) = xx match { ^ -exhausting.scala:36: error: match is not exhaustive! -missing combination Bar1 Bar2 -missing combination Bar1 Bar3 -missing combination Bar2 Bar1 -missing combination Bar2 Bar2 - +exhausting.scala:56: error: match may not be exhaustive. +It would fail on the following inputs: (Bar1, Bar2), (Bar1, Bar3), (Bar2, Bar1), (Bar2, Bar2) def fail5[T](xx: (Foo[T], Foo[T])) = xx match { ^ -5 errors found +6 errors found diff --git a/test/files/neg/exhausting.flags b/test/files/neg/exhausting.flags index b7eb21d5f5..85d8eb2ba2 100644 --- a/test/files/neg/exhausting.flags +++ b/test/files/neg/exhausting.flags @@ -1 +1 @@ --Xfatal-warnings -Xoldpatmat +-Xfatal-warnings diff --git a/test/files/neg/exhausting.scala b/test/files/neg/exhausting.scala index 14b05695aa..5554ee2671 100644 --- a/test/files/neg/exhausting.scala +++ b/test/files/neg/exhausting.scala @@ -16,30 +16,46 @@ object Test { def ex3[T](xx: (Foo[T], Foo[T])) = xx match { case (_: Foo[_], _: Foo[_]) => () } - + + // fails for: ::(_, ::(_, ::(_, _))) def fail1[T](xs: List[T]) = xs match { case Nil => "ok" case x :: y :: Nil => "ok" } + + // fails for: Nil def fail2[T](xs: List[T]) = xs match { case _ :: _ => "ok" } + + // fails for: ::(<not in (2, 1)>, _) + def fail3a(xs: List[Int]) = xs match { + case 1 :: _ => + case 2 :: _ => + case Nil => + } + + // fails for: Bar3 def fail3[T](x: Foo[T]) = x match { case Bar1 => "ok" case Bar2 => "ok" } + // fails for: (Bar1, Bar2) + // fails for: (Bar1, Bar3) + // fails for: (Bar2, Bar2) + // fails for: (Bar2, Bar1) def fail4[T <: AnyRef](xx: (Foo[T], Foo[T])) = xx match { case (Bar1, Bar1) => () case (Bar2, Bar3) => () case (Bar3, _) => () } + // fails for: (Bar1, Bar2) + // fails for: (Bar1, Bar3) + // fails for: (Bar2, Bar1) + // fails for: (Bar2, Bar2) def fail5[T](xx: (Foo[T], Foo[T])) = xx match { case (Bar1, Bar1) => () case (Bar2, Bar3) => () case (Bar3, _) => () } - - def main(args: Array[String]): Unit = { - - } } diff --git a/test/files/neg/names-defaults-neg.check b/test/files/neg/names-defaults-neg.check index b5f544c97e..2809350855 100644 --- a/test/files/neg/names-defaults-neg.check +++ b/test/files/neg/names-defaults-neg.check @@ -149,8 +149,12 @@ names-defaults-neg.scala:170: error: reference to x is ambiguous; it is both a m names-defaults-neg.scala:177: error: variable definition needs type because 'x' is used as a named argument in its body. class u15 { var x = u.f(x = 1) } ^ +names-defaults-neg.scala:177: warning: type-checking the invocation of method f checks if the named argument expression 'x = ...' is a valid assignment +in the current scope. The resulting type inference error (see above) can be fixed by providing an explicit type in the local definition for x. + class u15 { var x = u.f(x = 1) } + ^ names-defaults-neg.scala:180: error: reference to x is ambiguous; it is both a method parameter and a variable in scope. class u18 { var x: Int = u.f(x = 1) } ^ -one warning found +two warnings found 41 errors found diff --git a/test/files/neg/pat_unreachable.flags b/test/files/neg/pat_unreachable.flags index ba80cad69b..cb8324a345 100644 --- a/test/files/neg/pat_unreachable.flags +++ b/test/files/neg/pat_unreachable.flags @@ -1 +1 @@ --Xoldpatmat +-Xoldpatmat
\ No newline at end of file diff --git a/test/files/neg/patmatexhaust.check b/test/files/neg/patmatexhaust.check index 5426d61d31..6172811e13 100644 --- a/test/files/neg/patmatexhaust.check +++ b/test/files/neg/patmatexhaust.check @@ -1,54 +1,41 @@ -patmatexhaust.scala:7: error: match is not exhaustive! -missing combination Baz - +patmatexhaust.scala:7: error: match may not be exhaustive. +It would fail on the following input: Baz def ma1(x:Foo) = x match { ^ -patmatexhaust.scala:11: error: match is not exhaustive! -missing combination Bar - +patmatexhaust.scala:11: error: match may not be exhaustive. +It would fail on the following input: Bar(_) def ma2(x:Foo) = x match { ^ -patmatexhaust.scala:23: error: match is not exhaustive! -missing combination Kult Kult -missing combination Qult Qult - +patmatexhaust.scala:23: error: match may not be exhaustive. +It would fail on the following inputs: (Kult(_), Kult(_)), (Qult(), Qult()) def ma3(x:Mult) = (x,x) match { // not exhaustive ^ -patmatexhaust.scala:49: error: match is not exhaustive! -missing combination Gp -missing combination Gu - +patmatexhaust.scala:49: error: match may not be exhaustive. +It would fail on the following inputs: Gp(), Gu def ma4(x:Deep) = x match { // missing cases: Gu, Gp ^ -patmatexhaust.scala:53: error: match is not exhaustive! -missing combination Gp - - def ma5(x:Deep) = x match { // Gp +patmatexhaust.scala:53: error: match may not be exhaustive. +It would fail on the following input: Gp() + def ma5(x:Deep) = x match { ^ -patmatexhaust.scala:59: error: match is not exhaustive! -missing combination Nil - +patmatexhaust.scala:59: error: match may not be exhaustive. +It would fail on the following input: Nil def ma6() = List(1,2) match { // give up ^ -patmatexhaust.scala:75: error: match is not exhaustive! -missing combination B - +patmatexhaust.scala:75: error: match may not be exhaustive. +It would fail on the following input: B() def ma9(x: B) = x match { ^ -patmatexhaust.scala:100: error: match is not exhaustive! -missing combination C1 - +patmatexhaust.scala:100: error: match may not be exhaustive. +It would fail on the following input: C1() def ma10(x: C) = x match { // not exhaustive: C1 is not sealed. ^ -patmatexhaust.scala:114: error: match is not exhaustive! -missing combination D1 -missing combination D2 - +patmatexhaust.scala:114: error: match may not be exhaustive. +It would fail on the following inputs: D1, D2() def ma10(x: C) = x match { // not exhaustive: C1 has subclasses. ^ -patmatexhaust.scala:126: error: match is not exhaustive! -missing combination C1 - +patmatexhaust.scala:126: error: match may not be exhaustive. +It would fail on the following input: C1() def ma10(x: C) = x match { // not exhaustive: C1 is not abstract. ^ 10 errors found diff --git a/test/files/neg/patmatexhaust.flags b/test/files/neg/patmatexhaust.flags index b7eb21d5f5..85d8eb2ba2 100644 --- a/test/files/neg/patmatexhaust.flags +++ b/test/files/neg/patmatexhaust.flags @@ -1 +1 @@ --Xfatal-warnings -Xoldpatmat +-Xfatal-warnings diff --git a/test/files/neg/patmatexhaust.scala b/test/files/neg/patmatexhaust.scala index 9297e09d0d..ceb960ee97 100644 --- a/test/files/neg/patmatexhaust.scala +++ b/test/files/neg/patmatexhaust.scala @@ -50,7 +50,7 @@ class TestSealedExhaustive { // compile only case Ga => } - def ma5(x:Deep) = x match { // Gp + def ma5(x:Deep) = x match { case Gu => case _ if 1 == 0 => case Ga => diff --git a/test/files/neg/sealed-java-enums.check b/test/files/neg/sealed-java-enums.check index 9303c2df9c..20d00c8e91 100644 --- a/test/files/neg/sealed-java-enums.check +++ b/test/files/neg/sealed-java-enums.check @@ -1,9 +1,5 @@ -sealed-java-enums.scala:5: error: match is not exhaustive! -missing combination BLOCKED -missing combination State -missing combination TERMINATED -missing combination TIMED_WAITING - +sealed-java-enums.scala:5: error: match may not be exhaustive. +It would fail on the following inputs: BLOCKED, TERMINATED, TIMED_WAITING def f(state: State) = state match { ^ one error found diff --git a/test/files/neg/sealed-java-enums.flags b/test/files/neg/sealed-java-enums.flags index 312f3a87ec..e709c65918 100644 --- a/test/files/neg/sealed-java-enums.flags +++ b/test/files/neg/sealed-java-enums.flags @@ -1 +1 @@ --Xexperimental -Xfatal-warnings -Xoldpatmat +-Xexperimental -Xfatal-warnings diff --git a/test/files/neg/switch.check b/test/files/neg/switch.check index 7212c1a22b..8955c94b32 100644 --- a/test/files/neg/switch.check +++ b/test/files/neg/switch.check @@ -1,10 +1,10 @@ switch.scala:28: error: could not emit switch for @switch annotated match def fail1(c: Char) = (c: @switch) match { - ^ + ^ switch.scala:38: error: could not emit switch for @switch annotated match def fail2(c: Char) = (c: @switch @unchecked) match { - ^ + ^ switch.scala:45: error: could not emit switch for @switch annotated match def fail3(c: Char) = (c: @unchecked @switch) match { - ^ + ^ three errors found diff --git a/test/files/neg/switch.flags b/test/files/neg/switch.flags deleted file mode 100644 index 809e9ff2f2..0000000000 --- a/test/files/neg/switch.flags +++ /dev/null @@ -1 +0,0 @@ - -Xoldpatmat diff --git a/test/files/neg/t2405.check b/test/files/neg/t2405.check new file mode 100644 index 0000000000..78360bcc21 --- /dev/null +++ b/test/files/neg/t2405.check @@ -0,0 +1,8 @@ +t2405.scala:6: warning: imported `y' is permanently hidden by definition of method y + import A.{x => y} + ^ +t2405.scala:8: error: could not find implicit value for parameter e: Int + implicitly[Int] + ^ +one warning found +one error found diff --git a/test/files/neg/t2405.scala b/test/files/neg/t2405.scala new file mode 100644 index 0000000000..6982285b98 --- /dev/null +++ b/test/files/neg/t2405.scala @@ -0,0 +1,10 @@ +object A { implicit val x: Int = 1 } + +// Expecting shadowing #1 +object Test2 { + { + import A.{x => y} + def y: Int = 0 + implicitly[Int] + } +} diff --git a/test/files/neg/t2488.check b/test/files/neg/t2488.check new file mode 100644 index 0000000000..170dbf88ff --- /dev/null +++ b/test/files/neg/t2488.check @@ -0,0 +1,31 @@ +t2488.scala:7: error: overloaded method value f with alternatives: + ()Int <and> + (a: Int,b: Int)Int + cannot be applied to (b: Int, Int) + println(c.f(b = 2, 2)) + ^ +t2488.scala:8: error: overloaded method value f with alternatives: + ()Int <and> + (a: Int,b: Int)Int + cannot be applied to (a: Int, c: Int) + println(c.f(a = 2, c = 2)) + ^ +t2488.scala:9: error: overloaded method value f with alternatives: + ()Int <and> + (a: Int,b: Int)Int + cannot be applied to (Int, c: Int) + println(c.f(2, c = 2)) + ^ +t2488.scala:10: error: overloaded method value f with alternatives: + ()Int <and> + (a: Int,b: Int)Int + cannot be applied to (c: Int, Int) + println(c.f(c = 2, 2)) + ^ +t2488.scala:11: error: overloaded method value f with alternatives: + ()Int <and> + (a: Int,b: Int)Int + cannot be applied to (Int) + println(c.f(2)) + ^ +5 errors found diff --git a/test/files/neg/t2488.scala b/test/files/neg/t2488.scala new file mode 100644 index 0000000000..8db052eec1 --- /dev/null +++ b/test/files/neg/t2488.scala @@ -0,0 +1,12 @@ +class C { + def f(a:Int, b:Int) = 1 + def f() = 2 +} +object Test extends App { + val c = new C() + println(c.f(b = 2, 2)) + println(c.f(a = 2, c = 2)) + println(c.f(2, c = 2)) + println(c.f(c = 2, 2)) + println(c.f(2)) +} diff --git a/test/files/neg/t3098.check b/test/files/neg/t3098.check index 403da281c8..85829747b9 100644 --- a/test/files/neg/t3098.check +++ b/test/files/neg/t3098.check @@ -1,6 +1,5 @@ -b.scala:3: error: match is not exhaustive! -missing combination C - +b.scala:3: error: match may not be exhaustive. +It would fail on the following input: (_ : C) def f = (null: T) match { ^ one error found diff --git a/test/files/neg/t3098.flags b/test/files/neg/t3098.flags index b7eb21d5f5..85d8eb2ba2 100644 --- a/test/files/neg/t3098.flags +++ b/test/files/neg/t3098.flags @@ -1 +1 @@ --Xfatal-warnings -Xoldpatmat +-Xfatal-warnings diff --git a/test/files/neg/t3614.check b/test/files/neg/t3614.check new file mode 100644 index 0000000000..5fdb5cbf1f --- /dev/null +++ b/test/files/neg/t3614.check @@ -0,0 +1,4 @@ +t3614.scala:2: error: class type required but AnyRef{def a: <?>} found + def v = new ({ def a=0 }) + ^ +one error found
\ No newline at end of file diff --git a/test/files/neg/t3614.scala b/test/files/neg/t3614.scala new file mode 100644 index 0000000000..5b02cdf2b2 --- /dev/null +++ b/test/files/neg/t3614.scala @@ -0,0 +1,3 @@ +object t3614 { + def v = new ({ def a=0 }) +}
\ No newline at end of file diff --git a/test/files/neg/t3683a.check b/test/files/neg/t3683a.check index 18e80dd5e8..3de3ad784e 100644 --- a/test/files/neg/t3683a.check +++ b/test/files/neg/t3683a.check @@ -1,6 +1,5 @@ -t3683a.scala:14: error: match is not exhaustive! -missing combination XX - +t3683a.scala:14: error: match may not be exhaustive. +It would fail on the following input: XX() w match { ^ one error found diff --git a/test/files/neg/t3683a.flags b/test/files/neg/t3683a.flags index b7eb21d5f5..85d8eb2ba2 100644 --- a/test/files/neg/t3683a.flags +++ b/test/files/neg/t3683a.flags @@ -1 +1 @@ --Xfatal-warnings -Xoldpatmat +-Xfatal-warnings diff --git a/test/files/neg/t3692-new.flags b/test/files/neg/t3692-new.flags index 82becdfbfd..cb8324a345 100644 --- a/test/files/neg/t3692-new.flags +++ b/test/files/neg/t3692-new.flags @@ -1 +1 @@ - -Xoldpatmat
\ No newline at end of file +-Xoldpatmat
\ No newline at end of file diff --git a/test/files/neg/t3692-old.flags b/test/files/neg/t3692-old.flags index 82becdfbfd..cb8324a345 100644 --- a/test/files/neg/t3692-old.flags +++ b/test/files/neg/t3692-old.flags @@ -1 +1 @@ - -Xoldpatmat
\ No newline at end of file +-Xoldpatmat
\ No newline at end of file diff --git a/test/files/neg/t5044.check b/test/files/neg/t5044.check new file mode 100644 index 0000000000..197da2a4e8 --- /dev/null +++ b/test/files/neg/t5044.check @@ -0,0 +1,9 @@ +t5044.scala:7: error: recursive value a needs type + val id = m(a) + ^ +t5044.scala:6: warning: type-checking the invocation of method foo checks if the named argument expression 'id = ...' is a valid assignment +in the current scope. The resulting type inference error (see above) can be fixed by providing an explicit type in the local definition for id. + val a = foo(id = 1) + ^ +one warning found +one error found diff --git a/test/files/neg/t5044.scala b/test/files/neg/t5044.scala new file mode 100644 index 0000000000..2663ec1bbb --- /dev/null +++ b/test/files/neg/t5044.scala @@ -0,0 +1,9 @@ +class T { + def foo[T](id: T) = 0 + def m(a: Int) = 0 + + def f { + val a = foo(id = 1) + val id = m(a) + } +} diff --git a/test/files/neg/t5544.check b/test/files/neg/t5544.check new file mode 100644 index 0000000000..d4113935a3 --- /dev/null +++ b/test/files/neg/t5544.check @@ -0,0 +1,4 @@ +Test_2.scala:2: error: value baz is not a member of object Api + Api.baz + ^ +one error found diff --git a/test/files/neg/t5544/Api_1.scala b/test/files/neg/t5544/Api_1.scala new file mode 100644 index 0000000000..77637f440a --- /dev/null +++ b/test/files/neg/t5544/Api_1.scala @@ -0,0 +1,8 @@ +import scala.annotation.StaticAnnotation + +class ann(val bar: Any) extends StaticAnnotation + +object Api { + @ann({def baz = "baz!!"}) + def foo = println("foo") +} diff --git a/test/files/neg/t5544/Test_2.scala b/test/files/neg/t5544/Test_2.scala new file mode 100644 index 0000000000..4c8c99cbc7 --- /dev/null +++ b/test/files/neg/t5544/Test_2.scala @@ -0,0 +1,3 @@ +object Test extends App { + Api.baz +} diff --git a/test/files/neg/t5735.check b/test/files/neg/t5735.check new file mode 100644 index 0000000000..f6e0028044 --- /dev/null +++ b/test/files/neg/t5735.check @@ -0,0 +1,6 @@ +t5735.scala:6: error: type mismatch; + found : (x: Int)Int <and> => String + required: Int + val z: Int = a + ^ +one error found diff --git a/test/files/neg/t5735.scala b/test/files/neg/t5735.scala new file mode 100644 index 0000000000..fde71ff962 --- /dev/null +++ b/test/files/neg/t5735.scala @@ -0,0 +1,7 @@ +abstract class Base { + def a: String = "one" +} +class Clazz extends Base { + def a(x: Int): Int = 2 + val z: Int = a +} diff --git a/test/files/neg/t5760-pkgobj-warn.check b/test/files/neg/t5760-pkgobj-warn.check new file mode 100644 index 0000000000..a89398c3f7 --- /dev/null +++ b/test/files/neg/t5760-pkgobj-warn.check @@ -0,0 +1,4 @@ +stalepkg_2.scala:6: error: Foo is already defined as class Foo in package object stalepkg + class Foo + ^ +one error found diff --git a/test/files/neg/t5760-pkgobj-warn/stalepkg_1.scala b/test/files/neg/t5760-pkgobj-warn/stalepkg_1.scala new file mode 100644 index 0000000000..ed4b731bb0 --- /dev/null +++ b/test/files/neg/t5760-pkgobj-warn/stalepkg_1.scala @@ -0,0 +1,11 @@ + +package object stalepkg { + class Foo +} + +package stalepkg { + object Test { + def main(args: Array[String]) { + } + } +} diff --git a/test/files/neg/t5760-pkgobj-warn/stalepkg_2.scala b/test/files/neg/t5760-pkgobj-warn/stalepkg_2.scala new file mode 100644 index 0000000000..9abcdbab17 --- /dev/null +++ b/test/files/neg/t5760-pkgobj-warn/stalepkg_2.scala @@ -0,0 +1,11 @@ + +package object stalepkg { +} + +package stalepkg { + class Foo + object Test { + def main(args: Array[String]) { + } + } +} diff --git a/test/files/neg/t5801.check b/test/files/neg/t5801.check new file mode 100644 index 0000000000..abf8e6e932 --- /dev/null +++ b/test/files/neg/t5801.check @@ -0,0 +1,22 @@ +t5801.scala:1: error: object sth is not a member of package scala +import scala.sth + ^ +t5801.scala:4: error: not found: value sth + def foo(a: Int)(implicit b: sth.Sth): Unit = {} + ^ +t5801.scala:7: error: not found: value sth + def bar(x: Int)(implicit y: Int): sth.Sth = null + ^ +t5801.scala:8: error: could not find implicit value for parameter y: Int + bar(1) + ^ +t5801.scala:10: error: not found: value sth + def meh(x: Int)(implicit a: sth.Sth, b: Int): Unit = {} + ^ +t5801.scala:13: error: not found: value sth + def meh2(x: Int)(implicit b: Int, a: sth.Sth): Unit = {} + ^ +t5801.scala:14: error: could not find implicit value for parameter b: Int + meh2(1) + ^ +7 errors found diff --git a/test/files/neg/t5801.scala b/test/files/neg/t5801.scala new file mode 100644 index 0000000000..d452222ac8 --- /dev/null +++ b/test/files/neg/t5801.scala @@ -0,0 +1,16 @@ +import scala.sth + +object Test extends App { + def foo(a: Int)(implicit b: sth.Sth): Unit = {} + foo(1) + + def bar(x: Int)(implicit y: Int): sth.Sth = null + bar(1) + + def meh(x: Int)(implicit a: sth.Sth, b: Int): Unit = {} + meh(1) + + def meh2(x: Int)(implicit b: Int, a: sth.Sth): Unit = {} + meh2(1) +} + diff --git a/test/files/pos/exhaust_alternatives.flags b/test/files/pos/exhaust_alternatives.flags new file mode 100644 index 0000000000..e8fb65d50c --- /dev/null +++ b/test/files/pos/exhaust_alternatives.flags @@ -0,0 +1 @@ +-Xfatal-warnings
\ No newline at end of file diff --git a/test/files/pos/exhaust_alternatives.scala b/test/files/pos/exhaust_alternatives.scala new file mode 100644 index 0000000000..cc81d0be7d --- /dev/null +++ b/test/files/pos/exhaust_alternatives.scala @@ -0,0 +1,10 @@ +sealed abstract class X +sealed case class A(x: Boolean) extends X +case object B extends X + +object Test { + def test(x: X) = x match { + case A(true) => + case A(false) | B => + } +}
\ No newline at end of file diff --git a/test/files/pos/exhaustive_heuristics.scala b/test/files/pos/exhaustive_heuristics.scala new file mode 100644 index 0000000000..f6bea455a5 --- /dev/null +++ b/test/files/pos/exhaustive_heuristics.scala @@ -0,0 +1,16 @@ +// tests exhaustivity doesn't give warnings (due to its heuristic rewrites kicking in or it backing off) +object Test { + // List() => Nil + List(1) match { + case List() => + case x :: xs => + } + + // we don't look into guards + val turnOffChecks = true + List(1) match { + case _ if turnOffChecks => + } + + // TODO: we back off when there are any user-defined extractors +}
\ No newline at end of file diff --git a/test/files/pos/t2405.scala b/test/files/pos/t2405.scala new file mode 100644 index 0000000000..224b2ce83b --- /dev/null +++ b/test/files/pos/t2405.scala @@ -0,0 +1,23 @@ +object A { implicit val x: Int = 1 } + +// Problem as stated in the ticket. +object Test1 { + import A.{x => y} + implicitly[Int] +} + +// Testing for the absense of shadowing #1. +object Test2 { + import A.{x => y} + val x = 2 + implicitly[Int] +} + +// Testing for the absense of shadowing #2. +object Test3 { + { + import A.{x => y} + def x: Int = 0 + implicitly[Int] + } +} diff --git a/test/files/pos/t3097.scala b/test/files/pos/t3097.scala deleted file mode 100644 index a034b960f7..0000000000 --- a/test/files/pos/t3097.scala +++ /dev/null @@ -1,31 +0,0 @@ -package seal - -sealed trait ISimpleValue - -sealed trait IListValue extends ISimpleValue { - def items: List[IAtomicValue[_]] -} -sealed trait IAtomicValue[O] extends ISimpleValue { - def data: O -} - -sealed trait IAbstractDoubleValue[O] extends IAtomicValue[O] { } -sealed trait IDoubleValue extends IAbstractDoubleValue[Double] - -case class ListValue(val items: List[IAtomicValue[_]]) extends IListValue -class DoubleValue(val data: Double) extends IDoubleValue { - def asDouble = data -} - -object Test { - /** - * @param args the command line arguments - */ - def main(args: Array[String]): Unit = { - val v: ISimpleValue = new DoubleValue(1) - v match { - case m: IListValue => println("list") - case a: IAtomicValue[_] => println("atomic") - } - } -} diff --git a/test/files/pos/t3856.scala b/test/files/pos/t3856.scala index fd253a56a8..5ea4b84e2c 100644 --- a/test/files/pos/t3856.scala +++ b/test/files/pos/t3856.scala @@ -2,6 +2,7 @@ case class C[T](x: T) case class CS(xs: C[_]*) +// t3856 object Test { val x = CS(C(5), C("abc")) match { case CS(C(5), xs @ _*) => xs } println(x) diff --git a/test/files/pos/t3880.scala b/test/files/pos/t3880.scala new file mode 100644 index 0000000000..b6f06c43a3 --- /dev/null +++ b/test/files/pos/t3880.scala @@ -0,0 +1,16 @@ +abstract class Bar[+B] { +} +abstract class C1[+B] extends Bar[B] { + private[this] def g(x: C1[B]): Unit = () + + // this method is fine: notice that it allows the call to g, + // which requires C1[B], even though we matched on C1[_]. + // (That is good news.) + private[this] def f1(x: Bar[B]): Unit = x match { + case x: C1[_] => g(x) + } + // this one crashes. + private[this] def f2(x: Bar[B]): Unit = x match { + case x: C1[_] => f2(x) + } +}
\ No newline at end of file diff --git a/test/files/pos/t4717.scala b/test/files/pos/t4717.scala new file mode 100644 index 0000000000..4acfe489cc --- /dev/null +++ b/test/files/pos/t4717.scala @@ -0,0 +1,35 @@ + + + + + + + +trait Bug1[@specialized(Boolean) A] extends TraversableOnce[A] { + + def ++[B >: A](that: TraversableOnce[B]): Iterator[B] = new Iterator[B] { + lazy val it = that.toIterator + def hasNext = it.hasNext + def next = it.next + } + +} + + + +trait WorksFine[@specialized(Boolean) A] { + class SubBounds[B >: A] extends Bounds[B] { + lazy val it = ??? + } + def x[B >: A]: Unit = new SubBounds[B] +} + + +trait Bounds[@specialized(Boolean) A] { + // okay without `>: A` + def x[B >: A]: Unit = new Bounds[B] { + lazy val it = ??? // def or val okay + } +} + + diff --git a/test/files/pos/t4812.scala b/test/files/pos/t4812.scala new file mode 100644 index 0000000000..2a807ab05e --- /dev/null +++ b/test/files/pos/t4812.scala @@ -0,0 +1,4 @@ +trait Test1 { + def m1(sym: Symbol = 'TestSym) + def m2(s: String = "TestString") +} diff --git a/test/files/pos/t4975.scala b/test/files/pos/t4975.scala new file mode 100644 index 0000000000..12d889c0d5 --- /dev/null +++ b/test/files/pos/t4975.scala @@ -0,0 +1,12 @@ +object ImplicitScope { + class A[T] + + def foo { + trait B + object B { + implicit def ab = new A[B] + } + + implicitly[A[B]] // Error + } +} diff --git a/test/files/pos/t5305.scala b/test/files/pos/t5305.scala new file mode 100644 index 0000000000..4c32cd7c6d --- /dev/null +++ b/test/files/pos/t5305.scala @@ -0,0 +1,13 @@ +object t5305 { + def in(a: Any) = {} + + object O { + type F = Int + val v = "" + } + + in { + import O.{F, v} + type x = {type l = (F, v.type)} // not found: type F + } +} diff --git a/test/files/pos/t5779-numeq-warn.scala b/test/files/pos/t5779-numeq-warn.scala new file mode 100644 index 0000000000..76ef2970fd --- /dev/null +++ b/test/files/pos/t5779-numeq-warn.scala @@ -0,0 +1,13 @@ + +object Test { + def main(args: Array[String]) { + val d: Double = (BigInt(1) << 64).toDouble + val f: Float = d.toFloat + val n: java.lang.Number = d.toFloat + assert (d == f) // ok + assert (d == n) // was: comparing values of types Double and Number using `==' will always yield false + assert (n == d) // was: Number and Double are unrelated: they will most likely never compare equal + assert (f == n) + assert (n == f) + } +} diff --git a/test/files/pos/virtpatmat_exhaust.scala b/test/files/pos/virtpatmat_exhaust.scala new file mode 100644 index 0000000000..a2f47c88c8 --- /dev/null +++ b/test/files/pos/virtpatmat_exhaust.scala @@ -0,0 +1,24 @@ +sealed trait Option {} +case class Choice(a: Option, b: Option) extends Option; +case class Some(x: Boolean) extends Option; +case object None extends Option; + +object test { + +// drop any case and it will report an error +// note that booleans are taken into account + def f(opt: Option) = opt match { + case Choice(None, None) => 1; + case Choice(None, Some(_)) => 1; + case Choice(None, Choice(_, _)) => 1; + case Choice(Some(true), None) => 1; + case Choice(Some(false), None) => 1; + case Choice(Some(_), Some(_)) => 1; + case Choice(Some(_), Choice(_, _)) => 1; + case Choice(Choice(_, _), None) => 1; + case Choice(Choice(_, _), Some(_)) => 1; + case Choice(Choice(_, _), Choice(_, _)) => 1; + case Some(b) => 4; + case None => 5; + } +} diff --git a/test/files/pos/virtpatmat_exhaust_unchecked.flags b/test/files/pos/virtpatmat_exhaust_unchecked.flags new file mode 100644 index 0000000000..85d8eb2ba2 --- /dev/null +++ b/test/files/pos/virtpatmat_exhaust_unchecked.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/test/files/pos/virtpatmat_exhaust_unchecked.scala b/test/files/pos/virtpatmat_exhaust_unchecked.scala new file mode 100644 index 0000000000..641f2b4f9a --- /dev/null +++ b/test/files/pos/virtpatmat_exhaust_unchecked.scala @@ -0,0 +1,24 @@ +sealed trait Option {} +case class Choice(a: Option, b: Option) extends Option; +case class Some(x: Boolean) extends Option; +case object None extends Option; + +object test { + +// drop any case and it will report an error +// note that booleans are taken into account + def f(opt: Option) = (opt: @unchecked) match { + case Choice(None, None) => 1; + case Choice(None, Some(_)) => 1; + case Choice(None, Choice(_, _)) => 1; + case Choice(Some(true), None) => 1; + // case Choice(Some(false), None) => 1; + case Choice(Some(_), Some(_)) => 1; + case Choice(Some(_), Choice(_, _)) => 1; + case Choice(Choice(_, _), None) => 1; + case Choice(Choice(_, _), Some(_)) => 1; + case Choice(Choice(_, _), Choice(_, _)) => 1; + case Some(b) => 4; + case None => 5; + } +} diff --git a/test/files/run/inline-ex-handlers.check b/test/files/run/inline-ex-handlers.check index a5d7e93334..708fcc6985 100644 --- a/test/files/run/inline-ex-handlers.check +++ b/test/files/run/inline-ex-handlers.check @@ -1,27 +1,23 @@ 172c172 -< locals: value x$1, value x1, value x2, value x +< locals: value x$1, value x1 --- -> locals: value x$1, value x1, value x2, value x, variable boxed1 +> locals: value x$1, value x1, variable boxed1 174c174 -< blocks: [1,2,3,5,6,7] +< blocks: [1,2,3,4] --- -> blocks: [1,3,5,6] -180,182d179 +> blocks: [1,3,4] +186a187,188 +> 92 STORE_LOCAL(variable boxed1) +> 92 LOAD_LOCAL(variable boxed1) +195,197d196 < 92 JUMP 2 < < 2: -194,196d190 -< 92 JUMP 7 -< -< 7: -204a199,200 -> 92 STORE_LOCAL(variable boxed1) -> 92 LOAD_LOCAL(variable boxed1) -395c391 +385c384 < blocks: [1,2,3,4,5,8,11,13,14,16] --- > blocks: [1,2,3,5,8,11,13,14,16,17] -419c415,424 +409c408,417 < 103 THROW(MyException) --- > ? STORE_LOCAL(value ex5) @@ -34,15 +30,15 @@ > 106 LOAD_LOCAL(value x3) > 106 IS_INSTANCE REF(class MyException) > 106 CZJUMP (BOOL)NE ? 5 : 11 -432,434d436 +422,424d429 < 101 JUMP 4 < < 4: -522c524 +512c517 < blocks: [1,2,3,4,6,7,8,9,10] --- > blocks: [1,2,3,4,6,7,8,9,10,11,12,13] -551c553,558 +541c546,551 < 306 THROW(MyException) --- > ? JUMP 11 @@ -51,7 +47,7 @@ > ? LOAD_LOCAL(variable monitor4) > 305 MONITOR_EXIT > ? JUMP 12 -557c564,570 +547c557,563 < ? THROW(Throwable) --- > ? JUMP 12 @@ -61,7 +57,7 @@ > 304 MONITOR_EXIT > ? STORE_LOCAL(value t) > ? JUMP 13 -563c576,589 +553c569,582 < ? THROW(Throwable) --- > ? STORE_LOCAL(value t) @@ -78,30 +74,29 @@ > 310 CALL_PRIMITIVE(EndConcat) > 310 CALL_METHOD scala.Predef.println (dynamic) > 310 JUMP 2 -587c613 +577c606 < catch (Throwable) in ArrayBuffer(7, 8, 9, 10) starting at: 6 --- > catch (Throwable) in ArrayBuffer(7, 8, 9, 10, 11) starting at: 6 -590c616 +580c609 < catch (Throwable) in ArrayBuffer(4, 6, 7, 8, 9, 10) starting at: 3 --- > catch (Throwable) in ArrayBuffer(4, 6, 7, 8, 9, 10, 11, 12) starting at: 3 -622c648 +612c641 < blocks: [1,2,3,4,5,6,7,9,10] --- > blocks: [1,2,3,4,5,6,7,9,10,11,12] -646c672,673 +636c665,671 < 78 THROW(IllegalArgumentException) --- > ? STORE_LOCAL(value e) > ? JUMP 11 -647a675,679 +> > 11: > 81 LOAD_LOCAL(value e) > ? STORE_LOCAL(variable exc1) > ? JUMP 12 -> -675c707,721 +665c700,714 < 81 THROW(Exception) --- > ? STORE_LOCAL(variable exc1) @@ -119,15 +114,15 @@ > 84 STORE_LOCAL(variable result) > 84 LOAD_LOCAL(variable exc1) > 84 THROW(Throwable) -697c743 +687c736 < catch (<none>) in ArrayBuffer(4, 6, 7, 9) starting at: 3 --- > catch (<none>) in ArrayBuffer(4, 6, 7, 9, 11) starting at: 3 -723c769 +713c762 < blocks: [1,2,3,4,5,6,9,12,14,17,18,19,22,25,27,28,30,31] --- > blocks: [1,2,3,4,5,6,9,12,14,17,18,19,22,25,27,28,30,31,32,33,34] -747c793,800 +737c786,793 < 172 THROW(MyException) --- > ? STORE_LOCAL(value ex5) @@ -138,12 +133,12 @@ > 170 STORE_LOCAL(value x3) > 170 SCOPE_ENTER value x3 > 170 JUMP 18 -803c856,857 +793c849,850 < 177 THROW(MyException) --- > ? STORE_LOCAL(value ex5) > ? JUMP 33 -807c861,868 +797c854,861 < 170 THROW(Throwable) --- > ? STORE_LOCAL(value ex5) @@ -154,17 +149,17 @@ > 169 STORE_LOCAL(value x3) > 169 SCOPE_ENTER value x3 > 169 JUMP 5 -840c901,902 +830c894,895 < 182 THROW(MyException) --- > ? STORE_LOCAL(variable exc2) > ? JUMP 34 -844c906,907 +834c899,900 < 169 THROW(Throwable) --- > ? STORE_LOCAL(variable exc2) > ? JUMP 34 -845a909,921 +835a902,914 > 34: > 184 LOAD_MODULE object Predef > 184 CONSTANT("finally") @@ -178,19 +173,19 @@ > 185 LOAD_LOCAL(variable exc2) > 185 THROW(Throwable) > -866c942 +856c935 < catch (Throwable) in ArrayBuffer(17, 18, 19, 22, 25, 27, 28, 30) starting at: 4 --- > catch (Throwable) in ArrayBuffer(17, 18, 19, 22, 25, 27, 28, 30, 32) starting at: 4 -869c945 +859c938 < catch (<none>) in ArrayBuffer(4, 5, 6, 9, 12, 17, 18, 19, 22, 25, 27, 28, 30) starting at: 3 --- > catch (<none>) in ArrayBuffer(4, 5, 6, 9, 12, 17, 18, 19, 22, 25, 27, 28, 30, 32, 33) starting at: 3 -895c971 +885c964 < blocks: [1,2,3,6,7,8,11,14,16,17,19] --- > blocks: [1,2,3,6,7,8,11,14,16,17,19,20] -919c995,1002 +909c988,995 < 124 THROW(MyException) --- > ? STORE_LOCAL(value ex5) @@ -201,15 +196,15 @@ > 122 STORE_LOCAL(value x3) > 122 SCOPE_ENTER value x3 > 122 JUMP 7 -979c1062 +969c1055 < catch (IllegalArgumentException) in ArrayBuffer(6, 7, 8, 11, 14, 16, 17, 19) starting at: 3 --- > catch (IllegalArgumentException) in ArrayBuffer(6, 7, 8, 11, 14, 16, 17, 19, 20) starting at: 3 -1005c1088 +995c1081 < blocks: [1,2,3,4,5,8,11,15,16,17,19] --- > blocks: [1,2,3,5,8,11,15,16,17,19,20] -1029c1112,1121 +1019c1105,1114 < 148 THROW(MyException) --- > ? STORE_LOCAL(value ex5) @@ -222,15 +217,15 @@ > 154 LOAD_LOCAL(value x3) > 154 IS_INSTANCE REF(class MyException) > 154 CZJUMP (BOOL)NE ? 5 : 11 -1050,1052d1141 +1040,1042d1134 < 145 JUMP 4 < < 4: -1285c1374 +1275c1367 < blocks: [1,2,3,4,5,7] --- > blocks: [1,2,3,4,5,7,8] -1309c1398,1405 +1299c1391,1398 < 38 THROW(IllegalArgumentException) --- > ? STORE_LOCAL(value e) @@ -241,16 +236,16 @@ > 42 CONSTANT("IllegalArgumentException") > 42 CALL_METHOD scala.Predef.println (dynamic) > 42 JUMP 2 -1358c1454 +1348c1447 < blocks: [1,2,3,4,5,8,11,13,14,16,17,19] --- > blocks: [1,2,3,5,8,11,13,14,16,17,19,20] -1382c1478,1479 +1372c1471,1472 < 203 THROW(MyException) --- > ? STORE_LOCAL(value ex5) > ? JUMP 20 -1402c1499,1508 +1392c1492,1501 < 209 THROW(MyException) --- > ? STORE_LOCAL(value ex5) @@ -263,15 +258,15 @@ > 212 LOAD_LOCAL(value x3) > 212 IS_INSTANCE REF(class MyException) > 212 CZJUMP (BOOL)NE ? 5 : 11 -1415,1417d1520 +1405,1407d1513 < 200 JUMP 4 < < 4: -1477c1580 +1467c1573 < blocks: [1,2,3,4,5,7] --- > blocks: [1,2,3,4,5,7,8] -1501c1604,1611 +1491c1597,1604 < 58 THROW(IllegalArgumentException) --- > ? STORE_LOCAL(value e) @@ -282,11 +277,11 @@ > 62 CONSTANT("RuntimeException") > 62 CALL_METHOD scala.Predef.println (dynamic) > 62 JUMP 2 -1550c1660 +1540c1653 < blocks: [1,2,3,4] --- > blocks: [1,2,3,4,5] -1570c1680,1685 +1560c1673,1678 < 229 THROW(MyException) --- > ? JUMP 5 @@ -295,19 +290,19 @@ > ? LOAD_LOCAL(variable monitor1) > 228 MONITOR_EXIT > 228 THROW(Throwable) -1576c1691 +1566c1684 < ? THROW(Throwable) --- > 228 THROW(Throwable) -1604c1719 +1594c1712 < locals: value args, variable result, variable monitor2, variable monitorResult1 --- > locals: value exception$1, value args, variable result, variable monitor2, variable monitorResult1 -1606c1721 +1596c1714 < blocks: [1,2,3,4] --- > blocks: [1,2,3,4,5] -1629c1744,1752 +1619c1737,1745 < 245 THROW(MyException) --- > ? STORE_LOCAL(value exception$1) @@ -319,7 +314,7 @@ > ? LOAD_LOCAL(variable monitor2) > 244 MONITOR_EXIT > 244 THROW(Throwable) -1635c1758 +1625c1751 < ? THROW(Throwable) --- > 244 THROW(Throwable) diff --git a/test/files/run/t2488.check b/test/files/run/t2488.check new file mode 100644 index 0000000000..1af4bf8965 --- /dev/null +++ b/test/files/run/t2488.check @@ -0,0 +1,4 @@ +1 +1 +1 +2 diff --git a/test/files/run/t2488.scala b/test/files/run/t2488.scala new file mode 100644 index 0000000000..22abdf8af2 --- /dev/null +++ b/test/files/run/t2488.scala @@ -0,0 +1,11 @@ +class C { + def f(a:Int, b:Int) = 1 + def f() = 2 +} +object Test extends App { + val c = new C() + println(c.f(a = 1,2)) + println(c.f(a = 1, b = 2)) + println(c.f(b = 2, a = 1)) + println(c.f()) +} diff --git a/test/files/run/t3097.check b/test/files/run/t3097.check new file mode 100644 index 0000000000..63695f771b --- /dev/null +++ b/test/files/run/t3097.check @@ -0,0 +1 @@ +atomic diff --git a/test/files/run/t3097.scala b/test/files/run/t3097.scala new file mode 100644 index 0000000000..4aaf8056ca --- /dev/null +++ b/test/files/run/t3097.scala @@ -0,0 +1,18 @@ +sealed trait ISimpleValue + +sealed trait IListValue extends ISimpleValue +sealed trait IAtomicValue[O] extends ISimpleValue + +sealed trait IAbstractDoubleValue[O] extends IAtomicValue[O] +sealed trait IDoubleValue extends IAbstractDoubleValue[Double] + +case class ListValue(val items: List[IAtomicValue[_]]) extends IListValue +class DoubleValue(val data: Double) extends IDoubleValue + +object Test extends App { + // match is exhaustive + (new DoubleValue(1): ISimpleValue) match { + case m: IListValue => println("list") + case a: IAtomicValue[_] => println("atomic") + } +} diff --git a/test/files/run/t3488.check b/test/files/run/t3488.check new file mode 100644 index 0000000000..0d66ea1aee --- /dev/null +++ b/test/files/run/t3488.check @@ -0,0 +1,2 @@ +0 +1 diff --git a/test/files/run/t3488.scala b/test/files/run/t3488.scala new file mode 100644 index 0000000000..20a1400dce --- /dev/null +++ b/test/files/run/t3488.scala @@ -0,0 +1,6 @@ +object Test extends App { + def foo(p: => Unit)(x:Int = 0) = x + + println(foo { val List(_*)=List(0); 1 } ()) + println(foo { val List(_*)=List(0); 1 } (1)) +} diff --git a/test/files/run/t4138.check b/test/files/run/t4138.check new file mode 100644 index 0000000000..f561b5e6b0 --- /dev/null +++ b/test/files/run/t4138.check @@ -0,0 +1,2 @@ +[1.45] parsed: "lir 'de\' ' \\ \n / upa \"new\" \t parsing" +[1.5] parsed: "s " diff --git a/test/files/run/t4138.scala b/test/files/run/t4138.scala new file mode 100644 index 0000000000..131489e581 --- /dev/null +++ b/test/files/run/t4138.scala @@ -0,0 +1,6 @@ +object Test extends App { + object p extends scala.util.parsing.combinator.JavaTokenParsers + + println(p.parse(p.stringLiteral, """"lir 'de\' ' \\ \n / upa \"new\" \t parsing"""")) + println(p.parse(p.stringLiteral, """"s " lkjse"""")) +} diff --git a/test/files/run/t4461.check b/test/files/run/t4461.check index b05c7b5589..e9c01e769d 100644 --- a/test/files/run/t4461.check +++ b/test/files/run/t4461.check @@ -4,4 +4,8 @@ Include(End,3) Include(End,4) Include(End,5) Include(End,6) -Include(End,7)
\ No newline at end of file +Include(End,7) +Script([1] Include(Index(7),8), [2] Include(Index(8),9), [3] Include(Index(9),10)) +Include(Start,0) +Script([1] Include(Index(0),-2), [2] Include(Index(1),-1)) +Remove(Index(0),-2)
\ No newline at end of file diff --git a/test/files/run/t4461.scala b/test/files/run/t4461.scala index 99da122f6b..adc9201313 100644 --- a/test/files/run/t4461.scala +++ b/test/files/run/t4461.scala @@ -15,5 +15,9 @@ object Test { buf ++= ArrayBuffer(3, 4) // works buf ++= List(5) // works buf ++= collection.immutable.Vector(6, 7) // works + buf.insertAll(7, List(8, 9, 10)) + 0 +=: buf + List(-2, -1) ++=: buf + buf remove 0 } } diff --git a/test/files/run/t5125.check b/test/files/run/t5125.check new file mode 100644 index 0000000000..d8a0565005 --- /dev/null +++ b/test/files/run/t5125.check @@ -0,0 +1,4 @@ +public void O1$.f(java.lang.String[]) +public void O1$.f(scala.collection.Seq) +public void O2$.f(java.lang.String[]) +public void O2$.f(scala.collection.Seq) diff --git a/test/files/run/t5125.scala b/test/files/run/t5125.scala new file mode 100644 index 0000000000..7ec2b929d9 --- /dev/null +++ b/test/files/run/t5125.scala @@ -0,0 +1,24 @@ +object O1 { + def instance = this + @scala.annotation.varargs + def f(values:String*) = println("Calling O1.f(): " + values) +} + +object O2 { + def instance = this + @scala.annotation.varargs + def f(values:String*) = println("Calling O2.f(): " + values) + // uncommenting g() results in errors in A.java + def g(): String => Int = s => s.hashCode +} + +object Test extends App { + def check(c: Class[_]) { + val methodName = "f" + val methods = c.getDeclaredMethods.filter(_.getName == methodName) + println(methods.map(_.toString).sorted.mkString("\n")) + } + + check(O1.getClass) + check(O2.getClass) +}
\ No newline at end of file diff --git a/test/files/run/t5125b.check b/test/files/run/t5125b.check new file mode 100644 index 0000000000..ddbf908f04 --- /dev/null +++ b/test/files/run/t5125b.check @@ -0,0 +1,7 @@ +public void C1.f(java.lang.String[]) +public void C1.f(scala.collection.Seq) +public void C2.f(java.lang.String[]) +public void C2.f(scala.collection.Seq) +public void C2$C3.f(java.lang.String[]) +public void C2$C3.f(scala.collection.Seq) +public void C4.f(scala.collection.Seq) diff --git a/test/files/run/t5125b.scala b/test/files/run/t5125b.scala new file mode 100644 index 0000000000..29c08fee4c --- /dev/null +++ b/test/files/run/t5125b.scala @@ -0,0 +1,37 @@ +class C1 { + @scala.annotation.varargs + def f(values:String*) = println("Calling C1.f(): " + values) +} + +class C2 { + @scala.annotation.varargs + def f(values:String*) = println("Calling C2.f(): " + values) + def g(): String => Int = s => s.hashCode + + class C3 { + @scala.annotation.varargs + def f(values:String*) = println("Calling C3.f(): " + values) + } +} + +class C4 { + def f(values: String*) = println("Calling C4.f(): " + values) + + locally { + @scala.annotation.varargs + def f(values: String*) = println("Calling C4.<locally>.f(): " + values) + } +} + +object Test extends App { + def check(c: Class[_]) { + val methodName = "f" + val methods = c.getDeclaredMethods.filter(_.getName == methodName) + println(methods.map(_.toString).sorted.mkString("\n")) + } + + check(classOf[C1]) + check(classOf[C2]) + check(classOf[C2#C3]) + check(classOf[C4]) +} diff --git a/test/files/run/t5544.check b/test/files/run/t5544.check new file mode 100644 index 0000000000..257cc5642c --- /dev/null +++ b/test/files/run/t5544.check @@ -0,0 +1 @@ +foo diff --git a/test/files/run/t5544/Api_1.scala b/test/files/run/t5544/Api_1.scala new file mode 100644 index 0000000000..b4c92864de --- /dev/null +++ b/test/files/run/t5544/Api_1.scala @@ -0,0 +1,8 @@ +import scala.annotation.StaticAnnotation + +class ann(val bar: Any) extends StaticAnnotation + +object Api { + @ann({def foo = "foo!!"}) + def foo = println("foo") +} diff --git a/test/files/run/t5544/Test_2.scala b/test/files/run/t5544/Test_2.scala new file mode 100644 index 0000000000..285f8959e0 --- /dev/null +++ b/test/files/run/t5544/Test_2.scala @@ -0,0 +1,3 @@ +object Test extends App { + Api.foo +} diff --git a/test/files/run/t5804.check b/test/files/run/t5804.check new file mode 100644 index 0000000000..3ccc1c24d3 --- /dev/null +++ b/test/files/run/t5804.check @@ -0,0 +1,4 @@ +128 +16 +128 +32
\ No newline at end of file diff --git a/test/files/run/t5804.scala b/test/files/run/t5804.scala new file mode 100644 index 0000000000..b96a736035 --- /dev/null +++ b/test/files/run/t5804.scala @@ -0,0 +1,32 @@ + + +import collection.mutable._ + + +object Test { + + def main(args: Array[String]) { + class CustomHashMap extends HashMap[Int, Int] { + override def initialSize = 65 + + println(table.length) + } + + new CustomHashMap + new HashMap { + println(table.length) + } + + class CustomHashSet extends HashSet[Int] { + override def initialSize = 96 + + println(table.length) + } + + new CustomHashSet + new HashSet { + println(table.length) + } + } + +} diff --git a/test/files/pos/no-widen-locals.scala b/test/pending/pos/no-widen-locals.scala index 013e63f0a2..013e63f0a2 100644 --- a/test/files/pos/no-widen-locals.scala +++ b/test/pending/pos/no-widen-locals.scala diff --git a/test/pending/run/t3899.check b/test/pending/run/t3899.check new file mode 100644 index 0000000000..c317608eab --- /dev/null +++ b/test/pending/run/t3899.check @@ -0,0 +1,4 @@ +a,b +a,b +a,b +a,b diff --git a/test/pending/run/t3899/Base_1.java b/test/pending/run/t3899/Base_1.java new file mode 100644 index 0000000000..114cc0b7a6 --- /dev/null +++ b/test/pending/run/t3899/Base_1.java @@ -0,0 +1,5 @@ +public class Base_1 { + public String[] varargs1(String... as) { + return as; + } +} diff --git a/test/pending/run/t3899/Derived_2.scala b/test/pending/run/t3899/Derived_2.scala new file mode 100644 index 0000000000..bb4e53784d --- /dev/null +++ b/test/pending/run/t3899/Derived_2.scala @@ -0,0 +1,30 @@ +trait T extends Base_1 { + def t1(as: String*): Array[String] = { + varargs1(as: _*) + } + def t2(as: String*): Array[String] = { + // This is the bug reported in the ticket. + super.varargs1(as: _*) + } +} + +class C extends Base_1 { + def c1(as: String*): Array[String] = { + varargs1(as: _*) + } + def c2(as: String*): Array[String] = { + super.varargs1(as: _*) + } +} + + +object Test extends App { + val t = new T {} + println(t.t1("a", "b").mkString(",")) + println(t.t2("a", "b").mkString(",")) + + val c = new C {} + println(c.c1("a", "b").mkString(",")) + println(c.c2("a", "b").mkString(",")) + +} |