Skip to content

Commit 0e401ca

Browse files
committed
Allow several for type-bounds
In a `for` clause of an implied imports, allow several bounds, separated by commas. Writing ``` import a.b.{for C, D} ``` is equivalent to ``` import a.b.{for C | D} ``` but feels more natural.
1 parent 0f7e939 commit 0e401ca

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,15 +2310,22 @@ object Parsers {
23102310
/** ImportSelectors ::= `{' {ImportSelector `,'} FinalSelector ‘}’
23112311
* FinalSelector ::= ImportSelector
23122312
* | ‘_’
2313-
* | ‘for’ InfixType
2313+
* | ‘for’ InfixType {‘,’ InfixType}
23142314
*/
23152315
def importSelectors(): List[Tree] = in.token match {
23162316
case USCORE =>
23172317
wildcardIdent() :: Nil
23182318
case FOR =>
23192319
if (!importImplied)
23202320
syntaxError(em"`for` qualifier only allowed in `import implied`")
2321-
atSpan(in.skipToken()) { TypeBoundsTree(EmptyTree, infixType()) } :: Nil
2321+
atSpan(in.skipToken()) {
2322+
var t = infixType()
2323+
while (in.token == COMMA) {
2324+
val op = atSpan(in.skipToken()) { Ident(tpnme.raw.BAR) }
2325+
t = InfixOp(t, op, infixType())
2326+
}
2327+
TypeBoundsTree(EmptyTree, t)
2328+
} :: Nil
23222329
case _ =>
23232330
importSelector() :: {
23242331
if (in.token == COMMA) {

docs/docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ ImportExpr ::= StableId ‘.’ (id | ‘_’ | ImportSelectors)
338338
ImportSelectors ::= ‘{’ {ImportSelector ‘,’} FinalSelector ‘}’
339339
FinalSelector ::= ImportSelector Ident(name)
340340
| ‘_’ Pair(id, id)
341-
| ‘for’ InfixType TypeBoundsTree(EmptyTree, tpt)
341+
| ‘for’ InfixType {‘,’ InfixType} TypeBoundsTree(EmptyTree, tpt)
342342
ImportSelector ::= id [‘=>’ id | ‘=>’ ‘_’]
343343
Export ::= ‘export’ [‘implied’] ImportExpr {‘,’ ImportExpr}
344344
```

tests/run/implied-for.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ object A {
44

55
class B extends T
66
class C extends T
7+
class D
78

89
implied b for B
910
implied c for C
10-
implied d for T
11+
implied t for T
12+
implied d for D
1113
}
1214

1315
object Test extends App {
1416
import A._
15-
import implied A.{d, for B}
17+
import implied A.{t, for B, D}
1618

17-
println(d)
18-
val x: B = b
19+
val x1: B = b
20+
val x2: T = t
21+
val x3: D = d
1922

2023
assert(the[T].isInstanceOf[B])
24+
assert(the[D].isInstanceOf[D])
2125
}

0 commit comments

Comments
 (0)