Skip to content

Commit 55b36ea

Browse files
committed
Disallow context bounds in type lambdas
1 parent 93af7b8 commit 55b36ea

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -1798,8 +1798,22 @@ object Parsers {
17981798
val start = in.offset
17991799
val tparams = typeParamClause(ParamOwner.Type)
18001800
if in.token == TLARROW then
1801-
atSpan(start, in.skipToken()):
1802-
LambdaTypeTree(tparams, toplevelTyp())
1801+
val hasContextBounds = tparams.exists(_.rhs match {
1802+
case x: ContextBounds => true
1803+
case _ => false
1804+
})
1805+
if hasContextBounds then
1806+
// Filter illegal context bounds and report syntax error
1807+
atSpan(start, in.skipToken()):
1808+
LambdaTypeTree(tparams.map {
1809+
case TypeDef(name, rhs: ContextBounds) =>
1810+
syntaxError(em"context bounds are not allowed in type lambdas", rhs.span)
1811+
TypeDef(name, TypeBoundsTree(EmptyTree, EmptyTree))
1812+
case other => other
1813+
}, toplevelTyp())
1814+
else
1815+
atSpan(start, in.skipToken()):
1816+
LambdaTypeTree(tparams, toplevelTyp())
18031817
else if in.token == ARROW || isPureArrow(nme.PUREARROW) then
18041818
val arrowOffset = in.skipToken()
18051819
val body = toplevelTyp(nestedIntoOK(in.token))
@@ -3475,7 +3489,7 @@ object Parsers {
34753489
*
34763490
* HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} ‘]’
34773491
* HkTypeParam ::= {Annotation} [‘+’ | ‘-’]
3478-
* (id | ‘_’) [HkTypePamClause] TypeBounds
3492+
* (id | ‘_’) [HkTypeParamClause] TypeBounds
34793493
*/
34803494
def typeParamClause(paramOwner: ParamOwner): List[TypeDef] = inBracketsWithCommas {
34813495

tests/neg/i22552.check

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E040] Syntax Error: tests/neg/i22552.scala:3:10 --------------------------------------------------------------------
2+
3 | type A[X: TC] // error
3+
| ^
4+
| ']' expected, but ':' found
5+
-- Error: tests/neg/i22552.scala:4:15 ----------------------------------------------------------------------------------
6+
4 | type C = [X: TC] =>> List[X] // error
7+
| ^^
8+
| context bounds are not allowed in type lambdas

tests/neg/i22552.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait Foo:
2+
type TC[T]
3+
type A[X: TC] // error
4+
type C = [X: TC] =>> List[X] // error
5+
type D = [X: TC] => () => List[X] // allowed context bound

0 commit comments

Comments
 (0)