Skip to content

Commit 09d5589

Browse files
nicolasstuckiTheElectronWill
authored andcommitted
Avoid instrumentation of inline and erased definitions
These methods will not generate code and therefore do not need to be instrumented. Note that a retained inline method will have a `$retained` variant that will be instrumented. Found this issue while looking into #15490.
1 parent c66b8dc commit 09d5589

File tree

7 files changed

+258
-107
lines changed

7 files changed

+258
-107
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,22 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
132132
cpy.ValDef(tree)(rhs = rhs)
133133

134134
case tree: DefDef =>
135-
// Only transform the params (for the default values) and the rhs.
136-
val paramss = transformParamss(tree.paramss)
137-
val rhs = transform(tree.rhs)
138-
val finalRhs =
139-
if canInstrumentDefDef(tree) then
140-
// Ensure that the rhs is always instrumented, if possible
141-
instrumentBody(tree, rhs)
142-
else
143-
rhs
144-
cpy.DefDef(tree)(tree.name, paramss, tree.tpt, finalRhs)
145-
135+
if tree.symbol.isOneOf(Inline | Erased) then
136+
// Inline and erased definitions will not be in the generated code and therefore do not need to be instrumented.
137+
// Note that a retained inline method will have a `$retained` variant that will be instrumented.
138+
tree
139+
else
140+
// Only transform the params (for the default values) and the rhs.
141+
val paramss = transformParamss(tree.paramss)
142+
val rhs = transform(tree.rhs)
143+
val finalRhs =
144+
if canInstrumentDefDef(tree) then
145+
// Ensure that the rhs is always instrumented, if possible
146+
instrumentBody(tree, rhs)
147+
else
148+
rhs
149+
cpy.DefDef(tree)(tree.name, paramss, tree.tpt, finalRhs)
150+
end if
146151
case tree: PackageDef =>
147152
// only transform the statements of the package
148153
cpy.PackageDef(tree)(tree.pid, transform(tree.stats))
@@ -336,4 +341,4 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
336341

337342
object InstrumentCoverage:
338343
val name: String = "instrumentCoverage"
339-
val description: String = "instrument code for coverage cheking"
344+
val description: String = "instrument code for coverage checking"

compiler/test/dotty/tools/dotc/coverage/CoverageTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CoverageTests:
5656
val expected = fixWindowsPaths(Files.readAllLines(expectFile).asScala)
5757
val obtained = fixWindowsPaths(Files.readAllLines(targetFile).asScala)
5858
if expected != obtained then
59+
// FIXME: zip will drop part of the output if one is shorter (i.e. will not print anything of one is a refix of the other)
5960
for ((exp, actual),i) <- expected.zip(obtained).filter(_ != _).zipWithIndex do
6061
Console.err.println(s"wrong line ${i+1}:")
6162
Console.err.println(s" expected: $exp")

tests/coverage/pos/Inlined.scoverage.check

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -273,71 +273,3 @@ false
273273
false
274274
def testInlined
275275

276-
15
277-
Inlined.scala
278-
covtest
279-
Inlined$package$
280-
Object
281-
covtest.Inlined$package$
282-
assert
283-
288
284-
315
285-
10
286-
Scala3RunTime
287-
Select
288-
false
289-
0
290-
false
291-
scala.runtime.Scala3RunTime
292-
293-
16
294-
Inlined.scala
295-
covtest
296-
Inlined$package$
297-
Object
298-
covtest.Inlined$package$
299-
assert
300-
288
301-
330
302-
10
303-
assertFailed
304-
Apply
305-
false
306-
0
307-
false
308-
scala.runtime.Scala3RunTime.assertFailed()
309-
310-
17
311-
Inlined.scala
312-
covtest
313-
Inlined$package$
314-
Object
315-
covtest.Inlined$package$
316-
assert
317-
288
318-
330
319-
10
320-
<none>
321-
Block
322-
true
323-
0
324-
false
325-
scala.runtime.Scala3RunTime.assertFailed()
326-
327-
18
328-
Inlined.scala
329-
covtest
330-
Inlined$package$
331-
Object
332-
covtest.Inlined$package$
333-
assert
334-
202
335-
231
336-
9
337-
assert
338-
DefDef
339-
false
340-
0
341-
false
342-
transparent inline def assert
343-

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@ erased/test.scala
2424
test$package$
2525
Object
2626
<empty>.test$package$
27-
e
28-
54
29-
66
30-
2
31-
e
32-
DefDef
33-
false
34-
0
35-
false
36-
erased def e
37-
38-
1
39-
erased/test.scala
40-
<empty>
41-
test$package$
42-
Object
43-
<empty>.test$package$
4427
foo
4528
149
4629
162
@@ -52,7 +35,7 @@ false
5235
false
5336
s"foo(a)($b)"
5437

55-
2
38+
1
5639
erased/test.scala
5740
<empty>
5841
test$package$
@@ -69,7 +52,7 @@ false
6952
false
7053
println(s"foo(a)($b)")
7154

72-
3
55+
2
7356
erased/test.scala
7457
<empty>
7558
test$package$
@@ -86,7 +69,7 @@ false
8669
false
8770
def foo
8871

89-
4
72+
3
9073
erased/test.scala
9174
<empty>
9275
test$package$
@@ -103,7 +86,7 @@ false
10386
false
10487
s"identity($s)"
10588

106-
5
89+
4
10790
erased/test.scala
10891
<empty>
10992
test$package$
@@ -120,7 +103,7 @@ false
120103
false
121104
println(s"identity($s)")
122105

123-
6
106+
5
124107
erased/test.scala
125108
<empty>
126109
test$package$
@@ -137,7 +120,7 @@ false
137120
false
138121
def identity
139122

140-
7
123+
6
141124
erased/test.scala
142125
<empty>
143126
test$package$
@@ -154,7 +137,7 @@ false
154137
false
155138
e("a")
156139

157-
8
140+
7
158141
erased/test.scala
159142
<empty>
160143
test$package$
@@ -171,7 +154,7 @@ false
171154
false
172155
foo(e("a"))("b")
173156

174-
9
157+
8
175158
erased/test.scala
176159
<empty>
177160
test$package$
@@ -188,7 +171,7 @@ false
188171
false
189172
identity("idem")
190173

191-
10
174+
9
192175
erased/test.scala
193176
<empty>
194177
test$package$
@@ -205,7 +188,7 @@ false
205188
false
206189
foo(e("a"))(identity("idem"))
207190

208-
11
191+
10
209192
erased/test.scala
210193
<empty>
211194
test$package$
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
foo
3+
bar
4+
foo
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
abstract class B:
2+
val x: Int
3+
def foo: String
4+
5+
class A extends B:
6+
inline val x = 1
7+
inline val y = 2
8+
inline def foo: String = "foo".toString
9+
inline def bar: String = "bar".toString
10+
11+
@main
12+
def Test: Unit =
13+
val a = A()
14+
println(a.x)
15+
println(a.foo)
16+
println(a.bar)
17+
val b: B = a
18+
println(b.foo)

0 commit comments

Comments
 (0)