Skip to content

Commit eacb5af

Browse files
authored
Merge pull request #4159 from dotty-staging/fix-4157
Fix #4157: Broken error message when trying to eta-expand
2 parents a0e1000 + a58c5bc commit eacb5af

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,10 +1837,10 @@ object messages {
18371837
}
18381838
}
18391839

1840-
case class OnlyFunctionsCanBeFollowedByUnderscore(pt: Type)(implicit ctx: Context)
1840+
case class OnlyFunctionsCanBeFollowedByUnderscore(tp: Type)(implicit ctx: Context)
18411841
extends Message(OnlyFunctionsCanBeFollowedByUnderscoreID) {
18421842
val kind = "Syntax"
1843-
val msg = hl"Not a function: $pt: cannot be followed by ${"_"}"
1843+
val msg = hl"Only function types can be followed by ${"_"} but the current expression has type $tp"
18441844
val explanation =
18451845
hl"""The syntax ${"x _"} is no longer supported if ${"x"} is not a function.
18461846
|To convert to a function value, you need to explicitly write ${"() => x"}"""

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,9 +1661,10 @@ class Typer extends Namer
16611661
val nestedCtx = ctx.fresh.setNewTyperState()
16621662
val res = typed(qual, pt1)(nestedCtx)
16631663
res match {
1664-
case res @ closure(_, _, _) =>
1664+
case closure(_, _, _) =>
16651665
case _ =>
1666-
ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore(res.tpe), tree.pos)
1666+
val recovered = typed(qual)(ctx.fresh.setExploreTyperState())
1667+
ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore(recovered.tpe.widen), tree.pos)
16671668
if (ctx.scala2Mode) {
16681669
// Under -rewrite, patch `x _` to `(() => x)`
16691670
patch(Position(tree.pos.start), "(() => ")

compiler/test-resources/repl/errmsgs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ scala> val a: iDontExist = 1
7272
1 | val a: iDontExist = 1
7373
| ^^^^^^^^^^
7474
| not found: type iDontExist
75+
scala> def foo1(x: => Int) = x _
76+
1 | def foo1(x: => Int) = x _
77+
| ^^^
78+
|Only function types can be followed by _ but the current expression has type Int
79+
scala> def foo2(x: => Int): () => Int = x _
80+
1 | def foo2(x: => Int): () => Int = x _
81+
| ^^^
82+
|Only function types can be followed by _ but the current expression has type Int

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,8 @@ class ErrorMessagesTests extends ErrorMessagesTest {
10501050
implicit val ctx: Context = ictx
10511051

10521052
assertMessageCount(1, messages)
1053-
val OnlyFunctionsCanBeFollowedByUnderscore(pt) :: Nil = messages
1054-
assertEquals("String(n)", pt.show)
1053+
val OnlyFunctionsCanBeFollowedByUnderscore(tp) :: Nil = messages
1054+
assertEquals("String", tp.show)
10551055
}
10561056

10571057
@Test def missingEmptyArgumentList =

0 commit comments

Comments
 (0)