Skip to content

Commit 442141d

Browse files
authored
Merge pull request #543 from scala/backport-lts-3.3-23641
Backport "Handle default arguments in named parameters for inlay hints" to 3.3 LTS
2 parents 1b4ce46 + 5630085 commit 442141d

File tree

11 files changed

+69
-107
lines changed

11 files changed

+69
-107
lines changed

presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import dotty.tools.dotc.core.Contexts.Context
1919
import dotty.tools.dotc.core.Flags
2020
import dotty.tools.dotc.core.NameOps.fieldName
2121
import dotty.tools.dotc.core.Names.Name
22+
import dotty.tools.dotc.core.NameKinds.DefaultGetterName
2223
import dotty.tools.dotc.core.StdNames.*
2324
import dotty.tools.dotc.core.Symbols.*
2425
import dotty.tools.dotc.core.Types.*
@@ -457,6 +458,13 @@ object Parameters:
457458
case TypeApply(fun, _) => getUnderlyingFun(fun)
458459
case t => t
459460

461+
@tailrec
462+
def isDefaultArg(arg: Tree): Boolean = arg match
463+
case Ident(name) => name.is(DefaultGetterName)
464+
case Select(_, name) => name.is(DefaultGetterName)
465+
case Apply(fun, _) => isDefaultArg(fun)
466+
case _ => false
467+
460468
if (params.namedParameters() || params.byNameParameters()) then
461469
tree match
462470
case Apply(fun, args) if isRealApply(fun) =>
@@ -467,15 +475,16 @@ object Parameters:
467475
val funTp = fun.typeOpt.widenTermRefExpr
468476
val paramNames = funTp.paramNamess.flatten
469477
val paramInfos = funTp.paramInfoss.flatten
478+
470479
Some(
471-
// Check if the function is an infix function or the underlying function is an infix function
472480
isInfixFun(fun, args) || underlyingFun.isInfix,
473481
(
474482
args
475483
.zip(paramNames)
476484
.zip(paramInfos)
477485
.collect {
478-
case ((arg, paramName), paramInfo) if !arg.span.isZeroExtent => (paramName.fieldName, arg.sourcePos, paramInfo.isByName)
486+
case ((arg, paramName), paramInfo) if !arg.span.isZeroExtent && !isDefaultArg(arg) =>
487+
(paramName.fieldName, arg.sourcePos, paramInfo.isByName)
479488
}
480489
)
481490
)

presentation-compiler/test/dotty/tools/pc/tests/inlayHints/InlayHintsSuite.scala

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,4 +1243,61 @@ class InlayHintsSuite extends BaseInlayHintsSuite {
12431243
|}
12441244
|""".stripMargin
12451245
)
1246+
1247+
@Test def `default-parameter` =
1248+
check(
1249+
"""|object Main {
1250+
| def foo(a: Int, b: Int = 2) = a + b
1251+
| val x = foo(1)
1252+
|}
1253+
|""".stripMargin,
1254+
"""|object Main {
1255+
| def foo(a: Int, b: Int = 2)/*: Int<<scala/Int#>>*/ = a + b
1256+
| val x/*: Int<<scala/Int#>>*/ = foo(/*a = */1)
1257+
|}
1258+
|""".stripMargin
1259+
)
1260+
1261+
@Test def `default-parameter-2` =
1262+
check(
1263+
"""|object Main {
1264+
| def foo(a: Int = 10, b: Int = 2) = a + b
1265+
| val x = foo(b = 1)
1266+
|}
1267+
|""".stripMargin,
1268+
"""|object Main {
1269+
| def foo(a: Int = 10, b: Int = 2)/*: Int<<scala/Int#>>*/ = a + b
1270+
| val x/*: Int<<scala/Int#>>*/ = foo(b = 1)
1271+
|}
1272+
|""".stripMargin
1273+
)
1274+
1275+
@Test def `default-parameter-3` =
1276+
check(
1277+
"""|object Main {
1278+
| def foo(a: Int, b: Int = 2, c: Int) = a + b + c
1279+
| val x = foo(a = 1, c = 2)
1280+
|}
1281+
|""".stripMargin,
1282+
"""|object Main {
1283+
| def foo(a: Int, b: Int = 2, c: Int)/*: Int<<scala/Int#>>*/ = a + b + c
1284+
| val x/*: Int<<scala/Int#>>*/ = foo(a = 1, c = 2)
1285+
|}
1286+
|""".stripMargin
1287+
)
1288+
1289+
@Test def `default-parameter-4` =
1290+
check(
1291+
"""|object Main {
1292+
| def foo(a: Int, b: Int = 2, c: Int) = a + b + c
1293+
| val x = foo(1, 2, 3)
1294+
|}
1295+
|""".stripMargin,
1296+
"""|object Main {
1297+
| def foo(a: Int, b: Int = 2, c: Int)/*: Int<<scala/Int#>>*/ = a + b + c
1298+
| val x/*: Int<<scala/Int#>>*/ = foo(/*a = */1, /*b = */2, /*c = */3)
1299+
|}
1300+
|""".stripMargin
1301+
)
1302+
12461303
}

tests/neg-macros/i22616b.check

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/neg-macros/i22616b.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/run-macros/i22616c.check

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/run-macros/i22616c/Macro_4.scala

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/run-macros/i22616c/MyTypeRepr_3.scala

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/run-macros/i22616c/SealedTrait3_2.scala

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/run-macros/i22616c/Test_5.scala

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/run-macros/i22616c/caseName_1.scala

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)