Skip to content

Commit c4c48e3

Browse files
authored
Explain no expansion of ContextFunction0 (#23844)
Fixes #21321 Just an edge case. I tried emitting `TypeMismatch` with addenda, but it didn't render, so that was too much hassle just to add a string. This is the existing notice; it doesn't indicate why it doesn't work. ``` 2 |val v1: scala.ContextFunction0[String] = "x" // error | ^^^ | Found: ("x" : String) | Required: () ?=> String ```
2 parents 7762b23 + 3fdc94f commit c4c48e3

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
@@ -3751,12 +3751,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
37513751
val ifpt = defn.asContextFunctionType(pt)
37523752
val result =
37533753
if ifpt.exists
3754-
&& defn.functionArity(ifpt) > 0 // ContextFunction0 is only used after ElimByName
3754+
&& !ctx.isAfterTyper
3755+
&& {
3756+
// ContextFunction0 is only used after ElimByName
3757+
val arity = defn.functionArity(ifpt)
3758+
if arity == 0 then
3759+
report.error(em"context function types require at least one parameter", xtree.srcPos)
3760+
arity > 0
3761+
}
37553762
&& xtree.isTerm
37563763
&& !untpd.isContextualClosure(xtree)
37573764
&& !ctx.mode.is(Mode.Pattern)
37583765
&& !xtree.isInstanceOf[SplicePattern]
3759-
&& !ctx.isAfterTyper
37603766
&& !ctx.isInlineContext
37613767
then
37623768
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)