Skip to content

Fix #7683: Fix CompilationUnit handling in Context#withSource #7691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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("<highlighting>", 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) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class SyntaxHighlightingTests extends DottyTest {

test("def f1(x: Int) = 123", "<K|def> <V|f1>(<V|x>: <T|Int>) = <L|123>")
test("def f2[T](x: T) = { 123 }", "<K|def> <V|f2>[<T|T>](<V|x>: <T|T>) = { <L|123> }")

test("def f3[T[_", "<K|def> <V|f3>[<T|T>[<T|_>")
}

@Test
Expand Down