Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage: Do not lift applications of context functions #18498

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Loading