@@ -1412,48 +1412,14 @@ object Parsers {
1412
1412
def typ (): Tree = {
1413
1413
val start = in.offset
1414
1414
var imods = Modifiers ()
1415
- def functionRest (params : List [Tree ]): Tree =
1416
- val paramSpan = Span (start, in.lastOffset)
1417
- atSpan(start, in.offset) {
1418
- if in.token == TLARROW then
1419
- if ! imods.flags.isEmpty || params.isEmpty then
1420
- syntaxError(em " illegal parameter list for type lambda " , start)
1421
- in.token = ARROW
1422
- else
1423
- for case ValDef (_, tpt : ByNameTypeTree , _) <- params do
1424
- syntaxError(em " parameter of type lambda may not be call-by-name " , tpt.span)
1425
- in.nextToken()
1426
- return TermLambdaTypeTree (params.asInstanceOf [List [ValDef ]], typ())
1427
-
1428
- if in.token == CTXARROW then
1429
- in.nextToken()
1430
- imods |= Given
1431
- else
1432
- accept(ARROW )
1433
- val t = typ()
1434
-
1435
- if imods.isOneOf(Given | Erased ) then
1436
- if imods.is(Given ) && params.isEmpty then
1437
- syntaxError(" context function types require at least one parameter" , paramSpan)
1438
- new FunctionWithMods (params, t, imods)
1439
- else if ! ctx.settings.YkindProjector .isDefault then
1440
- val (newParams :+ newT, tparams) = replaceKindProjectorPlaceholders(params :+ t): @ unchecked
1441
-
1442
- lambdaAbstract(tparams, Function (newParams, newT))
1443
- else
1444
- Function (params, t)
1445
- }
1446
-
1447
1415
var isValParamList = false
1448
-
1449
1416
val t =
1450
- if ( in.token == LPAREN ) {
1417
+ if in.token == LPAREN then
1451
1418
in.nextToken()
1452
- if ( in.token == RPAREN ) {
1419
+ if in.token == RPAREN then
1453
1420
in.nextToken()
1454
- functionRest(Nil )
1455
- }
1456
- else {
1421
+ functionTypeRest(Nil , start, Modifiers ())
1422
+ else
1457
1423
if isErased then imods = addModifier(imods)
1458
1424
val paramStart = in.offset
1459
1425
val ts = in.currentRegion.withCommasExpected {
@@ -1468,8 +1434,8 @@ object Parsers {
1468
1434
}
1469
1435
accept(RPAREN )
1470
1436
if isValParamList || in.isArrow then
1471
- functionRest (ts)
1472
- else {
1437
+ functionTypeRest (ts, start, imods )
1438
+ else
1473
1439
val ts1 =
1474
1440
for (t <- ts) yield
1475
1441
t match {
@@ -1485,33 +1451,29 @@ object Parsers {
1485
1451
withTypeRest(
1486
1452
annotTypeRest(
1487
1453
simpleTypeRest(tuple)))))
1488
- }
1489
- }
1490
- }
1491
- else if (in.token == LBRACKET ) {
1454
+ else if in.token == LBRACKET then
1492
1455
val start = in.offset
1493
1456
val tparams = typeParamClause(ParamOwner .TypeParam )
1494
- if ( in.token == TLARROW )
1457
+ if in.token == TLARROW then
1495
1458
atSpan(start, in.skipToken())(LambdaTypeTree (tparams, toplevelTyp()))
1496
- else if ( in.token == ARROW ) {
1459
+ else if in.token == ARROW then
1497
1460
val arrowOffset = in.skipToken()
1498
1461
val body = toplevelTyp()
1499
1462
atSpan(start, arrowOffset) {
1500
1463
if (isFunction(body))
1501
1464
PolyFunction (tparams, body)
1502
- else {
1465
+ else
1503
1466
syntaxError(" Implementation restriction: polymorphic function types must have a value parameter" , arrowOffset)
1504
1467
Ident (nme.ERROR .toTypeName)
1505
- }
1506
1468
}
1507
- }
1508
- else { accept(TLARROW ); typ() }
1509
- }
1510
- else if ( in.token == INDENT ) enclosed(INDENT , typ())
1469
+ else
1470
+ accept(TLARROW )
1471
+ typ()
1472
+ else if in.token == INDENT then enclosed(INDENT , typ())
1511
1473
else infixType()
1512
1474
1513
1475
in.token match {
1514
- case ARROW | CTXARROW => functionRest (t :: Nil )
1476
+ case ARROW | CTXARROW => functionTypeRest (t :: Nil , start, imods )
1515
1477
case MATCH => matchType(t)
1516
1478
case FORSOME => syntaxError(ExistentialTypesNoLongerSupported ()); t
1517
1479
case _ =>
@@ -1521,6 +1483,39 @@ object Parsers {
1521
1483
}
1522
1484
}
1523
1485
1486
+ def functionTypeRest (params : List [Tree ], start : Offset , mods : Modifiers ): Tree =
1487
+ var imods = mods
1488
+ val paramSpan = Span (start, in.lastOffset)
1489
+ atSpan(start, in.offset) {
1490
+ if in.token == TLARROW then
1491
+ if ! imods.flags.isEmpty || params.isEmpty then
1492
+ syntaxError(em " illegal parameter list for type lambda " , start)
1493
+ in.token = ARROW
1494
+ else
1495
+ for case ValDef (_, tpt : ByNameTypeTree , _) <- params do
1496
+ syntaxError(em " parameter of type lambda may not be call-by-name " , tpt.span)
1497
+ in.nextToken()
1498
+ return TermLambdaTypeTree (params.asInstanceOf [List [ValDef ]], typ())
1499
+
1500
+ if in.token == CTXARROW then
1501
+ in.nextToken()
1502
+ imods |= Given
1503
+ else
1504
+ accept(ARROW )
1505
+ val t = typ()
1506
+
1507
+ if imods.isOneOf(Given | Erased ) then
1508
+ if imods.is(Given ) && params.isEmpty then
1509
+ syntaxError(" context function types require at least one parameter" , paramSpan)
1510
+ new FunctionWithMods (params, t, imods)
1511
+ else if ! ctx.settings.YkindProjector .isDefault then
1512
+ val (newParams :+ newT, tparams) = replaceKindProjectorPlaceholders(params :+ t): @ unchecked
1513
+ lambdaAbstract(tparams, Function (newParams, newT))
1514
+ else
1515
+ Function (params, t)
1516
+ }
1517
+ end functionTypeRest
1518
+
1524
1519
private def makeKindProjectorTypeDef (name : TypeName ): TypeDef = {
1525
1520
val isVarianceAnnotated = name.startsWith(" +" ) || name.startsWith(" -" )
1526
1521
// We remove the variance marker from the name without passing along the specified variance at all
@@ -1884,7 +1879,15 @@ object Parsers {
1884
1879
else TypeTree ().withSpan(Span (in.lastOffset))
1885
1880
1886
1881
def typeDependingOn (location : Location ): Tree =
1887
- if location.inParens then typ()
1882
+ if location.inParens then
1883
+ if sourceVersion.isAtLeast(`3.2`) then
1884
+ val start = in.offset
1885
+ var t = infixType()
1886
+ if in.isArrow then
1887
+ t = functionTypeRest(t :: Nil , start, Modifiers ())
1888
+ report.error(em " function type in type ascription must be enclosed in parentheses " , t.srcPos)
1889
+ t
1890
+ else typ()
1888
1891
else if location.inPattern then rejectWildcardType(refinedType())
1889
1892
else infixType()
1890
1893
0 commit comments