Skip to content

Commit 60ff423

Browse files
som-snyttWojciechMazur
authored andcommitted
Explain no expansion of ContextFunction0
[Cherry-picked 3fdc94f]
1 parent 48838a6 commit 60ff423

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,12 +3745,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
37453745
val ifpt = defn.asContextFunctionType(pt)
37463746
val result =
37473747
if ifpt.exists
3748-
&& defn.functionArity(ifpt) > 0 // ContextFunction0 is only used after ElimByName
3748+
&& !ctx.isAfterTyper
3749+
&& {
3750+
// ContextFunction0 is only used after ElimByName
3751+
val arity = defn.functionArity(ifpt)
3752+
if arity == 0 then
3753+
report.error(em"context function types require at least one parameter", xtree.srcPos)
3754+
arity > 0
3755+
}
37493756
&& xtree.isTerm
37503757
&& !untpd.isContextualClosure(xtree)
37513758
&& !ctx.mode.is(Mode.Pattern)
37523759
&& !xtree.isInstanceOf[SplicePattern]
3753-
&& !ctx.isAfterTyper
37543760
&& !ctx.isInlineContext
37553761
then
37563762
makeContextualFunction(xtree, ifpt)

tests/neg/context-function-syntax.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ val test =
22
(using x: Int) => x // error // error // error
33

44
val f = () ?=> 23 // error
5-
val g: ContextFunction0[Int] = ??? // ok
5+
val g: ContextFunction0[Int] = ??? // error at typer for RHS not expanded
66
val h: () ?=> Int = ??? // error
77

88
object Example3 extends App {

tests/neg/i21321.check

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Error: tests/neg/i21321.scala:3:42 ----------------------------------------------------------------------------------
2+
3 |val v1b: scala.ContextFunction0[String] = () ?=> "x" // error
3+
| ^^
4+
| context function literals require at least one formal parameter
5+
-- Error: tests/neg/i21321.scala:4:8 -----------------------------------------------------------------------------------
6+
4 |val v2: () ?=> String = "y" // error // error in parser
7+
| ^^
8+
| context function types require at least one parameter
9+
-- Error: tests/neg/i21321.scala:2:41 ----------------------------------------------------------------------------------
10+
2 |val v1: scala.ContextFunction0[String] = "x" // error
11+
| ^^^
12+
| context function types require at least one parameter
13+
-- [E007] Type Mismatch Error: tests/neg/i21321.scala:4:24 -------------------------------------------------------------
14+
4 |val v2: () ?=> String = "y" // error // error in parser
15+
| ^^^
16+
| Found: ("y" : String)
17+
| Required: () => String
18+
|
19+
| longer explanation available when compiling with `-explain`

tests/neg/i21321.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
val v1: scala.ContextFunction0[String] = "x" // error
3+
val v1b: scala.ContextFunction0[String] = () ?=> "x" // error
4+
val v2: () ?=> String = "y" // error // error in parser

0 commit comments

Comments
 (0)