8000 Merge pull request #10032 from som-snytt/issue/12565-improved · scala/scala@0b90bde · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b90bde

Browse files
authored
Merge pull request #10032 from som-snytt/issue/12565-improved
Forward port java annotation handling
2 parents 3ed3e3f + 012ed8f commit 0b90bde

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import scala.util.control.NonFatal
3939
* @author Martin Odersky
4040
*/
4141
abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
42+
import ClassfileParser._
43+
4244
val symbolTable: SymbolTable {
4345
def settings: Settings
4446
}
@@ -80,7 +82,6 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
8082
protected var srcfile0 : Option[AbstractFile] = None
8183
protected def moduleClass: Symbol = staticModule.moduleClass
8284
protected val TASTYUUIDLength: Int = 16
83-
private var sawPrivateConstructor = false
8485
private var YtastyReader = false
8586

8687
private def ownerForFlags(jflags: JavaAccFlags) = if (jflags.isStatic) moduleClass else clazz
@@ -543,14 +544,9 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
543544
parseAttributes(clazz, completer)
544545

545546
in.bp = fieldsStartBp
546-
0 until u2() foreach (_ => parseField())
547-
sawPrivateConstructor = false
548-
0 until u2() foreach (_ => parseMethod())
549-
val needsConstructor = (
550-
!sawPrivateConstructor
551-
&& !instanceScope.containsName(nme.CONSTRUCTOR)
552-
&& ((sflags & (INTERFACE|JAVA_ANNOTATION)) == (INTERFACE|JAVA_ANNOTATION))
553-
)
547+
u2() times parseField()
548+
u2() times parseMethod()
549+
val needsConstructor = (sflags & JAVA_ANNOTATION) != 0L
554550
if (needsConstructor)
555551
instanceScope enter clazz.newClassConstructor(NoPosition)
556552

@@ -568,7 +564,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
568564
var sym = clazz.owner
569565
while (sym.isClass && !sym.isModuleClass) {
570566
for (t <- sym.tpe.typeArgs)
571-
classTParams = classTParams + (t.typeSymbol.name -> t.typeSymbol)
567+
classTParams += (t.typeSymbol.name -> t.typeSymbol)
572568

573569
sym = sym.owner
574570
}
@@ -581,9 +577,9 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
581577
if ((sflags & PRIVATE) != 0L) {
582578
in.skip(4); skipAttributes()
583579
} else {
584-
val name = readName()
580+
val name = readName()
585581
val lazyInfo = new MemberTypeCompleter(name, jflags, pool.getExternalName(u2()).value)
586-
val sym = ownerForFlags(jflags).newValue(name.toTermName, NoPosition, sflags)
582+
val sym = ownerForFlags(jflags).newValue(name.toTermName, NoPosition, sflags)
587583

588584
// Note: the info may be overwritten later with a generic signature
589585
// parsed from SignatureATTR
@@ -615,27 +611,20 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
615611

616612
def parseMethod(): Unit = {
617613
val jflags = readMethodFlags()
618-
val sflags = jflags.toScalaFlags
619614
if (jflags.isPrivate) {
620-
val isConstructor = pool.getName(u2()).value == "<init>" // opt avoid interning a Name for private methods we're about to discard
621-
if (isConstructor)
622-
sawPrivateConstructor = true
623-
in.skip(2); skipAttributes()
615+
in.skip(4); skipAttributes()
624616
} else {
625-
if ((sflags & PRIVATE) != 0L) {
626-
in.skip(4); skipAttributes()
627-
} else {
628-
val name = readName()
629-
val sym = ownerForFlags(jflags).newMethod(name.toTermName, NoPosition, sflags)
630-
// Note: the info may be overwritten later with a generic signature
631-
// parsed from SignatureATTR
632-
val lazyInfo = new MemberTypeCompleter(name, jflags, pool.getExternalName(u2()).value)
633-
sym.info = lazyInfo
634-
propagatePackageBoundary(jflags, sym)
635-
parseAttributes(sym, lazyInfo)
636-
addJavaFlagsAnnotations(sym, jflags)
637-
getScope(jflags) enter sym
638-
}
617+
val name = readName()
618+
val sflags = jflags.toScalaFlags
619+
val sym = ownerForFlags(jflags).newMethod(name.toTermName, NoPosition, sflags)
620+
// Note: the info may be overwritten later with a generic signature
621+
// parsed from SignatureATTR
622+
val lazyInfo = new MemberTypeCompleter(name, jflags, pool.getExternalName(u2()).value)
623+
sym.info = lazyInfo
624+
propagatePackageBoundary(jflags, sym)
625+
parseAttributes(sym, lazyInfo)
626+
addJavaFlagsAnnotations(sym, jflags)
627+
getScope(jflags) enter sym
639628
}
640629
}
641630

@@ -865,7 +854,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
865854
case tpnme.RuntimeAnnotationATTR =>
866855
val numAnnots = u2()
867856
val annots = new ListBuffer[AnnotationInfo]
868-
for (n <- 0 until numAnnots; annot <- parseAnnotation(u2()))
857+
for (n <- 0 until numAnnots; annot <- parseAnnotation(u2()))
869858
annots += annot
870859
/* `sym.withAnnotations(annots)`, like `sym.addAnnotation(annot)`, prepends,
871860
* so if we parsed in classfile order we would wind up with the annotations
@@ -933,7 +922,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
933922
}
934923
}
935924
// begin parseAttributes
936-
for (i <- 0 until u2()) parseAttribute()
925+
u2() times parseAttribute()
937926
}
938927

939928

@@ -967,15 +956,16 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
967956
case ARRAY_TAG =>
968957
val arr = new ArrayBuffer[ClassfileAnnotArg]()
969958
var hasError = false
970-
for (i <- 0 until index)
959+
index times {
971960
parseAnnotArg() match {
972961
case Some(c) => arr += c
973962
case None => hasError = true
974963
}
964+
}
975965
if (hasError) None
976966
else Some(ArrayAnnotArg(arr.toArray))
977967
case ANNOTATION_TAG =>
978-
parseAnnotation(index) map (NestedAnnotArg(_))
968+
parseAnnotation(index).map(NestedAnnotArg(_))
979969
}
980970
}
981971

@@ -989,7 +979,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
989979
val nargs = u2()
990980
val nvpairs = new ListBuffer[(Name, ClassfileAnnotArg)]
991981
var hasError = false
992-
for (i <- 0 until nargs) {
982+
nargs times {
993983
val name = readName()
994984
parseAnnotArg() match {
995985
case Some(c) => nvpairs += ((name, c))
@@ -1016,7 +1006,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
10161006
* if the corresponding flag is set in `flags`.
10171007
*/
10181008
def addJavaFlagsAnnotations(sym: Symbol, flags: JavaAccFlags): Unit =
1019-
flags.toScalaAnnotations(symbolTable) foreach (ann => sym.addAnnotation(ann))
1009+
flags.toScalaAnnotations(symbolTable).foreach(sym.addAnnotation(_))
10201010

10211011
/** Enter own inner classes in the right scope. It needs the scopes to be set up,
10221012
* and implicitly current class' superclasses.
@@ -1179,7 +1169,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
11791169

11801170
case ARRAY_TAG =>
11811171
val num = u2()
1182-
for (i <- 0 until num) skipAnnotArg()
1172+
num times skipAnnotArg()
11831173

11841174
case ANNOTATION_TAG =>
11851175
in.skip(2) // type
@@ -1188,7 +1178,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
11881178

11891179
def skipAnnotArgs() = {
11901180
val numArgs = u2()
1191-
for (i <- 0 until numArgs) {
1181+
numArgs times {
11921182
in.skip(2)
11931183
skipAnnotArg()
11941184
}
@@ -1281,7 +1271,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
12811271
} else if (!isScalaRaw && innersStart != -1) {
12821272
in.bp = innersStart
12831273
val entries = u2()
1284-
for (_ <- 0 until entries) {
1274+
entries times {
12851275
val innerIndex, outerIndex, nameIndex = u2()
12861276
val jflags = readInnerClassFlags()
12871277
if (innerIndex != 0 && outerIndex != 0 && nameIndex != 0)
@@ -1502,3 +1492,8 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
15021492
protected def getScope(flags: JavaAccFlags): Scope =
15031493
if (flags.isStatic) staticScope else instanceScope
15041494
}
1495+
object ClassfileParser {
1496+
private implicit class GoodTimes(val n: Int) extends AnyVal {
1497+
def times(body: => Unit) = (1 to n).foreach(_ => body)
1498+
}
1499+
}

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5272,7 +5272,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
52725272
wrapErrors(t, _.typed1(t, mode, pt))
52735273
}
52745274

5275-
val sym = tree.symbol orElse member(qual.tpe, name) orElse inCompanionForJavaStatic(qual.symbol, name)
5275+
val sym = tree.symbol orElse member(qual.tpe, name) orElse inCompanionForJavaStatic(qual.tpe.typeSymbol, name)
52765276
if ((sym eq NoSymbol) && name != nme.CONSTRUCTOR && mode.inAny(EXPRmode | PATTERNmode)) {
52775277
// symbol not found? --> try to convert implicitly to a type that does have the required
52785278
// member. Added `| PATTERNmode` to allow enrichment in patterns (so we can add e.g., an

0 commit comments

Comments
 (0)
0