Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Explain no expansion of ContextFunction0 #23844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
odersky merged 1 commit into scala:main from som-snytt:issue/21321-CF0
Sep 10, 2025
Merged
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /