Skip to content

Commit a4b0c70

Browse files
committed
Fix curried conditional given instances
1 parent 0e5d3bf commit a4b0c70

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,10 +3468,12 @@ object Parsers {
34683468
stats.foreach(checkExtensionMethod(tparams, _))
34693469
ModuleDef(name, Template(makeConstructor(tparams, vparamss), Nil, Nil, self, stats))
34703470
else
3471+
def makeGiven(params: List[ValDef]): List[ValDef] =
3472+
params.map(param => param.withMods(param.mods | Given))
34713473
def conditionalParents(): List[Tree] =
34723474
accept(ARROW)
34733475
if in.token == LPAREN && followingIsParam() then
3474-
vparamss = vparamss :+ paramClause(vparamss.flatten.length)
3476+
vparamss = vparamss :+ makeGiven(paramClause(vparamss.flatten.length))
34753477
conditionalParents()
34763478
else
34773479
val constrs = constrApps(commaOK = true, templateCanFollow = true)
@@ -3493,7 +3495,7 @@ object Parsers {
34933495
in.nextToken()
34943496
TypeBoundsTree(EmptyTree, annotType()) :: Nil
34953497
else if isConditional then
3496-
vparamss = vparamss.head.map(param => param.withMods(param.mods | Given)) :: Nil
3498+
vparamss = vparamss.map(makeGiven)
34973499
conditionalParents()
34983500
else
34993501
if !hasLabel && !(name.isEmpty && tparams.isEmpty && vparamss.isEmpty) then

tests/run/i7788.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait Show[-A] with
2+
def show(a:A): String
3+
4+
given Show[String] = x => x
5+
given Show[Int] = _.toString
6+
7+
given showEither[A,B]: (sA: Show[A]) => Show[B] => Show[Either[A,B]] =
8+
_.fold(a => s"Left(${summon[Show[A]].show(a)})", _ => "Right(Ignored value of B)")
9+
given [A,B]: (sA: Show[A]) => (sB: Show[B]) => Show[(A,B)] = (a,b) => s"(${sA.show(a)}), ${sB.show(b)})"
10+
11+
@main def ShowDemo =
12+
println(summon[Show[(Int, String)]].show(0 -> "hello"))
13+
println(summon[Show[Either[Int, String]]].show(Left(-1)))
14+
println(summon[Show[Either[Int, String]]].show(Right("success message")))

0 commit comments

Comments
 (0)