diff --git a/compiler/src/dotty/tools/dotc/CompilationUnit.scala b/compiler/src/dotty/tools/dotc/CompilationUnit.scala index 527770b1731a..efe6fd2829f4 100644 --- a/compiler/src/dotty/tools/dotc/CompilationUnit.scala +++ b/compiler/src/dotty/tools/dotc/CompilationUnit.scala @@ -83,9 +83,14 @@ object CompilationUnit { unit1 } - def apply(source: SourceFile)(implicit ctx: Context): CompilationUnit = { + /** Create a compilation unit corresponding to `source`. + * If `mustExist` is true, this will fail if `source` does not exist. + */ + def apply(source: SourceFile, mustExist: Boolean = true)(implicit ctx: Context): CompilationUnit = { val src = - if (source.file.isDirectory) { + if (!mustExist) + source + else if (source.file.isDirectory) { ctx.error(s"expected file, received directory '${source.file.path}'") NoSource } diff --git a/compiler/src/dotty/tools/dotc/core/Contexts.scala b/compiler/src/dotty/tools/dotc/core/Contexts.scala index 16b1af74aa96..4bd0b591a249 100644 --- a/compiler/src/dotty/tools/dotc/core/Contexts.scala +++ b/compiler/src/dotty/tools/dotc/core/Contexts.scala @@ -468,7 +468,10 @@ object Contexts { else { val newCtx = fresh.setSource(source) if (newCtx.compilationUnit == null) - newCtx.setCompilationUnit(CompilationUnit(source)) + // `source` might correspond to a file not necessarily + // in the current project (e.g. when inlining library code), + // so set `mustExist` to false. + newCtx.setCompilationUnit(CompilationUnit(source, mustExist = false)) sourceCtx = sourceCtx.updated(source, newCtx) newCtx } diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index fe298c0cf046..f795781b05d9 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -1,4 +1,5 @@ -package dotty.tools.dotc.printing +package dotty.tools.dotc +package printing import dotty.tools.dotc.ast.untpd import dotty.tools.dotc.core.Contexts.Context @@ -32,8 +33,10 @@ object SyntaxHighlighting { def freshCtx = ctx.fresh.setReporter(Reporter.NoReporter) if (in.isEmpty || ctx.settings.color.value == "never") in else { - implicit val ctx = freshCtx val source = SourceFile.virtual("", in) + + implicit val ctx = freshCtx.setCompilationUnit(CompilationUnit(source, mustExist = false)(freshCtx)) + val colorAt = Array.fill(in.length)(NoColor) def highlightRange(from: Int, to: Int, color: String) = diff --git a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala index b60415205bba..8ac639db732f 100644 --- a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala +++ b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala @@ -110,6 +110,8 @@ class SyntaxHighlightingTests extends DottyTest { test("def f1(x: Int) = 123", " (: ) = ") test("def f2[T](x: T) = { 123 }", " [](: ) = { }") + + test("def f3[T[_", " [[") } @Test