@@ -3567,13 +3567,15 @@ object Parsers {
3567
3567
syntaxError(i " extension clause can only define methods " , stat.span)
3568
3568
}
3569
3569
3570
- /** GivenDef ::= [GivenParams] Type ['as' id] ‘=’ Expr
3570
+ /** GivenDef ::= [GivenParams | `=>` ] Type ['as' id] ‘=’ Expr
3571
3571
* | [GivenParams] ConstrApps ['as' id] [TemplateBody]
3572
- * GivenParams ::= [DefTypeParamClause '=>'] {FunArgTypes '=>'}
3572
+ * GivenParams ::= [DefTypeParamClause '=>'] {GivenParamClause '=>'}
3573
+ * GivenParamClause ::= `(` DefParams `)` | FunArgTypes
3573
3574
*/
3574
3575
def givenDef (start : Offset , mods : Modifiers , givenMod : Mod ) = atSpan(start, nameStart) {
3575
3576
var mods1 = addMod(mods, givenMod)
3576
3577
val start = in.offset
3578
+ var defOnly = false
3577
3579
val (name, tparams, vparamss, parents) = if followingIsGivenSig() then
3578
3580
val name = if isIdent then ident() else EmptyTermName
3579
3581
val tparams = typeParamClauseOpt(ParamOwner .Def )
@@ -3588,28 +3590,35 @@ object Parsers {
3588
3590
(name, tparams, vparamss, parents)
3589
3591
else if true then
3590
3592
val tparams = typeParamClauseOpt(ParamOwner .Def )
3591
- if tparams.nonEmpty then accept(ARROW )
3592
- var counter = 0
3593
- def nextIdx = { counter += 1 ; counter }
3594
- def givenHeadRest (params : List [Tree ], mods : Modifiers ): (List [List [ValDef ]], List [Tree ]) =
3595
- accept(ARROW )
3596
- val vparams = params match
3597
- case (_ : ValDef ) :: _ =>
3598
- params.asInstanceOf [List [ValDef ]].map(_.withFlags(Param | Given ))
3599
- case _ =>
3600
- params.map(makeSyntheticParameter(nextIdx, _, Param | Synthetic | Given ))
3601
- val (vparamss1, parents) = givenHead()
3602
- (vparams :: vparamss1, parents)
3603
- def givenHeadFinish (t : Tree ): (List [List [ValDef ]], List [Tree ]) =
3604
- (Nil , constrAppsRest(constrAppRest(t), commaOK = true ))
3605
- def givenHead (): (List [List [ValDef ]], List [Tree ]) =
3606
- if in.token == LPAREN then
3607
- maybeParams(paramClause(_), givenHeadFinish, givenHeadRest)
3593
+ val (vparamss, parents) =
3594
+ if tparams.isEmpty && in.token == ARROW then
3595
+ defOnly = true
3596
+ in.nextToken()
3597
+ (Nil , constrApps(commaOK = true ))
3608
3598
else
3609
- val constr = constrOrSimpleType()
3610
- if in.token == ARROW then givenHeadRest(constr :: Nil , EmptyModifiers )
3611
- else givenHeadFinish(constr)
3612
- val (vparamss, parents) = givenHead()
3599
+ if tparams.nonEmpty then accept(ARROW )
3600
+ var counter = 0
3601
+ def nextIdx = { counter += 1 ; counter }
3602
+ def givenHeadRest (params : List [Tree ], mods : Modifiers ): (List [List [ValDef ]], List [Tree ]) =
3603
+ accept(ARROW )
3604
+ val vparams = params match
3605
+ case (_ : ValDef ) :: _ =>
3606
+ params.asInstanceOf [List [ValDef ]].map(_.withFlags(Param | Given ))
3607
+ case _ =>
3608
+ params.map(makeSyntheticParameter(nextIdx, _, Param | Synthetic | Given ))
3609
+ val (vparamss1, parents) = givenHead()
3610
+ (vparams :: vparamss1, parents)
3611
+ def givenHeadFinish (t : Tree ): (List [List [ValDef ]], List [Tree ]) =
3612
+ (Nil , constrAppsRest(constrAppRest(t), commaOK = true ))
3613
+ def givenHead (): (List [List [ValDef ]], List [Tree ]) =
3614
+ if in.token == LPAREN then
3615
+ maybeParams(paramClause(_), givenHeadFinish, givenHeadRest)
3616
+ else
3617
+ val constr = constrOrSimpleType()
3618
+ if in.token == ARROW then givenHeadRest(constr :: Nil , EmptyModifiers )
3619
+ else givenHeadFinish(constr)
3620
+ givenHead()
3621
+ end if
3613
3622
newLinesOptWhenFollowedBy(nme.as)
3614
3623
val name =
3615
3624
if in.isIdent(nme.as) then
@@ -3620,10 +3629,12 @@ object Parsers {
3620
3629
else
3621
3630
(EmptyTermName , Nil , Nil , constrApps(commaOK = true ))
3622
3631
val gdef =
3623
- if in.token == EQUALS && parents.length == 1 && parents.head.isType then
3632
+ if defOnly || in.token == EQUALS then
3633
+ if parents.length != 1 || ! parents.head.isType then
3634
+ syntaxError(em " Given parent is not a type. It cannot be defined with an alias " )
3624
3635
accept(EQUALS )
3625
3636
mods1 |= Final
3626
- if tparams.isEmpty && vparamss.isEmpty && ! mods.is(Inline ) then
3637
+ if tparams.isEmpty && vparamss.isEmpty && ! mods.is(Inline ) && ! defOnly then
3627
3638
mods1 |= Lazy
3628
3639
ValDef (name, parents.head, subExpr())
3629
3640
else
0 commit comments