Skip to content

Commit dcbf416

Browse files
committed
Avoid crashes on missing positions
All positions should be set on these trees and symbols at this point. If one is missing it is due to a compiler bug. We default the position to 0 if there is a missing position to avoid crashing when calling methods on Position. To make it possible for us to fix these missing positions, we warn if there is a missing position when compiling with -Xcheck-macros.
1 parent feed938 commit dcbf416

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+14-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
9696

9797
given TreeMethods: TreeMethods with
9898
extension (self: Tree)
99-
def pos: Position = self.sourcePos
99+
def pos: Position =
100+
val treePos = self.sourcePos
101+
if treePos.exists then treePos
102+
else
103+
if xCheckMacro then report.warning(s"Missing tree position (defaulting to position 0): ${Printer.TreeStructure.show(self)}\nThis is a compiler bug. Please report it.")
104+
self.source.atSpan(dotc.util.Spans.Span(0))
105+
100106
def symbol: Symbol = self.symbol
101107
def show(using printer: Printer[Tree]): String = printer.show(self)
102108
def isExpr: Boolean =
@@ -2625,7 +2631,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
26252631
def info: TypeRepr = self.denot.info
26262632

26272633
def pos: Option[Position] =
2628-
if self.exists then Some(self.sourcePos) else None
2634+
if self.exists then
2635+
val symPos = self.sourcePos
2636+
if symPos.exists then Some(symPos)
2637+
else
2638+
if xCheckMacro then report.warning(s"Missing symbol position (defaulting to position 0): $self")
2639+
Some(self.source.atSpan(dotc.util.Spans.Span(0)))
2640+
else None
26292641

26302642
def docstring: Option[String] =
26312643
import dotc.core.Comments.CommentsContext

0 commit comments

Comments
 (0)