Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3749,12 +3749,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val ifpt = defn.asContextFunctionType(pt)
val result =
if ifpt.exists
&& defn.functionArity(ifpt) > 0 // ContextFunction0 is only used after ElimByName
&& !ctx.isAfterTyper
&& {
// ContextFunction0 is only used after ElimByName
val arity = defn.functionArity(ifpt)
if arity == 0 then
report.error(em"context function types require at least one parameter", xtree.srcPos)
arity > 0
}
&& xtree.isTerm
&& !untpd.isContextualClosure(xtree)
&& !ctx.mode.is(Mode.Pattern)
&& !xtree.isInstanceOf[SplicePattern]
&& !ctx.isAfterTyper
&& !ctx.isInlineContext
then
makeContextualFunction(xtree, ifpt)
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/context-function-syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ val test =
(using x: Int) => x // error // error // error

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

object Example3 extends App {
Expand Down
19 changes: 19 additions & 0 deletions tests/neg/i21321.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Error: tests/neg/i21321.scala:3:42 ----------------------------------------------------------------------------------
3 |val v1b: scala.ContextFunction0[String] = () ?=> "x" // error
| ^^
| context function literals require at least one formal parameter
-- Error: tests/neg/i21321.scala:4:8 -----------------------------------------------------------------------------------
4 |val v2: () ?=> String = "y" // error // error in parser
| ^^
| context function types require at least one parameter
-- Error: tests/neg/i21321.scala:2:41 ----------------------------------------------------------------------------------
2 |val v1: scala.ContextFunction0[String] = "x" // error
| ^^^
| context function types require at least one parameter
-- [E007] Type Mismatch Error: tests/neg/i21321.scala:4:24 -------------------------------------------------------------
4 |val v2: () ?=> String = "y" // error // error in parser
| ^^^
| Found: ("y" : String)
| Required: () => String
|
| longer explanation available when compiling with `-explain`
4 changes: 4 additions & 0 deletions tests/neg/i21321.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

val v1: scala.ContextFunction0[String] = "x" // error
val v1b: scala.ContextFunction0[String] = () ?=> "x" // error
val v2: () ?=> String = "y" // error // error in parser
Loading