Skip to content

Improve position of deprecation #23052

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

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object report:
ctx.reporter.report(warning)

def deprecationWarning(msg: Message, pos: SrcPos, origin: String = "")(using Context): Unit =
issueWarning(new DeprecationWarning(msg, pos.sourcePos, origin))
issueWarning(DeprecationWarning(msg, addInlineds(pos), origin))

def migrationWarning(msg: Message, pos: SrcPos)(using Context): Unit =
issueWarning(new MigrationWarning(msg, pos.sourcePos))
Expand Down Expand Up @@ -138,7 +138,9 @@ object report:

private def addInlineds(pos: SrcPos)(using Context): SourcePosition =
def recur(pos: SourcePosition, inlineds: List[Trees.Tree[?]]): SourcePosition = inlineds match
case inlined :: inlineds1 => pos.withOuter(recur(inlined.sourcePos, inlineds1))
case inlined :: inlineds =>
val outer = recur(inlined.sourcePos, inlineds)
pos.withOuter(outer)
case Nil => pos
recur(pos.sourcePos, tpd.enclosingInlineds)

Expand Down
14 changes: 9 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class CrossVersionChecks extends MiniPhase:
do
val msg = annot.argumentConstantString(0).map(msg => s": $msg").getOrElse("")
val since = annot.argumentConstantString(1).map(version => s" (since: $version)").getOrElse("")
report.deprecationWarning(em"inheritance from $psym is deprecated$since$msg", parent.srcPos, origin=psym.showFullName)
val composed = em"inheritance from $psym is deprecated$since$msg"
report.deprecationWarning(composed, parent.srcPos, origin = psym.showFullName)
}

private def unrollError(pos: SrcPos)(using Context): Unit =
Expand Down Expand Up @@ -203,16 +204,19 @@ object CrossVersionChecks:
* Also check for deprecation of the companion class for synthetic methods in the companion module.
*/
private[CrossVersionChecks] def checkDeprecatedRef(sym: Symbol, pos: SrcPos)(using Context): Unit =
def maybeWarn(annotee: Symbol, annot: Annotation) = if !skipWarning(sym) then
def warn(annotee: Symbol, annot: Annotation) =
val message = annot.argumentConstantString(0).filter(!_.isEmpty).map(": " + _).getOrElse("")
val since = annot.argumentConstantString(1).filter(!_.isEmpty).map(" since " + _).getOrElse("")
report.deprecationWarning(em"${annotee.showLocated} is deprecated${since}${message}", pos, origin=annotee.showFullName)
val composed = em"${annotee.showLocated} is deprecated${since}${message}"
report.deprecationWarning(composed, pos, origin = annotee.showFullName)
sym.getAnnotation(defn.DeprecatedAnnot) match
case Some(annot) => maybeWarn(sym, annot)
case Some(annot) => if !skipWarning(sym) then warn(sym, annot)
case _ =>
if sym.isAllOf(SyntheticMethod) then
val companion = sym.owner.companionClass
if companion.is(CaseClass) then companion.getAnnotation(defn.DeprecatedAnnot).foreach(maybeWarn(companion, _))
if companion.is(CaseClass) then
for annot <- companion.getAnnotation(defn.DeprecatedAnnot) if !skipWarning(sym) do
warn(companion, annot)

/** Decide whether the deprecation of `sym` should be ignored in this context.
*
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/util/Spans.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ object Spans {

/** The start of this span. */
def start: Int = {
assert(exists)
assert(exists, "start of NoSpan")
(coords & StartEndMask).toInt
}

/** The end of this span */
def end: Int = {
assert(exists)
assert(exists, "end of NoSpan")
((coords >>> StartEndBits) & StartEndMask).toInt
}

/** The point of this span, returns start for synthetic spans */
def point: Int = {
assert(exists)
assert(exists, "point of NoSpan")
val poff = pointDelta
if (poff == SyntheticPointDelta) start else start + poff
}
Expand Down
12 changes: 12 additions & 0 deletions tests/warn/i22795.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

-- Deprecation Warning: tests/warn/i22795/test_1.scala:4:7 -------------------------------------------------------------
4 | lib.m() // warn
| ^^^^^^^
| object A is deprecated
|---------------------------------------------------------------------------------------------------------------------
|Inline stack trace
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|This location contains code that was inlined from test_1.scala:5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code was inlined from lib_0.scala, not test_1.scala, or?

Copy link
Contributor Author

@som-snytt som-snytt Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that was the improvement (to do) I mention in the comment. I hoped to book the progress before opening the can of worms. A can of worms has a long shelf life and can be opened at any time.

I'll improve the PR comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note this is the (wrong) span it currently reports at.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks

5 |
| ^^^^^^^^
---------------------------------------------------------------------------------------------------------------------
11 changes: 11 additions & 0 deletions tests/warn/i22795/lib_0.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import scala.quoted._

@deprecated object A

object lib:
inline def m() = ${mImpl}

def mImpl(using Quotes): Expr[Any] =
import quotes.reflect._
Ref(Symbol.classSymbol("A$").companionModule).asExpr
4 changes: 4 additions & 0 deletions tests/warn/i22795/test_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using options -deprecation

@main def Test = println:
lib.m() // warn
Loading