Skip to content

Commit

Permalink
Avoid crashes on missing positions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicolasstucki committed Dec 12, 2023
1 parent feed938 commit 8d5a04c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

given TreeMethods: TreeMethods with
extension (self: Tree)
def pos: Position = self.sourcePos
def pos: Position =
val treePos = self.sourcePos
if treePos.exists then treePos
else
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.")
self.source.atSpan(dotc.util.Spans.Span(0))

def symbol: Symbol = self.symbol
def show(using printer: Printer[Tree]): String = printer.show(self)
def isExpr: Boolean =
Expand Down Expand Up @@ -2625,7 +2631,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def info: TypeRepr = self.denot.info

def pos: Option[Position] =
if self.exists then Some(self.sourcePos) else None
if self.exists then
val symPos = self.sourcePos
if symPos.exists then Some(symPos)
else
if xCheckMacro then report.warning(s"Missing symbol position (defaulting to position 0): $self\nThis is a compiler bug. Please report it.")
Some(self.source.atSpan(dotc.util.Spans.Span(0)))
else None

def docstring: Option[String] =
import dotc.core.Comments.CommentsContext
Expand Down

0 comments on commit 8d5a04c

Please sign in to comment.