-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Skip caret when source is missing in initialization checker #23926
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import core.* | |
import Contexts.* | ||
import ast.tpd.* | ||
import util.SourcePosition | ||
import util.SourceFile | ||
|
||
import Decorators.*, printing.SyntaxHighlighting | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just headOption,does the nonEmpty read the content? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean maybe just need to check the first char or line There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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 => | ||
|
@@ -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 | ||
"" | ||
|
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 |
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 ] | ||
| ^^^ |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FileContentNonEmpty?