Skip to content

Commit 8698691

Browse files
authored
Merge pull request #7691 from dotty-staging/careful-compilation-unit
Fix #7683: Fix CompilationUnit handling in Context#withSource
2 parents c3ba62b + 35b79c5 commit 8698691

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,14 @@ object CompilationUnit {
8383
unit1
8484
}
8585

86-
def apply(source: SourceFile)(implicit ctx: Context): CompilationUnit = {
86+
/** Create a compilation unit corresponding to `source`.
87+
* If `mustExist` is true, this will fail if `source` does not exist.
88+
*/
89+
def apply(source: SourceFile, mustExist: Boolean = true)(implicit ctx: Context): CompilationUnit = {
8790
val src =
88-
if (source.file.isDirectory) {
91+
if (!mustExist)
92+
source
93+
else if (source.file.isDirectory) {
8994
ctx.error(s"expected file, received directory '${source.file.path}'")
9095
NoSource
9196
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,10 @@ object Contexts {
468468
else {
469469
val newCtx = fresh.setSource(source)
470470
if (newCtx.compilationUnit == null)
471-
newCtx.setCompilationUnit(CompilationUnit(source))
471+
// `source` might correspond to a file not necessarily
472+
// in the current project (e.g. when inlining library code),
473+
// so set `mustExist` to false.
474+
newCtx.setCompilationUnit(CompilationUnit(source, mustExist = false))
472475
sourceCtx = sourceCtx.updated(source, newCtx)
473476
newCtx
474477
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc.printing
1+
package dotty.tools.dotc
2+
package printing
23

34
import dotty.tools.dotc.ast.untpd
45
import dotty.tools.dotc.core.Contexts.Context
@@ -32,8 +33,10 @@ object SyntaxHighlighting {
3233
def freshCtx = ctx.fresh.setReporter(Reporter.NoReporter)
3334
if (in.isEmpty || ctx.settings.color.value == "never") in
3435
else {
35-
implicit val ctx = freshCtx
3636
val source = SourceFile.virtual("<highlighting>", in)
37+
38+
implicit val ctx = freshCtx.setCompilationUnit(CompilationUnit(source, mustExist = false)(freshCtx))
39+
3740
val colorAt = Array.fill(in.length)(NoColor)
3841

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

compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class SyntaxHighlightingTests extends DottyTest {
110110

111111
test("def f1(x: Int) = 123", "<K|def> <V|f1>(<V|x>: <T|Int>) = <L|123>")
112112
test("def f2[T](x: T) = { 123 }", "<K|def> <V|f2>[<T|T>](<V|x>: <T|T>) = { <L|123> }")
113+
114+
test("def f3[T[_", "<K|def> <V|f3>[<T|T>[<T|_>")
113115
}
114116

115117
@Test

0 commit comments

Comments
 (0)