Skip to content

Fix #4157: Broken error message when trying to eta-expand #4159

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
merged 1 commit into from
Mar 23, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -1835,10 +1835,10 @@ object messages {
}
}

case class OnlyFunctionsCanBeFollowedByUnderscore(pt: Type)(implicit ctx: Context)
case class OnlyFunctionsCanBeFollowedByUnderscore(tp: Type)(implicit ctx: Context)
extends Message(OnlyFunctionsCanBeFollowedByUnderscoreID) {
val kind = "Syntax"
val msg = hl"Not a function: $pt: cannot be followed by ${"_"}"
val msg = hl"Only function types can be followed by ${"_"} but the current expression has type $tp"
val explanation =
hl"""The syntax ${"x _"} is no longer supported if ${"x"} is not a function.
|To convert to a function value, you need to explicitly write ${"() => x"}"""
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1645,9 +1645,10 @@ class Typer extends Namer
val nestedCtx = ctx.fresh.setNewTyperState()
val res = typed(qual, pt1)(nestedCtx)
res match {
case res @ closure(_, _, _) =>
case closure(_, _, _) =>
case _ =>
ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore(res.tpe), tree.pos)
val recovered = typed(qual)(ctx.fresh.setExploreTyperState())
ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore(recovered.tpe.widen), tree.pos)
if (ctx.scala2Mode) {
// Under -rewrite, patch `x _` to `(() => x)`
patch(Position(tree.pos.start), "(() => ")
Expand Down
8 changes: 8 additions & 0 deletions compiler/test-resources/repl/errmsgs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ scala> val a: iDontExist = 1
1 | val a: iDontExist = 1
| ^^^^^^^^^^
| not found: type iDontExist
scala> def foo1(x: => Int) = x _
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it an error message test instead. They are less fragile

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I really want to know what the exact output is here :). Can you help me figure out why the test doesn't pass?

1 | def foo1(x: => Int) = x _
| ^^^
|Only function types can be followed by _ but the current expression has type Int
scala> def foo2(x: => Int): () => Int = x _
1 | def foo2(x: => Int): () => Int = x _
| ^^^
|Only function types can be followed by _ but the current expression has type Int
Original file line number Diff line number Diff line change
Expand Up @@ -1050,8 +1050,8 @@ class ErrorMessagesTests extends ErrorMessagesTest {
implicit val ctx: Context = ictx

assertMessageCount(1, messages)
val OnlyFunctionsCanBeFollowedByUnderscore(pt) :: Nil = messages
assertEquals("String(n)", pt.show)
val OnlyFunctionsCanBeFollowedByUnderscore(tp) :: Nil = messages
assertEquals("String", tp.show)
}

@Test def missingEmptyArgumentList =
Expand Down