Skip to content

Commit 2e4e3c0

Browse files
committed
Only allow jar and tasty as input for -from-tasty
This ensures that we don't accidentally load other versions of the classes that are on the current classpath.
1 parent ca67e4d commit 2e4e3c0

File tree

4 files changed

+17
-77
lines changed

4 files changed

+17
-77
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,28 @@ 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 fromTastyFilter = ctx0.settings.YfromTastyFilter.value
90+
val filter =
91+
if fromTastyFilter.isEmpty then ".*".r
92+
else fromTastyFilter.r
8993
// Resolve classpath and class names of tasty files
90-
val (classPaths, classNames) = fileNames0.flatMap { name =>
91-
val path = Paths.get(name)
92-
if (name.endsWith(".jar"))
93-
new dotty.tools.io.Jar(File(name)).toList.collect {
94-
case e if e.getName.endsWith(".tasty") =>
95-
(name, e.getName.stripSuffix(".tasty").replace("/", "."))
94+
val (classPaths, classNames) = fileNames0.flatMap { fileName =>
95+
val path = Paths.get(fileName)
96+
if (fileName.endsWith(".jar"))
97+
new dotty.tools.io.Jar(File(fileName)).toList.collect {
98+
case e if e.getName.endsWith(".tasty") && filter.matches(e.getName) =>
99+
(fileName, e.getName.stripSuffix(".tasty").replace("/", "."))
96100
}
97-
else if (!name.endsWith(".tasty"))
98-
("", name) :: Nil
99-
else if (Files.exists(path))
101+
else if Files.exists(path) then
100102
TastyFileUtil.getClassName(path) match {
101103
case Some(res) => res:: Nil
102104
case _ =>
103-
report.error(s"Could not load classname from $name.")
104-
("", name) :: Nil
105+
report.error(s"Could not load classname from: $fileName")
106+
Nil
105107
}
106108
else {
107-
report.error(s"File $name does not exist.")
108-
("", name) :: Nil
109+
report.error(s"File not found: $fileName")
110+
Nil
109111
}
110112
}.unzip
111113
val ctx1 = ctx0.fresh

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 YfromTastyFilter: Setting[String] = StringSetting("-Yfrom-tasty-filter", "file", "Filter tasty files in jar files 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.", "")

tests/run-custom-args/tasty-inspector/i8215.scala

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/run-custom-args/tasty-inspector/i8558.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)