Skip to content

Commit 49c1343

Browse files
authored
Add warning for anonymous inline classes (#16723) (#20291)
Fixes #16723
2 parents 1cdf99f + 8f84df8 commit 49c1343

File tree

17 files changed

+101
-46
lines changed

17 files changed

+101
-46
lines changed

Diff for: compiler/src/dotty/tools/dotc/printing/Formatting.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ object Formatting {
4242
trait CtxShow:
4343
def run(using Context): Shown
4444

45-
private inline def CtxShow(inline x: Context ?=> Shown) = new CtxShow { def run(using Context) = x(using ctx) }
45+
private inline def CtxShow(inline x: Context ?=> Shown) =
46+
class InlinedCtxShow extends CtxShow { def run(using Context) = x(using ctx) }
47+
new InlinedCtxShow
4648
private def toStr[A: Show](x: A)(using Context): String = Shown.toStr(toShown(x))
4749
private def toShown[A: Show](x: A)(using Context): Shown = Show[A].show(x).runCtxShow
4850

Diff for: compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
208208
case UnstableInlineAccessorID // errorNumber: 192
209209
case VolatileOnValID // errorNumber: 193
210210
case ExtensionNullifiedByMemberID // errorNumber: 194
211+
case InlinedAnonClassWarningID // errorNumber: 195
211212

212213
def errorNumber = ordinal - 1
213214

Diff for: compiler/src/dotty/tools/dotc/reporting/messages.scala

+9
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,15 @@ extends SyntaxMsg(InlineGivenShouldNotBeFunctionID):
31133113
| inline def apply(x: A) = x.toB
31143114
"""
31153115

3116+
class InlinedAnonClassWarning()(using Context)
3117+
extends Message(InlinedAnonClassWarningID):
3118+
def kind = MessageKind.PotentialIssue
3119+
def msg(using Context) = "New anonymous class definition will be duplicated at each inline site"
3120+
def explain(using Context) =
3121+
i"""Anonymous class will be defined at each use site, which may lead to a larger number of classfiles.
3122+
|
3123+
|To inline class definitions, you may provide an explicit class name to avoid this warning."""
3124+
31163125
class ValueDiscarding(tp: Type)(using Context)
31173126
extends Message(ValueDiscardingID):
31183127
def kind = MessageKind.PotentialIssue

Diff for: compiler/src/dotty/tools/dotc/transform/PruneErasedDefs.scala

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Symbols.*
1010
import typer.RefChecks
1111
import MegaPhase.MiniPhase
1212
import ast.tpd
13+
import reporting.InlinedAnonClassWarning
1314

1415
import config.Feature
1516
import Decorators.*
@@ -51,6 +52,17 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
5152
else cpy.ValDef(tree)(rhs = trivialErasedTree(tree.rhs))
5253

5354
override def transformDefDef(tree: DefDef)(using Context): Tree =
55+
def checkNoInlineAnnoClasses(tree: DefDef)(using Context): Unit =
56+
if tree.symbol.is(Inline) then
57+
new TreeTraverser {
58+
def traverse(tree: Tree)(using Context): Unit =
59+
tree match
60+
case tree: TypeDef if tree.symbol.isAnonymousClass =>
61+
report.warning(new InlinedAnonClassWarning(), tree.symbol.sourcePos)
62+
case _ => traverseChildren(tree)
63+
}.traverse(tree)
64+
65+
checkNoInlineAnnoClasses(tree)
5466
checkErasedInExperimental(tree.symbol)
5567
if !tree.symbol.isEffectivelyErased || tree.rhs.isEmpty then tree
5668
else cpy.DefDef(tree)(rhs = trivialErasedTree(tree.rhs))

Diff for: tests/neg/i13044.check

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
-- Error: tests/neg/i13044.scala:65:40 ---------------------------------------------------------------------------------
2-
65 | implicit def typeSchema: Schema[A] = Schema.gen // error // error
1+
-- Error: tests/neg/i13044.scala:61:40 ---------------------------------------------------------------------------------
2+
61 | implicit def typeSchema: Schema[A] = Schema.gen // error // error
33
| ^^^^^^^^^^
44
| given instance gen is declared as `inline`, but was not inlined
55
|
@@ -12,47 +12,47 @@
1212
| ^
1313
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1414
|This location contains code that was inlined from i13044.scala:17
15-
31 | lazy val fields = recurse[m.MirroredElemTypes]
15+
29 | lazy val fields = recurse[m.MirroredElemTypes]
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1818
|This location contains code that was inlined from i13044.scala:17
19-
37 | inline given gen[A]: Schema[A] = derived
19+
33 | inline given gen[A]: Schema[A] = derived
2020
| ^^^^^^^
2121
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2222
|This location contains code that was inlined from i13044.scala:17
2323
17 | val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
2424
| ^
2525
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2626
|This location contains code that was inlined from i13044.scala:17
27-
31 | lazy val fields = recurse[m.MirroredElemTypes]
27+
29 | lazy val fields = recurse[m.MirroredElemTypes]
2828
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2929
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3030
|This location contains code that was inlined from i13044.scala:17
31-
37 | inline given gen[A]: Schema[A] = derived
31+
33 | inline given gen[A]: Schema[A] = derived
3232
| ^^^^^^^
3333
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3434
|This location contains code that was inlined from i13044.scala:17
3535
17 | val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
3636
| ^
3737
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3838
|This location contains code that was inlined from i13044.scala:17
39-
31 | lazy val fields = recurse[m.MirroredElemTypes]
39+
29 | lazy val fields = recurse[m.MirroredElemTypes]
4040
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4141
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4242
|This location contains code that was inlined from i13044.scala:17
43-
37 | inline given gen[A]: Schema[A] = derived
43+
33 | inline given gen[A]: Schema[A] = derived
4444
| ^^^^^^^
4545
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4646
|This location contains code that was inlined from i13044.scala:17
4747
17 | val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
4848
| ^
4949
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5050
|This location contains code that was inlined from i13044.scala:17
51-
31 | lazy val fields = recurse[m.MirroredElemTypes]
51+
29 | lazy val fields = recurse[m.MirroredElemTypes]
5252
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5353
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5454
|This location contains code that was inlined from i13044.scala:17
55-
37 | inline given gen[A]: Schema[A] = derived
55+
33 | inline given gen[A]: Schema[A] = derived
5656
| ^^^^^^^
5757
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5858
|This location contains code that was inlined from i13044.scala:17
@@ -64,15 +64,15 @@
6464
| ^^^^^^^^^^^
6565
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6666
|This location contains code that was inlined from i13044.scala:17
67-
31 | lazy val fields = recurse[m.MirroredElemTypes]
67+
29 | lazy val fields = recurse[m.MirroredElemTypes]
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6969
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7070
|This location contains code that was inlined from i13044.scala:17
71-
37 | inline given gen[A]: Schema[A] = derived
71+
33 | inline given gen[A]: Schema[A] = derived
7272
| ^^^^^^^
7373
--------------------------------------------------------------------------------------------------------------------
74-
-- Error: tests/neg/i13044.scala:65:40 ---------------------------------------------------------------------------------
75-
65 | implicit def typeSchema: Schema[A] = Schema.gen // error // error
74+
-- Error: tests/neg/i13044.scala:61:40 ---------------------------------------------------------------------------------
75+
61 | implicit def typeSchema: Schema[A] = Schema.gen // error // error
7676
| ^^^^^^^^^^
7777
| method recurse is declared as `inline`, but was not inlined
7878
|
@@ -85,47 +85,47 @@
8585
| ^^^^^^^
8686
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8787
|This location contains code that was inlined from i13044.scala:18
88-
31 | lazy val fields = recurse[m.MirroredElemTypes]
88+
29 | lazy val fields = recurse[m.MirroredElemTypes]
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9090
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9191
|This location contains code that was inlined from i13044.scala:18
92-
37 | inline given gen[A]: Schema[A] = derived
92+
33 | inline given gen[A]: Schema[A] = derived
9393
| ^^^^^^^
9494
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9595
|This location contains code that was inlined from i13044.scala:18
9696
17 | val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
9797
| ^
9898
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9999
|This location contains code that was inlined from i13044.scala:18
100-
31 | lazy val fields = recurse[m.MirroredElemTypes]
100+
29 | lazy val fields = recurse[m.MirroredElemTypes]
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102102
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103103
|This location contains code that was inlined from i13044.scala:18
104-
37 | inline given gen[A]: Schema[A] = derived
104+
33 | inline given gen[A]: Schema[A] = derived
105105
| ^^^^^^^
106106
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107107
|This location contains code that was inlined from i13044.scala:18
108108
17 | val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
109109
| ^
110110
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111111
|This location contains code that was inlined from i13044.scala:18
112-
31 | lazy val fields = recurse[m.MirroredElemTypes]
112+
29 | lazy val fields = recurse[m.MirroredElemTypes]
113113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114114
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115115
|This location contains code that was inlined from i13044.scala:18
116-
37 | inline given gen[A]: Schema[A] = derived
116+
33 | inline given gen[A]: Schema[A] = derived
117117
| ^^^^^^^
118118
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119119
|This location contains code that was inlined from i13044.scala:18
120120
17 | val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
121121
| ^
122122
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123123
|This location contains code that was inlined from i13044.scala:18
124-
31 | lazy val fields = recurse[m.MirroredElemTypes]
124+
29 | lazy val fields = recurse[m.MirroredElemTypes]
125125
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126126
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127127
|This location contains code that was inlined from i13044.scala:18
128-
37 | inline given gen[A]: Schema[A] = derived
128+
33 | inline given gen[A]: Schema[A] = derived
129129
| ^^^^^^^
130130
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131131
|This location contains code that was inlined from i13044.scala:18
@@ -137,10 +137,10 @@
137137
| ^^^^^^^^^^^
138138
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139139
|This location contains code that was inlined from i13044.scala:18
140-
31 | lazy val fields = recurse[m.MirroredElemTypes]
140+
29 | lazy val fields = recurse[m.MirroredElemTypes]
141141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
142142
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143143
|This location contains code that was inlined from i13044.scala:18
144-
37 | inline given gen[A]: Schema[A] = derived
144+
33 | inline given gen[A]: Schema[A] = derived
145145
| ^^^^^^^
146146
--------------------------------------------------------------------------------------------------------------------

Diff for: tests/neg/i13044.scala

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ trait SchemaDerivation {
2323
inline summonInline[Mirror.Of[A]] match {
2424
case m: Mirror.SumOf[A] =>
2525
lazy val subTypes = recurse[m.MirroredElemTypes]
26-
new Schema[A] {
27-
def build: A = ???
28-
}
26+
???
2927

3028
case m: Mirror.ProductOf[A] =>
3129
lazy val fields = recurse[m.MirroredElemTypes]
32-
new Schema[A] {
33-
def build: A = ???
34-
}
30+
???
3531
}
3632

3733
inline given gen[A]: Schema[A] = derived

Diff for: tests/pos/i17314.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ object circelike {
1313
inline final def derived[A](using conf: Configuration)(using
1414
inline mirror: Mirror.Of[A]
1515
): ConfiguredCodec[A] =
16-
new ConfiguredCodec[A]:
16+
class InlinedConfiguredCodec extends ConfiguredCodec[A]:
1717
val codec = summonInline[Codec[URI]] // simplification
18+
new InlinedConfiguredCodec
1819
}
1920

2021
object foo {

Diff for: tests/pos/not-looping-implicit.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ object Schema {
2424
inline summonInline[Mirror.Of[A]] match {
2525
case m: Mirror.SumOf[A] =>
2626
lazy val members = recurse[m.MirroredElemLabels, m.MirroredElemTypes]()
27-
new Schema[A] {}
27+
???
2828
case m: Mirror.ProductOf[A] =>
2929
lazy val fields = recurse[m.MirroredElemLabels, m.MirroredElemTypes]()
30-
new Schema[A] {}
30+
???
3131
}
3232

3333
inline given gen[A]: Schema[A] = derived[A]

Diff for: tests/run/i11050.scala

+7-5
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ object Show:
113113

114114
inline def show[T](x: T): String = summonInline[Show[T]].show(x)
115115

116-
transparent inline def derived[T](implicit ev: Mirror.Of[T]): Show[T] = new {
117-
def show(x: T): String = inline ev match {
118-
case m: Mirror.ProductOf[T] => showProduct(x.asInstanceOf[Product], m)
119-
case m: Mirror.SumOf[T] => showCases[m.MirroredElemTypes](0)(x, m.ordinal(x))
116+
transparent inline def derived[T](implicit ev: Mirror.Of[T]): Show[T] =
117+
class InlinedShow extends Show[T] { // provide name to anonymous class
118+
def show(x: T): String = inline ev match {
119+
case m: Mirror.ProductOf[T] => showProduct(x.asInstanceOf[Product], m)
120+
case m: Mirror.SumOf[T] => showCases[m.MirroredElemTypes](0)(x, m.ordinal(x))
121+
}
120122
}
121-
}
123+
new InlinedShow
122124

123125
transparent inline def showProduct[T](x: Product, m: Mirror.ProductOf[T]): String =
124126
constValue[m.MirroredLabel] + showElems[m.MirroredElemTypes, m.MirroredElemLabels](0, Nil)(x)

Diff for: tests/warn/i15503i.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ package foo.test.i16679a:
247247
import scala.deriving.Mirror
248248
object CaseClassByStringName:
249249
inline final def derived[A](using inline A: Mirror.Of[A]): CaseClassByStringName[A] =
250-
new CaseClassByStringName[A]:
250+
new CaseClassByStringName[A]: // warn
251251
def name: String = A.toString
252252

253253
object secondPackage:
@@ -263,7 +263,7 @@ package foo.test.i16679b:
263263
object CaseClassName:
264264
import scala.deriving.Mirror
265265
inline final def derived[A](using inline A: Mirror.Of[A]): CaseClassName[A] =
266-
new CaseClassName[A]:
266+
new CaseClassName[A]: // warn
267267
def name: String = A.toString
268268

269269
object Foo:
@@ -279,7 +279,7 @@ package foo.test.i17156:
279279
package a:
280280
trait Foo[A]
281281
object Foo:
282-
inline def derived[T]: Foo[T] = new Foo{}
282+
inline def derived[T]: Foo[T] = new Foo{} // warn
283283

284284
package b:
285285
import a.Foo

Diff for: tests/warn/i15503j.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ package foo.unused.summon.inlines:
4949

5050
transparent inline given conflictInside: C =
5151
summonInline[A]
52-
new {}
52+
???
5353

5454
transparent inline given potentialConflict: C =
5555
summonInline[B]
56-
new {}
56+
???
5757

5858
val b: B = summon[B]
5959
val c: C = summon[C]

Diff for: tests/warn/i16723.check

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E195] Potential Issue Warning: tests/warn/i16723.scala:3:2 ---------------------------------------------------------
2+
3 | new Object {} // warn
3+
| ^
4+
| New anonymous class definition will be duplicated at each inline site
5+
|
6+
| longer explanation available when compiling with `-explain`

Diff for: tests/warn/i16723.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inline def foo =
2+
class NotAnon
3+
new Object {} // warn

Diff for: tests/warn/i16723a.check

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E195] Potential Issue Warning: tests/warn/i16723a.scala:5:38 -------------------------------------------------------
2+
5 |inline given Converter[Int, String] = new Converter { // warn
3+
| ^
4+
| New anonymous class definition will be duplicated at each inline site
5+
|
6+
| longer explanation available when compiling with `-explain`

Diff for: tests/warn/i16723a.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Converter[A, B] {
2+
def convert: A => B
3+
}
4+
5+
inline given Converter[Int, String] = new Converter { // warn
6+
def convert = _.toString()
7+
}
8+
9+
def foo(using bar: Converter[Int, String]) =
10+
"foo"
11+
12+
@main
13+
def main =
14+
foo
15+
foo
16+
foo
17+
foo

0 commit comments

Comments
 (0)