Skip to content

Commit c3424ce

Browse files
Coverage: exclude all Erased method calls
1 parent 32297ad commit c3424ce

File tree

4 files changed

+58
-55
lines changed

4 files changed

+58
-55
lines changed

compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import java.io.File
55

66
import ast.tpd.*
77
import collection.mutable
8-
import core.Decorators.i
98
import core.Flags.*
109
import core.Contexts.{Context, ctx, inContext}
1110
import core.DenotTransformers.IdentityDenotTransformer
@@ -18,18 +17,17 @@ import typer.LiftCoverage
1817
import util.SourcePosition
1918
import util.Spans.Span
2019
import localopt.StringInterpolatorOpt
21-
import scala.util.chaining.*
2220

2321
/** Implements code coverage by inserting calls to scala.runtime.coverage.Invoker
2422
* ("instruments" the source code).
2523
* The result can then be consumed by the Scoverage tool.
2624
*/
2725
class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
28-
import InstrumentCoverage.{name, description, InstrumentedParts}
26+
import InstrumentCoverage.{InstrumentedParts, ExcludeMethodFlags}
2927

30-
override def phaseName = name
28+
override def phaseName = InstrumentCoverage.name
3129

32-
override def description = description
30+
override def description = InstrumentCoverage.description
3331

3432
// Enabled by argument "-coverage-out OUTPUT_DIR"
3533
override def isEnabled(using ctx: Context) =
@@ -412,7 +410,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
412410
/** Check if an Apply can be instrumented. Prevents this phase from generating incorrect code. */
413411
private def canInstrumentApply(tree: Apply)(using Context): Boolean =
414412
val sym = tree.symbol
415-
!sym.isOneOf(Synthetic | Artifact)
413+
!sym.isOneOf(ExcludeMethodFlags)
416414
&& !isCompilerIntrinsicMethod(sym)
417415
&& (tree.typeOpt match
418416
case AppliedType(tycon: NamedType, _) =>
@@ -446,7 +444,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
446444
* in post-erasure checking.
447445
*/
448446
private def canInstrumentParameterless(sym: Symbol)(using Context): Boolean =
449-
sym.is(Method, butNot = Synthetic | Artifact)
447+
sym.is(Method, butNot = ExcludeMethodFlags)
450448
&& sym.info.isParameterless
451449
&& !isCompilerIntrinsicMethod(sym)
452450
&& !sym.info.typeSymbol.name.isContextFunction // exclude context functions like in canInstrumentApply
@@ -466,6 +464,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
466464
object InstrumentCoverage:
467465
val name: String = "instrumentCoverage"
468466
val description: String = "instrument code for coverage checking"
467+
val ExcludeMethodFlags: FlagSet = Synthetic | Artifact | Erased
469468

470469
/**
471470
* An instrumented Tree, in 3 parts.

tests/coverage/run/erased/test.check

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
foo(a)(b)
2+
foo(a)(b)
23
identity(idem)
3-
foo(a)(idem)
4+
foo(a)(idem)

tests/coverage/run/erased/test.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import scala.language.experimental.erasedDefinitions
22

3+
erased def parameterless: String = "y"
4+
35
erased def e(x: String): String = "x"
46
def foo(erased a: String)(b: String): String =
57
println(s"foo(a)($b)")
@@ -11,5 +13,6 @@ def identity(s: String): String =
1113

1214
@main
1315
def Test: Unit =
16+
foo(parameterless)("b")
1417
foo(e("a"))("b")
1518
foo(e("a"))(identity("idem"))

tests/coverage/run/erased/test.scoverage.check

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ test$package$
2525
Object
2626
<empty>.test$package$
2727
foo
28-
149
29-
162
30-
4
31-
s
28+
181
29+
203
30+
6
31+
println
3232
Apply
3333
false
3434
0
3535
false
36-
s"foo(a)($b)"
36+
println(s"foo(a)($b)")
3737

3838
1
3939
erased/test.scala
@@ -42,15 +42,15 @@ test$package$
4242
Object
4343
<empty>.test$package$
4444
foo
45-
141
46-
163
47-
4
48-
println
45+
189
46+
202
47+
6
48+
s
4949
Apply
5050
false
5151
0
5252
false
53-
println(s"foo(a)($b)")
53+
s"foo(a)($b)"
5454

5555
2
5656
erased/test.scala
@@ -59,9 +59,9 @@ test$package$
5959
Object
6060
<empty>.test$package$
6161
foo
62-
92
63-
99
64-
3
62+
132
63+
139
64+
5
6565
foo
6666
DefDef
6767
false
@@ -76,15 +76,15 @@ test$package$
7676
Object
7777
<empty>.test$package$
7878
identity
79-
213
80-
228
81-
8
82-
s
79+
245
80+
269
81+
10
82+
println
8383
Apply
8484
false
8585
0
8686
false
87-
s"identity($s)"
87+
println(s"identity($s)")
8888

8989
4
9090
erased/test.scala
@@ -93,15 +93,15 @@ test$package$
9393
Object
9494
<empty>.test$package$
9595
identity
96-
205
97-
229
98-
8
99-
println
96+
253
97+
268
98+
10
99+
s
100100
Apply
101101
false
102102
0
103103
false
104-
println(s"identity($s)")
104+
s"identity($s)"
105105

106106
5
107107
erased/test.scala
@@ -110,9 +110,9 @@ test$package$
110110
Object
111111
<empty>.test$package$
112112
identity
113-
169
114-
181
115-
7
113+
209
114+
221
115+
9
116116
identity
117117
DefDef
118118
false
@@ -127,15 +127,15 @@ test$package$
127127
Object
128128
<empty>.test$package$
129129
Test
130-
264
131-
270
132-
13
133-
e
130+
300
131+
323
132+
15
133+
foo
134134
Apply
135135
false
136136
0
137137
false
138-
e("a")
138+
foo(parameterless)("b")
139139

140140
7
141141
erased/test.scala
@@ -144,9 +144,9 @@ test$package$
144144
Object
145145
<empty>.test$package$
146146
Test
147-
260
148-
276
149-
13
147+
326
148+
342
149+
16
150150
foo
151151
Apply
152152
false
@@ -161,15 +161,15 @@ test$package$
161161
Object
162162
<empty>.test$package$
163163
Test
164-
291
165-
307
166-
14
167-
identity
164+
345
165+
374
166+
17
167+
foo
168168
Apply
169169
false
170170
0
171171
false
172-
identity("idem")
172+
foo(e("a"))(identity("idem"))
173173

174174
9
175175
erased/test.scala
@@ -178,15 +178,15 @@ test$package$
178178
Object
179179
<empty>.test$package$
180180
Test
181-
279
182-
308
183-
14
184-
foo
181+
357
182+
373
183+
17
184+
identity
185185
Apply
186186
false
187187
0
188188
false
189-
foo(e("a"))(identity("idem"))
189+
identity("idem")
190190

191191
10
192192
erased/test.scala
@@ -195,9 +195,9 @@ test$package$
195195
Object
196196
<empty>.test$package$
197197
Test
198-
235
199-
249
200-
12
198+
275
199+
289
200+
14
201201
Test
202202
DefDef
203203
false

0 commit comments

Comments
 (0)