Skip to content

Commit 11925ef

Browse files
committed
Only allow jar and tasty as input for -from-tasty
1 parent 06d988a commit 11925ef

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,27 @@ class Driver {
8686
protected def fromTastySetup(fileNames0: List[String], ctx0: Context): (List[String], Context) =
8787
given Context = ctx0
8888
if (ctx0.settings.fromTasty.value) {
89+
val fromTastyBlacklist = ctx0.settings.YfromTastyBlacklist.value.toSet
8990
// Resolve classpath and class names of tasty files
9091
val (classPaths, classNames) = fileNames0.flatMap { name =>
9192
val path = Paths.get(name)
92-
if !name.endsWith(".jar") && !name.endsWith(".tasty") then // is class name
93-
("", name) :: Nil // TODO remove this case. We cannot rely on an expected tasty file beeing loaded.
94-
else if !Files.exists(path) then
93+
if !Files.exists(path) then
9594
report.error(s"File does not exist: $name")
9695
Nil
9796
else if name.endsWith(".jar") then
9897
new dotty.tools.io.Jar(File(name)).toList.collect {
99-
case e if e.getName.endsWith(".tasty") =>
98+
case e if e.getName.endsWith(".tasty") && !fromTastyBlacklist(e.getName) =>
10099
(name, e.getName.stripSuffix(".tasty").replace("/", "."))
101100
}
102-
else
103-
assert(name.endsWith(".tasty"))
101+
else if name.endsWith(".tasty") then
104102
TastyFileUtil.getClassName(path) match
105103
case Some(res) => res :: Nil
106104
case _ =>
107105
report.error(s"Could not load classname from: $name")
108106
Nil
107+
else
108+
report.error(s"File extension is not `tasty` or `jar`: $name")
109+
Nil
109110
}.unzip
110111
val ctx1 = ctx0.fresh
111112
val classPaths1 = classPaths.distinct.filter(_ != "")

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ScalaSettings extends Settings.SettingGroup {
4545
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.") withAbbreviation "--language"
4646
val rewrite: Setting[Option[Rewrites]] = OptionSetting[Rewrites]("-rewrite", "When used in conjunction with a `...-migration` source version, rewrites sources to migrate to new version.") withAbbreviation "--rewrite"
4747
val silentWarnings: Setting[Boolean] = BooleanSetting("-nowarn", "Silence all warnings.") withAbbreviation "--no-warnings"
48-
val fromTasty: Setting[Boolean] = BooleanSetting("-from-tasty", "Compile classes from tasty in classpath. The arguments are used as class names.") withAbbreviation "--from-tasty"
48+
val fromTasty: Setting[Boolean] = BooleanSetting("-from-tasty", "Compile classes from tasty files. The arguments are .tasty or .jar files.") withAbbreviation "--from-tasty"
4949

5050
val newSyntax: Setting[Boolean] = BooleanSetting("-new-syntax", "Require `then` and `do` in control expressions.")
5151
val oldSyntax: Setting[Boolean] = BooleanSetting("-old-syntax", "Require `(...)` around conditions.")
@@ -158,6 +158,7 @@ class ScalaSettings extends Settings.SettingGroup {
158158
val YretainTrees: Setting[Boolean] = BooleanSetting("-Yretain-trees", "Retain trees for top-level classes, accessible from ClassSymbol#tree")
159159
val Ysemanticdb: Setting[Boolean] = BooleanSetting("-Ysemanticdb", "Store information in SemanticDB.")
160160
val YshowTreeIds: Setting[Boolean] = BooleanSetting("-Yshow-tree-ids", "Uniquely tag all tree nodes in debugging output.")
161+
val YfromTastyBlacklist: Setting[List[String]] = MultiStringSetting("-Yfrom-tasty-blacklist", "file", "List of `tasty` files in jar files that will not be loaded when using -from-tasty")
161162

162163
val YprofileEnabled: Setting[Boolean] = BooleanSetting("-Yprofile-enabled", "Enable profiling.")
163164
val YprofileDestination: Setting[String] = StringSetting("-Yprofile-destination", "file", "Where to send profiling output - specify a file, default is to the console.", "")

project/scripts/cmdTests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ grep -qe "$EXPECTED_OUTPUT" "$tmp"
1010
# check that `sbt scalac` compiles and `sbt scala` runs it
1111
echo "testing sbt scalac -from-tasty and scala -classpath"
1212
clear_out "$OUT"
13-
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -from-tasty -classpath $OUT -d $OUT1 $MAIN ;scala -classpath $OUT1 $MAIN" > "$tmp"
13+
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -from-tasty -d $OUT1 $OUT1/$TASTY ;scala -classpath $OUT1 $MAIN" > "$tmp"
1414
grep -qe "$EXPECTED_OUTPUT" "$tmp"
1515

1616
echo "testing sbt scalac -from-tasty from a jar and scala -classpath"

stdlib-bootstrapped-tasty-tests/test/BootstrappedStdLibTASYyTest.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dotty.tools.dotc.util.ClasspathFromClassloader
99

1010
import scala.quoted._
1111

12-
import java.io.File.pathSeparator
12+
import java.io.File.{separator, pathSeparator}
1313

1414
class BootstrappedStdLibTASYyTest:
1515

@@ -28,7 +28,7 @@ class BootstrappedStdLibTASYyTest:
2828
val whitelist = Set(
2929
"scala.collection.mutable.StringBuilder"
3030
)
31-
compileFromTasty(x => !whitelist(x))
31+
compileFromTasty(whitelist)
3232

3333
@Test def blacklistNoDuplicates =
3434
def testDup(name: String, list: List[String], set: Set[String]) =
@@ -111,15 +111,15 @@ object BootstrappedStdLibTASYyTest:
111111
val hasErrors = inspector.inspectTastyFilesInJar(scalaLibJarPath)
112112
assert(!hasErrors, "Errors reported while loading from TASTy")
113113

114-
def compileFromTasty(blacklisted: String => Boolean): Unit = {
114+
def compileFromTasty(blacklisted: Iterable[String]): Unit = {
115115
val driver = new dotty.tools.dotc.Driver
116-
val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader)
117-
val classNames = scalaLibJarTastyClassNames.filterNot(blacklisted)
118116
val args = Array(
119-
"-classpath", s"$scalaLibJarPath$pathSeparator$currentClasspath",
117+
"-classpath", ClasspathFromClassloader(getClass.getClassLoader),
120118
"-from-tasty",
121-
"-nowarn"
122-
) ++ classNames
119+
"-nowarn",
120+
blacklisted.map(x => x.replace(".", separator) + ".tasty").mkString("-Yfrom-tasty-blacklist:", ",", ""),
121+
scalaLibJarPath,
122+
)
123123
val reporter = driver.process(args)
124124
assert(reporter.errorCount == 0, "Errors while re-compiling")
125125
}

0 commit comments

Comments
 (0)