File tree 4 files changed +15
-6
lines changed
test/dotty/tools/dotc/reporting
4 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -1837,10 +1837,10 @@ object messages {
1837
1837
}
1838
1838
}
1839
1839
1840
- case class OnlyFunctionsCanBeFollowedByUnderscore (pt : Type )(implicit ctx : Context )
1840
+ case class OnlyFunctionsCanBeFollowedByUnderscore (tp : Type )(implicit ctx : Context )
1841
1841
extends Message (OnlyFunctionsCanBeFollowedByUnderscoreID ) {
1842
1842
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 "
1844
1844
val explanation =
1845
1845
hl """ The syntax ${" x _" } is no longer supported if ${" x" } is not a function.
1846
1846
|To convert to a function value, you need to explicitly write ${" () => x" }"""
Original file line number Diff line number Diff line change @@ -1661,9 +1661,10 @@ class Typer extends Namer
1661
1661
val nestedCtx = ctx.fresh.setNewTyperState()
1662
1662
val res = typed(qual, pt1)(nestedCtx)
1663
1663
res match {
1664
- case res @ closure(_, _, _) =>
1664
+ case closure(_, _, _) =>
1665
1665
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)
1667
1668
if (ctx.scala2Mode) {
1668
1669
// Under -rewrite, patch `x _` to `(() => x)`
1669
1670
patch(Position (tree.pos.start), " (() => " )
Original file line number Diff line number Diff line change @@ -72,3 +72,11 @@ scala> val a: iDontExist = 1
72
72
1 | val a: iDontExist = 1
73
73
| ^^^^^^^^^^
74
74
| 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 {
1050
1050
implicit val ctx : Context = ictx
1051
1051
1052
1052
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)
1055
1055
}
1056
1056
1057
1057
@ Test def missingEmptyArgumentList =
You can’t perform that action at this time.
0 commit comments