Skip to content

Commit 1e3d387

Browse files
committed
Make virtual source files explicit
1 parent 890fba1 commit 1e3d387

File tree

11 files changed

+14
-13
lines changed

11 files changed

+14
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ object Comments {
119119
object UseCase {
120120
def apply(code: String, codePos: Span)(implicit ctx: Context): UseCase = {
121121
val tree = {
122-
val tree = new Parser(new SourceFile("<usecase>", code)).localDef(codePos.start)
122+
val tree = new Parser(SourceFile.virtual("<usecase>", code)).localDef(codePos.start)
123123
tree match {
124124
case tree: untpd.DefDef =>
125125
val newName = ctx.freshNames.newName(tree.name, NameKinds.DocArtifactName)

compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object SyntaxHighlighting {
3333
if (in.isEmpty || ctx.settings.color.value == "never") in
3434
else {
3535
implicit val ctx = freshCtx
36-
val source = new SourceFile("<highlighting>", in)
36+
val source = SourceFile.virtual("<highlighting>", in)
3737
val colorAt = Array.fill(in.length)(NoColor)
3838

3939
def highlightRange(from: Int, to: Int, color: String) =

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

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

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
5050
}
5151

5252
def this(file: AbstractFile, codec: Codec) = this(file, new String(file.toByteArray, codec.charSet).toCharArray)
53-
def this(name: String, content: String) = this(new VirtualFile(name, content.getBytes), scala.io.Codec.UTF8)
5453

5554
/** Tab increment; can be overridden */
5655
def tabInc: Int = 8
@@ -195,6 +194,8 @@ object SourceFile {
195194

196195
def fromId(id: Int): SourceFile = sourceOfChunk(id >> ChunkSizeLog)
197196

197+
def virtual(name: String, content: String) = new SourceFile(new VirtualFile(name, content.getBytes), scala.io.Codec.UTF8)
198+
198199
private final val ChunkSizeLog = 10
199200
private final val ChunkSize = 1 << ChunkSizeLog
200201
@sharable private var chunks: Int = 0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ final class JLineTerminal extends java.io.Closeable {
110110

111111
case class TokenData(token: Token, start: Int, end: Int)
112112
def currentToken: TokenData /* | Null */ = {
113-
val source = new SourceFile("<completions>", input)
113+
val source = SourceFile.virtual("<completions>", input)
114114
val scanner = new Scanner(source)(ctx.fresh.setReporter(Reporter.NoReporter))
115115
while (scanner.token != EOF) {
116116
val start = scanner.offset

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ object ParseResult {
109109
@sharable private[this] val CommandExtract = """(:[\S]+)\s*(.*)""".r
110110

111111
private def parseStats(sourceCode: String)(implicit ctx: Context): List[untpd.Tree] = {
112-
val source = new SourceFile("<console>", sourceCode)
112+
val source = SourceFile.virtual("<console>", sourceCode)
113113
val parser = new Parser(source)(ctx.withSource(source))
114114
val stats = parser.blockStatSeq()
115115
parser.accept(Tokens.EOF)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class ReplCompiler extends Compiler {
141141
}
142142

143143
private def createUnit(defs: Definitions, sourceCode: String): CompilationUnit = {
144-
val unit = new CompilationUnit(new SourceFile(objectName(defs.state).toString, sourceCode))
144+
val unit = new CompilationUnit(SourceFile.virtual(objectName(defs.state).toString, sourceCode))
145145
unit.untpdTree = wrapped(defs)
146146
unit
147147
}
@@ -267,7 +267,7 @@ class ReplCompiler extends Compiler {
267267
}
268268

269269

270-
val src = new SourceFile("<typecheck>", expr)
270+
val src = SourceFile.virtual("<typecheck>", expr)
271271
implicit val ctx: Context = state.context.fresh
272272
.setReporter(newStoreReporter)
273273
.setSetting(state.context.settings.YstopAfter, List("frontend"))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class ReplDriver(settings: Array[String],
165165
compiler
166166
.typeCheck(expr, errorsAllowed = true)
167167
.map { tree =>
168-
val file = new SourceFile("<completions>", expr)
168+
val file = SourceFile.virtual("<completions>", expr)
169169
val unit = new CompilationUnit(file)
170170
unit.tpdTree = tree
171171
implicit val ctx = state.context.fresh.setCompilationUnit(unit)

compiler/test/dotty/tools/dotc/ast/UntypedTreeMapTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UntpdTreeMapTest extends DottyTest {
1414
import untpd._
1515

1616
def parse(code: String): Tree = {
17-
val (_, stats) = new Parser(new SourceFile("<meta>", code)).templateStatSeq()
17+
val (_, stats) = new Parser(SourceFile.virtual("<meta>", code)).templateStatSeq()
1818
stats match { case List(stat) => stat; case stats => untpd.Thicket(stats) }
1919
}
2020

compiler/test/dotty/tools/dotc/parsing/ModifiersParsingTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object ModifiersParsingTest {
1717
implicit val ctx: Context = (new ContextBase).initialCtx
1818

1919
def parse(code: String): Tree = {
20-
val (_, stats) = new Parser(new SourceFile("<meta>", code)).templateStatSeq()
20+
val (_, stats) = new Parser(SourceFile.virtual("<meta>", code)).templateStatSeq()
2121
stats match { case List(stat) => stat; case stats => Thicket(stats) }
2222
}
2323

doc-tool/test/dotty/tools/dottydoc/TypeRendering.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TypeRenderingTestFromTasty extends TypeRenderingTest with CheckFromTasty
1313
class TypeRenderingTestFromSource extends TypeRenderingTest with CheckFromSource
1414
abstract class TypeRenderingTest extends DottyDocTest {
1515
@Test def renderImplicitFunctionType = {
16-
val source = new SourceFile(
16+
val source = SourceFile.virtual(
1717
"ImplicitFunctionType.scala",
1818
"""
1919
|package scala

0 commit comments

Comments
 (0)