File tree 12 files changed +75
-14
lines changed
compiler/src/dotty/tools/dotc
12 files changed +75
-14
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,9 @@ object Formatting {
42
42
trait CtxShow :
43
43
def run (using Context ): Shown
44
44
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
46
48
private def toStr [A : Show ](x : A )(using Context ): String = Shown .toStr(toShown(x))
47
49
private def toShown [A : Show ](x : A )(using Context ): Shown = Show [A ].show(x).runCtxShow
48
50
Original file line number Diff line number Diff line change @@ -208,6 +208,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
208
208
case UnstableInlineAccessorID // errorNumber: 192
209
209
case VolatileOnValID // errorNumber: 193
210
210
case ExtensionNullifiedByMemberID // errorNumber: 194
211
+ case InlinedAnonClassWarningID // errorNumber: 195
211
212
212
213
def errorNumber = ordinal - 1
213
214
Original file line number Diff line number Diff line change @@ -3104,6 +3104,15 @@ extends SyntaxMsg(InlineGivenShouldNotBeFunctionID):
3104
3104
| inline def apply(x: A) = x.toB
3105
3105
"""
3106
3106
3107
+ class InlinedAnonClassWarning ()(using Context )
3108
+ extends Message (InlinedAnonClassWarningID ):
3109
+ def kind = MessageKind .PotentialIssue
3110
+ def msg (using Context ) = " New anonymous class definition will be duplicated at each inline site"
3111
+ def explain (using Context ) =
3112
+ i """ Anonymous class will be defined at each use site, which may lead to a larger number of classfiles.
3113
+ |
3114
+ |To inline class definitions, you may provide an explicit class name to avoid this warning. """
3115
+
3107
3116
class ValueDiscarding (tp : Type )(using Context )
3108
3117
extends Message (ValueDiscardingID ):
3109
3118
def kind = MessageKind .PotentialIssue
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import Symbols.*
10
10
import typer .RefChecks
11
11
import MegaPhase .MiniPhase
12
12
import ast .tpd
13
+ import reporting .InlinedAnonClassWarning
13
14
14
15
import config .Feature
15
16
import Decorators .*
@@ -51,10 +52,21 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
51
52
else cpy.ValDef (tree)(rhs = trivialErasedTree(tree.rhs))
52
53
53
54
override def transformDefDef (tree : DefDef )(using Context ): Tree =
55
+ checkNoInlineAnnoClasses(tree)
54
56
checkErasedInExperimental(tree.symbol)
55
57
if ! tree.symbol.isEffectivelyErased || tree.rhs.isEmpty then tree
56
58
else cpy.DefDef (tree)(rhs = trivialErasedTree(tree.rhs))
57
59
60
+ def checkNoInlineAnnoClasses (tree : DefDef )(using Context ): Unit =
61
+ if tree.symbol.is(Inline ) then
62
+ new TreeTraverser {
63
+ def traverse (tree : Tree )(using Context ): Unit =
64
+ tree match
65
+ case tree : TypeDef if tree.symbol.isAnonymousClass =>
66
+ report.warning(new InlinedAnonClassWarning (), tree.symbol.sourcePos)
67
+ case _ => traverseChildren(tree)
68
+ }.traverse(tree)
69
+
58
70
override def transformTypeDef (tree : TypeDef )(using Context ): Tree =
59
71
checkErasedInExperimental(tree.symbol)
60
72
tree
Original file line number Diff line number Diff line change
1
+ -- [E195] Potential Issue Warning: tests/neg/i13044.scala:26:8 ---------------------------------------------------------
2
+ 26 | new Schema[A] {
3
+ | ^
4
+ | New anonymous class definition will be duplicated at each inline site
5
+ |
6
+ | longer explanation available when compiling with `-explain`
7
+ -- [E195] Potential Issue Warning: tests/neg/i13044.scala:32:8 ---------------------------------------------------------
8
+ 32 | new Schema[A] {
9
+ | ^
10
+ | New anonymous class definition will be duplicated at each inline site
11
+ |
12
+ | longer explanation available when compiling with `-explain`
1
13
-- Error: tests/neg/i13044.scala:65:40 ---------------------------------------------------------------------------------
2
14
65 | implicit def typeSchema: Schema[A] = Schema.gen // error // error
3
15
| ^^^^^^^^^^
Original file line number Diff line number Diff line change @@ -13,8 +13,9 @@ object circelike {
13
13
inline final def derived [A ](using conf : Configuration )(using
14
14
inline mirror : Mirror .Of [A ]
15
15
): ConfiguredCodec [A ] =
16
- new ConfiguredCodec [A ]:
16
+ class InlinedConfiguredCodec extends ConfiguredCodec [A ]:
17
17
val codec = summonInline[Codec [URI ]] // simplification
18
+ new InlinedConfiguredCodec
18
19
}
19
20
20
21
object foo {
Original file line number Diff line number Diff line change @@ -24,10 +24,12 @@ object Schema {
24
24
inline summonInline[Mirror .Of [A ]] match {
25
25
case m : Mirror .SumOf [A ] =>
26
26
lazy val members = recurse[m.MirroredElemLabels , m.MirroredElemTypes ]()
27
- new Schema [A ] {}
27
+ class InlinedSchema extends Schema [A ] {} // avoid inline anon class warning
28
+ new InlinedSchema
28
29
case m : Mirror .ProductOf [A ] =>
29
30
lazy val fields = recurse[m.MirroredElemLabels , m.MirroredElemTypes ]()
30
- new Schema [A ] {}
31
+ class InlinedSchema2 extends Schema [A ] {} // avoid inline anon class warning
32
+ new InlinedSchema2
31
33
}
32
34
33
35
inline given gen [A ]: Schema [A ] = derived[A ]
Original file line number Diff line number Diff line change @@ -113,12 +113,14 @@ object Show:
113
113
114
114
inline def show [T ](x : T ): String = summonInline[Show [T ]].show(x)
115
115
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
+ }
120
122
}
121
- }
123
+ new InlinedShow
122
124
123
125
transparent inline def showProduct [T ](x : Product , m : Mirror .ProductOf [T ]): String =
124
126
constValue[m.MirroredLabel ] + showElems[m.MirroredElemTypes , m.MirroredElemLabels ](0 , Nil )(x)
Original file line number Diff line number Diff line change @@ -247,7 +247,7 @@ package foo.test.i16679a:
247
247
import scala .deriving .Mirror
248
248
object CaseClassByStringName :
249
249
inline final def derived [A ](using inline A : Mirror .Of [A ]): CaseClassByStringName [A ] =
250
- new CaseClassByStringName [A ]:
250
+ new CaseClassByStringName [A ]: // warn
251
251
def name : String = A .toString
252
252
253
253
object secondPackage :
@@ -263,7 +263,7 @@ package foo.test.i16679b:
263
263
object CaseClassName :
264
264
import scala .deriving .Mirror
265
265
inline final def derived [A ](using inline A : Mirror .Of [A ]): CaseClassName [A ] =
266
- new CaseClassName [A ]:
266
+ new CaseClassName [A ]: // warn
267
267
def name : String = A .toString
268
268
269
269
object Foo :
@@ -279,7 +279,7 @@ package foo.test.i17156:
279
279
package a:
280
280
trait Foo [A ]
281
281
object Foo :
282
- inline def derived [T ]: Foo [T ] = new Foo {}
282
+ inline def derived [T ]: Foo [T ] = new Foo {} // warn
283
283
284
284
package b:
285
285
import a .Foo
Original file line number Diff line number Diff line change @@ -49,11 +49,11 @@ package foo.unused.summon.inlines:
49
49
50
50
transparent inline given conflictInside : C =
51
51
summonInline[A ]
52
- new {}
52
+ ???
53
53
54
54
transparent inline given potentialConflict : C =
55
55
summonInline[B ]
56
- new {}
56
+ ???
57
57
58
58
val b : B = summon[B ]
59
59
val c : C = summon[C ]
Original file line number Diff line number Diff line change
1
+ inline def foo =
2
+ class NotAnon
3
+ new Object {} // warn
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments