File tree Expand file tree Collapse file tree 4 files changed +15
-6
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -1835,10 +1835,10 @@ object messages {
18351835 }
18361836 }
18371837
1838- case class OnlyFunctionsCanBeFollowedByUnderscore (pt : Type )(implicit ctx : Context )
1838+ case class OnlyFunctionsCanBeFollowedByUnderscore (tp : Type )(implicit ctx : Context )
18391839 extends Message (OnlyFunctionsCanBeFollowedByUnderscoreID ) {
18401840 val kind = " Syntax"
1841- val msg = hl " Not a function: $pt : cannot be followed by ${" _" }"
1841+ val msg = hl " Only function types can be followed by ${" _" } but the current expression has type $tp "
18421842 val explanation =
18431843 hl """ The syntax ${" x _" } is no longer supported if ${" x" } is not a function.
18441844 |To convert to a function value, you need to explicitly write ${" () => x" }"""
Original file line number Diff line number Diff line change @@ -1645,9 +1645,10 @@ class Typer extends Namer
16451645 val nestedCtx = ctx.fresh.setNewTyperState()
16461646 val res = typed(qual, pt1)(nestedCtx)
16471647 res match {
1648- case res @ closure(_, _, _) =>
1648+ case closure(_, _, _) =>
16491649 case _ =>
1650- ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore (res.tpe), tree.pos)
1650+ val recovered = typed(qual)(ctx.fresh.setExploreTyperState())
1651+ ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore (recovered.tpe.widen), tree.pos)
16511652 if (ctx.scala2Mode) {
16521653 // Under -rewrite, patch `x _` to `(() => x)`
16531654 patch(Position (tree.pos.start), " (() => " )
Original file line number Diff line number Diff line change @@ -72,3 +72,11 @@ scala> val a: iDontExist = 1
72721 | 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
Original file line number Diff line number Diff 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 =
You can’t perform that action at this time.
0 commit comments