Skip to content

Commit b63b3c2

Browse files
committed
Fix #2512: Implicit keyword not allowed on return types
1 parent f3b462a commit b63b3c2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ object Parsers {
116116
/** Issue an error at given offset if beyond last error offset
117117
* and update lastErrorOffset.
118118
*/
119-
def syntaxError(msg: => Message, offset: Int = in.offset): Unit =
119+
def syntaxError(msg: => Message, offset: Int = in.offset, name: Name = in.name): Unit =
120120
if (offset > lastErrorOffset) {
121-
val length = if (in.name != null) in.name.show.length else 0
121+
val length = if (name != null) name.show.length else 0
122122
syntaxError(msg, Position(offset, offset + length))
123123
lastErrorOffset = in.offset
124124
}
@@ -730,11 +730,16 @@ object Parsers {
730730
def typ(): Tree = {
731731
val start = in.offset
732732
val isImplicit = in.token == IMPLICIT
733+
var isImplicitFun = false
733734
if (isImplicit) in.nextToken()
734735
def functionRest(params: List[Tree]): Tree =
735736
atPos(start, accept(ARROW)) {
736737
val t = typ()
737-
if (isImplicit) new ImplicitFunction(params, t) else Function(params, t)
738+
if (isImplicit) {
739+
isImplicitFun = true
740+
new ImplicitFunction(params, t)
741+
}
742+
else Function(params, t)
738743
}
739744
val t =
740745
if (in.token == LPAREN) {
@@ -771,6 +776,9 @@ object Parsers {
771776
}
772777
else infixType()
773778

779+
if (isImplicit && !isImplicitFun && in.token != ARROW)
780+
syntaxError("Types with implicit keyword can only be function types", start, nme.IMPLICITkw)
781+
774782
in.token match {
775783
case ARROW => functionRest(t :: Nil)
776784
case FORSOME => syntaxError(ExistentialTypesNoLongerSupported()); t

tests/neg/i2512.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
object Test {
3+
4+
def foo0(): implicit Int => Int = ???
5+
val foo01: implicit (String, Int) => Int = ???
6+
7+
def foo1(f: Int => implicit Int): Int = ??? // error
8+
def foo2(): implicit Int = ??? // error
9+
def foo3(): Int => implicit Int = ??? // error
10+
}

0 commit comments

Comments
 (0)