Skip to content

Commit d4a1742

Browse files
Backport "Avoid crashes on missing positions" to LTS (#20798)
Backports #19250 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 6d67f75 + 1cf11ec commit d4a1742

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)