Skip to content

Commit 1cf11ec

Browse files
nicolasstuckiWojciechMazur
authored andcommitted
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. [Cherry-picked 8d5a04c]
1 parent a9e6358 commit 1cf11ec

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
@@ -94,7 +94,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
9494

9595
given TreeMethods: TreeMethods with
9696
extension (self: Tree)
97-
def pos: Position = self.sourcePos
97+
def pos: Position =
98+
val treePos = self.sourcePos
99+
if treePos.exists then treePos
100+
else
101+
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.")
102+
self.source.atSpan(dotc.util.Spans.Span(0))
103+
98104
def symbol: Symbol = self.symbol
99105
def show(using printer: Printer[Tree]): String = printer.show(self)
100106
def isExpr: Boolean =
@@ -2593,7 +2599,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25932599
def info: TypeRepr = self.denot.info
25942600

25952601
def pos: Option[Position] =
2596-
if self.exists then Some(self.sourcePos) else None
2602+
if self.exists then
2603+
val symPos = self.sourcePos
2604+
if symPos.exists then Some(symPos)
2605+
else
2606+
if xCheckMacro then report.warning(s"Missing symbol position (defaulting to position 0): $self\nThis is a compiler bug. Please report it.")
2607+
Some(self.source.atSpan(dotc.util.Spans.Span(0)))
2608+
else None
25972609

25982610
def docstring: Option[String] =
25992611
import dotc.core.Comments.CommentsContext

0 commit comments

Comments
 (0)