Skip to content
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

Backport "Do not propagate @tailrec to exported methods" to LTS #20851

Merged
merged 3 commits into from
Jun 28, 2024
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
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,13 @@ class Namer { typer: Typer =>
newSymbol(cls, forwarderName, mbrFlags, mbrInfo, coord = span)

forwarder.info = avoidPrivateLeaks(forwarder)
forwarder.addAnnotations(sym.annotations.filterConserve(_.symbol != defn.BodyAnnot))
forwarder.addAnnotations(sym.annotations.filterConserve { annot =>
annot.symbol != defn.BodyAnnot
&& annot.symbol != defn.TailrecAnnot
&& annot.symbol != defn.MainAnnot
&& !annot.symbol.derivesFrom(defn.MacroAnnotationClass)
&& !annot.symbol.derivesFrom(defn.MainAnnotationClass)
})

if forwarder.isType then
buf += tpd.TypeDef(forwarder.asType).withSpan(span)
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/export-main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Foo:
@main def baz: Int = 1

object Bar:
export Foo.baz // export Foo.baz but not create an new main entry point
10 changes: 10 additions & 0 deletions tests/pos/i19505.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.annotation.tailrec

object Foo:
@tailrec
def foo(n: Int): Int =
if n == 0 then 0
else foo(n-1)

object Bar:
export Foo.foo // def foo here should not have `@tailrec`
13 changes: 13 additions & 0 deletions tests/run-macros/annot-export/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.quoted._
import scala.collection.mutable.Map

@experimental
class returnClassName extends MacroAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
import quotes.reflect._
tree match
case DefDef(name, params, tpt, _) =>
val rhs = Literal(StringConstant(Symbol.spliceOwner.name.stripSuffix("$")))
List(DefDef.copy(tree)(name, params, tpt, Some(rhs)))
}
10 changes: 10 additions & 0 deletions tests/run-macros/annot-export/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Bar:
@returnClassName
def f(): String = ??? // def f(): String = "Bar"

object Baz:
export Bar.f // def f(): String = Bar.f(n)

@main def Test =
assert(Bar.f() == "Bar")
assert(Baz.f() == "Bar")
8 changes: 8 additions & 0 deletions tests/warn/i19505.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.annotation.tailrec

object Foo:
@tailrec
def foo: Int = foo // warn: Infinite recursive call

object Bar:
export Foo.foo // def foo here should not have `@tailrec`