Skip to content

Commit 9a32a2c

Browse files
committed
Fix #7683: Fix CompilationUnit handling in Context#withSource
In e07a728, I changed Context#withSource to set a new CompilationUnit based on the source if no compilation unit is currently set (always having a CompilationUnit set is now needed since I moved the fresh name generator inside CompilationUnit), however CompilationUnit#apply will error out if the source file does not exist, which might happen for library code. Fixed by adding a parameter to disable this check, but this is not very pretty and we should really try to design a better way to distinguish between user source files and source files we happen to know the name of because it's stored in Tasty.
1 parent 0a33ad3 commit 9a32a2c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
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
}

0 commit comments

Comments
 (0)