@@ -357,6 +357,21 @@ object Parsers {
357
357
accept(SEMI )
358
358
}
359
359
360
+ def acceptNested (): Unit =
361
+ if in.token != LBRACE && in.token != INDENT then
362
+ syntaxError(i " indented definitions or `{' expected " )
363
+
364
+ /** Under -language:Scala2 or -old-syntax, flag
365
+ *
366
+ * extends p1 with new p1 with t1 with
367
+ * p2 p2 t2
368
+ *
369
+ * as a migration warning or error since that means something else under significant indentation.
370
+ */
371
+ def checkNotWithAtEOL (): Unit =
372
+ if (in.isScala2Mode || in.oldSyntax) && in.isAfterLineEnd then
373
+ in.errorOrMigrationWarning(" `with` cannot be followed by new line, place at beginning of next line instead" )
374
+
360
375
def rewriteNotice (additionalOption : String = " " ) = {
361
376
val optionStr = if (additionalOption.isEmpty) " " else " " ++ additionalOption
362
377
i " \n This construct can be rewritten automatically under $optionStr -rewrite. "
@@ -1254,8 +1269,7 @@ object Parsers {
1254
1269
def possibleTemplateStart (): Unit =
1255
1270
if in.token == WITH then
1256
1271
in.nextToken()
1257
- if in.token != LBRACE && in.token != INDENT then
1258
- syntaxError(i " indented definitions or `{' expected " )
1272
+ acceptNested()
1259
1273
else if silentTemplateIdent then
1260
1274
in.observeIndented()
1261
1275
newLineOptWhenFollowedBy(LBRACE )
@@ -1402,7 +1416,7 @@ object Parsers {
1402
1416
makeParameter(name, typ(), mods | Param )
1403
1417
}
1404
1418
1405
- /** InfixType ::= RefinedType {id [nl] refinedType }
1419
+ /** InfixType ::= RefinedType {id [nl] RefinedType }
1406
1420
*/
1407
1421
def infixType (): Tree = infixTypeRest(refinedType())
1408
1422
@@ -1413,7 +1427,7 @@ object Parsers {
1413
1427
def infixTypeRest (t : Tree ): Tree =
1414
1428
infixOps(t, canStartTypeTokens, refinedType, isType = true , isOperator = ! isPostfixStar)
1415
1429
1416
- /** RefinedType ::= WithType {Annotation | [nl ] Refinement}
1430
+ /** RefinedType ::= WithType {[nl | `with' ] Refinement}
1417
1431
*/
1418
1432
val refinedType : () => Tree = () => refinedTypeRest(withType())
1419
1433
@@ -1429,12 +1443,16 @@ object Parsers {
1429
1443
def withType (): Tree = withTypeRest(annotType())
1430
1444
1431
1445
def withTypeRest (t : Tree ): Tree =
1432
- if (in.token == WITH ) {
1433
- if (ctx.settings.strict.value)
1434
- deprecationWarning(DeprecatedWithOperator ())
1446
+ if in.token == WITH then
1447
+ val withOffset = in.offset
1435
1448
in.nextToken()
1436
- makeAndType(t, withType())
1437
- }
1449
+ if in.token == LBRACE || in.token == INDENT then
1450
+ t
1451
+ else
1452
+ checkNotWithAtEOL()
1453
+ if (ctx.settings.strict.value)
1454
+ deprecationWarning(DeprecatedWithOperator (), withOffset)
1455
+ makeAndType(t, withType())
1438
1456
else t
1439
1457
1440
1458
/** AnnotType ::= SimpleType {Annotation}
@@ -3434,14 +3452,7 @@ object Parsers {
3434
3452
in.nextToken()
3435
3453
if templateCanFollow && (in.token == LBRACE || in.token == INDENT ) then Nil
3436
3454
else
3437
- if (in.isScala2Mode || in.oldSyntax) && in.isAfterLineEnd then
3438
- // Disallow
3439
- //
3440
- // extends p1 with
3441
- // p2
3442
- //
3443
- // since that means something else under significant indentation
3444
- in.errorOrMigrationWarning(" `with` cannot be followed by new line, place at beginning of next line instead" )
3455
+ checkNotWithAtEOL()
3445
3456
constrApps(commaOK, templateCanFollow)
3446
3457
else if commaOK && in.token == COMMA then
3447
3458
in.nextToken()
0 commit comments