Skip to content

Commit 1b11e4b

Browse files
committed
Change -sourcepath to behave like -scansource -sourcepath
... and delete -scansource which is now unnecessary. The difference between these two settings is that the former assumes that a file A.scala contains a top-level class or object A, whereas the latter will use the outline parser to find definitions in A.scala and create symbols for them. So far we used the former to compile the standard library and dotty-library, and the latter for the IDE, but we might as well always use the latter, it's one less code path and dotty-library now uses top-level definitions (like `type IArray` in scala/IArray.scala) so the assumptions of -sourcepath do not apply to it anymore. (top-level definitions are still not handled correctly with -sourcepath after this commit, this is fixed in a latter commit in this PR) This change required moving WithBounds which was previously in the wrong directory.
1 parent c900e88 commit 1b11e4b

File tree

5 files changed

+47
-58
lines changed

5 files changed

+47
-58
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class ScalaSettings extends Settings.SettingGroup {
1717
val javabootclasspath: Setting[String] = PathSetting("-javabootclasspath", "Override java boot classpath.", Defaults.javaBootClassPath) withAbbreviation "--java-boot-class-path"
1818
val javaextdirs: Setting[String] = PathSetting("-javaextdirs", "Override java extdirs classpath.", Defaults.javaExtDirs) withAbbreviation "--java-extension-directories"
1919
val sourcepath: Setting[String] = PathSetting("-sourcepath", "Specify location(s) of source files.", Defaults.scalaSourcePath) withAbbreviation "--source-path"
20-
val scansource: Setting[Boolean] = BooleanSetting("-scansource", "Scan source files to locate classes for which class-name != file-name") withAbbreviation "--scan-source"
2120

2221
val classpath: Setting[String] = PathSetting("-classpath", "Specify where to find user class files.", defaultClasspath) withAbbreviation "-cp" withAbbreviation "--class-path"
2322
val outputDir: Setting[AbstractFile] = OutputSetting("-d", "directory|jar", "destination for generated classfiles.",

compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object SymbolLoaders {
3131
owner: Symbol, member: Symbol,
3232
completer: SymbolLoader, scope: Scope = EmptyScope)(implicit ctx: Context): Symbol = {
3333
val comesFromScan =
34-
completer.isInstanceOf[SourcefileLoader] && ctx.settings.scansource.value
34+
completer.isInstanceOf[SourcefileLoader]
3535
assert(comesFromScan || scope.lookup(member.name) == NoSymbol,
3636
s"${owner.fullName}.${member.name} already has a symbol")
3737
owner.asClass.enter(member, scope)
@@ -103,69 +103,60 @@ object SymbolLoaders {
103103
scope = scope)
104104
}
105105

106-
/** If setting -scansource is set:
107-
* Enter all toplevel classes and objects in file `src` into package `owner`, provided
108-
* they are in the right package. Issue a warning if a class or object is in the wrong
109-
* package, i.e. if the file path differs from the declared package clause.
110-
* If -scansource is not set:
111-
* Enter class and module with given `name` into scope of `owner`.
106+
/** Enter all toplevel classes and objects in file `src` into package `owner`, provided
107+
* they are in the right package. Issue a warning if a class or object is in the wrong
108+
* package, i.e. if the file path differs from the declared package clause.
112109
*
113110
* All entered symbols are given a source completer of `src` as info.
114111
*/
115112
def enterToplevelsFromSource(
116113
owner: Symbol, name: PreName, src: AbstractFile,
117-
scope: Scope = EmptyScope)(implicit ctx: Context): Unit = {
114+
scope: Scope = EmptyScope)(implicit ctx: Context): Unit =
115+
if src.exists && !src.isDirectory
116+
val completer = new SourcefileLoader(src)
117+
val filePath = owner.ownersIterator.takeWhile(!_.isRoot).map(_.name.toTermName).toList
118+
119+
def addPrefix(pid: RefTree, path: List[TermName]): List[TermName] = pid match {
120+
case Ident(name: TermName) => name :: path
121+
case Select(qual: RefTree, name: TermName) => name :: addPrefix(qual, path)
122+
case _ => path
123+
}
118124

119-
val completer = new SourcefileLoader(src)
120-
if (ctx.settings.scansource.value && ctx.run != null) {
121-
if (src.exists && !src.isDirectory) {
122-
val filePath = owner.ownersIterator.takeWhile(!_.isRoot).map(_.name.toTermName).toList
125+
def enterScanned(unit: CompilationUnit)(implicit ctx: Context) = {
123126

124-
def addPrefix(pid: RefTree, path: List[TermName]): List[TermName] = pid match {
125-
case Ident(name: TermName) => name :: path
126-
case Select(qual: RefTree, name: TermName) => name :: addPrefix(qual, path)
127-
case _ => path
127+
def checkPathMatches(path: List[TermName], what: String, tree: MemberDef): Boolean = {
128+
val ok = filePath == path
129+
if (!ok)
130+
ctx.warning(i"""$what ${tree.name} is in the wrong directory.
131+
|It was declared to be in package ${path.reverse.mkString(".")}
132+
|But it is found in directory ${filePath.reverse.mkString(File.separator)}""",
133+
tree.sourcePos)
134+
ok
128135
}
129136

130-
def enterScanned(unit: CompilationUnit)(implicit ctx: Context) = {
131-
132-
def checkPathMatches(path: List[TermName], what: String, tree: MemberDef): Boolean = {
133-
val ok = filePath == path
134-
if (!ok)
135-
ctx.warning(i"""$what ${tree.name} is in the wrong directory.
136-
|It was declared to be in package ${path.reverse.mkString(".")}
137-
|But it is found in directory ${filePath.reverse.mkString(File.separator)}""",
138-
tree.sourcePos)
139-
ok
140-
}
141-
142-
def traverse(tree: Tree, path: List[TermName]): Unit = tree match {
143-
case PackageDef(pid, body) =>
144-
val path1 = addPrefix(pid, path)
145-
for (stat <- body) traverse(stat, path1)
146-
case tree: TypeDef if tree.isClassDef =>
147-
if (checkPathMatches(path, "class", tree))
148-
enterClassAndModule(owner, tree.name, completer, scope = scope)
149-
// It might be a case class or implicit class,
150-
// so enter class and module to be on the safe side
151-
case tree: ModuleDef =>
152-
if (checkPathMatches(path, "object", tree))
153-
enterModule(owner, tree.name, completer, scope = scope)
154-
case _ =>
155-
}
156-
157-
traverse(
158-
if (unit.isJava) new OutlineJavaParser(unit.source).parse()
159-
else new OutlineParser(unit.source).parse(),
160-
Nil)
137+
def traverse(tree: Tree, path: List[TermName]): Unit = tree match {
138+
case PackageDef(pid, body) =>
139+
val path1 = addPrefix(pid, path)
140+
for (stat <- body) traverse(stat, path1)
141+
case tree: TypeDef if tree.isClassDef =>
142+
if (checkPathMatches(path, "class", tree))
143+
// It might be a case class or implicit class,
144+
// so enter class and module to be on the safe side
145+
enterClassAndModule(owner, tree.name, completer, scope = scope)
146+
case tree: ModuleDef =>
147+
if (checkPathMatches(path, "object", tree))
148+
enterModule(owner, tree.name, completer, scope = scope)
149+
case _ =>
161150
}
162151

163-
val unit = CompilationUnit(ctx.getSource(src.path))
164-
enterScanned(unit)(ctx.run.runContext.fresh.setCompilationUnit(unit))
152+
traverse(
153+
if (unit.isJava) new OutlineJavaParser(unit.source).parse()
154+
else new OutlineParser(unit.source).parse(),
155+
Nil)
165156
}
166-
}
167-
else enterClassAndModule(owner, name, completer, scope = scope)
168-
}
157+
158+
val unit = CompilationUnit(ctx.getSource(src.path))
159+
enterScanned(unit)(ctx.run.runContext.fresh.setCompilationUnit(unit))
169160

170161
/** The package objects of scala and scala.reflect should always
171162
* be loaded in binary if classfiles are available, even if sourcefiles

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class CompilationTests extends ParallelTesting {
4141
compileFile("tests/pos-special/utf16encoded.scala", explicitUTF16),
4242
compileFile("tests/pos-special/completeFromSource/Test.scala", defaultOptions.and("-sourcepath", "tests/pos-special")),
4343
compileFile("tests/pos-special/completeFromSource/Test2.scala", defaultOptions.and("-sourcepath", "tests/pos-special")),
44-
compileFile("tests/pos-special/completeFromSource/Test3.scala", defaultOptions.and("-sourcepath", "tests/pos-special", "-scansource")),
45-
compileFile("tests/pos-special/completeFromSource/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special", "-scansource")),
44+
compileFile("tests/pos-special/completeFromSource/Test3.scala", defaultOptions.and("-sourcepath", "tests/pos-special")),
45+
compileFile("tests/pos-special/completeFromSource/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special")),
4646
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-feature")),
4747
compileFilesInDir("tests/pos-special/spec-t5545", defaultOptions),
4848
compileFilesInDir("tests/pos-special/strawman-collections", defaultOptions),
@@ -135,7 +135,7 @@ class CompilationTests extends ParallelTesting {
135135
compileFilesInDir("tests/neg-custom-args/isInstanceOf", allowDeepSubtypes and "-Xfatal-warnings"),
136136
compileFile("tests/neg-custom-args/i3627.scala", allowDeepSubtypes),
137137
compileFile("tests/neg-custom-args/matchtype-loop.scala", allowDeepSubtypes),
138-
compileFile("tests/neg-custom-args/completeFromSource/nested/Test1.scala", defaultOptions.and("-sourcepath", "tests/neg-custom-args", "-scansource")),
138+
compileFile("tests/neg-custom-args/completeFromSource/nested/Test1.scala", defaultOptions.and("-sourcepath", "tests/neg-custom-args")),
139139
compileList("duplicate source", List(
140140
"tests/neg-custom-args/toplevel-samesource/S.scala",
141141
"tests/neg-custom-args/toplevel-samesource/nested/S.scala"),

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ class DottyLanguageServer extends LanguageServer
8787
config.compilerArguments.toList
8888
.update("-d", config.classDirectory.getAbsolutePath)
8989
.update("-classpath", (config.classDirectory +: config.dependencyClasspath).mkString(File.pathSeparator))
90-
.update("-sourcepath", config.sourceDirectories.mkString(File.pathSeparator)) :+
91-
"-scansource"
90+
.update("-sourcepath", config.sourceDirectories.mkString(File.pathSeparator))
9291
myDrivers(config) = new InteractiveDriver(settings)
9392
}
9493
myDrivers

0 commit comments

Comments
 (0)