|
| 1 | +package dotty.tools.dotc |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import org.junit.Ignore |
| 5 | +import org.junit.Assert._ |
| 6 | + |
| 7 | +import dotty.tools.io._ |
| 8 | +import dotty.tools.dotc.util.ClasspathFromClassloader |
| 9 | + |
| 10 | +import scala.quoted._ |
| 11 | + |
| 12 | +import java.io.File.pathSeparator |
| 13 | + |
| 14 | +class BootstrappedStdLibTASYyTest: |
| 15 | + |
| 16 | + import BootstrappedStdLibTASYyTest._ |
| 17 | + |
| 18 | + /** Test that we can load trees from TASTy */ |
| 19 | + @Test def testTastyInspector: Unit = |
| 20 | + loadWithTastyInspector(loadBlacklisted) |
| 21 | + |
| 22 | + /** Test that we can load and compile trees from TASTy */ |
| 23 | + @Test def testFromTasty: Unit = |
| 24 | + compileFromTasty(loadBlacklisted.union(compileBlacklisted)) |
| 25 | + |
| 26 | + @Ignore |
| 27 | + @Test def testWhiteListFromTasty: Unit = |
| 28 | + val whitelist = Set( |
| 29 | + "scala.collection.mutable.StringBuilder" |
| 30 | + ) |
| 31 | + compileFromTasty(x => !whitelist(x)) |
| 32 | + |
| 33 | + @Test def blacklistNoDuplicates = |
| 34 | + def testDup(name: String, list: List[String], set: Set[String]) = |
| 35 | + assert(list.size == set.size, |
| 36 | + list.diff(set.toSeq).mkString(s"`$name` has duplicate entries:\n ", "\n ", "\n\n")) |
| 37 | + testDup("loadBlacklist", loadBlacklist, loadBlacklisted) |
| 38 | + testDup("compileBlacklist", compileBlacklist, compileBlacklisted) |
| 39 | + |
| 40 | + @Test def blacklistsNoIntersection = |
| 41 | + val intersection = loadBlacklisted & compileBlacklisted |
| 42 | + assert(intersection.isEmpty, |
| 43 | + intersection.mkString( |
| 44 | + "`compileBlacklist` contains names that are already in `loadBlacklist`: \n ", "\n ", "\n\n")) |
| 45 | + |
| 46 | + @Test def blacklistsOnlyContainsClassesThatExist = |
| 47 | + val scalaLibJarTastyClassNamesSet = scalaLibJarTastyClassNames.toSet |
| 48 | + val intersection = loadBlacklisted & compileBlacklisted |
| 49 | + assert(loadBlacklisted.diff(scalaLibJarTastyClassNamesSet).isEmpty, |
| 50 | + loadBlacklisted.diff(scalaLibJarTastyClassNamesSet).mkString( |
| 51 | + "`loadBlacklisted` contains names that are not in `scalaLibJarTastyClassNames`: \n ", "\n ", "\n\n")) |
| 52 | + assert(compileBlacklisted.diff(scalaLibJarTastyClassNamesSet).isEmpty, |
| 53 | + compileBlacklisted.diff(scalaLibJarTastyClassNamesSet).mkString( |
| 54 | + "`loadBlacklisted` contains names that are not in `scalaLibJarTastyClassNames`: \n ", "\n ", "\n\n")) |
| 55 | + |
| 56 | + @Ignore |
| 57 | + @Test def testLoadBacklistIsMinimal = |
| 58 | + var shouldBeWhitelisted = List.empty[String] |
| 59 | + val size = loadBlacklisted.size |
| 60 | + for (notBlacklisted, i) <- loadBlacklist.zipWithIndex do |
| 61 | + val blacklist = loadBlacklisted - notBlacklisted |
| 62 | + println(s"Trying withouth $notBlacklisted in the blacklist (${i+1}/$size)") |
| 63 | + try { |
| 64 | + loadWithTastyInspector(blacklist) |
| 65 | + shouldBeWhitelisted = notBlacklisted :: shouldBeWhitelisted |
| 66 | + } |
| 67 | + catch { |
| 68 | + case ex: Throwable => // ok |
| 69 | + } |
| 70 | + assert(shouldBeWhitelisted.isEmpty, |
| 71 | + shouldBeWhitelisted.mkString("Some classes do not need to be blacklisted in `loadBlacklisted`\n ", "\n ", "\n\n")) |
| 72 | + |
| 73 | + @Ignore |
| 74 | + @Test def testCompileBlacklistIsMinimal = |
| 75 | + var shouldBeWhitelisted = List.empty[String] |
| 76 | + val size = compileBlacklisted.size |
| 77 | + val blacklist0 = loadBlacklisted.union(compileBlacklisted) |
| 78 | + for (notBlacklisted, i) <- compileBlacklist.zipWithIndex do |
| 79 | + val blacklist = blacklist0 - notBlacklisted |
| 80 | + println(s"Trying withouth $notBlacklisted in the blacklist (${i+1}/$size)") |
| 81 | + try { |
| 82 | + compileFromTasty(blacklist) |
| 83 | + shouldBeWhitelisted = notBlacklisted :: shouldBeWhitelisted |
| 84 | + } |
| 85 | + catch { |
| 86 | + case ex: Throwable => // ok |
| 87 | + } |
| 88 | + assert(shouldBeWhitelisted.isEmpty, |
| 89 | + shouldBeWhitelisted.mkString("Some classes do not need to be blacklisted in `compileBlacklisted`\n ", "\n ", "\n\n")) |
| 90 | + |
| 91 | +end BootstrappedStdLibTASYyTest |
| 92 | + |
| 93 | +object BootstrappedStdLibTASYyTest: |
| 94 | + |
| 95 | + val scalaLibJarPath = System.getProperty("dotty.scala.library") |
| 96 | + |
| 97 | + val scalaLibJarTastyClassNames = { |
| 98 | + val scalaLibJar = Jar(new File(java.nio.file.Paths.get(scalaLibJarPath))) |
| 99 | + scalaLibJar.toList.map(_.toString).filter(_.endsWith(".tasty")) |
| 100 | + .map(_.stripSuffix(".tasty").replace("/", ".")) |
| 101 | + .sorted |
| 102 | + } |
| 103 | + |
| 104 | + def loadWithTastyInspector(blacklisted: String => Boolean): Unit = |
| 105 | + val inspector = new scala.tasty.inspector.TastyInspector { |
| 106 | + def processCompilationUnit(using QuoteContext)(root: qctx.reflect.Tree): Unit = |
| 107 | + root.showExtractors // Check that we can traverse the full tree |
| 108 | + () |
| 109 | + } |
| 110 | + val classNames = scalaLibJarTastyClassNames.filterNot(blacklisted) |
| 111 | + val hasErrors = inspector.inspect(scalaLibJarPath, classNames) |
| 112 | + assert(!hasErrors, "Errors reported while loading from TASTy") |
| 113 | + |
| 114 | + def compileFromTasty(blacklisted: String => Boolean): Unit = { |
| 115 | + val driver = new dotty.tools.dotc.Driver |
| 116 | + val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader) |
| 117 | + val classNames = scalaLibJarTastyClassNames.filterNot(blacklisted) |
| 118 | + val args = Array( |
| 119 | + "-classpath", s"$scalaLibJarPath$pathSeparator$currentClasspath", |
| 120 | + "-from-tasty", |
| 121 | + "-nowarn" |
| 122 | + ) ++ classNames |
| 123 | + val reporter = driver.process(args) |
| 124 | + assert(reporter.errorCount == 0, "Errors while re-compiling") |
| 125 | + } |
| 126 | + |
| 127 | + /** List of classes that cannot be loaded from TASTy */ |
| 128 | + def loadBlacklist = List[String]( |
| 129 | + // No issues :) |
| 130 | + ) |
| 131 | + |
| 132 | + /** List of classes that cannot be recompilied from TASTy */ |
| 133 | + def compileBlacklist = List[String]( |
| 134 | + // See #10048 |
| 135 | + // failed: java.lang.AssertionError: assertion failed: class Boolean |
| 136 | + // at dotty.DottyPredef$.assertFail(DottyPredef.scala:17) |
| 137 | + // at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.assertClassNotArrayNotPrimitive(BCodeHelpers.scala:247) |
| 138 | + // at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.getClassBTypeAndRegisterInnerClass(BCodeHelpers.scala:265) |
| 139 | + // at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.getClassBTypeAndRegisterInnerClass$(BCodeHelpers.scala:210) |
| 140 | + // at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.getClassBTypeAndRegisterInnerClass(BCodeSkelBuilder.scala:62) |
| 141 | + // at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.internalName(BCodeHelpers.scala:237) |
| 142 | + "scala.Array", |
| 143 | + "scala.Boolean", |
| 144 | + "scala.Byte", |
| 145 | + "scala.Char", |
| 146 | + "scala.Double", |
| 147 | + "scala.Float", |
| 148 | + "scala.Int", |
| 149 | + "scala.Long", |
| 150 | + "scala.Short", |
| 151 | + "scala.Unit", |
| 152 | + |
| 153 | + // See #9994 |
| 154 | + // -- Error: |
| 155 | + // | def addOne(kv: (K, V)) = { |
| 156 | + // | ^ |
| 157 | + // |error overriding method addOne in trait Growable of type (elem: (K, V)): (TrieMap.this : scala.collection.concurrent.TrieMap[K, V]); |
| 158 | + // | method addOne of type (kv: (K, V)): (TrieMap.this : scala.collection.concurrent.TrieMap[K, V]) has incompatible type |
| 159 | + // -- Error: |
| 160 | + // | def subtractOne(k: K) = { |
| 161 | + // | ^ |
| 162 | + // |error overriding method subtractOne in trait Shrinkable of type (elem: K): (TrieMap.this : scala.collection.concurrent.TrieMap[K, V]); |
| 163 | + // | method subtractOne of type (k: K): (TrieMap.this : scala.collection.concurrent.TrieMap[K, V]) has incompatible type |
| 164 | + "scala.collection.concurrent.TrieMap", |
| 165 | + "scala.collection.immutable.HashMapBuilder", |
| 166 | + "scala.collection.immutable.HashSetBuilder", |
| 167 | + "scala.collection.immutable.LazyList", |
| 168 | + "scala.collection.immutable.ListMapBuilder", |
| 169 | + "scala.collection.immutable.MapBuilderImpl", |
| 170 | + "scala.collection.immutable.SetBuilderImpl", |
| 171 | + "scala.collection.immutable.TreeSeqMap", |
| 172 | + "scala.collection.immutable.VectorBuilder", |
| 173 | + "scala.collection.immutable.VectorMapBuilder", |
| 174 | + "scala.collection.mutable.AnyRefMap", |
| 175 | + "scala.collection.mutable.ArrayBuilder", |
| 176 | + "scala.collection.mutable.CollisionProofHashMap", |
| 177 | + "scala.collection.mutable.LongMap", |
| 178 | + "scala.collection.mutable.SortedMap", |
| 179 | + "scala.collection.mutable.StringBuilder", |
| 180 | + "scala.jdk.AnyAccumulator", |
| 181 | + "scala.jdk.DoubleAccumulator", |
| 182 | + "scala.jdk.IntAccumulator", |
| 183 | + "scala.jdk.LongAccumulator", |
| 184 | + |
| 185 | + // See #9994 |
| 186 | + // -- Error: |
| 187 | + // | override def filterInPlace(p: A => Boolean): this.type = { |
| 188 | + // | ^ |
| 189 | + // |error overriding method filterInPlace in trait SetOps of type (p: A => Boolean): (HashSet.this : scala.collection.mutable.HashSet[A]); |
| 190 | + // | method filterInPlace of type (p: A => Boolean): (HashSet.this : scala.collection.mutable.HashSet[A]) has incompatible type |
| 191 | + "scala.collection.mutable.HashSet", |
| 192 | + |
| 193 | + // See #9994 |
| 194 | + // -- Error: |
| 195 | + // | def force: this.type = { |
| 196 | + // | ^ |
| 197 | + // |error overriding method force in class Stream of type => (Cons.this : scala.collection.immutable.Stream.Cons[A]); |
| 198 | + // | method force of type => (Cons.this : scala.collection.immutable.Stream.Cons[A]) has incompatible type |
| 199 | + "scala.collection.immutable.Stream", |
| 200 | + |
| 201 | + ) |
| 202 | + |
| 203 | + /** Set of classes that cannot be loaded from TASTy */ |
| 204 | + def loadBlacklisted = loadBlacklist.toSet |
| 205 | + |
| 206 | + /** Set of classes that cannot be recompilied from TASTy */ |
| 207 | + def compileBlacklisted = compileBlacklist.toSet |
| 208 | + |
| 209 | +end BootstrappedStdLibTASYyTest |
0 commit comments