Skip to content

Commit 361bc03

Browse files
committed
Avoid extra lookahead
1 parent b779cd1 commit 361bc03

File tree

3 files changed

+116
-44
lines changed

3 files changed

+116
-44
lines changed

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,25 +1092,6 @@ object Parsers {
10921092
|| in.lookahead.token == EOF // important for REPL completions
10931093
|| ctx.mode.is(Mode.Interactive) // in interactive mode the next tokens might be missing
10941094

1095-
/** Under `qualifiedTypes` language import: is the token sequence following
1096-
* the current `{` classified as a qualified type? This is the case if the
1097-
* next token is an `IDENT`, followed by `:`.
1098-
*/
1099-
def followingIsQualifiedType(): Boolean =
1100-
in.featureEnabled(Feature.qualifiedTypes) && {
1101-
val lookahead = in.LookaheadScanner(allowIndent = true)
1102-
1103-
if in.token == INDENT then
1104-
() // The LookaheadScanner doesn't see previous indents, so no need to skip it
1105-
else
1106-
lookahead.nextToken() // skips the opening brace
1107-
1108-
lookahead.token == IDENTIFIER && {
1109-
lookahead.nextToken()
1110-
lookahead.token == COLONfollow
1111-
}
1112-
}
1113-
11141095
/* --------- OPERAND/OPERATOR STACK --------------------------------------- */
11151096

11161097
var opStack: List[OpInfo] = Nil
@@ -2071,10 +2052,11 @@ object Parsers {
20712052
atSpan(in.offset) {
20722053
makeTupleOrParens(inParensWithCommas(argTypes(namedOK = false, wildOK = true, tupleOK = true)))
20732054
}
2074-
else if in.token == LBRACE && followingIsQualifiedType() then
2075-
qualifiedType()
20762055
else if in.token == LBRACE then
2077-
atSpan(in.offset) { RefinedTypeTree(EmptyTree, refinement(indentOK = false)) }
2056+
if in.featureEnabled(Feature.qualifiedTypes) && in.lookahead.token == IDENTIFIER then
2057+
qualifiedType()
2058+
else
2059+
atSpan(in.offset) { RefinedTypeTree(EmptyTree, refinement(indentOK = false)) }
20782060
else if (isSplice)
20792061
splice(isType = true)
20802062
else

tests/printing/qualifiers.check

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ package example {
1515
new example.qualifiers$package()
1616
final module class qualifiers$package() extends Object() {
1717
this: example.qualifiers$package.type =>
18-
type Useless =
18+
type Neg =
1919
Int @qualified[Int](
2020
{
21-
def $anonfun(x: Int): Boolean = true
21+
def $anonfun(x: Int): Boolean = x.<(0)
2222
closure($anonfun)
2323
}
2424
)
@@ -29,14 +29,39 @@ package example {
2929
closure($anonfun)
3030
}
3131
)
32-
type Neg =
32+
type Pos2 =
3333
Int @qualified[Int](
3434
{
35-
def $anonfun(x: Int): Boolean = x.<(0)
35+
def $anonfun(x: Int): Boolean = x.>(0)
3636
closure($anonfun)
3737
}
3838
)
39-
type Nesting =
39+
type Pos3 =
40+
Int @qualified[Int](
41+
{
42+
def $anonfun(x: Int): Boolean = x.>(0)
43+
closure($anonfun)
44+
}
45+
)
46+
type Pos4 =
47+
Int @qualified[Int](
48+
{
49+
def $anonfun(x: Int): Boolean = x.>(0)
50+
closure($anonfun)
51+
}
52+
)
53+
type Pos5 =
54+
Int @qualified[Int](
55+
{
56+
def $anonfun(x: Int): Boolean =
57+
{
58+
val res: Boolean = x.>(0)
59+
res:Boolean
60+
}
61+
closure($anonfun)
62+
}
63+
)
64+
type Nested =
4065
Int @qualified[Int](
4166
{
4267
def $anonfun(x: Int): Boolean =
@@ -54,7 +79,7 @@ package example {
5479
closure($anonfun)
5580
}
5681
)
57-
type Pos2 =
82+
type Intersection =
5883
Int &
5984
Int @qualified[Int](
6085
{
@@ -76,7 +101,7 @@ package example {
76101
def id[T >: Nothing <: Any](x: T): T = x
77102
def test(): Unit =
78103
{
79-
val x1: example.Pos = 1
104+
val x: example.Pos = 1
80105
val x2:
81106
Int @qualified[Int](
82107
{
@@ -88,12 +113,28 @@ package example {
88113
val x3:
89114
Int @qualified[Int](
90115
{
91-
def $anonfun(x3: Int): Boolean = x3.>(0)
116+
def $anonfun(x: Int): Boolean = x.>(0)
117+
closure($anonfun)
118+
}
119+
)
120+
= 1
121+
val x4:
122+
Int @qualified[Int](
123+
{
124+
def $anonfun(x: Int): Boolean = x.>(0)
92125
closure($anonfun)
93126
}
94127
)
95128
= 1
96-
val x4: Int =
129+
val x5:
130+
Int @qualified[Int](
131+
{
132+
def $anonfun(x5: Int): Boolean = x.>(0)
133+
closure($anonfun)
134+
}
135+
)
136+
= 1
137+
val x6: Int =
97138
example.id[
98139
Int @qualified[Int](
99140
{
@@ -137,12 +178,38 @@ package example {
137178
val b: Boolean = false
138179
example.id[Boolean](true)
139180
}
140-
type B =
181+
type T1 =
182+
Object
183+
{
184+
val x: Int
185+
}
186+
type T2 =
187+
Object
188+
{
189+
val x: Int
190+
}
191+
type T3 =
192+
Object
193+
{
194+
type T = Int
195+
}
196+
type T4 =
197+
Object
198+
{
199+
def x: Int
200+
}
201+
type T5 =
202+
Object
203+
{
204+
def x: Int
205+
def x_=(x$1: Int): _root_.scala.Unit
206+
}
207+
type T6 =
141208
Object
142209
{
143210
val x: Int
144211
}
145-
type C =
212+
type T7 =
146213
Object
147214
{
148215
val x: Int

tests/printing/qualifiers.scala

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
package example
22

3-
type Useless = {x: Int with true}
4-
type Pos =
3+
type Neg = {x: Int with x < 0}
4+
type Pos = {x: Int with x > 0}
5+
type Pos2 = {x: Int
6+
with x > 0
7+
}
8+
type Pos3 = {x: Int with
9+
x > 0
10+
}
11+
type Pos4 =
512
{x: Int with x > 0}
6-
type Neg = {x: Int with
7-
x < 0
13+
type Pos5 = {x: Int with
14+
val res = x > 0
15+
res
816
}
9-
type Nesting = {x: Int with { val y: {z: Int with z > 0} = ??? ; x > y }}
10-
type Pos2 = Int & {x: Int with x > 0}
17+
18+
type Nested = {x: Int with { val y: {z: Int with z > 0} = ??? ; x > y }}
19+
type Intersection = Int & {x: Int with x > 0}
1120
type ValRefinement = {val x: Int with x > 0}
1221

1322
def id[T](x: T): T = x
1423

1524
def test() =
16-
val x1: Pos = 1
25+
val x: Pos = 1
1726
val x2: {x: Int with x > 0} = 1
18-
val x3: Int with x3 > 0 = 1
19-
val x4: Int = id[{x: Int with x < 0}](1) + id[Neg](-1)
27+
val x3: {
28+
x: Int with x > 0
29+
} = 1
30+
val x4: {x: Int with
31+
x > 0
32+
} = 1
33+
val x5: Int with x > 0 = 1
34+
val x6: Int = id[{x: Int with x < 0}](1) + id[Neg](-1)
2035

2136
def bar(x: Int with x > 0) = ???
2237
def secondGreater1(x: Int, y: Int)(z: {w: Int with x > y}) = ???
@@ -32,5 +47,13 @@ given A with
3247
id(true)
3348

3449
// Also not qualified types:
35-
type B = {val x: Int}
36-
type C = Object {val x: Int}
50+
type T1 = {val x: Int}
51+
type T2 = {
52+
val x: Int
53+
}
54+
type T3 = {type T = Int}
55+
type T4 = {def x: Int}
56+
type T5 = {var x: Int}
57+
type T6 = Object {val x: Int}
58+
type T7 = Object:
59+
val x: Int

0 commit comments

Comments
 (0)