Skip to content

Commit b0061c4

Browse files
authored
Merge pull request #12001 from dotty-staging/fix-11997
Allow by-name types in using clauses
2 parents 7ca45d2 + d8b8fc1 commit b0061c4

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1324,9 +1324,9 @@ object Parsers {
13241324
* | MatchType
13251325
* | InfixType
13261326
* FunType ::= (MonoFunType | PolyFunType)
1327-
* MonoFunType ::= FunArgTypes (‘=>’ | ‘?=>’) Type
1327+
* MonoFunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
13281328
* PolyFunType ::= HKTypeParamClause '=>' Type
1329-
* FunArgTypes ::= InfixType
1329+
* FunTypeArgs ::= InfixType
13301330
* | `(' [ [ ‘[using]’ ‘['erased'] FunArgType {`,' FunArgType } ] `)'
13311331
* | '(' [ ‘[using]’ ‘['erased'] TypedFunParam {',' TypedFunParam } ')'
13321332
*/
@@ -1364,7 +1364,7 @@ object Parsers {
13641364
else
13651365
Function(params, t)
13661366
}
1367-
def funArgTypesRest(first: Tree, following: () => Tree) = {
1367+
def funTypeArgsRest(first: Tree, following: () => Tree) = {
13681368
val buf = new ListBuffer[Tree] += first
13691369
while (in.token == COMMA) {
13701370
in.nextToken()
@@ -1387,11 +1387,11 @@ object Parsers {
13871387
val ts = funArgType() match {
13881388
case Ident(name) if name != tpnme.WILDCARD && in.isColon() =>
13891389
isValParamList = true
1390-
funArgTypesRest(
1390+
funTypeArgsRest(
13911391
typedFunParam(paramStart, name.toTermName, imods),
13921392
() => typedFunParam(in.offset, ident(), imods))
13931393
case t =>
1394-
funArgTypesRest(t, funArgType)
1394+
funTypeArgsRest(t, funArgType)
13951395
}
13961396
accept(RPAREN)
13971397
if isValParamList || in.isArrow then
@@ -2907,10 +2907,10 @@ object Parsers {
29072907
def typeParamClauseOpt(ownerKind: ParamOwner.Value): List[TypeDef] =
29082908
if (in.token == LBRACKET) typeParamClause(ownerKind) else Nil
29092909

2910-
/** ContextTypes ::= Type {‘,’ Type}
2910+
/** ContextTypes ::= FunArgType {‘,’ FunArgType}
29112911
*/
29122912
def contextTypes(ofClass: Boolean, nparams: Int, impliedMods: Modifiers): List[ValDef] =
2913-
val tps = commaSeparated(typ)
2913+
val tps = commaSeparated(funArgType)
29142914
var counter = nparams
29152915
def nextIdx = { counter += 1; counter }
29162916
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param

docs/docs/internals/syntax.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ Type ::= FunType
162162
| FunParamClause ‘=>>’ Type TermLambdaTypeTree(ps, t)
163163
| MatchType
164164
| InfixType
165-
FunType ::= FunArgTypes (‘=>’ | ‘?=>’) Type Function(ts, t)
165+
FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type Function(ts, t)
166166
| HKTypeParamClause '=>' Type PolyFunction(ps, t)
167-
FunArgTypes ::= InfixType
168-
| ‘(’ [ FunArgType {‘,’ FunArgType } ] ‘)’
167+
FunTypeArgs ::= InfixType
168+
| ‘(’ [ FunArgTypes ] ‘)’
169169
| FunParamClause
170170
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
171171
TypedFunParam ::= id ‘:’ Type
@@ -191,6 +191,7 @@ Singleton ::= SimpleRef
191191
Singletons ::= Singleton { ‘,’ Singleton }
192192
FunArgType ::= Type
193193
| ‘=>’ Type PrefixOp(=>, t)
194+
FunArgTypes ::= FunArgType { ‘,’ FunArgType }
194195
ParamType ::= [‘=>’] ParamValueType
195196
ParamValueType ::= Type [‘*’] PostfixOp(t, "*")
196197
TypeArgs ::= ‘[’ Types ‘]’ ts
@@ -323,15 +324,15 @@ HkTypeParam ::= {Annotation} [‘+’ | ‘-’] (id [HkTypeParamClause]
323324
324325
ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
325326
ClsParamClause ::= [nl] ‘(’ ClsParams ‘)’
326-
| [nl] ‘(’ ‘using’ (ClsParams | Types) ‘)’
327+
| [nl] ‘(’ ‘using’ (ClsParams | FunArgTypes) ‘)’
327328
ClsParams ::= ClsParam {‘,’ ClsParam}
328329
ClsParam ::= {Annotation} ValDef(mods, id, tpe, expr) -- point of mods on val/var
329330
[{Modifier} (‘val’ | ‘var’) | ‘inline’] Param
330331
Param ::= id ‘:’ ParamType [‘=’ Expr]
331332
332333
DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
333334
DefParamClause ::= [nl] ‘(’ DefParams ‘)’ | UsingParamClause
334-
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | Types) ‘)’
335+
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | FunArgTypes) ‘)’
335336
DefParams ::= DefParam {‘,’ DefParam}
336337
DefParam ::= {Annotation} [‘inline’] Param ValDef(mods, id, tpe, expr) -- point of mods at id.
337338
```

docs/docs/reference/syntax.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ Type ::= FunType
161161
| FunParamClause ‘=>>’ Type
162162
| MatchType
163163
| InfixType
164-
FunType ::= FunArgTypes (‘=>’ | ‘?=>’) Type
164+
FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
165165
| HKTypeParamClause '=>' Type
166-
FunArgTypes ::= InfixType
167-
| ‘(’ [ FunArgType {‘,’ FunArgType } ] ‘)’
166+
FunTypeArgs ::= InfixType
167+
| ‘(’ [ FunArgTypes ] ‘)’
168168
| FunParamClause
169169
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
170170
TypedFunParam ::= id ‘:’ Type
@@ -189,6 +189,7 @@ Singleton ::= SimpleRef
189189
190190
FunArgType ::= Type
191191
| ‘=>’ Type
192+
FunArgTypes ::= FunArgType { ‘,’ FunArgType }
192193
ParamType ::= [‘=>’] ParamValueType
193194
ParamValueType ::= Type [‘*’]
194195
TypeArgs ::= ‘[’ Types ‘]’
@@ -311,14 +312,14 @@ HkTypeParam ::= {Annotation} [‘+’ | ‘-’] (id [HkTypeParamClause]
311312
312313
ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
313314
ClsParamClause ::= [nl] ‘(’ ClsParams ‘)’
314-
| [nl] ‘(’ ‘using’ (ClsParams | Types) ‘)’
315+
| [nl] ‘(’ ‘using’ (ClsParams | FunArgTypes) ‘)’
315316
ClsParams ::= ClsParam {‘,’ ClsParam}
316317
ClsParam ::= {Annotation} [{Modifier} (‘val’ | ‘var’) | ‘inline’] Param
317318
Param ::= id ‘:’ ParamType [‘=’ Expr]
318319
319320
DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
320321
DefParamClause ::= [nl] ‘(’ DefParams ‘)’ | UsingParamClause
321-
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | Types) ‘)’
322+
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | FunArgTypes) ‘)’
322323
DefParams ::= DefParam {‘,’ DefParam}
323324
DefParam ::= {Annotation} [‘inline’] Param
324325
```

tests/pos/i11997.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def a(using i: => Int ) = () // ok
2+
def b(using => Int ) = () // ok
3+

0 commit comments

Comments
 (0)