-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Do not propagate @tailrec
to exported methods
#19509
Merged
+53
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternatively, could the TailRec check ignore exported methods? - this filtering approach might not be scalable for e.g. macro annotations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we also need to filter macro annotations.
I think that disabling the behavior where it is checked will be less scalable. For example, I disable the macro annotations on exports I have to find one or more places where this restriction needs to be taken into account. And those places are usually code generation details like exports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also added
@main
. Might might still need to filter others.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should have a whitelist approach with this one. This would make sure that we never accidentally keep an annotation we do not want.
I tested this on
scala3-bootstrapped/testCompilation
and it only required the@targetName
annotation. Others that should be required are@experimental
,@publicInBinary
, andshowAsInfix
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is that we will never know what should be the behavior for custom annotations.
The only holistic solution here is to have an
@meta
-based indication of what to do. This is the approach used to determine whether annotations should go to fields/getters/setters. It would also be appropriate to determine whether it should go to exports/bridges/etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we need
ExportableAnnotation
trait that all such annotations should extend to get exported.