diff options
123 files changed, 1538 insertions, 533 deletions
diff --git a/src/compiler/scala/tools/nsc/CompilationUnits.scala b/src/compiler/scala/tools/nsc/CompilationUnits.scala index df5952a4cf..c2caed70a0 100644 --- a/src/compiler/scala/tools/nsc/CompilationUnits.scala +++ b/src/compiler/scala/tools/nsc/CompilationUnits.scala @@ -98,6 +98,11 @@ trait CompilationUnits { global: Global => override def toString = map.toString } + // namer calls typer.computeType(rhs) on DefDef / ValDef when tpt is empty. the result + // is cached here and re-used in typedDefDef / typedValDef + // Also used to cache imports type-checked by namer. + val transformed = new mutable.AnyRefMap[Tree, Tree] + /** things to check at end of compilation unit */ val toCheck = new ListBuffer[() => Unit] diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 5492e563dd..1617db7517 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -81,6 +81,8 @@ class Global(var currentSettings: Settings, var reporter: Reporter) def picklerPhase: Phase = if (currentRun.isDefined) currentRun.picklerPhase else NoPhase + def erasurePhase: Phase = if (currentRun.isDefined) currentRun.erasurePhase else NoPhase + // platform specific elements protected class GlobalPlatform extends { @@ -527,7 +529,7 @@ class Global(var currentSettings: Settings, var reporter: Reporter) } with Erasure // phaseName = "posterasure" - object postErasure extends { + override object postErasure extends { val global: Global.this.type = Global.this val runsAfter = List("erasure") val runsRightAfter = Some("erasure") diff --git a/src/compiler/scala/tools/nsc/ast/parser/CommonTokens.scala b/src/compiler/scala/tools/nsc/ast/parser/CommonTokens.scala new file mode 100644 index 0000000000..5fcb02814b --- /dev/null +++ b/src/compiler/scala/tools/nsc/ast/parser/CommonTokens.scala @@ -0,0 +1,112 @@ +package scala.tools.nsc +package ast.parser + +/** Common code between Scala's Tokens and JavaTokens. */ +abstract class CommonTokens { + + def isIdentifier(code: Int): Boolean + def isLiteral(code: Int): Boolean + + /** special tokens */ + final val EMPTY = -3 + final val UNDEF = -2 + final val ERROR = -1 + final val EOF = 0 + + /** literals */ + final val CHARLIT = 1 + final val INTLIT = 2 + final val LONGLIT = 3 + final val FLOATLIT = 4 + final val DOUBLELIT = 5 + final val STRINGLIT = 6 + + /** keywords */ + final val NEW = 20 + final val THIS = 21 + final val SUPER = 23 + + final val NULL = 24 + final val TRUE = 25 + final val FALSE = 26 + + // J: INSTANCEOF = 27 + // J: CONST = 28 + + /** modifiers */ + // S: IMPLICIT = 40 + // S: OVERRIDE = 41 + // J: PUBLIC = 42 + final val PROTECTED = 43 + final val PRIVATE = 44 + // S: SEALED = 45 + final val ABSTRACT = 46 + // J: DEFAULT = 47 + // J: STATIC = 48 + final val FINAL = 49 + // J: TRANSIENT = 50 + // J: VOLATILE = 51 + // J: SYNCHRONIZED = 52 + // J: NATIVE = 53 + // J: STRICTFP = 54 + // S: LAZY = 55 + // J: THROWS = 56 + // S: MACRO = 57 + + /** templates */ + final val PACKAGE = 60 + final val IMPORT = 61 + final val CLASS = 62 + // S: CASECLASS = 63 + // S: OBJECT = 64 + // S: CASEOBJECT = 65 + // S: TRAIT, J: INTERFACE = 66 + // J: ENUM = 67 + final val EXTENDS = 68 + // S: WITH, J: IMPLEMENTS = 69 + // S: TYPE = 70 + // S: FORSOME = 71 + // S: DEF = 72 + // S: VAL = 73 + // S: VAR = 74 + + /** control structures */ + final val IF = 80 + // S: THEN = 81 + final val ELSE = 82 + final val WHILE = 83 + final val DO = 84 + final val FOR = 85 + // S: YIELD = 86 + // J: BREAK = 87 + // J: CONTINUE = 88 + // J: GOTO = 89 + final val THROW = 90 + final val TRY = 91 + final val CATCH = 92 + final val FINALLY = 93 + // J: SWITCH = 94 + // S: MATCH = 95 + final val CASE = 96 + final val RETURN = 97 + // J: ASSERT = 98 + + /** parenthesis */ + final val LPAREN = 100 + final val RPAREN = 101 + final val LBRACKET = 102 + final val RBRACKET = 103 + final val LBRACE = 104 + final val RBRACE = 105 + + /** special symbols */ + final val COMMA = 120 + final val SEMI = 121 + final val DOT = 122 + final val COLON = 123 + final val EQUALS = 124 + final val AT = 125 + // S: <special symbols> = 130 - 139 + // J: <special symbols> = 140 - 179 + // J: <primitive types> = 180 - 189 +} diff --git a/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala b/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala index 5a7dc4950d..e624aec88c 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala @@ -6,123 +6,57 @@ package scala.tools.nsc package ast.parser -/** Common code between JavaTokens and Tokens. Not as much (and not as concrete) - * as one might like because JavaTokens for no clear reason chose new numbers for - * identical token sets. - */ -abstract class Tokens { - /** special tokens */ - final val EMPTY = -3 - final val UNDEF = -2 - final val ERROR = -1 - final val EOF = 0 - - /** literals */ - final val CHARLIT = 1 - final val INTLIT = 2 - final val LONGLIT = 3 - final val FLOATLIT = 4 - final val DOUBLELIT = 5 - final val STRINGLIT = 6 - - def LPAREN: Int - def RBRACE: Int - - def isIdentifier(code: Int): Boolean - def isLiteral(code: Int): Boolean -} - -object Tokens extends Tokens { - final val STRINGPART = 7 // a part of an interpolated string +object Tokens extends CommonTokens { + final val STRINGPART = 7 // a part of an interpolated string final val SYMBOLLIT = 8 final val INTERPOLATIONID = 9 // the lead identifier of an interpolated string - def isLiteral(code: Int) = - code >= CHARLIT && code <= INTERPOLATIONID + def isLiteral(code: Int) = code >= CHARLIT && code <= INTERPOLATIONID /** identifiers */ final val IDENTIFIER = 10 final val BACKQUOTED_IDENT = 11 - def isIdentifier(code: Int) = code >= IDENTIFIER && code <= BACKQUOTED_IDENT // used by ide - - /** keywords */ - final val IF = 20 - final val FOR = 21 - final val ELSE = 22 - final val THIS = 23 - final val NULL = 24 - final val NEW = 25 - final val WITH = 26 - final val SUPER = 27 - final val CASE = 28 - final val CASECLASS = 29 - final val CASEOBJECT = 30 - final val VAL = 31 - final val ABSTRACT = 32 - final val FINAL = 33 - final val PRIVATE = 34 - final val PROTECTED = 35 - final val OVERRIDE = 36 - final val IMPLICIT = 37 - final val VAR = 38 - final val DEF = 39 - final val TYPE = 40 - final val EXTENDS = 41 - final val TRUE = 42 - final val FALSE = 43 - final val OBJECT = 44 - final val CLASS = 45 - - final val IMPORT = 46 - final val PACKAGE = 47 - final val YIELD = 48 - final val DO = 49 - final val TRAIT = 50 - final val SEALED = 51 - final val THROW = 52 - final val TRY = 53 - final val CATCH = 54 - final val FINALLY = 55 - final val WHILE = 56 - final val RETURN = 57 - final val MATCH = 58 - final val FORSOME = 59 - final val LAZY = 61 - final val MACRO = 62 // not yet used in 2.10 - final val THEN = 63 // not yet used in 2.10 + def isIdentifier(code: Int) = code == IDENTIFIER || code == BACKQUOTED_IDENT // used by ide + + /** modifiers */ + final val IMPLICIT = 40 + final val OVERRIDE = 41 + final val SEALED = 45 + final val LAZY = 55 + final val MACRO = 57 + + /** templates */ + final val CASECLASS = 63 + final val OBJECT = 64 + final val CASEOBJECT = 65 + final val TRAIT = 66 + final val WITH = 69 + final val TYPE = 70 + final val FORSOME = 71 + final val DEF = 72 + final val VAL = 73 + final val VAR = 74 + + /** control structures */ + final val THEN = 81 + final val YIELD = 86 + final val MATCH = 95 /** special symbols */ - final val COMMA = 70 - final val SEMI = 71 - final val DOT = 72 - final val USCORE = 73 - final val COLON = 74 - final val EQUALS = 75 - final val LARROW = 76 - final val ARROW = 77 - final val NEWLINE = 78 - final val NEWLINES = 79 - final val SUBTYPE = 80 - final val SUPERTYPE = 81 - final val HASH = 82 - final val AT = 83 - final val VIEWBOUND = 84 - - /** parenthesis */ - final val LPAREN = 90 - final val RPAREN = 91 - final val LBRACKET = 92 - final val RBRACKET = 93 - final val LBRACE = 94 - final val RBRACE = 95 - - /** XML mode */ - final val XMLSTART = 96 + final val HASH = 130 + final val USCORE = 131 + final val ARROW = 132 + final val LARROW = 133 + final val SUBTYPE = 134 + final val SUPERTYPE = 135 + final val VIEWBOUND = 136 + final val NEWLINE = 137 + final val NEWLINES = 138 + final val XMLSTART = 139 /** for IDE only */ - final val COMMENT = 97 - - final val WHITESPACE = 105 - final val IGNORE = 106 - final val ESCAPE = 109 + final val COMMENT = 200 + final val WHITESPACE = 201 + final val IGNORE = 202 + final val ESCAPE = 203 } diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala index eb40e1dbde..b03519ce7d 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala @@ -171,10 +171,10 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM { var pickledBytes = 0 // statistics - val javaNameCache = perRunCaches.newMap[Symbol, Name]() + val javaNameCache = perRunCaches.newAnyRefMap[Symbol, Name]() // unlike javaNameCache, reverseJavaName contains entries only for class symbols and their internal names. - val reverseJavaName = perRunCaches.newMap[String, Symbol]() + val reverseJavaName = perRunCaches.newAnyRefMap[String, Symbol]() private def mkFlags(args: Int*) = args.foldLeft(0)(_ | _) private def hasPublicBitSet(flags: Int) = (flags & asm.Opcodes.ACC_PUBLIC) != 0 diff --git a/src/compiler/scala/tools/nsc/javac/JavaParsers.scala b/src/compiler/scala/tools/nsc/javac/JavaParsers.scala index 9875d27047..a61ad392ee 100644 --- a/src/compiler/scala/tools/nsc/javac/JavaParsers.scala +++ b/src/compiler/scala/tools/nsc/javac/JavaParsers.scala @@ -190,7 +190,7 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners { case GTGTGT => GTGT case GTGTEQ => GTEQ case GTGT => GT - case GTEQ => ASSIGN + case GTEQ => EQUALS } if (closers isDefinedAt in.token) in.token = closers(in.token) else accept(GT) @@ -538,7 +538,7 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners { in.nextToken() if (in.token == IDENTIFIER) { // if there's an ident after the comma ... val name = ident() - if (in.token == ASSIGN || in.token == SEMI) { // ... followed by a `=` or `;`, we know it's a real variable definition + if (in.token == EQUALS || in.token == SEMI) { // ... followed by a `=` or `;`, we know it's a real variable definition buf ++= maybe buf += varDecl(in.currentPos, mods, tpt.duplicate, name.toTermName) maybe.clear() @@ -563,7 +563,7 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners { def varDecl(pos: Position, mods: Modifiers, tpt: Tree, name: TermName): ValDef = { val tpt1 = optArrayBrackets(tpt) - if (in.token == ASSIGN && !mods.isParameter) skipTo(COMMA, SEMI) + if (in.token == EQUALS && !mods.isParameter) skipTo(COMMA, SEMI) val mods1 = if (mods.isFinal) mods &~ Flags.FINAL else mods | Flags.MUTABLE atPos(pos) { ValDef(mods1, name, tpt1, blankExpr) diff --git a/src/compiler/scala/tools/nsc/javac/JavaScanners.scala b/src/compiler/scala/tools/nsc/javac/JavaScanners.scala index b7ea70e2c7..c5401219dd 100644 --- a/src/compiler/scala/tools/nsc/javac/JavaScanners.scala +++ b/src/compiler/scala/tools/nsc/javac/JavaScanners.scala @@ -155,7 +155,6 @@ trait JavaScanners extends ast.parser.ScannersCommon { case AMP => "`&'" case AMPAMP => "`&&'" case AMPEQ => "`&='" - case ASSIGN => "`='" case ASTERISK => "`*'" case ASTERISKEQ => "`*='" case AT => "`@'" @@ -169,6 +168,7 @@ trait JavaScanners extends ast.parser.ScannersCommon { case DOT => "`.'" case DOTDOTDOT => "`...'" case EQEQ => "`=='" + case EQUALS => "`='" case GT => "`>'" case GTEQ => "`>='" case GTGT => "`>>'" @@ -337,7 +337,7 @@ trait JavaScanners extends ast.parser.ScannersCommon { return case '=' => - token = ASSIGN + token = EQUALS in.next() if (in.ch == '=') { token = EQEQ diff --git a/src/compiler/scala/tools/nsc/javac/JavaTokens.scala b/src/compiler/scala/tools/nsc/javac/JavaTokens.scala index 953a3c6d82..9b31e6e8a2 100644 --- a/src/compiler/scala/tools/nsc/javac/JavaTokens.scala +++ b/src/compiler/scala/tools/nsc/javac/JavaTokens.scala @@ -6,117 +6,89 @@ package scala.tools.nsc package javac -object JavaTokens extends ast.parser.Tokens { +object JavaTokens extends ast.parser.CommonTokens { - def isLiteral(code : Int) = + def isLiteral(code: Int) = code >= CHARLIT && code <= STRINGLIT /** identifiers */ final val IDENTIFIER = 10 - def isIdentifier(code : Int) = + def isIdentifier(code: Int) = code == IDENTIFIER /** keywords */ - final val ABSTRACT = 20 - final val ASSERT = 21 - final val BOOLEAN = 22 - final val BREAK = 23 - final val BYTE = 24 - final val CASE = 25 - final val CATCH = 26 - final val CHAR = 27 - final val CLASS = 28 - final val CONST = 29 - final val CONTINUE = 30 - final val DEFAULT = 31 - final val DO = 32 - final val DOUBLE = 33 - final val ELSE = 34 - final val ENUM = 35 - final val EXTENDS = 36 - final val FINAL = 37 - final val FINALLY = 38 - final val FLOAT = 39 - final val FOR = 40 - final val IF = 41 - final val GOTO = 42 - final val IMPLEMENTS = 43 - final val IMPORT = 44 - final val INSTANCEOF = 45 - final val INT = 46 - final val INTERFACE = 47 - final val LONG = 48 - final val NATIVE = 49 - final val NEW = 50 - final val PACKAGE = 51 - final val PRIVATE = 52 - final val PROTECTED = 53 - final val PUBLIC = 54 - final val RETURN = 55 - final val SHORT = 56 - final val STATIC = 57 - final val STRICTFP = 58 - final val SUPER = 59 - final val SWITCH = 60 - final val SYNCHRONIZED = 61 - final val THIS = 62 - final val THROW = 63 - final val THROWS = 64 - final val TRANSIENT = 65 - final val TRY = 66 - final val VOID = 67 - final val VOLATILE = 68 - final val WHILE = 69 + final val INSTANCEOF = 27 + final val CONST = 28 + + /** modifiers */ + final val PUBLIC = 42 + final val DEFAULT = 47 + final val STATIC = 48 + final val TRANSIENT = 50 + final val VOLATILE = 51 + final val SYNCHRONIZED = 52 + final val NATIVE = 53 + final val STRICTFP = 54 + final val THROWS = 56 + + /** templates */ + final val INTERFACE = 66 + final val ENUM = 67 + final val IMPLEMENTS = 69 + + /** control structures */ + final val BREAK = 87 + final val CONTINUE = 88 + final val GOTO = 89 + final val SWITCH = 94 + final val ASSERT = 98 /** special symbols */ - final val COMMA = 70 - final val SEMI = 71 - final val DOT = 72 - final val AT = 73 - final val COLON = 74 - final val ASSIGN = 75 - final val EQEQ = 76 - final val BANGEQ = 77 - final val LT = 78 - final val GT = 79 - final val LTEQ = 80 - final val GTEQ = 81 - final val BANG = 82 - final val QMARK = 83 - final val AMP = 84 - final val BAR = 85 - final val PLUS = 86 - final val MINUS = 87 - final val ASTERISK = 88 - final val SLASH = 89 - final val PERCENT = 90 - final val HAT = 91 - final val LTLT = 92 - final val GTGT = 93 - final val GTGTGT = 94 - final val AMPAMP = 95 - final val BARBAR = 96 - final val PLUSPLUS = 97 - final val MINUSMINUS = 98 - final val TILDE = 99 - final val DOTDOTDOT = 100 - final val AMPEQ = 104 - final val BAREQ = 105 - final val PLUSEQ = 106 - final val MINUSEQ = 107 - final val ASTERISKEQ = 1010 - final val SLASHEQ = 109 - final val PERCENTEQ = 110 - final val HATEQ = 111 - final val LTLTEQ = 112 - final val GTGTEQ = 113 - final val GTGTGTEQ = 114 + final val EQEQ = 140 + final val BANGEQ = 141 + final val LT = 142 + final val GT = 143 + final val LTEQ = 144 + final val GTEQ = 145 + final val BANG = 146 + final val QMARK = 147 + final val AMP = 148 + final val BAR = 149 + final val PLUS = 150 + final val MINUS = 151 + final val ASTERISK = 152 + final val SLASH = 153 + final val PERCENT = 154 + final val HAT = 155 + final val LTLT = 156 + final val GTGT = 157 + final val GTGTGT = 158 + final val AMPAMP = 159 + final val BARBAR = 160 + final val PLUSPLUS = 161 + final val MINUSMINUS = 162 + final val TILDE = 163 + final val DOTDOTDOT = 164 + final val AMPEQ = 165 + final val BAREQ = 166 + final val PLUSEQ = 167 + final val MINUSEQ = 168 + final val ASTERISKEQ = 169 + final val SLASHEQ = 170 + final val PERCENTEQ = 171 + final val HATEQ = 172 + final val LTLTEQ = 173 + final val GTGTEQ = 174 + final val GTGTGTEQ = 175 - /** parenthesis */ - final val LPAREN = 115 - final val RPAREN = 116 - final val LBRACKET = 117 - final val RBRACKET = 118 - final val LBRACE = 119 - final val RBRACE = 120 + /** primitive types */ + final val VOID = 180 + final val BOOLEAN = 181 + final val BYTE = 182 + final val SHORT = 183 + final val CHAR = 184 + final val INT = 185 + final val LONG = 186 + final val FLOAT = 187 + final val DOUBLE = 188 } diff --git a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala index 933a2f70a1..d81a5d5755 100644 --- a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala +++ b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala @@ -232,6 +232,10 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre val parents = addSerializable(abstractFunctionErasedType) val funOwner = originalFunction.symbol.owner + // TODO harmonize the naming of delamdafy anon-fun classes with those spun up by Uncurry + // - make `anonClass.isAnonymousClass` true. + // - use `newAnonymousClassSymbol` or push the required variations into a similar factory method + // - reinstate the assertion in `Erasure.resolveAnonymousBridgeClash` val suffix = "$lambda$" + ( if (funOwner.isPrimaryConstructor) "" else "$" + funOwner.name @@ -282,18 +286,11 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre if (sym == NoSymbol) sym.toString else s"$sym: ${sym.tpe} in ${sym.owner}" - def clashError(bm: Symbol) = { - unit.error( - applyMethodDef.symbol.pos, - sm"""bridge generated for member ${fulldef(applyMethodDef.symbol)} - |which overrides ${fulldef(getMember(abstractFunctionErasedType.typeSymbol, nme.apply))} - |clashes with definition of the member itself; - |both have erased type ${exitingPostErasure(bm.tpe)}""") - } - bridgeMethod foreach (bm => + // TODO SI-6260 maybe just create the apply method with the signature (Object => Object) in all cases + // rather than the method+bridge pair. if (bm.symbol.tpe =:= applyMethodDef.symbol.tpe) - clashError(bm.symbol) + erasure.resolveAnonymousBridgeClash(applyMethodDef.symbol, bm.symbol) ) val body = members ++ List(constr, applyMethodDef) ++ bridgeMethod diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala index ccfddab94a..60c1553ef3 100644 --- a/src/compiler/scala/tools/nsc/transform/Erasure.scala +++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala @@ -403,19 +403,19 @@ abstract class Erasure extends AddInterfaces * @param other The overidden symbol for which the bridge was generated * @param bridge The bridge */ - def checkBridgeOverrides(member: Symbol, other: Symbol, bridge: Symbol): Boolean = { + def checkBridgeOverrides(member: Symbol, other: Symbol, bridge: Symbol): Seq[(Position, String)] = { def fulldef(sym: Symbol) = if (sym == NoSymbol) sym.toString else s"$sym: ${sym.tpe} in ${sym.owner}" var noclash = true + val clashErrors = mutable.Buffer[(Position, String)]() def clashError(what: String) = { - noclash = false - unit.error( - if (member.owner == root) member.pos else root.pos, - sm"""bridge generated for member ${fulldef(member)} - |which overrides ${fulldef(other)} - |clashes with definition of $what; - |both have erased type ${exitingPostErasure(bridge.tpe)}""") + val pos = if (member.owner == root) member.pos else root.pos + val msg = sm"""bridge generated for member ${fulldef(member)} + |which overrides ${fulldef(other)} + |clashes with definition of $what; + |both have erased type ${exitingPostErasure(bridge.tpe)}""" + clashErrors += Tuple2(pos, msg) } for (bc <- root.baseClasses) { if (settings.debug) @@ -440,7 +440,7 @@ abstract class Erasure extends AddInterfaces } } } - noclash + clashErrors } /** TODO - work through this logic with a fine-toothed comb, incorporating @@ -478,8 +478,18 @@ abstract class Erasure extends AddInterfaces bridge setInfo (otpe cloneInfo bridge) bridgeTarget(bridge) = member - if (!(member.tpe exists (_.typeSymbol.isDerivedValueClass)) || - checkBridgeOverrides(member, other, bridge)) { + def sigContainsValueClass = (member.tpe exists (_.typeSymbol.isDerivedValueClass)) + + val shouldAdd = ( + !sigContainsValueClass + || (checkBridgeOverrides(member, other, bridge) match { + case Nil => true + case es if member.owner.isAnonymousClass => resolveAnonymousBridgeClash(member, bridge); true + case es => for ((pos, msg) <- es) unit.error(pos, msg); false + }) + ) + + if (shouldAdd) { exitingErasure(root.info.decls enter bridge) if (other.owner == root) { exitingErasure(root.info.decls.unlink(other)) @@ -1127,5 +1137,13 @@ abstract class Erasure extends AddInterfaces } } + final def resolveAnonymousBridgeClash(sym: Symbol, bridge: Symbol) { + // TODO reinstate this after Delambdafy generates anonymous classes that meet this requirement. + // require(sym.owner.isAnonymousClass, sym.owner) + log(s"Expanding name of ${sym.debugLocationString} as it clashes with bridge. Renaming deemed safe because the owner is anonymous.") + sym.expandName(sym.owner) + bridge.resetFlag(BRIDGE) + } + private class TypeRefAttachment(val tpe: TypeRef) } diff --git a/src/compiler/scala/tools/nsc/transform/PostErasure.scala b/src/compiler/scala/tools/nsc/transform/PostErasure.scala index cc78e27282..32987fed8c 100644 --- a/src/compiler/scala/tools/nsc/transform/PostErasure.scala +++ b/src/compiler/scala/tools/nsc/transform/PostErasure.scala @@ -8,7 +8,7 @@ package transform /** This phase maps ErasedValueTypes to the underlying unboxed representation and * performs peephole optimizations. */ -trait PostErasure extends InfoTransform with TypingTransformers { +trait PostErasure extends InfoTransform with TypingTransformers with scala.reflect.internal.transform.PostErasure { val global: Global import global._ @@ -19,16 +19,6 @@ trait PostErasure extends InfoTransform with TypingTransformers { def newTransformer(unit: CompilationUnit): Transformer = new PostErasureTransformer(unit) override def changesBaseClasses = false - object elimErasedValueType extends TypeMap { - def apply(tp: Type) = tp match { - case ConstantType(Constant(tp: Type)) => ConstantType(Constant(apply(tp))) - case ErasedValueType(_, underlying) => underlying - case _ => mapOver(tp) - } - } - - def transformInfo(sym: Symbol, tp: Type) = elimErasedValueType(tp) - class PostErasureTransformer(unit: CompilationUnit) extends TypingTransformer(unit) { override def transform(tree: Tree) = { def finish(res: Tree) = logResult(s"Posterasure reduction\n Old: $tree\n New")(res) diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index e5907e1a0f..c065fb54b7 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -89,6 +89,8 @@ trait Contexts { self: Analyzer => if (settings.noimports) Nil else if (unit.isJava) RootImports.javaList else if (settings.nopredef || treeInfo.noPredefImportForUnit(unit.body)) { + // SI-8258 Needed for the presentation compiler using -sourcepath, otherwise cycles can occur. See the commit + // message for this ticket for an example. debuglog("Omitted import of Predef._ for " + unit) RootImports.javaAndScalaList } @@ -457,7 +459,9 @@ trait Contexts { self: Analyzer => c.prefix = prefixInChild c.enclClass = if (isTemplateOrPackage) c else enclClass c(ConstructorSuffix) = !isTemplateOrPackage && c(ConstructorSuffix) - c.enclMethod = if (isDefDef) c else enclMethod + + // SI-8245 `isLazy` need to skip lazy getters to ensure `return` binds to the right place + c.enclMethod = if (isDefDef && !owner.isLazy) c else enclMethod registerContext(c.asInstanceOf[analyzer.Context]) debuglog("[context] ++ " + c.unit + " / " + tree.summaryString) diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala index 997fd6fc65..2d6c94349b 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala @@ -174,7 +174,8 @@ trait Infer extends Checkable { private lazy val stdErrorValue = stdErrorClass.newErrorValue(nme.ERROR) /** The context-dependent inferencer part */ - class Inferencer(context: Context) extends InferencerContextErrors with InferCheckable { + abstract class Inferencer extends InferencerContextErrors with InferCheckable { + def context: Context import InferErrorGen._ /* -- Error Messages --------------------------------------------------- */ diff --git a/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala b/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala index ec2b7d49f5..ba183fe3e6 100644 --- a/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala +++ b/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala @@ -427,7 +427,7 @@ trait MethodSynthesis { override def derivedSym = basisSym.lazyAccessor override def derivedTree: DefDef = { val ValDef(_, _, tpt0, rhs0) = tree - val rhs1 = transformed.getOrElse(rhs0, rhs0) + val rhs1 = context.unit.transformed.getOrElse(rhs0, rhs0) val body = ( if (tree.symbol.owner.isTrait || hasUnitType(basisSym)) rhs1 else gen.mkAssignAndReturn(basisSym, rhs1) diff --git a/src/compiler/scala/tools/nsc/typechecker/Namers.scala b/src/compiler/scala/tools/nsc/typechecker/Namers.scala index 632e25aa2e..60cae0c880 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Namers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Namers.scala @@ -1408,12 +1408,18 @@ trait Namers extends MethodSynthesis { if (expr1.isErrorTyped) ErrorType else { - if (!treeInfo.isStableIdentifierPattern(expr1)) - typer.TyperErrorGen.UnstableTreeError(expr1) + expr1 match { + case This(_) => + // SI-8207 okay, typedIdent expands Ident(self) to C.this which doesn't satisfy the next case + // TODO should we change `typedIdent` not to expand to the `Ident` to a `This`? + case _ if treeInfo.isStableIdentifierPattern(expr1) => + case _ => + typer.TyperErrorGen.UnstableTreeError(expr1) + } val newImport = treeCopy.Import(imp, expr1, selectors).asInstanceOf[Import] checkSelectors(newImport) - transformed(imp) = newImport + context.unit.transformed(imp) = newImport // copy symbol and type attributes back into old expression // so that the structure builder will find it. expr setSymbol expr1.symbol setType expr1.tpe diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 10fe530445..088aa5216a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -13,7 +13,7 @@ package scala package tools.nsc package typechecker -import scala.collection.{ mutable, immutable } +import scala.collection.{mutable, immutable} import scala.reflect.internal.util.{ BatchSourceFile, Statistics, shortClassOfInstance } import mutable.ListBuffer import symtab.Flags._ @@ -39,7 +39,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper // namer calls typer.computeType(rhs) on DefDef / ValDef when tpt is empty. the result // is cached here and re-used in typedDefDef / typedValDef // Also used to cache imports type-checked by namer. - val transformed = new mutable.HashMap[Tree, Tree] + val transformed = new mutable.AnyRefMap[Tree, Tree] final val shortenImports = false @@ -52,7 +52,6 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper //println("resetTyper called") resetContexts() resetImplicits() - transformed.clear() resetDocComments() } @@ -108,7 +107,10 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper val runDefinitions = currentRun.runDefinitions import runDefinitions._ - val infer = new Inferencer(context0) { + private val transformed: mutable.Map[Tree, Tree] = unit.transformed + + val infer = new Inferencer { + def context = Typer.this.context // See SI-3281 re undoLog override def isCoercible(tp: Type, pt: Type) = undoLog undo viewExists(tp, pt) } @@ -4901,6 +4903,9 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper ) arg match { case Bind(_, _) => enhanceBounds() + // TODO: consolidate fixes for SI-6169 and SI-1786 by dropping the Ident case, + // in favor of doing sharpenQuantifierBounds for all ExistentialTypes, not just java-defined ones + // (need to figure out how to sharpen the bounds on creation without running into cycles) case Ident(name) if canEnhanceIdent => enhanceBounds() case _ => } diff --git a/src/compiler/scala/tools/nsc/util/package.scala b/src/compiler/scala/tools/nsc/util/package.scala index 4237f36ade..bd95fdbb50 100644 --- a/src/compiler/scala/tools/nsc/util/package.scala +++ b/src/compiler/scala/tools/nsc/util/package.scala @@ -7,7 +7,7 @@ package scala package tools package nsc -import java.io.{ OutputStream, PrintStream, ByteArrayOutputStream, PrintWriter, StringWriter } +import java.io.{ OutputStream, PrintStream, ByteArrayOutputStream, PrintWriter, StringWriter, Reader } package object util { // forwarder for old code that builds against 2.9 and 2.10 @@ -46,6 +46,17 @@ package object util { (result, ts2 filterNot (ts1 contains _)) } + def stringFromReader(reader: Reader) = { + val writer = new StringWriter() + var c = reader.read() + while(c != -1) { + writer.write(c) + c = reader.read() + } + reader.close() + writer.toString() + } + /** Generate a string using a routine that wants to write on a stream. */ def stringFromWriter(writer: PrintWriter => Unit): String = { val stringWriter = new StringWriter() diff --git a/src/compiler/scala/tools/reflect/ReflectGlobal.scala b/src/compiler/scala/tools/reflect/ReflectGlobal.scala index f8ded56ec6..6f369212ad 100644 --- a/src/compiler/scala/tools/reflect/ReflectGlobal.scala +++ b/src/compiler/scala/tools/reflect/ReflectGlobal.scala @@ -12,9 +12,10 @@ class ReflectGlobal(currentSettings: Settings, reporter: Reporter, override val extends Global(currentSettings, reporter) with scala.tools.reflect.ReflectSetup with scala.reflect.runtime.SymbolTable { override def transformedType(sym: Symbol) = - erasure.transformInfo(sym, - uncurry.transformInfo(sym, - refChecks.transformInfo(sym, sym.info))) + postErasure.transformInfo(sym, + erasure.transformInfo(sym, + uncurry.transformInfo(sym, + refChecks.transformInfo(sym, sym.info)))) override def isCompilerUniverse = true diff --git a/src/compiler/scala/tools/reflect/ToolBox.scala b/src/compiler/scala/tools/reflect/ToolBox.scala index 02a458214f..4a3db09909 100644 --- a/src/compiler/scala/tools/reflect/ToolBox.scala +++ b/src/compiler/scala/tools/reflect/ToolBox.scala @@ -96,6 +96,15 @@ trait ToolBox[U <: scala.reflect.api.Universe] { */ def compile(tree: u.Tree): () => Any + /** Defines a top-level class, trait or module in this ToolBox, + * putting it into a uniquely-named package and returning a symbol that references the defined entity. + * For a ClassDef, a ClassSymbol is returned, and for a ModuleDef, a ModuleSymbol is returned (not a module class, but a module itself). + * + * This method can be used to generate definitions that will later be re-used by subsequent calls to + * `compile`, `define` or `eval`. To refer to the generated definition in a tree, use q"$sym". + */ + def define(tree: u.ImplDef): u.Symbol + /** Compiles and runs a tree using this ToolBox. * Is equivalent to `compile(tree)()`. */ diff --git a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala index 541a915adb..7bae3203c2 100644 --- a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala +++ b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala @@ -193,6 +193,19 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => analyzer.inferImplicit(tree, pt, isView, currentTyper.context, silent, withMacrosDisabled, pos, (pos, msg) => throw ToolBoxError(msg)) }) + private def wrapInPackageAndCompile(packageName: TermName, tree: ImplDef): Symbol = { + val pdef = PackageDef(Ident(packageName), List(tree)) + val unit = new CompilationUnit(NoSourceFile) + unit.body = pdef + + val run = new Run + reporter.reset() + run.compileUnits(List(unit), run.namerPhase) + throwIfErrors() + + tree.symbol + } + def compile(expr0: Tree): () => Any = { val expr = wrapIntoTerm(expr0) @@ -200,7 +213,7 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => val thunks = freeTerms map (fte => () => fte.value) // need to be lazy in order not to distort evaluation order verify(expr) - def wrap(expr0: Tree): ModuleDef = { + def wrapInModule(expr0: Tree): ModuleDef = { val (expr, freeTerms) = extractFreeTerms(expr0, wrapFreeTermRefs = true) val (obj, _) = rootMirror.EmptyPackageClass.newModuleAndClassSymbol( @@ -241,17 +254,10 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => cleanedUp.asInstanceOf[ModuleDef] } - val mdef = wrap(expr) - val pdef = PackageDef(Ident(mdef.name), List(mdef)) - val unit = new CompilationUnit(NoSourceFile) - unit.body = pdef + val mdef = wrapInModule(expr) + val msym = wrapInPackageAndCompile(mdef.name, mdef) - val run = new Run - reporter.reset() - run.compileUnits(List(unit), run.namerPhase) - throwIfErrors() - - val className = mdef.symbol.fullName + val className = msym.fullName if (settings.debug) println("generated: "+className) def moduleFileName(className: String) = className + "$" val jclazz = jClass.forName(moduleFileName(className), true, classLoader) @@ -278,6 +284,13 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => } } + def define(tree: ImplDef): Symbol = { + val freeTerms = tree.freeTerms + if (freeTerms.nonEmpty) throw ToolBoxError(s"reflective toolbox has failed: cannot have free terms in a top-level definition") + verify(tree) + wrapInPackageAndCompile(nextWrapperModuleName(), tree) + } + def parse(code: String): Tree = { reporter.reset() val tree = gen.mkTreeOrBlock(newUnitParser(code, "<toolbox>").parseStatsOrPackages()) @@ -413,6 +426,18 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => compiler.compile(ctree) } + def define(tree: u.ImplDef): u.Symbol = withCompilerApi { compilerApi => + import compilerApi._ + + if (compiler.settings.verbose) println("importing "+tree) + val ctree: compiler.ImplDef = importer.importTree(tree).asInstanceOf[compiler.ImplDef] + + if (compiler.settings.verbose) println("defining "+ctree) + val csym: compiler.Symbol = compiler.define(ctree) + val usym = exporter.importSymbol(csym) + usym + } + def eval(tree: u.Tree): Any = compile(tree)() } } diff --git a/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala b/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala index fcb8734644..9f6807fe17 100644 --- a/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala +++ b/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala @@ -200,7 +200,7 @@ trait Parsers { self: Quasiquotes => object PatternParser extends Parser { def entryPoint = { parser => - val pat = parser.noSeq.pattern1() + val pat = parser.noSeq.pattern() gen.patvarTransformer.transform(pat) } } diff --git a/src/interactive/scala/tools/nsc/interactive/Global.scala b/src/interactive/scala/tools/nsc/interactive/Global.scala index 0e897d6492..95027a26b1 100644 --- a/src/interactive/scala/tools/nsc/interactive/Global.scala +++ b/src/interactive/scala/tools/nsc/interactive/Global.scala @@ -534,7 +534,6 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "") threadId += 1 compileRunner = new PresentationCompilerThread(this, projectName) compileRunner.setDaemon(true) - compileRunner.start() compileRunner } @@ -638,6 +637,7 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "") unit.problems.clear() unit.body = EmptyTree unit.status = NotLoaded + unit.transformed.clear() } /** Parse unit and create a name index, unless this has already been done before */ @@ -1252,11 +1252,21 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "") forceSymbolsUsedByParser() + /** Start the compiler background thread and turn on thread confinement checks */ + private def finishInitialization(): Unit = { + // this flag turns on `assertCorrectThread checks` + initializing = false + + // Only start the thread if initialization was successful. A crash while forcing symbols (for example + // if the Scala library is not on the classpath) can leave running threads behind. See Scala IDE #1002016 + compileRunner.start() + } + /** The compiler has been initialized. Constructors are evaluated in textual order, - * so this is set to true only after all super constructors and the primary constructor + * if we reached here, all super constructors and the primary constructor * have been executed. */ - initializing = false + finishInitialization() } object CancelException extends Exception diff --git a/src/library-aux/scala/AnyRef.scala b/src/library-aux/scala/AnyRef.scala index 362fbcf0f5..8c1862e729 100644 --- a/src/library-aux/scala/AnyRef.scala +++ b/src/library-aux/scala/AnyRef.scala @@ -76,8 +76,8 @@ trait AnyRef extends Any { * @param arg0 the object to compare against this object for equality. * @return `true` if the receiver object is equivalent to the argument; `false` otherwise. */ - final def ==(that: AnyRef): Boolean = - if (this eq null) that eq null + final def ==(that: Any): Boolean = + if (this eq null) that.asInstanceOf[AnyRef] eq null else this equals that /** Create a copy of the receiver object. diff --git a/src/library/scala/collection/Iterable.scala b/src/library/scala/collection/Iterable.scala index 973efc447e..a5ab8efd5c 100644 --- a/src/library/scala/collection/Iterable.scala +++ b/src/library/scala/collection/Iterable.scala @@ -51,4 +51,4 @@ object Iterable extends TraversableFactory[Iterable] { } /** Explicit instantiation of the `Iterable` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractIterable[+A] extends AbstractTraversable[A] with Iterable[A] +abstract class AbstractIterable[+A] extends AbstractTraversable[A] with Iterable[A] diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 72a23a0dd0..01a0aa3b51 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -1171,4 +1171,4 @@ trait Iterator[+A] extends TraversableOnce[A] { } /** Explicit instantiation of the `Iterator` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractIterator[+A] extends Iterator[A] +abstract class AbstractIterator[+A] extends Iterator[A] diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala index 761b65723c..1e40fd8c24 100644 --- a/src/library/scala/collection/Map.scala +++ b/src/library/scala/collection/Map.scala @@ -56,4 +56,4 @@ object Map extends MapFactory[Map] { } /** Explicit instantiation of the `Map` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractMap[A, +B] extends AbstractIterable[(A, B)] with Map[A, B] +abstract class AbstractMap[A, +B] extends AbstractIterable[(A, B)] with Map[A, B] diff --git a/src/library/scala/collection/Seq.scala b/src/library/scala/collection/Seq.scala index b21acdd9b7..2f4b3e5f8a 100644 --- a/src/library/scala/collection/Seq.scala +++ b/src/library/scala/collection/Seq.scala @@ -38,4 +38,4 @@ object Seq extends SeqFactory[Seq] { } /** Explicit instantiation of the `Seq` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractSeq[+A] extends AbstractIterable[A] with Seq[A] +abstract class AbstractSeq[+A] extends AbstractIterable[A] with Seq[A] diff --git a/src/library/scala/collection/Set.scala b/src/library/scala/collection/Set.scala index 46d5dfa056..f74c26571a 100644 --- a/src/library/scala/collection/Set.scala +++ b/src/library/scala/collection/Set.scala @@ -44,4 +44,4 @@ object Set extends SetFactory[Set] { } /** Explicit instantiation of the `Set` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractSet[A] extends AbstractIterable[A] with Set[A] +abstract class AbstractSet[A] extends AbstractIterable[A] with Set[A] diff --git a/src/library/scala/collection/Traversable.scala b/src/library/scala/collection/Traversable.scala index 61d9a42f04..b53724c568 100644 --- a/src/library/scala/collection/Traversable.scala +++ b/src/library/scala/collection/Traversable.scala @@ -101,4 +101,4 @@ object Traversable extends TraversableFactory[Traversable] { self => } /** Explicit instantiation of the `Traversable` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractTraversable[+A] extends Traversable[A] +abstract class AbstractTraversable[+A] extends Traversable[A] diff --git a/src/library/scala/collection/immutable/Map.scala b/src/library/scala/collection/immutable/Map.scala index 8933c7cf77..5178d5a862 100644 --- a/src/library/scala/collection/immutable/Map.scala +++ b/src/library/scala/collection/immutable/Map.scala @@ -32,9 +32,9 @@ trait Map[A, +B] extends Iterable[(A, B)] with MapLike[A, B, Map[A, B]] { self => override def empty: Map[A, B] = Map.empty - + /** Returns this $coll as an immutable map. - * + * * A new map will not be built; lazy collections will stay lazy. */ @deprecatedOverriding("Immutable maps should do nothing on toMap except return themselves cast as a map.", "2.11.0") @@ -191,4 +191,4 @@ object Map extends ImmutableMapFactory[Map] { } /** Explicit instantiation of the `Map` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractMap[A, +B] extends scala.collection.AbstractMap[A, B] with Map[A, B] +abstract class AbstractMap[A, +B] extends scala.collection.AbstractMap[A, B] with Map[A, B] diff --git a/src/library/scala/collection/mutable/AnyRefMap.scala b/src/library/scala/collection/mutable/AnyRefMap.scala index 29c92a111c..47fb66744e 100644 --- a/src/library/scala/collection/mutable/AnyRefMap.scala +++ b/src/library/scala/collection/mutable/AnyRefMap.scala @@ -22,9 +22,9 @@ import generic.CanBuildFrom * on a map that will no longer have elements removed but will be * used heavily may save both time and storage space. * - * This map is not indended to contain more than 2^29 entries (approximately - * 500 million). The maximum capacity is 2^30, but performance will degrade - * rapidly as 2^30 is approached. + * This map is not intended to contain more than 2^29^ entries (approximately + * 500 million). The maximum capacity is 2^30^, but performance will degrade + * rapidly as 2^30^ is approached. * */ final class AnyRefMap[K <: AnyRef, V] private[collection] (defaultEntry: K => V, initialBufferSize: Int, initBlank: Boolean) @@ -291,24 +291,21 @@ extends AbstractMap[K, V] private[this] val vz = _values private[this] var index = 0 - private[this] var found = false - def hasNext = found || (index<hz.length && { + def hasNext: Boolean = index<hz.length && { var h = hz(index) - if (h+h != 0) found = true - else { + while (h+h == 0) { index += 1 - while (index < hz.length && { h = hz(index); h+h == 0 }) index += 1 - found = (index < hz.length) + if (index >= hz.length) return false + h = hz(index) } - found - }) + true + } - def next = { - if (found || hasNext) { - val ans = (_keys(index).asInstanceOf[K], _values(index).asInstanceOf[V]) + def next: (K, V) = { + if (hasNext) { + val ans = (kz(index).asInstanceOf[K], vz(index).asInstanceOf[V]) index += 1 - found = false ans } else throw new NoSuchElementException("next") diff --git a/src/library/scala/collection/mutable/Buffer.scala b/src/library/scala/collection/mutable/Buffer.scala index d2e33badbe..7ec7b06333 100644 --- a/src/library/scala/collection/mutable/Buffer.scala +++ b/src/library/scala/collection/mutable/Buffer.scala @@ -46,4 +46,4 @@ object Buffer extends SeqFactory[Buffer] { } /** Explicit instantiation of the `Buffer` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractBuffer[A] extends AbstractSeq[A] with Buffer[A] +abstract class AbstractBuffer[A] extends AbstractSeq[A] with Buffer[A] diff --git a/src/library/scala/collection/mutable/Iterable.scala b/src/library/scala/collection/mutable/Iterable.scala index f7a794e357..92313c9ccd 100644 --- a/src/library/scala/collection/mutable/Iterable.scala +++ b/src/library/scala/collection/mutable/Iterable.scala @@ -38,4 +38,4 @@ object Iterable extends TraversableFactory[Iterable] { } /** Explicit instantiation of the `Iterable` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractIterable[A] extends scala.collection.AbstractIterable[A] with Iterable[A] +abstract class AbstractIterable[A] extends scala.collection.AbstractIterable[A] with Iterable[A] diff --git a/src/library/scala/collection/mutable/Map.scala b/src/library/scala/collection/mutable/Map.scala index 01f6f725ab..fe29ce4221 100644 --- a/src/library/scala/collection/mutable/Map.scala +++ b/src/library/scala/collection/mutable/Map.scala @@ -89,4 +89,4 @@ object Map extends MutableMapFactory[Map] { } /** Explicit instantiation of the `Map` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractMap[A, B] extends scala.collection.AbstractMap[A, B] with Map[A, B] +abstract class AbstractMap[A, B] extends scala.collection.AbstractMap[A, B] with Map[A, B] diff --git a/src/library/scala/collection/mutable/Seq.scala b/src/library/scala/collection/mutable/Seq.scala index 11fbdd13f3..eafde70a2d 100644 --- a/src/library/scala/collection/mutable/Seq.scala +++ b/src/library/scala/collection/mutable/Seq.scala @@ -45,4 +45,4 @@ object Seq extends SeqFactory[Seq] { } /** Explicit instantiation of the `Seq` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractSeq[A] extends scala.collection.AbstractSeq[A] with Seq[A] +abstract class AbstractSeq[A] extends scala.collection.AbstractSeq[A] with Seq[A] diff --git a/src/library/scala/collection/mutable/Set.scala b/src/library/scala/collection/mutable/Set.scala index 4439880976..97574718e8 100644 --- a/src/library/scala/collection/mutable/Set.scala +++ b/src/library/scala/collection/mutable/Set.scala @@ -43,4 +43,4 @@ object Set extends MutableSetFactory[Set] { } /** Explicit instantiation of the `Set` trait to reduce class file size in subclasses. */ -private[scala] abstract class AbstractSet[A] extends AbstractIterable[A] with Set[A] +abstract class AbstractSet[A] extends AbstractIterable[A] with Set[A] diff --git a/src/library/scala/io/StdIn.scala b/src/library/scala/io/StdIn.scala index 36568b59b7..64836ecd6e 100644 --- a/src/library/scala/io/StdIn.scala +++ b/src/library/scala/io/StdIn.scala @@ -17,7 +17,7 @@ private[scala] trait StdIn { */ def readLine(): String = in.readLine() - /** Print formatted text to the default output and read a full line from the default input. + /** Print and flush formatted text to the default output, and read a full line from the default input. * Returns `null` if the end of the input stream has been reached. * * @param text the format of the text to print out, as in `printf`. @@ -26,6 +26,7 @@ private[scala] trait StdIn { */ def readLine(text: String, args: Any*): String = { printf(text, args: _*) + out.flush() readLine() } diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala index b70426a145..6743b9e42a 100644 --- a/src/library/scala/util/matching/Regex.scala +++ b/src/library/scala/util/matching/Regex.scala @@ -647,9 +647,10 @@ object Regex { /** A class to step through a sequence of regex matches. * * All methods inherited from [[scala.util.matching.Regex.MatchData]] will throw - * an [[java.lang.IllegalStateException]] until the matcher is initialized by - * calling `hasNext` or `next()` or causing these methods to be called, such as - * by invoking `toString` or iterating through the iterator's elements. + * a [[java.lang.IllegalStateException]] until the matcher is initialized. The + * matcher can be initialized by calling `hasNext` or `next()` or causing these + * methods to be called, such as by invoking `toString` or iterating through + * the iterator's elements. * * @see [[java.util.regex.Matcher]] */ diff --git a/src/reflect/scala/reflect/api/StandardLiftables.scala b/src/reflect/scala/reflect/api/StandardLiftables.scala index 5a03996dd9..a1292d04ef 100644 --- a/src/reflect/scala/reflect/api/StandardLiftables.scala +++ b/src/reflect/scala/reflect/api/StandardLiftables.scala @@ -143,7 +143,7 @@ trait StandardLiftables { self: Universe => case Apply(ScalaDot(symbol), List(Literal(Constant(name: String)))) if symbol == nme.Symbol => scala.Symbol(name) } - implicit def unliftName[T <: Name : ClassTag]: Unliftable[T] = Unliftable[T] { case Ident(name: T) => name; case Bind(name: T, Ident(nme.WILDCARD)) => name} + implicit def unliftName[T <: Name : ClassTag]: Unliftable[T] = Unliftable[T] { case Ident(name: T) => name; case Bind(name: T, Ident(nme.WILDCARD)) => name } implicit def unliftType: Unliftable[Type] = Unliftable[Type] { case tt: TypeTree if tt.tpe != null => tt.tpe } implicit def unliftConstant: Unliftable[Constant] = Unliftable[Constant] { case Literal(const) => const } diff --git a/src/reflect/scala/reflect/api/TypeTags.scala b/src/reflect/scala/reflect/api/TypeTags.scala index 7457910226..be76758224 100644 --- a/src/reflect/scala/reflect/api/TypeTags.scala +++ b/src/reflect/scala/reflect/api/TypeTags.scala @@ -134,7 +134,7 @@ import scala.language.implicitConversions * reflection APIs provided by Java (for classes) and Scala (for types).</li> * * <li>'''Certain manifest operations(i.e., <:<, >:> and typeArguments) are not - * supported.''' <br/>Instead, one culd use the reflection APIs provided by Java (for + * supported.''' <br/>Instead, one could use the reflection APIs provided by Java (for * classes) and Scala (for types).</li> *</ul> * diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index 7a0c70caf6..78e639fdff 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -35,7 +35,8 @@ trait Definitions extends api.StandardDefinitions { private def newMethod(owner: Symbol, name: TermName, formals: List[Type], restpe: Type, flags: Long): MethodSymbol = { val msym = owner.newMethod(name.encode, NoPosition, flags) val params = msym.newSyntheticValueParams(formals) - msym setInfo MethodType(params, restpe) markAllCompleted + val info = if (owner.isJavaDefined) JavaMethodType(params, restpe) else MethodType(params, restpe) + msym setInfo info markAllCompleted } private def enterNewMethod(owner: Symbol, name: TermName, formals: List[Type], restpe: Type, flags: Long = 0L): MethodSymbol = owner.info.decls enter newMethod(owner, name, formals, restpe, flags) @@ -904,8 +905,14 @@ trait Definitions extends api.StandardDefinitions { existentialAbstraction(clazz.unsafeTypeParams, clazz.tpe_*) // members of class scala.Any + + // TODO these aren't final! They are now overriden in AnyRef/Object. Prior to the fix + // for SI-8129, they were actually *overloaded* by the members in AnyRef/Object. + // We should unfinalize these, override in AnyValClass, and make the overrides final. + // Refchecks never actually looks at these, so its just for consistency. lazy val Any_== = enterNewMethod(AnyClass, nme.EQ, AnyTpe :: Nil, BooleanTpe, FINAL) lazy val Any_!= = enterNewMethod(AnyClass, nme.NE, AnyTpe :: Nil, BooleanTpe, FINAL) + lazy val Any_equals = enterNewMethod(AnyClass, nme.equals_, AnyTpe :: Nil, BooleanTpe) lazy val Any_hashCode = enterNewMethod(AnyClass, nme.hashCode_, Nil, IntTpe) lazy val Any_toString = enterNewMethod(AnyClass, nme.toString_, Nil, StringTpe) @@ -1012,8 +1019,8 @@ trait Definitions extends api.StandardDefinitions { // members of class java.lang.{ Object, String } lazy val Object_## = enterNewMethod(ObjectClass, nme.HASHHASH, Nil, IntTpe, FINAL) - lazy val Object_== = enterNewMethod(ObjectClass, nme.EQ, AnyRefTpe :: Nil, BooleanTpe, FINAL) - lazy val Object_!= = enterNewMethod(ObjectClass, nme.NE, AnyRefTpe :: Nil, BooleanTpe, FINAL) + lazy val Object_== = enterNewMethod(ObjectClass, nme.EQ, AnyTpe :: Nil, BooleanTpe, FINAL) + lazy val Object_!= = enterNewMethod(ObjectClass, nme.NE, AnyTpe :: Nil, BooleanTpe, FINAL) lazy val Object_eq = enterNewMethod(ObjectClass, nme.eq, AnyRefTpe :: Nil, BooleanTpe, FINAL) lazy val Object_ne = enterNewMethod(ObjectClass, nme.ne, AnyRefTpe :: Nil, BooleanTpe, FINAL) lazy val Object_isInstanceOf = newT1NoParamsMethod(ObjectClass, nme.isInstanceOf_Ob, FINAL | SYNTHETIC | ARTIFACT)(_ => BooleanTpe) diff --git a/src/reflect/scala/reflect/internal/Required.scala b/src/reflect/scala/reflect/internal/Required.scala index 14db252a16..009bc39d4c 100644 --- a/src/reflect/scala/reflect/internal/Required.scala +++ b/src/reflect/scala/reflect/internal/Required.scala @@ -6,6 +6,9 @@ import settings.MutableSettings trait Required { self: SymbolTable => def picklerPhase: Phase + + def erasurePhase: Phase + def settings: MutableSettings @deprecated("Interactive is implemented with a custom Global; this flag is ignored", "2.11.0") def forInteractive = false diff --git a/src/reflect/scala/reflect/internal/SymbolTable.scala b/src/reflect/scala/reflect/internal/SymbolTable.scala index 571c4cfa5d..802bd18a4e 100644 --- a/src/reflect/scala/reflect/internal/SymbolTable.scala +++ b/src/reflect/scala/reflect/internal/SymbolTable.scala @@ -371,6 +371,8 @@ abstract class SymbolTable extends macros.Universe def newMap[K, V]() = recordCache(mutable.HashMap[K, V]()) def newSet[K]() = recordCache(mutable.HashSet[K]()) def newWeakSet[K <: AnyRef]() = recordCache(new WeakHashSet[K]()) + + def newAnyRefMap[K <: AnyRef, V]() = recordCache(mutable.AnyRefMap[K, V]()) def newGeneric[T](f: => T): () => T = { val NoCached: T = null.asInstanceOf[T] var cached: T = NoCached diff --git a/src/reflect/scala/reflect/internal/TreeInfo.scala b/src/reflect/scala/reflect/internal/TreeInfo.scala index 7bab15b0f4..8cad2497c1 100644 --- a/src/reflect/scala/reflect/internal/TreeInfo.scala +++ b/src/reflect/scala/reflect/internal/TreeInfo.scala @@ -735,6 +735,17 @@ abstract class TreeInfo { unapply(dissectApplied(tree)) } + /** Does list of trees start with a definition of + * a class of module with given name (ignoring imports) + */ + def firstDefinesClassOrObject(trees: List[Tree], name: Name): Boolean = trees match { + case Import(_, _) :: xs => firstDefinesClassOrObject(xs, name) + case Annotated(_, tree1) :: _ => firstDefinesClassOrObject(List(tree1), name) + case ModuleDef(_, `name`, _) :: _ => true + case ClassDef(_, `name`, _, _) :: _ => true + case _ => false + } + /** Locates the synthetic Apply node corresponding to an extractor's call to * unapply (unwrapping nested Applies) and returns the fun part of that Apply. */ @@ -750,8 +761,7 @@ abstract class TreeInfo { } /** Is this file the body of a compilation unit which should not - * have Predef imported? This is the case iff the first import in the - * unit explicitly refers to Predef. + * have Predef imported? */ def noPredefImportForUnit(body: Tree) = { // Top-level definition whose leading imports include Predef. @@ -760,7 +770,13 @@ abstract class TreeInfo { case Import(expr, _) => isReferenceToPredef(expr) case _ => false } - isLeadingPredefImport(body) + // Compilation unit is class or object 'name' in package 'scala' + def isUnitInScala(tree: Tree, name: Name) = tree match { + case PackageDef(Ident(nme.scala_), defs) => firstDefinesClassOrObject(defs, name) + case _ => false + } + + isUnitInScala(body, nme.Predef) || isLeadingPredefImport(body) } def isAbsTypeDef(tree: Tree) = tree match { diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala index 74b2208278..17a58d79f6 100644 --- a/src/reflect/scala/reflect/internal/Types.scala +++ b/src/reflect/scala/reflect/internal/Types.scala @@ -899,7 +899,7 @@ trait Types -1 } - /** If this is a poly- or methodtype, a copy with cloned type / value parameters + /** If this is a ExistentialType, PolyType or MethodType, a copy with cloned type / value parameters * owned by `owner`. Identity for all other types. */ def cloneInfo(owner: Symbol) = this @@ -2683,8 +2683,57 @@ trait Types override def baseTypeSeq = underlying.baseTypeSeq map maybeRewrap override def isHigherKinded = false - override def skolemizeExistential(owner: Symbol, origin: AnyRef) = + /** [SI-6169, SI-8197 -- companion to SI-1786] + * + * Approximation to improve the bounds of a Java-defined existential type, + * based on the bounds of the type parameters of the quantified type + * In Scala syntax, given a java-defined class C[T <: String], the existential type C[_] + * is improved to C[_ <: String] before skolemization, which captures (get it?) what Java does: + * enter the type paramers' bounds into the context when checking subtyping/type equality of existential types + * + * (Also tried doing this once during class file parsing or when creating the existential type, + * but that causes cyclic errors because it happens too early.) + * + * TODO: figure out how to do this earlier without running into cycles, so this can subsume the fix for SI-1786 + */ + private def sharpenQuantifierBounds(): Unit = { + /* Check that we're looking at rawToExistential's handiwork + * (`existentialAbstraction(eparams, typeRef(apply(pre), sym, eparams map (_.tpe)))`). + * We can't do this sharpening there because we'll run into cycles. + */ + def rawToExistentialCreatedMe = (quantified corresponds underlying.typeArgs){ (q, a) => q.tpe =:= a } + + if (underlying.typeSymbol.isJavaDefined && rawToExistentialCreatedMe) { + val tpars = underlying.typeSymbol.initialize.typeParams // TODO: is initialize needed? + debuglog(s"sharpen bounds: $this | ${underlying.typeArgs.map(_.typeSymbol)} <-- ${tpars.map(_.info)}") + + foreach2(quantified, tpars) { (quant, tparam) => + // TODO: check `tparam.info.substSym(tpars, quantified) <:< quant.info` instead (for some weird reason not working for test/t6169/ExistF) + // for now, crude approximation for the common case + if (quant.info.bounds.isEmptyBounds && !tparam.info.bounds.isEmptyBounds) { + // avoid creating cycles [pos/t2940] that consist of an existential quantifier's + // bounded by an existential type that unhygienically has that quantifier as its own quantifier + // (TODO: clone latter existential with fresh quantifiers -- not covering this case for now) + if ((existentialsInType(tparam.info) intersect quantified).isEmpty) + quant setInfo tparam.info.substSym(tpars, quantified) + } + } + } + + _sharpenQuantifierBounds = false + } + private[this] var _sharpenQuantifierBounds = true + + override def skolemizeExistential(owner: Symbol, origin: AnyRef) = { + // do this here because it's quite close to what Java does: + // when checking subtyping/type equality, enter constraints + // derived from the existentially quantified type into the typing environment + // (aka \Gamma, which tracks types for variables and constraints/kinds for types) + // as a nice bonus, delaying this until we need it avoids cyclic errors + if (_sharpenQuantifierBounds) sharpenQuantifierBounds + deriveType(quantified, tparam => (owner orElse tparam.owner).newExistentialSkolem(tparam, origin))(underlying) + } private def wildcardArgsString(qset: Set[Symbol], args: List[Type]): List[String] = args map { case TypeRef(_, sym, _) if (qset contains sym) => @@ -2905,6 +2954,8 @@ trait Types override def params: List[Symbol] = zippedArgs map (_._1) override def typeArgs: List[Type] = zippedArgs map (_._2) + + override def safeToString: String = super.safeToString + typeArgs.map(_.safeToString).mkString("[", ", ", "]") } trait UntouchableTypeVar extends TypeVar { @@ -3025,15 +3076,19 @@ trait Types def addLoBound(tp: Type, isNumericBound: Boolean = false) { assert(tp != this, tp) // implies there is a cycle somewhere (?) //println("addLoBound: "+(safeToString, debugString(tp))) //DEBUG - undoLog record this - constr.addLoBound(tp, isNumericBound) + if (!sharesConstraints(tp)) { + undoLog record this + constr.addLoBound(tp, isNumericBound) + } } def addHiBound(tp: Type, isNumericBound: Boolean = false) { // assert(tp != this) //println("addHiBound: "+(safeToString, debugString(tp))) //DEBUG - undoLog record this - constr.addHiBound(tp, isNumericBound) + if (!sharesConstraints(tp)) { + undoLog record this + constr.addHiBound(tp, isNumericBound) + } } // </region> @@ -3045,6 +3100,16 @@ trait Types case ConstantTrue => true case tv: TypeVar => tv.suspended } + + /** `AppliedTypeVar`s share the same `TypeConstraint` with the `HKTypeVar` that it was spawned from. + * A type inference session can also have more than one ATV. + * If we don't detect that, we end up with "cyclic constraint" when we try to instantiate type parameters + * after solving in, pos/t8237 + */ + protected final def sharesConstraints(other: Type): Boolean = other match { + case other: TypeVar => constr == other.constr // SI-8237 avoid cycles. Details in pos/t8237.scala + case _ => false + } private[Types] def suspended_=(b: Boolean): Unit = _suspended = if (b) ConstantTrue else ConstantFalse // SI-7785 Link the suspended attribute of a TypeVar created in, say, a TypeMap (e.g. AsSeenFrom) to its originator private[Types] def linkSuspended(origin: TypeVar): Unit = _suspended = origin diff --git a/src/reflect/scala/reflect/internal/tpe/TypeConstraints.scala b/src/reflect/scala/reflect/internal/tpe/TypeConstraints.scala index e2159d30f5..564cbb1ce3 100644 --- a/src/reflect/scala/reflect/internal/tpe/TypeConstraints.scala +++ b/src/reflect/scala/reflect/internal/tpe/TypeConstraints.scala @@ -257,6 +257,12 @@ private[internal] trait TypeConstraints { // println("solving "+tvars+"/"+tparams+"/"+(tparams map (_.info))) foreach3(tvars, tparams, variances)(solveOne) - tvars forall (tv => tv.instWithinBounds || util.andFalse(log(s"Inferred type for ${tv.originString} does not conform to bounds: ${tv.constr}"))) + + def logBounds(tv: TypeVar) = log { + val what = if (!tv.instValid) "is invalid" else s"does not conform to bounds: ${tv.constr}" + s"Inferred type for ${tv.originString} (${tv.inst}) $what" + } + + tvars forall (tv => tv.instWithinBounds || util.andFalse(logBounds(tv))) } } diff --git a/src/reflect/scala/reflect/internal/transform/PostErasure.scala b/src/reflect/scala/reflect/internal/transform/PostErasure.scala new file mode 100644 index 0000000000..f0c7d0f050 --- /dev/null +++ b/src/reflect/scala/reflect/internal/transform/PostErasure.scala @@ -0,0 +1,19 @@ +package scala.reflect +package internal +package transform + +trait PostErasure { + val global: SymbolTable + import global._ + import definitions._ + + object elimErasedValueType extends TypeMap { + def apply(tp: Type) = tp match { + case ConstantType(Constant(tp: Type)) => ConstantType(Constant(apply(tp))) + case ErasedValueType(_, underlying) => underlying + case _ => mapOver(tp) + } + } + + def transformInfo(sym: Symbol, tp: Type) = elimErasedValueType(tp) +} diff --git a/src/reflect/scala/reflect/internal/transform/Transforms.scala b/src/reflect/scala/reflect/internal/transform/Transforms.scala index fa185db22f..296ccde443 100644 --- a/src/reflect/scala/reflect/internal/transform/Transforms.scala +++ b/src/reflect/scala/reflect/internal/transform/Transforms.scala @@ -26,17 +26,20 @@ trait Transforms { self: SymbolTable => private val refChecksLazy = new Lazy(new { val global: Transforms.this.type = self } with RefChecks) private val uncurryLazy = new Lazy(new { val global: Transforms.this.type = self } with UnCurry) private val erasureLazy = new Lazy(new { val global: Transforms.this.type = self } with Erasure) + private val postErasureLazy = new Lazy(new { val global: Transforms.this.type = self } with PostErasure) def refChecks = refChecksLazy.force def uncurry = uncurryLazy.force def erasure = erasureLazy.force + def postErasure = postErasureLazy.force def transformedType(sym: Symbol) = - erasure.transformInfo(sym, - uncurry.transformInfo(sym, - refChecks.transformInfo(sym, sym.info))) + postErasure.transformInfo(sym, + erasure.transformInfo(sym, + uncurry.transformInfo(sym, + refChecks.transformInfo(sym, sym.info)))) def transformedType(tpe: Type) = - erasure.scalaErasure(uncurry.uncurry(tpe)) + postErasure.elimErasedValueType(erasure.scalaErasure(uncurry.uncurry(tpe))) } diff --git a/src/reflect/scala/reflect/runtime/JavaMirrors.scala b/src/reflect/scala/reflect/runtime/JavaMirrors.scala index 68c67bb1f8..bc95b839b7 100644 --- a/src/reflect/scala/reflect/runtime/JavaMirrors.scala +++ b/src/reflect/scala/reflect/runtime/JavaMirrors.scala @@ -16,7 +16,7 @@ import java.io.IOException import scala.reflect.internal.{ MissingRequirementError, JavaAccFlags, JMethodOrConstructor } import internal.pickling.ByteCodecs import internal.pickling.UnPickler -import scala.collection.mutable.{ HashMap, ListBuffer } +import scala.collection.mutable.{ HashMap, ListBuffer, ArrayBuffer } import internal.Flags._ import ReflectionUtils._ import scala.language.existentials @@ -118,15 +118,16 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni private def abort(msg: String) = throw new ScalaReflectionException(msg) - private def ErrorInnerClass(sym: Symbol) = abort(s"$sym is an inner class, use reflectClass on an InstanceMirror to obtain its ClassMirror") - private def ErrorInnerModule(sym: Symbol) = abort(s"$sym is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror") - private def ErrorStaticClass(sym: Symbol) = abort(s"$sym is a static class, use reflectClass on a RuntimeMirror to obtain its ClassMirror") - private def ErrorStaticModule(sym: Symbol) = abort(s"$sym is a static module, use reflectModule on a RuntimeMirror to obtain its ModuleMirror") - private def ErrorNotMember(sym: Symbol, owner: Symbol) = abort(s"expected a member of $owner, you provided ${sym.kindString} ${sym.fullName}") - private def ErrorNotField(sym: Symbol) = abort(s"expected a field or an accessor method symbol, you provided $sym") - private def ErrorNotConstructor(sym: Symbol, owner: Symbol) = abort(s"expected a constructor of $owner, you provided $sym") - private def ErrorFree(member: Symbol, freeType: Symbol) = abort(s"cannot reflect ${member.kindString} ${member.name}, because it's a member of a weak type ${freeType.name}") - private def ErrorNonExistentField(sym: Symbol) = abort( + private def ErrorInnerClass(sym: Symbol) = abort(s"$sym is an inner class, use reflectClass on an InstanceMirror to obtain its ClassMirror") + private def ErrorInnerModule(sym: Symbol) = abort(s"$sym is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror") + private def ErrorStaticClass(sym: Symbol) = abort(s"$sym is a static class, use reflectClass on a RuntimeMirror to obtain its ClassMirror") + private def ErrorStaticModule(sym: Symbol) = abort(s"$sym is a static module, use reflectModule on a RuntimeMirror to obtain its ModuleMirror") + private def ErrorNotMember(sym: Symbol, owner: Symbol) = abort(s"expected a member of $owner, you provided ${sym.kindString} ${sym.fullName}") + private def ErrorNotField(sym: Symbol) = abort(s"expected a field or an accessor method symbol, you provided $sym") + private def ErrorNotConstructor(sym: Symbol, owner: Symbol) = abort(s"expected a constructor of $owner, you provided $sym") + private def ErrorArrayConstructor(sym: Symbol, owner: Symbol) = abort(s"Cannot instantiate arrays with mirrors. Consider using `scala.reflect.ClassTag(<class of element>).newArray(<length>)` instead") + private def ErrorFree(member: Symbol, freeType: Symbol) = abort(s"cannot reflect ${member.kindString} ${member.name}, because it's a member of a weak type ${freeType.name}") + private def ErrorNonExistentField(sym: Symbol) = abort( sm"""Scala field ${sym.name} isn't represented as a Java field, neither it has a Java accessor method |note that private parameters of class constructors don't get mapped onto fields and/or accessors, |unless they are used outside of their declaring constructors.""") @@ -221,6 +222,7 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni private def checkConstructorOf(sym: Symbol, owner: ClassSymbol) { if (!sym.isClassConstructor) ErrorNotConstructor(sym, owner) + if (owner == ArrayClass) ErrorArrayConstructor(sym, owner) ensuringNotFree(sym) { if (!owner.info.decls.toList.contains(sym)) ErrorNotConstructor(sym, owner) } @@ -247,7 +249,7 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni } def reflectMethod(method: MethodSymbol): MethodMirror = { checkMemberOf(method, symbol) - mkJavaMethodMirror(instance, method) + mkMethodMirror(instance, method) } def reflectClass(cls: ClassSymbol): ClassMirror = { if (cls.isStatic) ErrorStaticClass(cls) @@ -262,16 +264,35 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni override def toString = s"instance mirror for $instance" } - private class JavaFieldMirror(val receiver: Any, val symbol: TermSymbol) + // caches value class metadata, so that we minimize the work that needs to be done during Mirror.apply + private class DerivedValueClassMetadata(info: Type) { + val symbol = info.typeSymbol + val isDerivedValueClass = symbol.isDerivedValueClass + lazy val boxer = runtimeClass(symbol.toType).getDeclaredConstructors().head + lazy val unboxer = { + val fields @ (field :: _) = symbol.toType.declarations.collect{ case ts: TermSymbol if ts.isParamAccessor && ts.isMethod => ts }.toList + assert(fields.length == 1, s"$symbol: $fields") + runtimeClass(symbol.asClass).getDeclaredMethod(field.name.toString) + } + } + + private class JavaFieldMirror(val receiver: Any, val symbol: TermSymbol, metadata: DerivedValueClassMetadata) extends FieldMirror { + def this(receiver: Any, symbol: TermSymbol) = this(receiver, symbol, new DerivedValueClassMetadata(symbol.info)) + def bind(newReceiver: Any) = new JavaFieldMirror(newReceiver, symbol, metadata) + import metadata._ + lazy val jfield = ensureAccessible(fieldToJava(symbol)) - def get = jfield get receiver + def get = { + val value = jfield get receiver + if (isDerivedValueClass) boxer.newInstance(value) else value + } def set(value: Any) = { // it appears useful to be able to set values of vals, therefore I'm disabling this check // if (!symbol.isMutable) ErrorSetImmutableField(symbol) - jfield.set(receiver, value) + jfield.set(receiver, if (isDerivedValueClass) unboxer.invoke(value) else value) } - def bind(newReceiver: Any) = new JavaFieldMirror(newReceiver, symbol) + override def toString = s"field mirror for ${symbol.fullName} (bound to $receiver)" } @@ -312,13 +333,17 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni bytecodelessMethodOwners(meth.owner) && !bytecodefulObjectMethods(meth) } + private def isByNameParam(p: Type) = isByNameParamType(p) + private def isValueClassParam(p: Type) = p.typeSymbol.isDerivedValueClass + // unlike other mirrors, method mirrors are created by a factory // that's because we want to have decent performance // therefore we move special cases into separate subclasses // rather than have them on a hot path them in a unified implementation of the `apply` method - private def mkJavaMethodMirror[T: ClassTag](receiver: T, symbol: MethodSymbol): JavaMethodMirror = { - if (isBytecodelessMethod(symbol)) new JavaBytecodelessMethodMirror(receiver, symbol) - else if (symbol.paramss.flatten exists (p => isByNameParamType(p.info))) new JavaByNameMethodMirror(receiver, symbol) + private def mkMethodMirror[T: ClassTag](receiver: T, symbol: MethodSymbol): MethodMirror = { + def existsParam(pred: Type => Boolean) = symbol.paramss.flatten.map(_.info).exists(pred) + if (isBytecodelessMethod(symbol)) new BytecodelessMethodMirror(receiver, symbol) + else if (existsParam(isByNameParam) || existsParam(isValueClassParam)) new JavaTransformingMethodMirror(receiver, symbol) else { symbol.paramss.flatten.length match { case 0 => new JavaVanillaMethodMirror0(receiver, symbol) @@ -330,68 +355,123 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni } } } - private abstract class JavaMethodMirror(val symbol: MethodSymbol) extends MethodMirror { + + private abstract class JavaMethodMirror(val symbol: MethodSymbol, protected val ret: DerivedValueClassMetadata) extends MethodMirror { lazy val jmeth = ensureAccessible(methodToJava(symbol)) - def jinvokeraw(jmeth: jMethod, receiver: Any, args: Seq[Any]) = jmeth.invoke(receiver, args.asInstanceOf[Seq[AnyRef]]: _*) + lazy val jconstr = ensureAccessible(constructorToJava(symbol)) - def jinvoke(jmeth: jMethod, receiver: Any, args: Seq[Any]): Any = { - val result = jinvokeraw(jmeth, receiver, args) - if (jmeth.getReturnType == java.lang.Void.TYPE) () + def jinvokeraw(args: Seq[Any]) = + if (!symbol.isConstructor) jmeth.invoke(receiver, args.asInstanceOf[Seq[AnyRef]]: _*) + else if (receiver == null) jconstr.newInstance(args.asInstanceOf[Seq[AnyRef]]: _*) + else jconstr.newInstance((receiver +: args).asInstanceOf[Seq[AnyRef]]: _*) + def jinvoke(args: Seq[Any]): Any = { + val result = jinvokeraw(args) + if (!symbol.isConstructor && jmeth.getReturnType == java.lang.Void.TYPE) () + else if (!symbol.isConstructor && ret.isDerivedValueClass) ret.boxer.newInstance(result.asInstanceOf[AnyRef]) else result } - override def toString = s"method mirror for ${showMethodSig(symbol)} (bound to $receiver)" - } - - private class JavaVanillaMethodMirror(val receiver: Any, symbol: MethodSymbol) - extends JavaMethodMirror(symbol) { - def bind(newReceiver: Any) = new JavaVanillaMethodMirror(newReceiver, symbol) - def apply(args: Any*): Any = jinvoke(jmeth, receiver, args) - } - - private class JavaVanillaMethodMirror0(receiver: Any, symbol: MethodSymbol) - extends JavaVanillaMethodMirror(receiver, symbol) { - override def bind(newReceiver: Any) = new JavaVanillaMethodMirror0(newReceiver, symbol) - override def jinvokeraw(jmeth: jMethod, receiver: Any, args: Seq[Any]) = jmeth.invoke(receiver) - } - - private class JavaVanillaMethodMirror1(receiver: Any, symbol: MethodSymbol) - extends JavaVanillaMethodMirror(receiver, symbol) { - override def bind(newReceiver: Any) = new JavaVanillaMethodMirror1(newReceiver, symbol) - override def jinvokeraw(jmeth: jMethod, receiver: Any, args: Seq[Any]) = jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef]) - } - - private class JavaVanillaMethodMirror2(receiver: Any, symbol: MethodSymbol) - extends JavaVanillaMethodMirror(receiver, symbol) { - override def bind(newReceiver: Any) = new JavaVanillaMethodMirror2(newReceiver, symbol) - override def jinvokeraw(jmeth: jMethod, receiver: Any, args: Seq[Any]) = jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef]) - } - - private class JavaVanillaMethodMirror3(receiver: Any, symbol: MethodSymbol) - extends JavaVanillaMethodMirror(receiver, symbol) { - override def bind(newReceiver: Any) = new JavaVanillaMethodMirror3(newReceiver, symbol) - override def jinvokeraw(jmeth: jMethod, receiver: Any, args: Seq[Any]) = jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef]) + override def toString = { + val what = if (symbol.isConstructor) "constructor mirror" else "method mirror" + s"$what for ${showMethodSig(symbol)} (bound to $receiver)" + } } - private class JavaVanillaMethodMirror4(receiver: Any, symbol: MethodSymbol) - extends JavaVanillaMethodMirror(receiver, symbol) { - override def bind(newReceiver: Any) = new JavaVanillaMethodMirror4(newReceiver, symbol) - override def jinvokeraw(jmeth: jMethod, receiver: Any, args: Seq[Any]) = jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef], args(3).asInstanceOf[AnyRef]) - } + private class JavaVanillaMethodMirror(val receiver: Any, symbol: MethodSymbol, ret: DerivedValueClassMetadata) + extends JavaMethodMirror(symbol, ret) { + def this(receiver: Any, symbol: MethodSymbol) = this(receiver, symbol, new DerivedValueClassMetadata(symbol.returnType)) + def bind(newReceiver: Any) = new JavaVanillaMethodMirror(newReceiver, symbol, ret) + def apply(args: Any*): Any = jinvoke(args) + } + + private class JavaVanillaMethodMirror0(receiver: Any, symbol: MethodSymbol, ret: DerivedValueClassMetadata) + extends JavaVanillaMethodMirror(receiver, symbol, ret) { + def this(receiver: Any, symbol: MethodSymbol) = this(receiver, symbol, new DerivedValueClassMetadata(symbol.returnType)) + override def bind(newReceiver: Any) = new JavaVanillaMethodMirror0(newReceiver, symbol, ret) + override def jinvokeraw(args: Seq[Any]) = + if (!symbol.isConstructor) jmeth.invoke(receiver) + else if (receiver == null) jconstr.newInstance() + else jconstr.newInstance(receiver.asInstanceOf[AnyRef]) + } + + private class JavaVanillaMethodMirror1(receiver: Any, symbol: MethodSymbol, ret: DerivedValueClassMetadata) + extends JavaVanillaMethodMirror(receiver, symbol, ret) { + def this(receiver: Any, symbol: MethodSymbol) = this(receiver, symbol, new DerivedValueClassMetadata(symbol.returnType)) + override def bind(newReceiver: Any) = new JavaVanillaMethodMirror1(newReceiver, symbol, ret) + override def jinvokeraw(args: Seq[Any]) = + if (!symbol.isConstructor) jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef]) + else if (receiver == null) jconstr.newInstance(args(0).asInstanceOf[AnyRef]) + else jconstr.newInstance(receiver.asInstanceOf[AnyRef], args(0).asInstanceOf[AnyRef]) + } + + private class JavaVanillaMethodMirror2(receiver: Any, symbol: MethodSymbol, ret: DerivedValueClassMetadata) + extends JavaVanillaMethodMirror(receiver, symbol, ret) { + def this(receiver: Any, symbol: MethodSymbol) = this(receiver, symbol, new DerivedValueClassMetadata(symbol.returnType)) + override def bind(newReceiver: Any) = new JavaVanillaMethodMirror2(newReceiver, symbol, ret) + override def jinvokeraw(args: Seq[Any]) = + if (!symbol.isConstructor) jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef]) + else if (receiver == null) jconstr.newInstance(args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef]) + else jconstr.newInstance(receiver.asInstanceOf[AnyRef], args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef]) + } + + private class JavaVanillaMethodMirror3(receiver: Any, symbol: MethodSymbol, ret: DerivedValueClassMetadata) + extends JavaVanillaMethodMirror(receiver, symbol, ret) { + def this(receiver: Any, symbol: MethodSymbol) = this(receiver, symbol, new DerivedValueClassMetadata(symbol.returnType)) + override def bind(newReceiver: Any) = new JavaVanillaMethodMirror3(newReceiver, symbol, ret) + override def jinvokeraw(args: Seq[Any]) = + if (!symbol.isConstructor) jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef]) + else if (receiver == null) jconstr.newInstance(args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef]) + else jconstr.newInstance(receiver.asInstanceOf[AnyRef], args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef]) + } + + private class JavaVanillaMethodMirror4(receiver: Any, symbol: MethodSymbol, ret: DerivedValueClassMetadata) + extends JavaVanillaMethodMirror(receiver, symbol, ret) { + def this(receiver: Any, symbol: MethodSymbol) = this(receiver, symbol, new DerivedValueClassMetadata(symbol.returnType)) + override def bind(newReceiver: Any) = new JavaVanillaMethodMirror4(newReceiver, symbol, ret) + override def jinvokeraw(args: Seq[Any]) = + if (!symbol.isConstructor) jmeth.invoke(receiver, args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef], args(3).asInstanceOf[AnyRef]) + else if (receiver == null) jconstr.newInstance(args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef], args(3).asInstanceOf[AnyRef]) + else jconstr.newInstance(receiver.asInstanceOf[AnyRef], args(0).asInstanceOf[AnyRef], args(1).asInstanceOf[AnyRef], args(2).asInstanceOf[AnyRef], args(3).asInstanceOf[AnyRef]) + } + + // caches MethodSymbol metadata, so that we minimize the work that needs to be done during Mirror.apply + // TODO: vararg is only supported in the last parameter list (SI-6182), so we don't need to worry about the rest for now + private class MethodMetadata(symbol: MethodSymbol) { + private val params = symbol.paramss.flatten.toArray + private val vcMetadata = params.map(p => new DerivedValueClassMetadata(p.info)) + val isByName = params.map(p => isByNameParam(p.info)) + def isDerivedValueClass(i: Int) = vcMetadata(i).isDerivedValueClass + def paramUnboxers(i: Int) = vcMetadata(i).unboxer + val paramCount = params.length + val ret = new DerivedValueClassMetadata(symbol.returnType) + } + + private class JavaTransformingMethodMirror(val receiver: Any, symbol: MethodSymbol, metadata: MethodMetadata) + extends JavaMethodMirror(symbol, metadata.ret) { + def this(receiver: Any, symbol: MethodSymbol) = this(receiver, symbol, new MethodMetadata(symbol)) + override def bind(newReceiver: Any) = new JavaTransformingMethodMirror(newReceiver, symbol, metadata) + import metadata._ - private class JavaByNameMethodMirror(val receiver: Any, symbol: MethodSymbol) - extends JavaMethodMirror(symbol) { - def bind(newReceiver: Any) = new JavaByNameMethodMirror(newReceiver, symbol) def apply(args: Any*): Any = { - val transformed = map2(args.toList, symbol.paramss.flatten)((arg, param) => if (isByNameParamType(param.info)) () => arg else arg) - jinvoke(jmeth, receiver, transformed) + val args1 = new Array[Any](args.length) + var i = 0 + while (i < args1.length) { + val arg = args(i) + if (i >= paramCount) args1(i) = arg // don't transform varargs + else if (isByName(i)) args1(i) = () => arg // don't transform by-name value class params + else if (isDerivedValueClass(i)) args1(i) = paramUnboxers(i).invoke(arg) + i += 1 + } + jinvoke(args1) } } - private class JavaBytecodelessMethodMirror[T: ClassTag](val receiver: T, symbol: MethodSymbol) - extends JavaMethodMirror(symbol) { - def bind(newReceiver: Any) = new JavaBytecodelessMethodMirror(newReceiver.asInstanceOf[T], symbol) - def apply(args: Any*): Any = { + private class BytecodelessMethodMirror[T: ClassTag](val receiver: T, val symbol: MethodSymbol) + extends MethodMirror { + def bind(newReceiver: Any) = new BytecodelessMethodMirror(newReceiver.asInstanceOf[T], symbol) + override def toString = s"bytecodeless method mirror for ${showMethodSig(symbol)} (bound to $receiver)" + + def apply(args: Any*): Any = { // checking type conformance is too much of a hassle, so we don't do it here // actually it's not even necessary, because we manually dispatch arguments below val params = symbol.paramss.flatten @@ -414,7 +494,10 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni def invokePrimitiveMethod = { val jmeths = classOf[BoxesRunTime].getDeclaredMethods.filter(_.getName == nme.primitiveMethodName(symbol.name).toString) assert(jmeths.length == 1, jmeths.toList) - jinvoke(jmeths.head, null, objReceiver +: objArgs) + val jmeth = jmeths.head + val result = jmeth.invoke(null, (objReceiver +: objArgs).asInstanceOf[Seq[AnyRef]]: _*) + if (jmeth.getReturnType == java.lang.Void.TYPE) () + else result } symbol match { @@ -445,23 +528,6 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni } } - private class JavaConstructorMirror(val outer: AnyRef, val symbol: MethodSymbol) - extends MethodMirror { - def bind(newReceiver: Any) = new JavaConstructorMirror(newReceiver.asInstanceOf[AnyRef], symbol) - override val receiver = outer - lazy val jconstr = ensureAccessible(constructorToJava(symbol)) - def apply(args: Any*): Any = { - if (symbol.owner == ArrayClass) - abort("Cannot instantiate arrays with mirrors. Consider using `scala.reflect.ClassTag(<class of element>).newArray(<length>)` instead") - - val effectiveArgs = - if (outer == null) args.asInstanceOf[Seq[AnyRef]] - else outer +: args.asInstanceOf[Seq[AnyRef]] - jconstr.newInstance(effectiveArgs: _*) - } - override def toString = s"constructor mirror for ${showMethodSig(symbol)} (bound to $outer)" - } - private abstract class JavaTemplateMirror extends TemplateMirror { def outer: AnyRef @@ -474,7 +540,7 @@ private[reflect] trait JavaMirrors extends internal.SymbolTable with api.JavaUni def isStatic = false def reflectConstructor(constructor: MethodSymbol) = { checkConstructorOf(constructor, symbol) - new JavaConstructorMirror(outer, constructor) + mkMethodMirror(outer, constructor) } override def toString = s"class mirror for ${symbol.fullName} (bound to $outer)" } diff --git a/src/reflect/scala/reflect/runtime/JavaUniverse.scala b/src/reflect/scala/reflect/runtime/JavaUniverse.scala index cfd66744ff..f6556a442d 100644 --- a/src/reflect/scala/reflect/runtime/JavaUniverse.scala +++ b/src/reflect/scala/reflect/runtime/JavaUniverse.scala @@ -12,6 +12,8 @@ class JavaUniverse extends internal.SymbolTable with JavaUniverseForce with Refl override def inform(msg: String): Unit = log(msg) def picklerPhase = internal.SomePhase + def erasurePhase = internal.SomePhase + lazy val settings = new Settings private val isLogging = sys.props contains "scala.debug.reflect" diff --git a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala index 813c0e1386..d642b25127 100644 --- a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala +++ b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala @@ -12,7 +12,7 @@ import scala.reflect.internal.util.AbstractFileClassLoader /** A few java-reflection oriented utility functions useful during reflection bootstrapping. */ -private[scala] object ReflectionUtils { +object ReflectionUtils { // Unwraps some chained exceptions which arise during reflective calls. def unwrapThrowable(x: Throwable): Throwable = x match { case _: InvocationTargetException | // thrown by reflectively invoked method or constructor diff --git a/src/repl/scala/tools/nsc/interpreter/IMain.scala b/src/repl/scala/tools/nsc/interpreter/IMain.scala index 8fba1e538e..d261fc5be9 100644 --- a/src/repl/scala/tools/nsc/interpreter/IMain.scala +++ b/src/repl/scala/tools/nsc/interpreter/IMain.scala @@ -18,7 +18,7 @@ import scala.reflect.internal.util.{ BatchSourceFile, SourceFile } import scala.tools.util.PathResolver import scala.tools.nsc.io.AbstractFile import scala.tools.nsc.typechecker.{ TypeStrings, StructuredTypeStrings } -import scala.tools.nsc.util.{ ScalaClassLoader, stringFromWriter, StackTraceOps } +import scala.tools.nsc.util.{ ScalaClassLoader, stringFromReader, stringFromWriter, StackTraceOps } import scala.tools.nsc.util.Exceptional.unwrap import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine, ScriptEngineFactory, ScriptException, CompiledScript, Compilable} @@ -534,8 +534,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set var code = "" var bound = false - @throws[ScriptException] - def compile(script: String): CompiledScript = { + def compiled(script: String): CompiledScript = { if (!bound) { quietBind("engine" -> this.asInstanceOf[ScriptEngine]) bound = true @@ -562,18 +561,6 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set } } - @throws[ScriptException] - def compile(reader: java.io.Reader): CompiledScript = { - val writer = new java.io.StringWriter() - var c = reader.read() - while(c != -1) { - writer.write(c) - c = reader.read() - } - reader.close() - compile(writer.toString()) - } - private class WrappedRequest(val req: Request) extends CompiledScript { var recorded = false @@ -1014,10 +1001,16 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set } @throws[ScriptException] - def eval(script: String, context: ScriptContext): Object = compile(script).eval(context) + def compile(script: String): CompiledScript = eval("new javax.script.CompiledScript { def eval(context: javax.script.ScriptContext): Object = { " + script + " }.asInstanceOf[Object]; def getEngine: javax.script.ScriptEngine = engine }").asInstanceOf[CompiledScript] + + @throws[ScriptException] + def compile(reader: java.io.Reader): CompiledScript = compile(stringFromReader(reader)) + + @throws[ScriptException] + def eval(script: String, context: ScriptContext): Object = compiled(script).eval(context) @throws[ScriptException] - def eval(reader: java.io.Reader, context: ScriptContext): Object = compile(reader).eval(context) + def eval(reader: java.io.Reader, context: ScriptContext): Object = eval(stringFromReader(reader), context) override def finalize = close diff --git a/test/disabled/run/t7843-jsr223-service.scala b/test/disabled/run/t7843-jsr223-service.scala deleted file mode 100644 index e2ea850698..0000000000 --- a/test/disabled/run/t7843-jsr223-service.scala +++ /dev/null @@ -1,18 +0,0 @@ -/*DISABLED: - For Paul, it steals focus when it runs. - - For me, it fails with some platform specific extra output: - - -ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider apple.applescript.AppleScriptEngin - n: Object = 10 - 12345678910 -*/ -import javax.script._ -import scala.tools.nsc.interpreter.IMain - -object Test extends App { - val engine = new ScriptEngineManager getEngineByName "scala" - engine.asInstanceOf[IMain].settings.usejavacp.value = true - engine put ("n", 10) - engine eval "1 to n.asInstanceOf[Int] foreach print" -} diff --git a/test/files/neg/names-defaults-neg.check b/test/files/neg/names-defaults-neg.check index 880ddc4327..20ddd55f1f 100644 --- a/test/files/neg/names-defaults-neg.check +++ b/test/files/neg/names-defaults-neg.check @@ -88,7 +88,7 @@ names-defaults-neg.scala:76: error: no type parameters for method test4: (x: T[T --- because --- argument expression's type is not compatible with formal parameter type; found : List[Int] - required: ?T + required: ?T[?T[List[?T[X forSome { type X }]]]] Error occurred in an application involving default arguments. test4() ^ diff --git a/test/files/neg/t6260-named.check b/test/files/neg/t6260-named.check new file mode 100644 index 0000000000..ed6ab5e76f --- /dev/null +++ b/test/files/neg/t6260-named.check @@ -0,0 +1,13 @@ +t6260-named.scala:12: error: bridge generated for member method apply: (a: C[Any])C[Any] in object O +which overrides method apply: (v1: T1)R in trait Function1 +clashes with definition of the member itself; +both have erased type (v1: Object)Object + def apply(a: C[Any]) = a + ^ +t6260-named.scala:14: error: bridge generated for member method apply: (a: C[Any])C[Any] in class X +which overrides method apply: (a: A)A in trait T +clashes with definition of the member itself; +both have erased type (a: Object)Object + class X extends T[C[Any]] { def apply(a: C[Any]) = a } + ^ +two errors found diff --git a/test/files/neg/t6260-named.scala b/test/files/neg/t6260-named.scala new file mode 100644 index 0000000000..7cd9ce8473 --- /dev/null +++ b/test/files/neg/t6260-named.scala @@ -0,0 +1,15 @@ +class C[A](private val a: Any) extends AnyVal +trait T[A] { + def apply(a: A): A +} + +object Test { + (x: C[Any]) => {println(s"f($x)"); x} // okay + new T[C[Any]] { def apply(a: C[Any]) = a } // okay + + // we can't rename the specific apply method to avoid the clash + object O extends Function1[C[Any], C[Any]] { + def apply(a: C[Any]) = a + } + class X extends T[C[Any]] { def apply(a: C[Any]) = a } +} diff --git a/test/files/neg/t6260.check b/test/files/neg/t6260.check deleted file mode 100644 index 60c4add143..0000000000 --- a/test/files/neg/t6260.check +++ /dev/null @@ -1,13 +0,0 @@ -t6260.scala:3: error: bridge generated for member method apply: (bx: Box[X])Box[Y] in <$anon: Box[X] => Box[Y]> -which overrides method apply: (v1: T1)R in trait Function1 -clashes with definition of the member itself; -both have erased type (v1: Object)Object - ((bx: Box[X]) => new Box(f(bx.x)))(this) - ^ -t6260.scala:8: error: bridge generated for member method apply: (bx: Box[X])Box[Y] in <$anon: Box[X] => Box[Y]> -which overrides method apply: (v1: T1)R in trait Function1 -clashes with definition of the member itself; -both have erased type (v1: Object)Object - ((bx: Box[X]) => new Box(f(bx.x)))(self) - ^ -two errors found diff --git a/test/files/neg/t6260b.check b/test/files/neg/t6260b.check deleted file mode 100644 index 3a7e8947aa..0000000000 --- a/test/files/neg/t6260b.check +++ /dev/null @@ -1,7 +0,0 @@ -t6260b.scala:3: error: bridge generated for member method apply: ()X in <$anon: () => X> -which overrides method apply: ()R in trait Function0 -clashes with definition of the member itself; -both have erased type ()Object -class Y { def f = new X("") or new X("") } - ^ -one error found diff --git a/test/files/neg/t8207.check b/test/files/neg/t8207.check new file mode 100644 index 0000000000..59facd897a --- /dev/null +++ b/test/files/neg/t8207.check @@ -0,0 +1,7 @@ +t8207.scala:1: error: '.' expected but '}' found. +class C { import C.this.toString } + ^ +t8207.scala:3: error: '.' expected but '}' found. +class D { import D.this.toString } + ^ +two errors found diff --git a/test/files/neg/t8207.scala b/test/files/neg/t8207.scala new file mode 100644 index 0000000000..738ce381f4 --- /dev/null +++ b/test/files/neg/t8207.scala @@ -0,0 +1,3 @@ +class C { import C.this.toString } + +class D { import D.this.toString } diff --git a/test/files/neg/t8219-any-any-ref-equals.check b/test/files/neg/t8219-any-any-ref-equals.check new file mode 100644 index 0000000000..95d2536fba --- /dev/null +++ b/test/files/neg/t8219-any-any-ref-equals.check @@ -0,0 +1,10 @@ +t8219-any-any-ref-equals.scala:5: error: method ==: (x$1: Any)Boolean does not take type parameters. + "".==[Int] + ^ +t8219-any-any-ref-equals.scala:6: error: method ==: (x$1: Any)Boolean does not take type parameters. + ("": AnyRef).==[Int] + ^ +t8219-any-any-ref-equals.scala:7: error: method ==: (x$1: Any)Boolean does not take type parameters. + ("": Object).==[Int] + ^ +three errors found diff --git a/test/files/neg/t8219-any-any-ref-equals.scala b/test/files/neg/t8219-any-any-ref-equals.scala new file mode 100644 index 0000000000..f1b81fa734 --- /dev/null +++ b/test/files/neg/t8219-any-any-ref-equals.scala @@ -0,0 +1,8 @@ +object Test { + // The error message tells us that AnyRef#== and Any#== are overloaded. + // A real class couldn't define such an overload, why do we allow AnyRef + // to do so? + "".==[Int] + ("": AnyRef).==[Int] + ("": Object).==[Int] +} diff --git a/test/files/neg/t8237-default.check b/test/files/neg/t8237-default.check new file mode 100644 index 0000000000..59fe21ed03 --- /dev/null +++ b/test/files/neg/t8237-default.check @@ -0,0 +1,13 @@ +t8237-default.scala:5: error: no type parameters for method test4: (x: T[T[List[T[X forSome { type X }]]]])Nothing exist so that it can be applied to arguments (List[Int]) + --- because --- +argument expression's type is not compatible with formal parameter type; + found : List[Int] + required: ?T[?T[List[?T[X forSome { type X }]]]] + test4(test4$default$1) + ^ +t8237-default.scala:5: error: type mismatch; + found : List[Int] + required: T[T[List[T[X forSome { type X }]]]] + test4(test4$default$1) + ^ +two errors found diff --git a/test/files/neg/t8237-default.scala b/test/files/neg/t8237-default.scala new file mode 100644 index 0000000000..f695aa523f --- /dev/null +++ b/test/files/neg/t8237-default.scala @@ -0,0 +1,29 @@ +// This test case was extracte from `names-defaults-neg.scala` +// It pinpoints an improvement an error message that results from +// a type inference failure +object Test extends App { + test4(test4$default$1) + + def test4[T[P]](x: T[T[List[T[X forSome { type X }]]]]) = ??? + def test4$default$1[T[P]]: List[Int] = ??? +} + +/* +OLD: + no type parameters for method test4: (x: T[T[List[T[X forSome { type X }]]]])Nothing exist so that it can be applied to arguments (List[Int]) + --- because --- +argument expression's type is not compatible with formal parameter type; + found : List[Int] + required: ?T + test4(test4$default$1) + ^ + +NEW: + +no type parameters for method test4: (x: T[T[List[T[X forSome { type X }]]]])Nothing exist so that it can be applied to arguments (List[Int]) + --- because --- +argument expression's type is not compatible with formal parameter type; + found : List[Int] + required: ?T[?T[List[?T[X forSome { type X }]]] + test4(test4$default$1) +*/ diff --git a/test/files/neg/delambdafy_t6260_method.check b/test/files/pos/delambdafy_t6260_method.check index f5cd6947d1..f5cd6947d1 100644 --- a/test/files/neg/delambdafy_t6260_method.check +++ b/test/files/pos/delambdafy_t6260_method.check diff --git a/test/files/neg/delambdafy_t6260_method.flags b/test/files/pos/delambdafy_t6260_method.flags index 48b438ddf8..48b438ddf8 100644 --- a/test/files/neg/delambdafy_t6260_method.flags +++ b/test/files/pos/delambdafy_t6260_method.flags diff --git a/test/files/neg/delambdafy_t6260_method.scala b/test/files/pos/delambdafy_t6260_method.scala index 93b5448227..93b5448227 100644 --- a/test/files/neg/delambdafy_t6260_method.scala +++ b/test/files/pos/delambdafy_t6260_method.scala diff --git a/test/files/pos/t6169/Exist.java b/test/files/pos/t6169/Exist.java new file mode 100644 index 0000000000..dfc6b36b33 --- /dev/null +++ b/test/files/pos/t6169/Exist.java @@ -0,0 +1,4 @@ +public class Exist<T extends String> { + // java helpfully re-interprets Exist<?> as Exist<? extends String> + public Exist<?> foo() { throw new RuntimeException(); } +}
\ No newline at end of file diff --git a/test/files/pos/t6169/ExistF.java b/test/files/pos/t6169/ExistF.java new file mode 100644 index 0000000000..70fabd74cf --- /dev/null +++ b/test/files/pos/t6169/ExistF.java @@ -0,0 +1,4 @@ +public class ExistF<T extends ExistF<T>> { + // java helpfully re-interprets ExistF<?> as ExistF<?0 extends ExistF<?0>> + public ExistF<?> foo() { throw new RuntimeException(); } +}
\ No newline at end of file diff --git a/test/files/pos/t6169/ExistIndir.java b/test/files/pos/t6169/ExistIndir.java new file mode 100644 index 0000000000..e66d1698c4 --- /dev/null +++ b/test/files/pos/t6169/ExistIndir.java @@ -0,0 +1,4 @@ +public class ExistIndir<T extends String, U extends T> { + // java helpfully re-interprets ExistIndir<?> as ExistIndir<? extends String> + public ExistIndir<?, ?> foo() { throw new RuntimeException(); } +} diff --git a/test/files/pos/t6169/OP.java b/test/files/pos/t6169/OP.java new file mode 100644 index 0000000000..15e4c5640f --- /dev/null +++ b/test/files/pos/t6169/OP.java @@ -0,0 +1 @@ +public abstract class OP<T> { } diff --git a/test/files/pos/t6169/Skin.java b/test/files/pos/t6169/Skin.java new file mode 100644 index 0000000000..780de1ee09 --- /dev/null +++ b/test/files/pos/t6169/Skin.java @@ -0,0 +1 @@ +public interface Skin<C extends Skinnable> { } diff --git a/test/files/pos/t6169/Skinnable.java b/test/files/pos/t6169/Skinnable.java new file mode 100644 index 0000000000..f91eaa30d8 --- /dev/null +++ b/test/files/pos/t6169/Skinnable.java @@ -0,0 +1,3 @@ +public interface Skinnable { + OP<Skin<?>> skinProperty(); +} diff --git a/test/files/pos/t6169/skinnable.scala b/test/files/pos/t6169/skinnable.scala new file mode 100644 index 0000000000..3ba2734526 --- /dev/null +++ b/test/files/pos/t6169/skinnable.scala @@ -0,0 +1,14 @@ +object ObjectProperty { + implicit def jfxObjectProperty2sfx[T](p: OP[T]) = new ObjectProperty[T](p) +} + +class ObjectProperty[T](val delegate: OP[T]) + +trait TestWildcardBoundInference { + def delegate: Skinnable + def skin: ObjectProperty[Skin[_ /* inferred: <: Skinnable */]] = ObjectProperty.jfxObjectProperty2sfx(delegate.skinProperty) + skin: ObjectProperty[Skin[_ <: Skinnable]] + + def skinCheckInference = delegate.skinProperty + skinCheckInference: ObjectProperty[Skin[_ <: Skinnable]] +}
\ No newline at end of file diff --git a/test/files/pos/t6169/t6169.scala b/test/files/pos/t6169/t6169.scala new file mode 100644 index 0000000000..37f42619ca --- /dev/null +++ b/test/files/pos/t6169/t6169.scala @@ -0,0 +1,7 @@ +class Test { + class MyExist extends ExistF[MyExist] + // SI-8197, SI-6169: java infers the bounds of existentials, so we have to as well now that SI-1786 is fixed... + def stringy: Exist[_ <: String] = (new Exist[String]).foo + def fbounded: (ExistF[t] forSome {type t <: ExistF[t] }) = (new MyExist).foo + def indir: ExistIndir[_ <: String, _ <: String] = (new ExistIndir[String, String]).foo +}
\ No newline at end of file diff --git a/test/files/neg/t6260.flags b/test/files/pos/t6260.flags index 2349d8294d..2349d8294d 100644 --- a/test/files/neg/t6260.flags +++ b/test/files/pos/t6260.flags diff --git a/test/files/neg/t6260.scala b/test/files/pos/t6260.scala index 93b5448227..93b5448227 100644 --- a/test/files/neg/t6260.scala +++ b/test/files/pos/t6260.scala diff --git a/test/files/neg/t6260b.scala b/test/files/pos/t6260b.scala index 73e2e58f73..73e2e58f73 100644 --- a/test/files/neg/t6260b.scala +++ b/test/files/pos/t6260b.scala diff --git a/test/files/pos/t6948.scala b/test/files/pos/t6948.scala new file mode 100644 index 0000000000..12a1d7eaf2 --- /dev/null +++ b/test/files/pos/t6948.scala @@ -0,0 +1,10 @@ +object t6948 { + val rand = new scala.util.Random() + def a1 = rand.shuffle(0 to 5) + // Tis not to be + // def a2 = rand.shuffle(0 until 5) + def a3 = rand.shuffle(Vector(1, 2, 3)) + def a4 = rand.shuffle(scala.collection.Seq(1, 2, 3)) + def a5 = rand.shuffle(scala.collection.immutable.Seq(1, 2, 3)) + def a6 = rand.shuffle(scala.collection.mutable.Seq(1, 2, 3)) +} diff --git a/test/files/pos/t8207.scala b/test/files/pos/t8207.scala new file mode 100644 index 0000000000..680b40f379 --- /dev/null +++ b/test/files/pos/t8207.scala @@ -0,0 +1,6 @@ +class C { me => + import me.{toString => ts} + locally(this: me.type) + trait T + type X = me.T +} diff --git a/test/files/pos/t8219.scala b/test/files/pos/t8219.scala new file mode 100644 index 0000000000..e1653b6238 --- /dev/null +++ b/test/files/pos/t8219.scala @@ -0,0 +1,15 @@ +trait Equalizer[T] +trait Gen[A] + +class Broken { + implicit def const[T](x: T): Gen[T] = ??? + implicit def convertToEqualizer[T](left: T): Equalizer[T] = ??? + + def in(a: Any) = () + in { + import scala.None // any import will do.. + "" == "" // this no longer triggers the bug, as Object#== now overrides Any#== + } + + // We can still trigger the bug with a structural type, see pending/neg/t8219.scala +} diff --git a/test/files/pos/t8219b.scala b/test/files/pos/t8219b.scala new file mode 100644 index 0000000000..d55d3139e1 --- /dev/null +++ b/test/files/pos/t8219b.scala @@ -0,0 +1,49 @@ +trait Equalizer[T] +trait Gen[A] + +class Broken { + implicit def const[T](x: T): Gen[T] = ??? + implicit def convertToEqualizer[T](left: T): Equalizer[T] = ??? + + def in(a: Any) = () + in { + import scala.None // any import will do.. + "" == "" // no longer a problem, see pos/t8129.scala + } + + // We used to fall into the errant code path above when `Any#==` and `AnyRef#==` + // were overloaded. + // + // Real classes couldn't get away with that overloading; it would result in + // a compiler error because the variants would collapse into an overriding + // relationship after erasure. + // + // + // But, a structural type can! This triggers the same error, and served as + // a backstop for this test if we change the signatures of `AnyRef#==` to + // override `Any#==`. + type T = { + def a(a: AnyRef): Boolean + def a(a: Any): Boolean + } + + def t: T = ??? + + in { + import scala.None // any import will do.. + t.a("") + } + + // Or, we can get here with ambiguous implicits from the formal parameter + // type of the less specific overload to that of the more specific. + object T { + def foo(a: Any) = true + def foo(a: String) = true + } + in { + import scala.None + implicit def any2str1(a: Any) = "" + implicit def any2str2(a: Any) = "" + T.foo("") + } +} diff --git a/test/files/pos/t8237.scala b/test/files/pos/t8237.scala new file mode 100644 index 0000000000..005089079e --- /dev/null +++ b/test/files/pos/t8237.scala @@ -0,0 +1,29 @@ +import scala.language.higherKinds + +object TestExplicit { + trait TC[A] + def fTt[A,E[X] <: List[X]](a: A)(implicit tt: TC[E[A]]) = a + implicit def tc[T]: TC[T] = ??? + + // Typechecking results in SOE in TypeVar.isGround + fTt(1)(tc) + // fun = TestExplicit.this.fTt[Int, E](1) + // args = TestExplicit.this.tc[E[Int]] + // argTpes.head.instantiateTypeParams = TC[?E#1[Int]] + // formals.head.instantiateTypeParams = TC[?E#2[Int]] + // (where ?E#1 and ?E#2 as distinct AppliedTypeVars that resulted + // from separate applications of type args to the same HKTypeVar, ?E) + // + // As we check if the argument conforms to the formal, we would have + // AppliedTypeVars sharing the same TypeConstraints on the LHS and RHS, + // which leads to a cyclic constraint. +} + +object TestImplicit { + trait TC[A] + def fTt[A,E[X] <: List[X]](a: A)(implicit tt: TC[E[A]]) = a + implicit def tc[T]: TC[T] = ??? + + // Oddly enough, this one works. + fTt(1) +} diff --git a/test/files/pos/t8237b.scala b/test/files/pos/t8237b.scala new file mode 100644 index 0000000000..52bb310e8b --- /dev/null +++ b/test/files/pos/t8237b.scala @@ -0,0 +1,10 @@ +import scala.language.higherKinds +import scala.reflect.runtime.universe._ +object Test { + + def fTt[A,E[X]<:List[X]](a: A)(implicit tt: TypeTag[E[A]]) = a + + trait TC[A] + implicit def TCListInt[A]: TC[A] = ??? + fTt(1) +} diff --git a/test/files/presentation/callcc-interpreter.check b/test/files/presentation/callcc-interpreter.check index 1f868097ca..4bf68b3d4e 100644 --- a/test/files/presentation/callcc-interpreter.check +++ b/test/files/presentation/callcc-interpreter.check @@ -3,7 +3,7 @@ reload: CallccInterpreter.scala askTypeCompletion at CallccInterpreter.scala(51,34) ================================================================================ [response] askTypeCompletion at (51,34) -retrieved 59 members +retrieved 57 members abstract trait Term extends AnyRef abstract trait Value extends AnyRef case class Add extends callccInterpreter.Term with Product with Serializable @@ -38,10 +38,8 @@ def toString(): String def unitM[A](a: A): callccInterpreter.M[A] def →[B](y: B): (callccInterpreter.type, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/completion-implicit-chained.check b/test/files/presentation/completion-implicit-chained.check index f9d77f7a53..c583b7877c 100644 --- a/test/files/presentation/completion-implicit-chained.check +++ b/test/files/presentation/completion-implicit-chained.check @@ -3,7 +3,7 @@ reload: Completions.scala askTypeCompletion at Completions.scala(11,16) ================================================================================ [response] askTypeCompletion at (11,16) -retrieved 24 members +retrieved 22 members [inaccessible] protected[package lang] def clone(): Object [inaccessible] protected[package lang] def finalize(): Unit def equals(x$1: Any): Boolean @@ -11,10 +11,8 @@ def hashCode(): Int def map(x: Int => Int)(implicit a: DummyImplicit): test.O.type def toString(): String final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/ide-bug-1000349.check b/test/files/presentation/ide-bug-1000349.check index c59fa6843f..79bfde5343 100644 --- a/test/files/presentation/ide-bug-1000349.check +++ b/test/files/presentation/ide-bug-1000349.check @@ -3,7 +3,7 @@ reload: CompletionOnEmptyArgMethod.scala askTypeCompletion at CompletionOnEmptyArgMethod.scala(2,17) ================================================================================ [response] askTypeCompletion at (2,17) -retrieved 32 members +retrieved 30 members def +(other: String): String def ->[B](y: B): (Foo, B) def ensuring(cond: Boolean): Foo @@ -17,10 +17,8 @@ def hashCode(): Int def toString(): String def →[B](y: B): (Foo, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/ide-bug-1000475.check b/test/files/presentation/ide-bug-1000475.check index f5b4253e1a..4fb7f18285 100644 --- a/test/files/presentation/ide-bug-1000475.check +++ b/test/files/presentation/ide-bug-1000475.check @@ -3,7 +3,7 @@ reload: Foo.scala askTypeCompletion at Foo.scala(3,7) ================================================================================ [response] askTypeCompletion at (3,7) -retrieved 31 members +retrieved 29 members [inaccessible] protected[package lang] def clone(): Object [inaccessible] protected[package lang] def finalize(): Unit def +(other: String): String @@ -18,10 +18,8 @@ def hashCode(): Int def toString(): String def →[B](y: B): (Object, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean @@ -37,7 +35,7 @@ final def wait(x$1: Long,x$2: Int): Unit askTypeCompletion at Foo.scala(6,10) ================================================================================ [response] askTypeCompletion at (6,10) -retrieved 31 members +retrieved 29 members [inaccessible] protected[package lang] def clone(): Object [inaccessible] protected[package lang] def finalize(): Unit def +(other: String): String @@ -52,10 +50,8 @@ def hashCode(): Int def toString(): String def →[B](y: B): (Object, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean @@ -71,7 +67,7 @@ final def wait(x$1: Long,x$2: Int): Unit askTypeCompletion at Foo.scala(7,7) ================================================================================ [response] askTypeCompletion at (7,7) -retrieved 31 members +retrieved 29 members [inaccessible] protected[package lang] def clone(): Object [inaccessible] protected[package lang] def finalize(): Unit def +(other: String): String @@ -86,10 +82,8 @@ def hashCode(): Int def toString(): String def →[B](y: B): (Object, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/ide-bug-1000531.check b/test/files/presentation/ide-bug-1000531.check index dff89b155b..ef8a1d12de 100644 --- a/test/files/presentation/ide-bug-1000531.check +++ b/test/files/presentation/ide-bug-1000531.check @@ -3,7 +3,7 @@ reload: CrashOnLoad.scala askTypeCompletion at CrashOnLoad.scala(6,12) ================================================================================ [response] askTypeCompletion at (6,12) -retrieved 120 members +retrieved 118 members [inaccessible] protected[package lang] def clone(): Object [inaccessible] protected[package lang] def finalize(): Unit [inaccessible] protected[this] def reversed: List[B] @@ -107,10 +107,8 @@ def zipWithIndex: Iterator[(B, Int)] def zip[B](that: Iterator[B]): Iterator[(B, B)] def →[B](y: B): (java.util.Iterator[B], B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/implicit-member.check b/test/files/presentation/implicit-member.check index 5ad52b4dd3..3bd3d8af41 100644 --- a/test/files/presentation/implicit-member.check +++ b/test/files/presentation/implicit-member.check @@ -3,7 +3,7 @@ reload: ImplicitMember.scala askTypeCompletion at ImplicitMember.scala(7,7) ================================================================================ [response] askTypeCompletion at (7,7) -retrieved 34 members +retrieved 32 members def +(other: String): String def ->[B](y: B): (Implicit.type, B) def ensuring(cond: Boolean): Implicit.type @@ -17,10 +17,8 @@ def toString(): String def →[B](y: B): (Implicit.type, B) final class AppliedImplicit[A] extends AnyRef final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/ping-pong.check b/test/files/presentation/ping-pong.check index 20f17aa7d0..ab4cfc9e7a 100644 --- a/test/files/presentation/ping-pong.check +++ b/test/files/presentation/ping-pong.check @@ -3,7 +3,7 @@ reload: PingPong.scala askTypeCompletion at PingPong.scala(10,23) ================================================================================ [response] askTypeCompletion at (10,23) -retrieved 35 members +retrieved 33 members [inaccessible] private[this] val ping: Ping [inaccessible] protected[package lang] def clone(): Object [inaccessible] protected[package lang] def finalize(): Unit @@ -19,10 +19,8 @@ def hashCode(): Int def poke(): Unit def →[B](y: B): (Pong, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean @@ -40,7 +38,7 @@ private[this] val name: String askTypeCompletion at PingPong.scala(19,20) ================================================================================ [response] askTypeCompletion at (19,20) -retrieved 35 members +retrieved 33 members [inaccessible] protected[package lang] def clone(): Object [inaccessible] protected[package lang] def finalize(): Unit def +(other: String): String @@ -57,10 +55,8 @@ def name: String def poke: Unit def →[B](y: B): (Ping, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/t5708.check b/test/files/presentation/t5708.check index 04806b5867..4b33893e98 100644 --- a/test/files/presentation/t5708.check +++ b/test/files/presentation/t5708.check @@ -3,7 +3,7 @@ reload: Completions.scala askTypeCompletion at Completions.scala(17,9) ================================================================================ [response] askTypeCompletion at (17,9) -retrieved 39 members +retrieved 37 members [inaccessible] private def privateM: String [inaccessible] private[this] val privateV: String [inaccessible] private[this] val protectedV: String @@ -22,10 +22,8 @@ def hashCode(): Int def toString(): String def →[B](y: B): (test.Compat.type, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/presentation/visibility.check b/test/files/presentation/visibility.check index 217da34b9c..1ae1213f2d 100644 --- a/test/files/presentation/visibility.check +++ b/test/files/presentation/visibility.check @@ -3,7 +3,7 @@ reload: Completions.scala askTypeCompletion at Completions.scala(14,12) ================================================================================ [response] askTypeCompletion at (14,12) -retrieved 37 members +retrieved 35 members [inaccessible] private[this] def secretPrivateThis(): Unit def +(other: String): String def ->[B](y: B): (accessibility.Foo, B) @@ -19,10 +19,8 @@ def someTests(other: accessibility.Foo): Unit def toString(): String def →[B](y: B): (accessibility.Foo, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean @@ -43,7 +41,7 @@ protected[package lang] def finalize(): Unit askTypeCompletion at Completions.scala(16,11) ================================================================================ [response] askTypeCompletion at (16,11) -retrieved 37 members +retrieved 35 members def +(other: String): String def ->[B](y: B): (accessibility.Foo, B) def ensuring(cond: Boolean): accessibility.Foo @@ -58,10 +56,8 @@ def someTests(other: accessibility.Foo): Unit def toString(): String def →[B](y: B): (accessibility.Foo, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean @@ -83,7 +79,7 @@ protected[package lang] def finalize(): Unit askTypeCompletion at Completions.scala(22,11) ================================================================================ [response] askTypeCompletion at (22,11) -retrieved 37 members +retrieved 35 members [inaccessible] private def secretPrivate(): Unit def +(other: String): String def ->[B](y: B): (accessibility.AccessibilityChecks, B) @@ -100,10 +96,8 @@ def someTests: Unit def toString(): String def →[B](y: B): (accessibility.AccessibilityChecks, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean @@ -123,7 +117,7 @@ protected[package lang] def finalize(): Unit askTypeCompletion at Completions.scala(28,10) ================================================================================ [response] askTypeCompletion at (28,10) -retrieved 37 members +retrieved 35 members [inaccessible] private def secretPrivate(): Unit [inaccessible] private[this] def secretPrivateThis(): Unit [inaccessible] protected def secretProtected(): Unit @@ -143,10 +137,8 @@ def someTests(other: accessibility.Foo): Unit def toString(): String def →[B](y: B): (accessibility.Foo, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean @@ -163,7 +155,7 @@ protected[package accessibility] def secretProtectedInPackage(): Unit askTypeCompletion at Completions.scala(37,8) ================================================================================ [response] askTypeCompletion at (37,8) -retrieved 37 members +retrieved 35 members [inaccessible] private def secretPrivate(): Unit [inaccessible] private[this] def secretPrivateThis(): Unit [inaccessible] protected def secretProtected(): Unit @@ -184,10 +176,8 @@ def someTests(other: accessibility.Foo): Unit def toString(): String def →[B](y: B): (accessibility.Foo, B) final def !=(x$1: Any): Boolean -final def !=(x$1: AnyRef): Boolean final def ##(): Int final def ==(x$1: Any): Boolean -final def ==(x$1: AnyRef): Boolean final def asInstanceOf[T0]: T0 final def eq(x$1: AnyRef): Boolean final def isInstanceOf[T0]: Boolean diff --git a/test/files/run/inferred-type-constructors.check b/test/files/run/inferred-type-constructors.check new file mode 100644 index 0000000000..5992ef02ad --- /dev/null +++ b/test/files/run/inferred-type-constructors.check @@ -0,0 +1,56 @@ +warning: there were 2 feature warning(s); re-run with -feature for details + p.Iterable[Int] + p.Set[Int] + p.Seq[Int] + p.m.Set[Int] + p.m.Seq[Int] + private[m] p.m.ASet[Int] + p.i.Seq[Int] + private[i] p.i.ASet[Int] + private[i] p.i.ASeq[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Set[Int] + p.Iterable[Int] + p.Set[Int] + p.Iterable[Int] + p.Set[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Seq[Int] + p.Iterable[Int] + p.Seq[Int] + p.Iterable[Int] + p.Seq[Int] + p.Iterable[Int] + p.m.Set[Int] + p.Iterable[Int] + p.Set[Int] + p.Iterable[Int] + p.Iterable[Int] + p.Seq[Int] + p.Iterable[Int] + p.Seq[Int] + p.Iterable[Int] + private[p] p.ASet[Int] + private[p] p.AIterable[Int] + p.Iterable[Int] + p.i.Seq[Int] + private[p] p.AIterable[Int] + List[Nothing] + scala.collection.immutable.Vector[Nothing] + scala.collection.immutable.Iterable[(Int, Int)] + scala.collection.immutable.Set[Int] + Seq[Int] + Array[Int] + scala.collection.AbstractSet[Int] + Comparable[java.lang.String] + scala.collection.immutable.LinearSeq[Int] + Iterable[Int] diff --git a/test/files/run/inferred-type-constructors.scala b/test/files/run/inferred-type-constructors.scala new file mode 100644 index 0000000000..79a8653f68 --- /dev/null +++ b/test/files/run/inferred-type-constructors.scala @@ -0,0 +1,125 @@ +package p { + trait TCon[+CC[X]] { + def fPublic: CC[Int] = ??? + private[p] def fPackagePrivate: CC[Int] = ??? + protected[p] def fPackageProtected: CC[Int] = ??? + } + trait Iterable[+A] extends TCon[Iterable] + trait Set[A] extends Iterable[A] with TCon[Set] + trait Seq[+A] extends Iterable[A] with TCon[Seq] + + private[p] abstract class AIterable[+A] extends Iterable[A] + private[p] abstract class ASeq[+A] extends AIterable[A] with Seq[A] + private[p] abstract class ASet[A] extends AIterable[A] with Set[A] + + package m { + private[m] abstract class ASeq[A] extends p.ASeq[A] with Seq[A] + private[m] abstract class ASet[A] extends p.ASet[A] with Set[A] + trait Set[A] extends p.Set[A] with TCon[Set] + trait Seq[A] extends p.Seq[A] with TCon[Seq] + trait BitSet extends ASet[Int] + trait IntSeq extends ASeq[Int] + } + + package i { + private[i] abstract class ASeq[+A] extends p.ASeq[A] with Seq[A] + private[i] abstract class ASet[A] extends p.ASet[A] with Set[A] + trait Set[A] extends p.Set[A] with TCon[Set] + trait Seq[+A] extends p.Seq[A] with TCon[Seq] + trait BitSet extends ASet[Int] + trait IntSeq extends ASeq[Int] + } +} + +object Test { + import scala.reflect.runtime.universe._ + // Complicated by the absence of usable type constructor type tags. + def extract[A, CC[X]](xs: CC[A]): CC[A] = xs + def whatis[T: TypeTag](x: T): Unit = { + val tpe = typeOf[T] + val access = tpe.typeSymbol.asInstanceOf[scala.reflect.internal.HasFlags].accessString.replaceAllLiterally("package ", "") + println(f"$access%15s $tpe") + } + + trait IntIterable extends p.Iterable[Int] + trait IntSet extends p.Set[Int] + trait IntSeq extends p.Seq[Int] + + trait MutableIntSet extends p.m.Set[Int] + trait MutableIntSeq extends p.m.Seq[Int] + + trait ImmutableIntSet extends p.i.Set[Int] + trait ImmutableIntSeq extends p.i.Seq[Int] + + def f1: IntIterable = null + def f2: IntSet = null + def f3: IntSeq = null + + def g1: MutableIntSet = null + def g2: MutableIntSeq = null + def g3: p.m.BitSet = null + + def h1: ImmutableIntSeq = null + def h2: p.i.BitSet = null + def h3: p.i.IntSeq = null + + def main(args: Array[String]): Unit = { + whatis(extract(f1)) + whatis(extract(f2)) + whatis(extract(f3)) + whatis(extract(g1)) + whatis(extract(g2)) + whatis(extract(g3)) + whatis(extract(h1)) + whatis(extract(h2)) + whatis(extract(h3)) + + whatis(extract(if (true) f1 else f2)) + whatis(extract(if (true) f1 else f3)) + whatis(extract(if (true) f1 else g1)) + whatis(extract(if (true) f1 else g2)) + whatis(extract(if (true) f1 else g3)) + whatis(extract(if (true) f1 else h1)) + whatis(extract(if (true) f1 else h2)) + whatis(extract(if (true) f1 else h3)) + whatis(extract(if (true) f2 else f3)) + whatis(extract(if (true) f2 else g1)) + whatis(extract(if (true) f2 else g2)) + whatis(extract(if (true) f2 else g3)) + whatis(extract(if (true) f2 else h1)) + whatis(extract(if (true) f2 else h2)) + whatis(extract(if (true) f2 else h3)) + whatis(extract(if (true) f3 else g1)) + whatis(extract(if (true) f3 else g2)) + whatis(extract(if (true) f3 else g3)) + whatis(extract(if (true) f3 else h1)) + whatis(extract(if (true) f3 else h2)) + whatis(extract(if (true) f3 else h3)) + whatis(extract(if (true) g1 else g2)) + whatis(extract(if (true) g1 else g3)) + whatis(extract(if (true) g1 else h1)) + whatis(extract(if (true) g1 else h2)) + whatis(extract(if (true) g1 else h3)) + whatis(extract(if (true) g2 else g3)) + whatis(extract(if (true) g2 else h1)) + whatis(extract(if (true) g2 else h2)) + whatis(extract(if (true) g2 else h3)) + whatis(extract(if (true) g3 else h1)) + whatis(extract(if (true) g3 else h2)) + whatis(extract(if (true) g3 else h3)) + whatis(extract(if (true) h1 else h2)) + whatis(extract(if (true) h1 else h3)) + whatis(extract(if (true) h2 else h3)) + + whatis(extract(Nil)) + whatis(extract(Vector())) + whatis(extract(Map[Int,Int]())) + whatis(extract(Set[Int]())) + whatis(extract(Seq[Int]())) + whatis(extract(Array[Int]())) + whatis(extract(scala.collection.immutable.BitSet(1))) + whatis(extract("abc")) + whatis(extract(if (true) Stream(1) else List(1))) + whatis(extract(if (true) Seq(1) else Set(1))) + } +} diff --git a/test/files/run/reflection-magicsymbols-invoke.check b/test/files/run/reflection-magicsymbols-invoke.check index 352aefaf25..037ba33d21 100644 --- a/test/files/run/reflection-magicsymbols-invoke.check +++ b/test/files/run/reflection-magicsymbols-invoke.check @@ -28,7 +28,7 @@ it's important to print the list of AnyVal's members if some of them change (possibly, adding and/or removing magic symbols), we must update this test constructor AnyVal: ()AnyVal method getClass: ()Class[_ <: AnyVal] -testing AnyVal.<init>: class java.lang.InstantiationException: null +testing AnyVal.<init>: class scala.ScalaReflectionException: unsupported symbol constructor AnyVal when invoking bytecodeless method mirror for scala.AnyVal.<init>(): AnyVal (bound to null) testing AnyVal.getClass: class scala.ScalaReflectionException: expected a member of class Integer, you provided method scala.AnyVal.getClass ============ AnyRef @@ -36,12 +36,10 @@ it's important to print the list of AnyRef's members if some of them change (possibly, adding and/or removing magic symbols), we must update this test constructor Object: ()java.lang.Object method !=: (x$1: Any)Boolean -method !=: (x$1: AnyRef)Boolean method ##: ()Int method $asInstanceOf: [T0]()T0 method $isInstanceOf: [T0]()Boolean method ==: (x$1: Any)Boolean -method ==: (x$1: AnyRef)Boolean method asInstanceOf: [T0]=> T0 method clone: ()java.lang.Object method eq: (x$1: AnyRef)Boolean @@ -84,12 +82,10 @@ if some of them change (possibly, adding and/or removing magic symbols), we must constructor Array: (_length: Int)Array[T] constructor Cloneable: ()java.lang.Cloneable method !=: (x$1: Any)Boolean -method !=: (x$1: AnyRef)Boolean method ##: ()Int method $asInstanceOf: [T0]()T0 method $isInstanceOf: [T0]()Boolean method ==: (x$1: Any)Boolean -method ==: (x$1: AnyRef)Boolean method apply: (i: Int)T method asInstanceOf: [T0]=> T0 method clone: ()Array[T] diff --git a/test/files/run/t6260-delambdafy.check b/test/files/run/t6260-delambdafy.check new file mode 100644 index 0000000000..b2a7bed988 --- /dev/null +++ b/test/files/run/t6260-delambdafy.check @@ -0,0 +1,4 @@ +f(C@2e) + +Test$lambda$1$$apply +apply diff --git a/test/files/run/t6260-delambdafy.flags b/test/files/run/t6260-delambdafy.flags new file mode 100644 index 0000000000..48b438ddf8 --- /dev/null +++ b/test/files/run/t6260-delambdafy.flags @@ -0,0 +1 @@ +-Ydelambdafy:method diff --git a/test/files/run/t6260-delambdafy.scala b/test/files/run/t6260-delambdafy.scala new file mode 100644 index 0000000000..056b1edd4e --- /dev/null +++ b/test/files/run/t6260-delambdafy.scala @@ -0,0 +1,12 @@ +class C[A](private val a: Any) extends AnyVal + +object Test { + val f = (x: C[Any]) => {println(s"f($x)"); x} + def main(args: Array[String]) { + f(new C(".")) + val methods = f.getClass.getDeclaredMethods.map(_.getName).sorted + println("") + println(methods.mkString("\n")) + } +} + diff --git a/test/files/run/t6260c.check b/test/files/run/t6260c.check new file mode 100644 index 0000000000..1a57f2d741 --- /dev/null +++ b/test/files/run/t6260c.check @@ -0,0 +1,5 @@ +f(C@2e) + +Test$$anonfun$$apply +apply +g(C@2e) diff --git a/test/files/run/t6260c.scala b/test/files/run/t6260c.scala new file mode 100644 index 0000000000..845dc157b7 --- /dev/null +++ b/test/files/run/t6260c.scala @@ -0,0 +1,17 @@ +class C[A](private val a: Any) extends AnyVal + +object Test { + val f = (x: C[Any]) => {println(s"f($x)"); x} + trait T[A] { + def apply(a: A): A + } + val g = new T[C[Any]] { def apply(a: C[Any]) = { println(s"g($a)"); a } } + def main(args: Array[String]) { + f(new C(".")) + val methods = f.getClass.getDeclaredMethods.map(_.getName).sorted + println("") + println(methods.mkString("\n")) + g.apply(new C(".")) + } +} + diff --git a/test/files/run/t6411a.check b/test/files/run/t6411a.check new file mode 100644 index 0000000000..9226146195 --- /dev/null +++ b/test/files/run/t6411a.check @@ -0,0 +1,96 @@ +meth = method yg_1 +as seen by Scala reflection: def yg_1[T](y: Y[T]): T +as seen by Java reflection: public java.lang.Object a$.yg_1(java.lang.Object) +result = 1 +meth = method yg_1 +as seen by Scala reflection: def yg_1[T](y: Y[T]): T +as seen by Java reflection: public java.lang.Object a$.yg_1(java.lang.Object) +result = 1 +meth = method yi_2 +as seen by Scala reflection: def yi_2(y: Y[Int]): Int +as seen by Java reflection: public int a$.yi_2(java.lang.Integer) +result = 2 +meth = method yi_2 +as seen by Scala reflection: def yi_2(y: Y[Int]): Int +as seen by Java reflection: public int a$.yi_2(java.lang.Integer) +result = class java.lang.IllegalArgumentException: argument type mismatch +meth = method ys_3 +as seen by Scala reflection: def ys_3(y: Y[String]): String +as seen by Java reflection: public java.lang.String a$.ys_3(java.lang.String) +result = class java.lang.IllegalArgumentException: argument type mismatch +meth = method ys_3 +as seen by Scala reflection: def ys_3(y: Y[String]): String +as seen by Java reflection: public java.lang.String a$.ys_3(java.lang.String) +result = 3 +meth = method ya_4 +as seen by Scala reflection: def ya_4(ys: Array[Y[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.ya_4(Y[]) +result = class java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String +meth = method ya_4 +as seen by Scala reflection: def ya_4(ys: Array[Y[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.ya_4(Y[]) +result = List(4) +meth = method yl_5 +as seen by Scala reflection: def yl_5(ys: List[Y[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.yl_5(scala.collection.immutable.List) +result = class java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String +meth = method yl_5 +as seen by Scala reflection: def yl_5(ys: List[Y[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.yl_5(scala.collection.immutable.List) +result = List(5) +meth = method yni_7 +as seen by Scala reflection: def yni_7(y: => Y[Int]): Int +as seen by Java reflection: public int a$.yni_7(scala.Function0) +result = 7 +meth = method yns_8 +as seen by Scala reflection: def yns_8(y: => Y[String]): String +as seen by Java reflection: public java.lang.String a$.yns_8(scala.Function0) +result = 8 +meth = method zg_1 +as seen by Scala reflection: def zg_1[T](z: Z[T]): T +as seen by Java reflection: public java.lang.Object a$.zg_1(Z) +result = 1 +meth = method zg_1 +as seen by Scala reflection: def zg_1[T](z: Z[T]): T +as seen by Java reflection: public java.lang.Object a$.zg_1(Z) +result = 1 +meth = method zi_2 +as seen by Scala reflection: def zi_2(z: Z[Int]): Int +as seen by Java reflection: public int a$.zi_2(Z) +result = 2 +meth = method zi_2 +as seen by Scala reflection: def zi_2(z: Z[Int]): Int +as seen by Java reflection: public int a$.zi_2(Z) +result = class java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer +meth = method zs_3 +as seen by Scala reflection: def zs_3(z: Z[String]): String +as seen by Java reflection: public java.lang.String a$.zs_3(Z) +result = class java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String +meth = method zs_3 +as seen by Scala reflection: def zs_3(z: Z[String]): String +as seen by Java reflection: public java.lang.String a$.zs_3(Z) +result = 3 +meth = method za_4 +as seen by Scala reflection: def za_4(zs: Array[Z[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.za_4(Z[]) +result = class java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String +meth = method za_4 +as seen by Scala reflection: def za_4(zs: Array[Z[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.za_4(Z[]) +result = List(4) +meth = method zl_5 +as seen by Scala reflection: def zl_5(zs: List[Z[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.zl_5(scala.collection.immutable.List) +result = class java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String +meth = method zl_5 +as seen by Scala reflection: def zl_5(zs: List[Z[String]]): List[String] +as seen by Java reflection: public scala.collection.immutable.List a$.zl_5(scala.collection.immutable.List) +result = List(5) +meth = method zni_7 +as seen by Scala reflection: def zni_7(z: => Z[Int]): Int +as seen by Java reflection: public int a$.zni_7(scala.Function0) +result = 7 +meth = method zns_8 +as seen by Scala reflection: def zns_8(z: => Z[String]): String +as seen by Java reflection: public java.lang.String a$.zns_8(scala.Function0) +result = 8 diff --git a/test/files/run/t6411a.scala b/test/files/run/t6411a.scala new file mode 100644 index 0000000000..3bfeac2890 --- /dev/null +++ b/test/files/run/t6411a.scala @@ -0,0 +1,81 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.language.reflectiveCalls + +class Y[T](val i: T) extends AnyVal { + override def toString = s"Y($i)" +} +class Z[T](val i: T) extends AnyRef { + override def toString = s"Z($i)" +} + +object a { + def yg_1[T](y: Y[T]) = y.i + def yi_2(y: Y[Int]) = y.i + def ys_3(y: Y[String]) = y.i + def ya_4(ys: Array[Y[String]]) = ys.toList.map(_.i) + def yl_5(ys: List[Y[String]]) = ys.map(_.i) + def yv_6(ys: Y[String]*) = ys.toList.map(_.i) + def yni_7(y: => Y[Int]) = y.i + def yns_8(y: => Y[String]) = y.i + + def zg_1[T](z: Z[T]) = z.i + def zi_2(z: Z[Int]) = z.i + def zs_3(z: Z[String]) = z.i + def za_4(zs: Array[Z[String]]) = zs.toList.map(_.i) + def zl_5(zs: List[Z[String]]) = zs.map(_.i) + def zv_6(zs: Z[String]*) = zs.toList.map(_.i) + def zni_7(z: => Z[Int]) = z.i + def zns_8(z: => Z[String]) = z.i +} + +object Test extends App { + def test(methName: String, arg: Any) = { + val moduleA = cm.reflect(a) + val msym = moduleA.symbol.typeSignature.declaration(TermName(methName)).asMethod + println(s"meth = $msym") + val mmirror = moduleA.reflectMethod(msym) + val mresult = + try { mmirror(arg) } + catch { + case ex: Exception => + val ex1 = scala.reflect.runtime.ReflectionUtils.unwrapThrowable(ex) + s"${ex1.getClass}: ${ex1.getMessage}" + } + println(s"as seen by Scala reflection: ${msym.asInstanceOf[scala.reflect.internal.Symbols#Symbol].defString}") + println(s"as seen by Java reflection: ${mmirror.asInstanceOf[{val jmeth: java.lang.reflect.Method}].jmeth}") + println(s"result = $mresult") + } + + test("yg_1", new Y(1)) + test("yg_1", new Y("1")) + test("yi_2", new Y(2)) + test("yi_2", new Y("2")) + test("ys_3", new Y(3)) + test("ys_3", new Y("3")) + test("ya_4", Array(new Y(4))) + test("ya_4", Array(new Y("4"))) + test("yl_5", List(new Y(5))) + test("yl_5", List(new Y("5"))) + // FIXME: disabled because of SI-7056 + // test("yv_6", new Y(6)) + // test("yv_6", new Y("6")) + test("yni_7", new Y(7)) + test("yns_8", new Y("8")) + + test("zg_1", new Z(1)) + test("zg_1", new Z("1")) + test("zi_2", new Z(2)) + test("zi_2", new Z("2")) + test("zs_3", new Z(3)) + test("zs_3", new Z("3")) + test("za_4", Array(new Z(4))) + test("za_4", Array(new Z("4"))) + test("zl_5", List(new Z(5))) + test("zl_5", List(new Z("5"))) + // FIXME: disabled because of SI-7056 + // test("zv_6", new Z(6)) + // test("zv_6", new Z("6")) + test("zni_7", new Z(7)) + test("zns_8", new Z("8")) +}
\ No newline at end of file diff --git a/test/files/run/t6411b.check b/test/files/run/t6411b.check new file mode 100644 index 0000000000..e20bed6d8d --- /dev/null +++ b/test/files/run/t6411b.check @@ -0,0 +1 @@ +Bar(Foo(3)) diff --git a/test/files/run/t6411b.scala b/test/files/run/t6411b.scala new file mode 100644 index 0000000000..af30108826 --- /dev/null +++ b/test/files/run/t6411b.scala @@ -0,0 +1,12 @@ +import scala.reflect.runtime.universe._ + +case class Foo(n: Int) extends AnyVal +case class Bar(foo: Foo) + +object Test extends App { + val mirror = runtimeMirror(getClass.getClassLoader) + val cm = mirror.reflectClass(typeOf[Bar].typeSymbol.asClass) + val ctor = typeOf[Bar].declaration(nme.CONSTRUCTOR).asMethod + val ctorm = cm.reflectConstructor(ctor) + println(ctorm(Foo(3))) +}
\ No newline at end of file diff --git a/test/files/run/t7319.check b/test/files/run/t7319.check index d03ee3a6cf..b7443aa0c4 100644 --- a/test/files/run/t7319.check +++ b/test/files/run/t7319.check @@ -21,7 +21,7 @@ scala> convert(Some[Int](0)) --- because --- argument expression's type is not compatible with formal parameter type; found : Some[Int] - required: ?F forSome { type _$1 <: ?F forSome { type _$2 } } + required: ?F[_$1] forSome { type _$1 <: ?F[_$2] forSome { type _$2 } } convert(Some[Int](0)) ^ <console>:12: error: type mismatch; diff --git a/test/files/run/t7328.check b/test/files/run/t7328.check new file mode 100644 index 0000000000..e386fe70d9 --- /dev/null +++ b/test/files/run/t7328.check @@ -0,0 +1,4 @@ +Foo +Foo(3) +Foo(3) +Foo(5) diff --git a/test/files/run/t7328.scala b/test/files/run/t7328.scala new file mode 100644 index 0000000000..8816fa2347 --- /dev/null +++ b/test/files/run/t7328.scala @@ -0,0 +1,18 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} + +case class Foo(x: Int) extends AnyVal +case class Bar(foo: Foo) + +object Test extends App { + val foo = typeOf[Bar].declaration(TermName("foo")).asMethod + println(foo.returnType) // Foo + + val bar = Bar(Foo(3)) + println(bar.foo) // Foo(3) + + val im = cm.reflect(bar) + println(im.reflectField(foo).get) // incorrectly gives java.lang.Integer(3) not Foo(3) + im.reflectField(foo).set(Foo(5)) // java.lang.IllegalArgumentException: Can not set int field Bar.foo to Foo + println(im.reflectMethod(foo)()) // incorrectly gives java.lang.Integer(3) not Foo(3) +}
\ No newline at end of file diff --git a/test/files/run/t7570a.check b/test/files/run/t7570a.check new file mode 100644 index 0000000000..3cc58df837 --- /dev/null +++ b/test/files/run/t7570a.check @@ -0,0 +1 @@ +C diff --git a/test/files/run/t7570a.scala b/test/files/run/t7570a.scala new file mode 100644 index 0000000000..b8b4ddeaf2 --- /dev/null +++ b/test/files/run/t7570a.scala @@ -0,0 +1,11 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.ToolBox +import definitions._ +import Flag._ + +object Test extends App { + val tb = cm.mkToolBox() + val csym = tb.define(q"""class C { override def toString = "C" }""") + println(tb.eval(q"new $csym")) +}
\ No newline at end of file diff --git a/test/files/run/t7570b.check b/test/files/run/t7570b.check new file mode 100644 index 0000000000..0c28247025 --- /dev/null +++ b/test/files/run/t7570b.check @@ -0,0 +1 @@ +compilation failed: reflective toolbox has failed: cannot have free terms in a top-level definition diff --git a/test/files/run/t7570b.scala b/test/files/run/t7570b.scala new file mode 100644 index 0000000000..f1db193186 --- /dev/null +++ b/test/files/run/t7570b.scala @@ -0,0 +1,17 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import definitions._ +import Flag._ + +object Test extends App { + val tb = cm.mkToolBox() + val msg = build.newFreeTerm("msg", "C") + build.setTypeSignature(msg, typeOf[String]) + try { + val csym = tb.define(q"""class C { override def toString = $msg }""") + println(tb.eval(q"new $csym")) + } catch { + case ToolBoxError(message, _) => println(s"compilation failed: $message") + } +}
\ No newline at end of file diff --git a/test/files/run/t7570c.check b/test/files/run/t7570c.check new file mode 100644 index 0000000000..61e659d9e0 --- /dev/null +++ b/test/files/run/t7570c.check @@ -0,0 +1,2 @@ +(class C,true,false,false) +(object D,false,true,false) diff --git a/test/files/run/t7570c.scala b/test/files/run/t7570c.scala new file mode 100644 index 0000000000..a5bdbffe18 --- /dev/null +++ b/test/files/run/t7570c.scala @@ -0,0 +1,13 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import definitions._ +import Flag._ + +object Test extends App { + val tb = cm.mkToolBox() + val csym = tb.define(q"""class C { override def toString = "C" }""") + println((csym, csym.isClass, csym.isModule, csym.isModuleClass)) + val dsym = tb.define(q"""object D { override def toString = "D" }""".asInstanceOf[ModuleDef]) + println((dsym, dsym.isClass, dsym.isModule, dsym.isModuleClass)) +}
\ No newline at end of file diff --git a/test/disabled/run/t7843-jsr223-service.check b/test/files/run/t7843-jsr223-service.check index a668df3567..a668df3567 100644 --- a/test/disabled/run/t7843-jsr223-service.check +++ b/test/files/run/t7843-jsr223-service.check diff --git a/test/files/run/t7843-jsr223-service.scala b/test/files/run/t7843-jsr223-service.scala new file mode 100644 index 0000000000..31112212ea --- /dev/null +++ b/test/files/run/t7843-jsr223-service.scala @@ -0,0 +1,8 @@ +import scala.tools.nsc.interpreter.IMain + +object Test extends App { + val engine = new IMain.Factory getScriptEngine() + engine.asInstanceOf[IMain].settings.usejavacp.value = true + engine put ("n", 10) + engine eval "1 to n.asInstanceOf[Int] foreach print" +} diff --git a/test/files/run/t7933.check b/test/files/run/t7933.check new file mode 100644 index 0000000000..317e9677c3 --- /dev/null +++ b/test/files/run/t7933.check @@ -0,0 +1,2 @@ +hello +hello diff --git a/test/files/run/t7933.scala b/test/files/run/t7933.scala new file mode 100644 index 0000000000..b06dffcd80 --- /dev/null +++ b/test/files/run/t7933.scala @@ -0,0 +1,11 @@ +import scala.tools.nsc.interpreter.IMain + +object Test extends App { + val engine = new IMain.Factory getScriptEngine() + engine.asInstanceOf[IMain].settings.usejavacp.value = true + val res2 = engine.asInstanceOf[javax.script.Compilable] + res2 compile "8" eval() + val res5 = res2 compile """println("hello") ; 8""" + res5 eval() + res5 eval() +} diff --git a/test/files/run/t8245.scala b/test/files/run/t8245.scala new file mode 100644 index 0000000000..d44defbb9e --- /dev/null +++ b/test/files/run/t8245.scala @@ -0,0 +1,14 @@ +object Test { + def foo(o: Option[Int]): Int = { + lazy val i: Int = { + def local: Int = {if ("".isEmpty) return 42; -42} + assert(local == 42) + o.getOrElse(return -1) + } + i + 1 + } + + def main(args: Array[String]) { + assert(foo(None) == -1) + } +} diff --git a/test/files/scalacheck/quasiquotes/PatternConstructionProps.scala b/test/files/scalacheck/quasiquotes/PatternConstructionProps.scala index 582e915258..ca4c8609ac 100644 --- a/test/files/scalacheck/quasiquotes/PatternConstructionProps.scala +++ b/test/files/scalacheck/quasiquotes/PatternConstructionProps.scala @@ -29,4 +29,8 @@ object PatternConstructionProps extends QuasiquoteProperties("pattern constructi property("splice into casedef") = forAll { (pat: Tree, cond: Tree, body: Tree) => cq"$pat if $cond => $body" ≈ CaseDef(pat, cond, body) } -}
\ No newline at end of file + + property("splice into alternative") = forAll { (first: Tree, rest: List[Tree]) => + pq"$first | ..$rest" ≈ Alternative(first :: rest) + } +} diff --git a/test/files/scalacheck/quasiquotes/PatternDeconstructionProps.scala b/test/files/scalacheck/quasiquotes/PatternDeconstructionProps.scala index c8e66c7ef5..ad3266bcec 100644 --- a/test/files/scalacheck/quasiquotes/PatternDeconstructionProps.scala +++ b/test/files/scalacheck/quasiquotes/PatternDeconstructionProps.scala @@ -36,4 +36,9 @@ object PatternDeconstructionProps extends QuasiquoteProperties("pattern deconstr val cq"$pat0 if $cond0 => $body0" = cq"$pat if $cond => $body" pat0 ≈ pat && cond0 ≈ cond && body0 ≈ body } + + property("extract alternative") = forAll { (first: Tree, rest: List[Tree]) => + val pq"$first1 | ..$rest1" = pq"$first | ..$rest" + first1 ≈ first && rest1 ≈ rest + } } diff --git a/test/junit/scala/collection/SetMapConsistencyTest.scala b/test/junit/scala/collection/SetMapConsistencyTest.scala index 7bb8ca958b..0d6f43db06 100644 --- a/test/junit/scala/collection/SetMapConsistencyTest.scala +++ b/test/junit/scala/collection/SetMapConsistencyTest.scala @@ -478,7 +478,7 @@ class SetMapConsistencyTest { } @Test - def si8213() { + def testSI8213() { val am = new scala.collection.mutable.AnyRefMap[String, Int] for (i <- 0 until 1024) am += i.toString -> i am.getOrElseUpdate("1024", { am.clear; -1 }) @@ -488,4 +488,23 @@ class SetMapConsistencyTest { lm.getOrElseUpdate(1024, { lm.clear; -1 }) assert(lm == scala.collection.mutable.LongMap(1024L -> -1)) } + + // Mutating when an iterator is in the wild shouldn't produce random junk in the iterator + // Todo: test all sets/maps this way + @Test + def testSI8154() { + def f() = { + val xs = scala.collection.mutable.AnyRefMap[String, Int]("a" -> 1) + val it = xs.iterator + it.hasNext + xs.clear() + + if (it.hasNext) Some(it.next) + else None + } + assert(f() match { + case Some((a,b)) if (a==null || b==null) => false + case _ => true + }) + } } diff --git a/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala b/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala index a3699a4eeb..b42e9a07cb 100644 --- a/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala +++ b/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala @@ -69,6 +69,7 @@ class SymbolTableForUnitTesting extends SymbolTable { // Members declared in scala.reflect.internal.Required def picklerPhase: scala.reflect.internal.Phase = SomePhase + def erasurePhase: scala.reflect.internal.Phase = SomePhase // Members declared in scala.reflect.internal.SymbolTable def currentRunId: Int = 1 |