Skip to content
Open
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
27 changes: 22 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/init/Trace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import core.*
import Contexts.*
import ast.tpd.*
import util.SourcePosition
import util.SourceFile

import Decorators.*, printing.SyntaxHighlighting

Expand Down Expand Up @@ -42,17 +43,33 @@ object Trace:

inline def extendTrace[T](node: Tree)(using t: Trace)(op: Trace ?=> T): T = op(using t.add(node))

/**
* Returns whether the source file exists
*
* The method SourceFile#exists always return true thus cannot be used.
*/
def fileExists(source: SourceFile): Boolean =
source.content().nonEmpty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileContentNonEmpty?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just headOption,does the nonEmpty read the content?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SourceFile#content is a special method with cache. If it is not empty, we will need to read lines from it (thus force loading the content).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean maybe just need to check the first char or line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. If you check the overall design for code caching and SourceFile#content, you will see there is no need/opportunity for optimization here.


def buildStacktrace(trace: Trace, preamble: String)(using Context): String = if trace.isEmpty then "" else preamble + {
var lastLineNum = -1
var lines: mutable.ArrayBuffer[String] = new mutable.ArrayBuffer
trace.foreach { tree =>
val isLastTraceItem = tree `eq` trace.last
val pos = tree.sourcePos
val hasSource = fileExists(pos.source)
val line =
if pos.source.exists then
val loc = "[ " + pos.source.file.name + ":" + (pos.line + 1) + " ]"
val code = SyntaxHighlighting.highlight(pos.lineContent.trim)
i"$code\t$loc"
if pos.exists then
// Show more information for external code without source
val file = if hasSource then pos.source.file.name else pos.source.file.path
val loc = file + ":" + (pos.line + 1)
val code =
if hasSource then
SyntaxHighlighting.highlight(pos.lineContent.trim)
else
"(no source)"

i"$code\t[ $loc ]"
else
tree match
case defDef: DefTree =>
Expand All @@ -62,7 +79,7 @@ object Trace:
tree.show.split(System.lineSeparator(), 2).head

val positionMarkerLine =
if pos.exists && pos.source.exists then
if pos.exists && hasSource then
(if isLastTraceItem then EMPTY_PADDING else CONNECTING_INDENT)+ positionMarker(pos)
else
""
Expand Down
16 changes: 15 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class CompilationTests {
compileFilesInDir("tests/explicit-nulls/run", explicitNullsOptions)
}.checkRuns()

// initialization tests
// initialization tests for global objects
@Test def checkInitGlobal: Unit = {
implicit val testGroup: TestGroup = TestGroup("checkInitGlobal")
compileFilesInDir("tests/init-global/warn", defaultOptions.and("-Ysafe-init-global"), FileFilter.exclude(TestSources.negInitGlobalScala2LibraryTastyExcludelisted)).checkWarnings()
Expand All @@ -252,6 +252,20 @@ class CompilationTests {
compileFilesInDir("tests/init-global/warn-tasty", defaultOptions.and("-Ysafe-init-global"), FileFilter.exclude(TestSources.negInitGlobalScala2LibraryTastyExcludelisted)).checkWarnings()
compileFilesInDir("tests/init-global/pos-tasty", defaultOptions.and("-Ysafe-init-global", "-Werror"), FileFilter.exclude(TestSources.posInitGlobalScala2LibraryTastyExcludelisted)).checkCompile()
end if

locally {
val group = TestGroup("checkInitGlobal/tastySource")
val tastSourceOptions = defaultOptions.and("-Ysafe-init-global")
val outDirLib = defaultOutputDir + group + "/A/tastySource/A"

// Set -sourceroot such that the source code cannot be found by the compiler
val libOptions = tastSourceOptions.and("-sourceroot", "tests/init-global/special")
val lib = compileFile("tests/init-global/special/tastySource/A.scala", libOptions)(group).keepOutput.checkCompile()

compileFile("tests/init-global/special/tastySource/B.scala", tastSourceOptions.withClasspath(outDirLib))(group).checkWarnings()

lib.delete()
}
}

// initialization tests
Expand Down
4 changes: 4 additions & 0 deletions tests/init-global/special/tastySource/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object A:
def foo(fn: => Int) = bar(fn)

def bar(fn: => Int) = fn
13 changes: 13 additions & 0 deletions tests/init-global/special/tastySource/B.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Warning: tests/init-global/special/tastySource/B.scala:4:14 ---------------------------------------------------------
4 | def bar = C.n * 3 // warn
| ^^^
|Reading mutable state of object C during initialization of object B.
|Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. Calling trace:
|├── object B: [ B.scala:1 ]
|│ ^
|├── (no source) [ tastySource/A.scala:2 ]
|├── (no source) [ tastySource/A.scala:4 ]
|├── var y = A.foo(bar) * 2 [ B.scala:2 ]
|│ ^^^
|└── def bar = C.n * 3 // warn [ B.scala:4 ]
| ^^^
7 changes: 7 additions & 0 deletions tests/init-global/special/tastySource/B.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object B:
var y = A.foo(bar) * 2

def bar = C.n * 3 // warn

object C:
var n = 10
Loading