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

Add warning for anonymous inline classes (#16723) #20291

Merged
merged 1 commit into from
May 7, 2024

Conversation

aherlihy
Copy link
Contributor

@aherlihy aherlihy commented Apr 29, 2024

Fixes #16723

@aherlihy aherlihy requested a review from hamzaremmal May 2, 2024 12:16
tests/warn/i16723a.scala Outdated Show resolved Hide resolved
tests/warn/i16723.scala Show resolved Hide resolved
compiler/src/dotty/tools/dotc/reporting/messages.scala Outdated Show resolved Hide resolved
Comment on lines 60 to 69
def checkNoInlineAnnoClasses(tree: DefDef)(using Context): Unit =
if tree.symbol.is(Inline) then
new TreeTraverser {
def traverse(tree: Tree)(using Context): Unit =
tree match
case tree: TypeDef if tree.symbol.isAnonymousClass =>
report.warning(new InlinedAnonClassWarning(), tree.symbol.sourcePos)
case _ => traverseChildren(tree)
}.traverse(tree)

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is the correct place to put this check in, I think it makes more sense to add it in RefChecks'.

new RefChecks) :: // Various checks mostly related to abstract members and overriding

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I don't feel strongly so I have moved it, although if this method is ever called from somewhere other than where it was originally defined then the warning itself would very likely need to change.

Comment on lines 1 to 12
-- [E195] Potential Issue Warning: tests/neg/i13044.scala:26:8 ---------------------------------------------------------
26 | new Schema[A] {
| ^
| New anonymous class definition will be duplicated at each inline site
|
| longer explanation available when compiling with `-explain`
-- [E195] Potential Issue Warning: tests/neg/i13044.scala:32:8 ---------------------------------------------------------
32 | new Schema[A] {
| ^
| New anonymous class definition will be duplicated at each inline site
|
| longer explanation available when compiling with `-explain`
Copy link
Member

Choose a reason for hiding this comment

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

It is better to replace the anonymous class in the test than adding the warnings here. You can do that by simply replacing them by ???

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this particular test, I thought it was necessary to keep it as is since replacing those line with ??? will cause the compilation to pass. I could add @nowarn as an alternative to modifying the checkfile, but I don't know the exact context in which the test was constructed so I am hesitant to change it too significantly in case it ends up passing but not testing what it was intended to test.

Copy link
Member

Choose a reason for hiding this comment

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

Mmmh, but the test behaves as expected if we change it to ??? (I can reproduce it correctly locally)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh, maybe I'm misunderstanding, what I'm testing looks like this: https://github.com/aherlihy/scala3/compare/i15503...aherlihy:scala3:i16723-2?expand=1

Is that what you're running as well?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not pass for me, not sure why it would behave differently

tests/pos/not-looping-implicit.scala Outdated Show resolved Hide resolved
tests/pos/not-looping-implicit.scala Outdated Show resolved Hide resolved
tests/warn/i15503i.scala Outdated Show resolved Hide resolved
@hamzaremmal
Copy link
Member

hamzaremmal commented May 3, 2024

Can you also squash everything in a single commit

@aherlihy
Copy link
Contributor Author

aherlihy commented May 3, 2024

@hamzaremmal Thank you for the review! Is there some strong preference for rebase merge over squash merge in the repo? Normally irrelevant commits and their messages will be removed upon squashing.

@@ -51,6 +52,7 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
else cpy.ValDef(tree)(rhs = trivialErasedTree(tree.rhs))

override def transformDefDef(tree: DefDef)(using Context): Tree =
RefChecks.checkNoInlineAnnoClasses(tree)
Copy link
Member

Choose a reason for hiding this comment

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

Oh, I think my previous comment was not clear. I meant why adding the check in the PruneErasedDefs and not in RefChecks ?

Copy link
Contributor Author

@aherlihy aherlihy May 3, 2024

Choose a reason for hiding this comment

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

Ah, I see! I asked @nicolasstucki and he mentioned that if it happened at a later stage then some inlined definitions wouldn't be available (e.g. it's best to do it as late as possible, and this is the latest phase before it's no longer possible), but I'm not 100% sure about that.

Comment on lines 1 to 12
-- [E195] Potential Issue Warning: tests/neg/i13044.scala:26:8 ---------------------------------------------------------
26 | new Schema[A] {
| ^
| New anonymous class definition will be duplicated at each inline site
|
| longer explanation available when compiling with `-explain`
-- [E195] Potential Issue Warning: tests/neg/i13044.scala:32:8 ---------------------------------------------------------
32 | new Schema[A] {
| ^
| New anonymous class definition will be duplicated at each inline site
|
| longer explanation available when compiling with `-explain`
Copy link
Member

Choose a reason for hiding this comment

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

Mmmh, but the test behaves as expected if we change it to ??? (I can reproduce it correctly locally)

@hamzaremmal
Copy link
Member

@hamzaremmal Thank you for the review! Is there some strong preference for rebase merge over squash merge in the repo? Normally irrelevant commits and their messages will be removed upon squashing.

I agree, but this was a recommendation from @nicolasstucki to follow for this repository, maybe he can explain it here

@aherlihy aherlihy enabled auto-merge May 7, 2024 05:51
@aherlihy aherlihy merged commit 49c1343 into scala:main May 7, 2024
18 checks passed
@Kordyjan Kordyjan added this to the 3.5.0 milestone May 10, 2024
WojciechMazur added a commit that referenced this pull request Jul 8, 2024
…21110)

Backports #20291 to the LTS branch.

PR submitted by the release tooling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn or optimize when inline given inlines anonymous class creation
3 participants