Skip to content

Commit fafeb51

Browse files
committed
Allow vals in using clauses of givens
1 parent d21aa2f commit fafeb51

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ object Parsers {
6262
case ExtensionFollow // extension clause, following extension parameter
6363

6464
def isClass = // owner is a class
65-
this == Class || this == CaseClass
65+
this == Class || this == CaseClass || this == Given
6666
def takesOnlyUsingClauses = // only using clauses allowed for this owner
6767
this == Given || this == ExtensionFollow
6868
def acceptsVariance =
@@ -3291,7 +3291,7 @@ object Parsers {
32913291
val isAbstractOwner = paramOwner == ParamOwner.Type || paramOwner == ParamOwner.TypeParam
32923292
val start = in.offset
32933293
var mods = annotsAsMods() | Param
3294-
if paramOwner == ParamOwner.Class || paramOwner == ParamOwner.CaseClass then
3294+
if paramOwner.isClass then
32953295
mods |= PrivateLocal
32963296
if isIdent(nme.raw.PLUS) && checkVarianceOK() then
32973297
mods |= Covariant
@@ -4011,6 +4011,14 @@ object Parsers {
40114011
val nameStart = in.offset
40124012
val name = if isIdent && followingIsGivenSig() then ident() else EmptyTermName
40134013

4014+
// TODO Change syntax description
4015+
def adjustDefParams(paramss: List[ParamClause]): List[ParamClause] =
4016+
paramss.nestedMap: param =>
4017+
if !param.mods.isAllOf(PrivateLocal) then
4018+
syntaxError(em"method parameter ${param.name} may not be `a val`", param.span)
4019+
param.withMods(param.mods &~ (AccessFlags | ParamAccessor | Mutable) | Param)
4020+
.asInstanceOf[List[ParamClause]]
4021+
40144022
val gdef =
40154023
val tparams = typeParamClauseOpt(ParamOwner.Given)
40164024
newLineOpt()
@@ -4032,16 +4040,17 @@ object Parsers {
40324040
mods1 |= Lazy
40334041
ValDef(name, parents.head, subExpr())
40344042
else
4035-
DefDef(name, joinParams(tparams, vparamss), parents.head, subExpr())
4043+
DefDef(name, adjustDefParams(joinParams(tparams, vparamss)), parents.head, subExpr())
40364044
else if (isStatSep || isStatSeqEnd) && parentsIsType then
40374045
if name.isEmpty then
40384046
syntaxError(em"anonymous given cannot be abstract")
4039-
DefDef(name, joinParams(tparams, vparamss), parents.head, EmptyTree)
4047+
DefDef(name, adjustDefParams(joinParams(tparams, vparamss)), parents.head, EmptyTree)
40404048
else
4041-
val tparams1 = tparams.map(tparam => tparam.withMods(tparam.mods | PrivateLocal))
4042-
val vparamss1 = vparamss.map(_.map(vparam =>
4043-
vparam.withMods(vparam.mods &~ Param | ParamAccessor | Protected)))
4044-
val constr = makeConstructor(tparams1, vparamss1)
4049+
val vparamss1 = vparamss.nestedMap: vparam =>
4050+
if vparam.mods.is(Private)
4051+
then vparam.withMods(vparam.mods &~ PrivateLocal | Protected)
4052+
else vparam
4053+
val constr = makeConstructor(tparams, vparamss1)
40454054
val templ =
40464055
if isStatSep || isStatSeqEnd then Template(constr, parents, Nil, EmptyValDef, Nil)
40474056
else withTemplate(constr, parents)

0 commit comments

Comments
 (0)