Skip to content

Commit 1c7dfe5

Browse files
committed
0 arg ctx function is syntax error
1 parent edfafbf commit 1c7dfe5

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,7 @@ object Parsers {
13641364
val start = in.offset
13651365
var imods = Modifiers()
13661366
def functionRest(params: List[Tree]): Tree =
1367+
val paramSpan = Span(start, in.lastOffset)
13671368
atSpan(start, in.offset) {
13681369
if in.token == TLARROW then
13691370
if !imods.flags.isEmpty || params.isEmpty then
@@ -1382,14 +1383,16 @@ object Parsers {
13821383
accept(ARROW)
13831384
val t = typ()
13841385

1385-
if (imods.isOneOf(Given | Erased)) new FunctionWithMods(params, t, imods)
1386-
else if (ctx.settings.YkindProjector.value) {
1386+
if imods.isOneOf(Given | Erased) then
1387+
if imods.is(Given) && params.isEmpty then
1388+
syntaxError("context function types require at least one parameter", paramSpan)
1389+
new FunctionWithMods(params, t, imods)
1390+
else if ctx.settings.YkindProjector.value then
13871391
val (newParams :+ newT, tparams) = replaceKindProjectorPlaceholders(params :+ t)
13881392

13891393
lambdaAbstract(tparams, Function(newParams, newT))
1390-
} else {
1394+
else
13911395
Function(params, t)
1392-
}
13931396
}
13941397
def funArgTypesRest(first: Tree, following: () => Tree) = {
13951398
val buf = new ListBuffer[Tree] += first
@@ -2183,7 +2186,12 @@ object Parsers {
21832186

21842187
def closureRest(start: Int, location: Location, params: List[Tree]): Tree =
21852188
atSpan(start, in.offset) {
2186-
if in.token == CTXARROW then in.nextToken() else accept(ARROW)
2189+
if in.token == CTXARROW then
2190+
if params.isEmpty then
2191+
syntaxError("context function literals require at least one formal parameter", Span(start, in.lastOffset))
2192+
in.nextToken()
2193+
else
2194+
accept(ARROW)
21872195
Function(params, if (location == Location.InBlock) block() else expr())
21882196
}
21892197

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
val test =
22
(using x: Int) => x // error // error
3+
4+
val f = () ?=> 23 // error
5+
val g: ContextFunction0[Int] = ??? // ok
6+
val h: () ?=> Int = ??? // error
7+
8+
object Example3 extends App {
9+
final case class Foo[A, B](run: () ?=> Int) // error
10+
}

tests/pos/i7778.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,3 @@ object Example extends App {
55
object Example2 extends App {
66
final case class Foo[A, B](run: (A, B) ?=> Int)
77
}
8-
9-
10-
object Example3 extends App {
11-
final case class Foo[A, B](run: () ?=> Int)
12-
}

0 commit comments

Comments
 (0)