Skip to content

Commit 0d6a5ef

Browse files
committed
Sourcefile refactoring
Don't check whether a sourcefile exists when creating it - it might lead to spurious errors. Do it instead at the point where we create a CompilationUnit from a source file.
1 parent 1e3d387 commit 0d6a5ef

File tree

9 files changed

+36
-33
lines changed

9 files changed

+36
-33
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import dotty.tools.dotc.core.Contexts.Context
99
import dotty.tools.dotc.core.SymDenotations.ClassDenotation
1010
import dotty.tools.dotc.core.Symbols._
1111
import dotty.tools.dotc.transform.SymUtils._
12+
import util.{NoSource, SourceFile}
1213

13-
class CompilationUnit(val source: SourceFile) {
14+
class CompilationUnit protected (val source: SourceFile) {
1415

1516
override def toString: String = source.toString
1617

@@ -35,11 +36,11 @@ class CompilationUnit(val source: SourceFile) {
3536
object CompilationUnit {
3637

3738
/** Make a compilation unit for top class `clsd` with the contents of the `unpickled` tree */
38-
def mkCompilationUnit(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit =
39-
mkCompilationUnit(new SourceFile(clsd.symbol.associatedFile, Array.empty[Char]), unpickled, forceTrees)
39+
def apply(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit =
40+
apply(new SourceFile(clsd.symbol.associatedFile, Array.empty[Char]), unpickled, forceTrees)
4041

4142
/** Make a compilation unit, given picked bytes and unpickled tree */
42-
def mkCompilationUnit(source: SourceFile, unpickled: Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit = {
43+
def apply(source: SourceFile, unpickled: Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit = {
4344
assert(!unpickled.isEmpty, unpickled)
4445
val unit1 = new CompilationUnit(source)
4546
unit1.tpdTree = unpickled
@@ -51,6 +52,20 @@ object CompilationUnit {
5152
unit1
5253
}
5354

55+
def apply(source: SourceFile)(implicit ctx: Context): CompilationUnit = {
56+
val src =
57+
if (source.file.isDirectory) {
58+
ctx.error(s"expected file, received directory '${source.file.path}'")
59+
NoSource
60+
}
61+
else if (!source.file.exists) {
62+
ctx.error(s"not found: ${source.file.path}")
63+
NoSource
64+
}
65+
else source
66+
new CompilationUnit(source)
67+
}
68+
5469
/** Force the tree to be loaded */
5570
private class Force extends TreeTraverser {
5671
var needsStaging = false

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
100100
private[this] var finalizeActions = mutable.ListBuffer[() => Unit]()
101101

102102
def compile(fileNames: List[String]): Unit = try {
103-
val sources = fileNames.map(ctx.getSource)
103+
val sources = fileNames.map(ctx.getSource(_))
104104
compileSources(sources)
105105
} catch {
106106
case NonFatal(ex) =>
@@ -116,7 +116,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
116116
*/
117117
def compileSources(sources: List[SourceFile]): Unit =
118118
if (sources forall (_.exists)) {
119-
units = sources map (new CompilationUnit(_))
119+
units = sources.map(CompilationUnit(_))
120120
compileUnits()
121121
}
122122

@@ -192,7 +192,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
192192
def lateCompile(file: AbstractFile, typeCheck: Boolean)(implicit ctx: Context): Unit =
193193
if (!files.contains(file) && !lateFiles.contains(file)) {
194194
lateFiles += file
195-
val unit = new CompilationUnit(ctx.getSource(file.path))
195+
val unit = CompilationUnit(ctx.getSource(file.path))
196196
def process()(implicit ctx: Context) = {
197197
unit.untpdTree =
198198
if (unit.isJava) new JavaParser(unit.source).parse()

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,9 @@ object Contexts {
244244
source
245245
case None =>
246246
val f = new PlainFile(Path(path.toString))
247-
if (f.isDirectory) {
248-
error(s"expected file, received directory '$path'")
249-
NoSource
250-
}
251-
else if (f.exists) {
252-
val src = getSource(f)
253-
base.sourceNamed(path) = src
254-
src
255-
}
256-
else {
257-
error(s"not found: $path")
258-
NoSource
259-
}
247+
val src = getSource(f)
248+
base.sourceNamed(path) = src
249+
src
260250
}
261251

262252
/** Sourcefile with given path, memoized */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ object SymbolLoaders {
159159
Nil)
160160
}
161161

162-
val unit = new CompilationUnit(ctx.getSource(src.path))
162+
val unit = CompilationUnit(ctx.getSource(src.path))
163163
enterScanned(unit)(ctx.run.runContext.fresh.setCompilationUnit(unit))
164164
}
165165
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,7 @@ object Symbols {
775775
def sourceFromTopLevel(implicit ctx: Context) =
776776
denot.topLevelClass.unforcedAnnotation(defn.SourceFileAnnot) match {
777777
case Some(sourceAnnot) => sourceAnnot.argumentConstant(0) match {
778-
case Some(Constant(path: String)) =>
779-
ctx.getSource(path)
778+
case Some(Constant(path: String)) => ctx.getSource(path)
780779
case none => NoSource
781780
}
782781
case none => NoSource

compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Symbols.{Symbol, ClassSymbol}
99
import SymDenotations.ClassDenotation
1010
import NameOps._
1111
import ast.Trees.Tree
12-
import CompilationUnit.mkCompilationUnit
1312
import Phases.Phase
1413

1514

@@ -44,7 +43,7 @@ class ReadTasty extends Phase {
4443
case unpickler: tasty.DottyUnpickler =>
4544
if (cls.rootTree.isEmpty) None
4645
else {
47-
val unit = mkCompilationUnit(cls, cls.rootTree, forceTrees = true)
46+
val unit = CompilationUnit(cls, cls.rootTree, forceTrees = true)
4847
unit.pickled += (cls -> unpickler.unpickler.bytes)
4948
Some(unit)
5049
}

compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ class QuoteCompiler extends Compiler {
5252
if (putInClass) inClass(exprUnit.expr)
5353
else PickledQuotes.quotedExprToTree(exprUnit.expr)
5454
val source = SourceFile.virtual("<quoted.Expr>", "")
55-
CompilationUnit.mkCompilationUnit(source, tree, forceTrees = true)
55+
CompilationUnit(source, tree, forceTrees = true)
5656
case typeUnit: TypeCompilationUnit =>
5757
assert(!putInClass)
5858
val tree = PickledQuotes.quotedTypeToTree(typeUnit.tpe)
5959
val source = SourceFile.virtual("<quoted.Type>", "")
60-
CompilationUnit.mkCompilationUnit(source, tree, forceTrees = true)
60+
CompilationUnit(source, tree, forceTrees = true)
6161
}
6262
}
6363

compiler/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ class ReplCompiler extends Compiler {
140140
PackageDef(Ident(nme.EMPTY_PACKAGE), List(module))
141141
}
142142

143-
private def createUnit(defs: Definitions, sourceCode: String): CompilationUnit = {
144-
val unit = new CompilationUnit(SourceFile.virtual(objectName(defs.state).toString, sourceCode))
143+
private def createUnit(defs: Definitions, sourceCode: String)(implicit ctx: Context): CompilationUnit = {
144+
val unit = CompilationUnit(SourceFile.virtual(objectName(defs.state).toString, sourceCode))
145145
unit.untpdTree = wrapped(defs)
146146
unit
147147
}
@@ -156,7 +156,7 @@ class ReplCompiler extends Compiler {
156156

157157
final def compile(parsed: Parsed)(implicit state: State): Result[(CompilationUnit, State)] = {
158158
val defs = definitions(parsed.trees, state)
159-
val unit = createUnit(defs, parsed.sourceCode)
159+
val unit = createUnit(defs, parsed.sourceCode)(state.context)
160160
runCompilationUnit(unit, defs.state)
161161
}
162162

@@ -273,7 +273,7 @@ class ReplCompiler extends Compiler {
273273
.setSetting(state.context.settings.YstopAfter, List("frontend"))
274274

275275
wrapped(expr, src, state).flatMap { pkg =>
276-
val unit = new CompilationUnit(src)
276+
val unit = CompilationUnit(src)
277277
unit.untpdTree = pkg
278278
ctx.run.compileUnits(unit :: Nil, ctx)
279279

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class ReplDriver(settings: Array[String],
149149

150150
/** Extract possible completions at the index of `cursor` in `expr` */
151151
protected[this] final def completions(cursor: Int, expr: String, state0: State): List[Candidate] = {
152-
def makeCandidate(completion: Completion)(implicit ctx: Context) = {
152+
def makeCandidate(completion: Completion) = {
153153
val displ = completion.label
154154
new Candidate(
155155
/* value = */ displ,
@@ -166,7 +166,7 @@ class ReplDriver(settings: Array[String],
166166
.typeCheck(expr, errorsAllowed = true)
167167
.map { tree =>
168168
val file = SourceFile.virtual("<completions>", expr)
169-
val unit = new CompilationUnit(file)
169+
val unit = CompilationUnit(file)(state.context)
170170
unit.tpdTree = tree
171171
implicit val ctx = state.context.fresh.setCompilationUnit(unit)
172172
val srcPos = SourcePosition(file, Span(cursor))

0 commit comments

Comments
 (0)