Skip to content

Commit

Permalink
Coverage: Do not lift applications of context functions
Browse files Browse the repository at this point in the history
closes lampepfl#16502
  • Loading branch information
KacperFKorban committed Sep 1, 2023
1 parent 4c77f62 commit 3e7dae8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,13 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
* they shouldn't be lifted.
*/
val sym = fun.symbol
sym.exists && (isShortCircuitedOp(sym) || StringInterpolatorOpt.isCompilerIntrinsic(sym) || sym == defn.Object_synchronized)
end
sym.exists && (
isShortCircuitedOp(sym)
|| StringInterpolatorOpt.isCompilerIntrinsic(sym)
|| sym == defn.Object_synchronized
|| isContextFunctionApply(fun)
)
end isUnliftableFun

val fun = tree.fun
val nestedApplyNeedsLift = fun match
Expand All @@ -463,6 +468,12 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
nestedApplyNeedsLift ||
!isUnliftableFun(fun) && !tree.args.isEmpty && !tree.args.forall(LiftCoverage.noLift)

private def isContextFunctionApply(fun: Tree)(using Context): Boolean =
fun match
case Select(prefix, nme.apply) =>
defn.isContextFunctionType(prefix.tpe.widen)
case _ => false

/** Check if an Apply can be instrumented. Prevents this phase from generating incorrect code. */
private def canInstrumentApply(tree: Apply)(using Context): Boolean =
def isSecondaryCtorDelegateCall: Boolean = tree.fun match
Expand Down
8 changes: 8 additions & 0 deletions tests/coverage/pos/i16502.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.concurrent.*

def asyncSum: ExecutionContext ?=> Future[Int] = Future(1)

@main
def Test(): Unit =
import scala.concurrent.ExecutionContext.Implicits.global
asyncSum
88 changes: 88 additions & 0 deletions tests/coverage/pos/i16502.scoverage.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Coverage data, format version: 3.0
# Statement data:
# - id
# - source path
# - package name
# - class name
# - class type (Class, Object or Trait)
# - full class name
# - method name
# - start offset
# - end offset
# - line number
# - symbol name
# - tree name
# - is branch
# - invocations count
# - is ignored
# - description (can be multi-line)
# ' ' sign
# ------------------------------------------
0
i16502.scala
<empty>
i16502$package$
Object
<empty>.i16502$package$
$anonfun
76
85
2
apply
Apply
false
0
false
Future(1)

1
i16502.scala
<empty>
i16502$package$
Object
<empty>.i16502$package$
asyncSum
27
39
2
asyncSum
DefDef
false
0
false
def asyncSum

2
i16502.scala
<empty>
i16502$package$
Object
<empty>.i16502$package$
Test
174
182
7
apply
Apply
false
0
false
asyncSum

3
i16502.scala
<empty>
i16502$package$
Object
<empty>.i16502$package$
Test
87
101
5
Test
DefDef
false
0
false
@main\ndef Test

0 comments on commit 3e7dae8

Please sign in to comment.