Skip to content

Commit c3f9d62

Browse files
authored
bugfix: Fix issues with Scala JS and coverage (#24769)
Tested it with the reproduction in #20255 Fixes #20255
1 parent 402be90 commit c3f9d62

File tree

3 files changed

+443
-0
lines changed

3 files changed

+443
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import core.Contexts.{Context, ctx, inContext}
1010
import core.DenotTransformers.IdentityDenotTransformer
1111
import core.Symbols.{defn, Symbol}
1212
import core.Constants.Constant
13+
import core.NameKinds.DefaultGetterName
1314
import core.NameOps.isContextFunction
1415
import core.StdNames.nme
1516
import core.Types.*
@@ -554,6 +555,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
554555
!sym.isOneOf(ExcludeMethodFlags)
555556
&& !isCompilerIntrinsicMethod(sym)
556557
&& !(sym.isClassConstructor && isSecondaryCtorDelegateCall)
558+
&& !sym.name.is(DefaultGetterName) // https://github.com/scala/scala3/issues/20255
557559
&& (tree.typeOpt match
558560
case AppliedType(tycon: NamedType, _) =>
559561
/* If the last expression in a block is a context function, we'll try to
@@ -590,6 +592,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
590592
&& sym.info.isParameterless
591593
&& !isCompilerIntrinsicMethod(sym)
592594
&& !sym.info.typeSymbol.name.isContextFunction // exclude context functions like in canInstrumentApply
595+
&& !sym.name.is(DefaultGetterName) // https://github.com/scala/scala3/issues/20255
593596

594597
/** Does sym refer to a "compiler intrinsic" method, which only exist during compilation,
595598
* like Any.isInstanceOf?
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package covtest
2+
3+
// Test case for default arguments with coverage.
4+
// Default getter methods ($default$N) should not be instrumented/lifted
5+
// as this can cause issues with ScalaJS IR (https://github.com/scala/scala3/issues/20255).
6+
7+
class DefaultArgs:
8+
def methodWithDefaults(
9+
a: String,
10+
b: Int = 0,
11+
c: Boolean = true
12+
): String =
13+
s"$a, $b, $c"
14+
15+
def caller(): String =
16+
// This call uses default arguments for b and c
17+
methodWithDefaults("test")
18+
19+
def callerPartial(): String =
20+
// This call uses default argument only for c
21+
methodWithDefaults("test", 42)
22+
23+
object DefaultArgs:
24+
def staticMethod(x: Int = 10, y: Int = 20): Int =
25+
x + y
26+
27+
def staticCaller(): Int =
28+
staticMethod() + staticMethod(5) + staticMethod(5, 15)
29+

0 commit comments

Comments
 (0)