@@ -1388,48 +1388,14 @@ object Parsers {
1388
1388
def typ (): Tree = {
1389
1389
val start = in.offset
1390
1390
var imods = Modifiers ()
1391
- def functionRest (params : List [Tree ]): Tree =
1392
- val paramSpan = Span (start, in.lastOffset)
1393
- atSpan(start, in.offset) {
1394
- if in.token == TLARROW then
1395
- if ! imods.flags.isEmpty || params.isEmpty then
1396
- syntaxError(em " illegal parameter list for type lambda " , start)
1397
- in.token = ARROW
1398
- else
1399
- for case ValDef (_, tpt : ByNameTypeTree , _) <- params do
1400
- syntaxError(em " parameter of type lambda may not be call-by-name " , tpt.span)
1401
- in.nextToken()
1402
- return TermLambdaTypeTree (params.asInstanceOf [List [ValDef ]], typ())
1403
-
1404
- if in.token == CTXARROW then
1405
- in.nextToken()
1406
- imods |= Given
1407
- else
1408
- accept(ARROW )
1409
- val t = typ()
1410
-
1411
- if imods.isOneOf(Given | Erased ) then
1412
- if imods.is(Given ) && params.isEmpty then
1413
- syntaxError(" context function types require at least one parameter" , paramSpan)
1414
- new FunctionWithMods (params, t, imods)
1415
- else if ! ctx.settings.YkindProjector .isDefault then
1416
- val (newParams :+ newT, tparams) = replaceKindProjectorPlaceholders(params :+ t): @ unchecked
1417
-
1418
- lambdaAbstract(tparams, Function (newParams, newT))
1419
- else
1420
- Function (params, t)
1421
- }
1422
-
1423
1391
var isValParamList = false
1424
-
1425
1392
val t =
1426
- if ( in.token == LPAREN ) {
1393
+ if in.token == LPAREN then
1427
1394
in.nextToken()
1428
- if ( in.token == RPAREN ) {
1395
+ if in.token == RPAREN then
1429
1396
in.nextToken()
1430
- functionRest(Nil )
1431
- }
1432
- else {
1397
+ functionTypeRest(Nil , start, Modifiers ())
1398
+ else
1433
1399
if isErased then imods = addModifier(imods)
1434
1400
val paramStart = in.offset
1435
1401
val ts = in.currentRegion.withCommasExpected {
@@ -1444,8 +1410,8 @@ object Parsers {
1444
1410
}
1445
1411
accept(RPAREN )
1446
1412
if isValParamList || in.isArrow then
1447
- functionRest (ts)
1448
- else {
1413
+ functionTypeRest (ts, start, imods )
1414
+ else
1449
1415
val ts1 =
1450
1416
for (t <- ts) yield
1451
1417
t match {
@@ -1461,33 +1427,29 @@ object Parsers {
1461
1427
withTypeRest(
1462
1428
annotTypeRest(
1463
1429
simpleTypeRest(tuple)))))
1464
- }
1465
- }
1466
- }
1467
- else if (in.token == LBRACKET ) {
1430
+ else if in.token == LBRACKET then
1468
1431
val start = in.offset
1469
1432
val tparams = typeParamClause(ParamOwner .TypeParam )
1470
- if ( in.token == TLARROW )
1433
+ if in.token == TLARROW then
1471
1434
atSpan(start, in.skipToken())(LambdaTypeTree (tparams, toplevelTyp()))
1472
- else if ( in.token == ARROW ) {
1435
+ else if in.token == ARROW then
1473
1436
val arrowOffset = in.skipToken()
1474
1437
val body = toplevelTyp()
1475
1438
atSpan(start, arrowOffset) {
1476
1439
if (isFunction(body))
1477
1440
PolyFunction (tparams, body)
1478
- else {
1441
+ else
1479
1442
syntaxError(" Implementation restriction: polymorphic function types must have a value parameter" , arrowOffset)
1480
1443
Ident (nme.ERROR .toTypeName)
1481
- }
1482
1444
}
1483
- }
1484
- else { accept(TLARROW ); typ() }
1485
- }
1486
- else if ( in.token == INDENT ) enclosed(INDENT , typ())
1445
+ else
1446
+ accept(TLARROW )
1447
+ typ()
1448
+ else if in.token == INDENT then enclosed(INDENT , typ())
1487
1449
else infixType()
1488
1450
1489
1451
in.token match {
1490
- case ARROW | CTXARROW => functionRest (t :: Nil )
1452
+ case ARROW | CTXARROW => functionTypeRest (t :: Nil , start, imods )
1491
1453
case MATCH => matchType(t)
1492
1454
case FORSOME => syntaxError(ExistentialTypesNoLongerSupported ()); t
1493
1455
case _ =>
@@ -1497,6 +1459,39 @@ object Parsers {
1497
1459
}
1498
1460
}
1499
1461
1462
+ def functionTypeRest (params : List [Tree ], start : Offset , mods : Modifiers ): Tree =
1463
+ var imods = mods
1464
+ val paramSpan = Span (start, in.lastOffset)
1465
+ atSpan(start, in.offset) {
1466
+ if in.token == TLARROW then
1467
+ if ! imods.flags.isEmpty || params.isEmpty then
1468
+ syntaxError(em " illegal parameter list for type lambda " , start)
1469
+ in.token = ARROW
1470
+ else
1471
+ for case ValDef (_, tpt : ByNameTypeTree , _) <- params do
1472
+ syntaxError(em " parameter of type lambda may not be call-by-name " , tpt.span)
1473
+ in.nextToken()
1474
+ return TermLambdaTypeTree (params.asInstanceOf [List [ValDef ]], typ())
1475
+
1476
+ if in.token == CTXARROW then
1477
+ in.nextToken()
1478
+ imods |= Given
1479
+ else
1480
+ accept(ARROW )
1481
+ val t = typ()
1482
+
1483
+ if imods.isOneOf(Given | Erased ) then
1484
+ if imods.is(Given ) && params.isEmpty then
1485
+ syntaxError(" context function types require at least one parameter" , paramSpan)
1486
+ new FunctionWithMods (params, t, imods)
1487
+ else if ! ctx.settings.YkindProjector .isDefault then
1488
+ val (newParams :+ newT, tparams) = replaceKindProjectorPlaceholders(params :+ t): @ unchecked
1489
+ lambdaAbstract(tparams, Function (newParams, newT))
1490
+ else
1491
+ Function (params, t)
1492
+ }
1493
+ end functionTypeRest
1494
+
1500
1495
private def makeKindProjectorTypeDef (name : TypeName ): TypeDef = {
1501
1496
val isVarianceAnnotated = name.startsWith(" +" ) || name.startsWith(" -" )
1502
1497
// We remove the variance marker from the name without passing along the specified variance at all
@@ -1860,7 +1855,15 @@ object Parsers {
1860
1855
else TypeTree ().withSpan(Span (in.lastOffset))
1861
1856
1862
1857
def typeDependingOn (location : Location ): Tree =
1863
- if location.inParens then typ()
1858
+ if location.inParens then
1859
+ if sourceVersion.isAtLeast(`3.2`) then
1860
+ val start = in.offset
1861
+ var t = infixType()
1862
+ if in.isArrow then
1863
+ t = functionTypeRest(t :: Nil , start, Modifiers ())
1864
+ report.error(em " function type in type ascription must be enclosed in parentheses " , t.srcPos)
1865
+ t
1866
+ else typ()
1864
1867
else if location.inPattern then rejectWildcardType(refinedType())
1865
1868
else infixType()
1866
1869
0 commit comments