Skip to content

Commit ee1f600

Browse files
committed
Remove syntactic deviation for ascriptions in parentheses
Previously the parser accepted `(x: A => B)` as an expression, but only if there were parentheses around it. This was necessary so that we could parse a potential formal parameter of a closure `(x: A => B) => ...` as an expression and then convert to a type. But it is not backed by the grammar. The grammar requires `x: (A => B)` whether in parentheses or not. This commit removes the deviation and aligns the parser with the syntax.
1 parent f35df41 commit ee1f600

File tree

6 files changed

+14
-15
lines changed

6 files changed

+14
-15
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ object Parsers {
4040

4141
case class OpInfo(operand: Tree, operator: Ident, offset: Offset)
4242

43-
enum Location(val inParens: Boolean, val inPattern: Boolean, val inArgs: Boolean):
44-
case InParens extends Location(true, false, false)
45-
case InArgs extends Location(true, false, true)
46-
case InPattern extends Location(false, true, false)
47-
case InGuard extends Location(false, false, false)
48-
case InPatternArgs extends Location(false, true, true) // InParens not true, since it might be an alternative
49-
case InBlock extends Location(false, false, false)
50-
case ElseWhere extends Location(false, false, false)
43+
enum Location(val inPattern: Boolean, val inArgs: Boolean):
44+
case InParens extends Location(false, false)
45+
case InArgs extends Location(false, true)
46+
case InPattern extends Location(true, false)
47+
case InGuard extends Location(false, false)
48+
case InPatternArgs extends Location(true, true)
49+
case InBlock extends Location(false, false)
50+
case ElseWhere extends Location(false, false)
5151

5252
enum ParamOwner:
5353
case Class, Type, TypeParam, Def
@@ -1860,8 +1860,7 @@ object Parsers {
18601860
else TypeTree().withSpan(Span(in.lastOffset))
18611861

18621862
def typeDependingOn(location: Location): Tree =
1863-
if location.inParens then typ()
1864-
else if location.inPattern then rejectWildcardType(refinedType())
1863+
if location.inPattern then rejectWildcardType(refinedType())
18651864
else infixType()
18661865

18671866
/* ----------- EXPRESSIONS ------------------------------------------------ */

tests/pos/i7851.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object WrapperTest {
3030
val test2: (Wrapped[Float], Wrapped[Float], Wrapped[Float]) => Wrapped[Float] = { (x, y, z) => x }
3131

3232
def main(args: Array[String]): Unit = {
33-
wrappedFunction(test1: (Wrapped[Float], Wrapped[Float], Wrapped[Float]) => Wrapped[Float])(5f, 11f, 3f)
33+
wrappedFunction(test1: ((Wrapped[Float], Wrapped[Float], Wrapped[Float]) => Wrapped[Float]))(5f, 11f, 3f)
3434
wrappedFunction(test1)(5f, 11f, 3f)
3535
wrappedFunction(test2)(5f, 11f, 3f)
3636
}

tests/pos/t4176b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Test {
22
def foo(a1: String*) = a1
33
// val fooEta = foo _
4-
(foo: Seq[String] => Seq[String])
4+
foo: (Seq[String] => Seq[String])
55
}

tests/pos/t8120.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ object A {
55
implicit class RichAny(a: Any) {
66
def m(a: Any): Int = 0
77
}
8-
(new C).m({ case (x, y) => x } : Any => Any)
8+
(new C).m({ case (x, y) => x } : (Any => Any))
99
}

tests/run-macros/quote-matching-optimize-4/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object Macro {
2222
case '{ ($ls: List[T]).filter($f).filter($g) } =>
2323
optimize('{ $ls.filter(x => $f(x) && $g(x)) })
2424

25-
case '{ type u; type v; ($ls: List[`u`]).map($f: `u` => `v`).map($g: `v` => T) } =>
25+
case '{ type u; type v; ($ls: List[`u`]).map($f: (`u` => `v`)).map($g: (`v` => T)) } =>
2626
optimize('{ $ls.map(x => $g($f(x))) })
2727

2828
case _ => x

tests/run/lambda-unit.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Test {
1313

1414
def main(args: Array[String]): Unit = {
1515
fun("")
16-
(fun: Object => Any)("")
16+
(fun: (Object => Any))("")
1717
sam.foo("")
1818
genericSam.foo("")
1919
}

0 commit comments

Comments
 (0)