From 3ef8e9c0e040e95f27850459573cc9936e56e3b0 Mon Sep 17 00:00:00 2001 From: Hamza REMMAL Date: Mon, 5 Feb 2024 22:16:35 +0100 Subject: [PATCH] Improve when deprecation warning are emitted --- .../tools/dotc/typer/CrossVersionChecks.scala | 6 +++-- tests/warn/i19302.check | 20 ++++++++++++++++ tests/warn/i19302.scala | 23 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/warn/i19302.check create mode 100644 tests/warn/i19302.scala diff --git a/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala b/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala index 29c5ca96049c..c22d03ca77d7 100644 --- a/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala @@ -159,10 +159,12 @@ class CrossVersionChecks extends MiniPhase: val tpe = tree.tpe tpe.foreachPart { case TypeRef(_, sym: Symbol) => - checkDeprecated(sym, tree.srcPos) + if tree.span.isSourceDerived then + checkDeprecated(sym, tree.srcPos) checkExperimentalRef(sym, tree.srcPos) case TermRef(_, sym: Symbol) => - checkDeprecated(sym, tree.srcPos) + if tree.span.isSourceDerived then + checkDeprecated(sym, tree.srcPos) checkExperimentalRef(sym, tree.srcPos) case _ => } diff --git a/tests/warn/i19302.check b/tests/warn/i19302.check new file mode 100644 index 000000000000..8f439ad1320d --- /dev/null +++ b/tests/warn/i19302.check @@ -0,0 +1,20 @@ +-- Deprecation Warning: tests/warn/i19302.scala:8:20 ------------------------------------------------------------------- +8 | def doo(): Option[Thing] = // warn + | ^^^^^ + | class Thing is deprecated since n/a: is deprecated +-- Deprecation Warning: tests/warn/i19302.scala:9:13 ------------------------------------------------------------------- +9 | Some(new Thing(1)) // warn + | ^^^^^ + | class Thing is deprecated since n/a: is deprecated +-- Deprecation Warning: tests/warn/i19302.scala:10:23 ------------------------------------------------------------------ +10 | def wop(x: => Option[Thing]) = println(x) // warn + | ^^^^^ + | class Thing is deprecated since n/a: is deprecated +-- Deprecation Warning: tests/warn/i19302.scala:13:16 ------------------------------------------------------------------ +13 | doo().map((t: Thing) => println(t)) // warn + | ^^^^^ + | class Thing is deprecated since n/a: is deprecated +-- Deprecation Warning: tests/warn/i19302.scala:18:18 ------------------------------------------------------------------ +18 | val thing = new Thing(42) // warn + | ^^^^^ + | class Thing is deprecated since n/a: is deprecated diff --git a/tests/warn/i19302.scala b/tests/warn/i19302.scala new file mode 100644 index 000000000000..1638b44104c1 --- /dev/null +++ b/tests/warn/i19302.scala @@ -0,0 +1,23 @@ +//> using options -deprecation + +@deprecated("is deprecated", "n/a") +class Thing(val value: Int) + +object Main extends App { + + def doo(): Option[Thing] = // warn + Some(new Thing(1)) // warn + def wop(x: => Option[Thing]) = println(x) // warn + + doo().map(t => println(t)) + doo().map((t: Thing) => println(t)) // warn + doo().map(println) + doo().foreach(println) + for (x <- doo()) println(x) + + val thing = new Thing(42) // warn + println(thing) + + val something = Some(thing) + wop(something) +} \ No newline at end of file