Skip to content

Commit 164720f

Browse files
jan-pieternatsukagami
authored andcommitted
Disallow context bounds in type lambdas (scala#22659)
Fixes scala#22552 --------- Co-authored-by: Natsu Kagami <[email protected]>
1 parent 8571068 commit 164720f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1677,8 +1677,9 @@ object Parsers {
16771677
val start = in.offset
16781678
val tparams = typeParamClause(ParamOwner.TypeParam)
16791679
if in.token == TLARROW then
1680+
// Filter illegal context bounds and report syntax error
16801681
atSpan(start, in.skipToken()):
1681-
LambdaTypeTree(tparams, toplevelTyp())
1682+
LambdaTypeTree(tparams.mapConserve(stripContextBounds("type lambdas")), toplevelTyp())
16821683
else if in.token == ARROW || isPureArrow(nme.PUREARROW) then
16831684
val arrowOffset = in.skipToken()
16841685
val body = toplevelTyp()
@@ -1699,6 +1700,13 @@ object Parsers {
16991700
typeRest(infixType())
17001701
end typ
17011702

1703+
/** Removes context bounds from TypeDefs and returns a syntax error. */
1704+
private def stripContextBounds(in: String)(tparam: TypeDef) = tparam match
1705+
case TypeDef(name, rhs: ContextBounds) =>
1706+
syntaxError(em"context bounds are not allowed in $in", rhs.span)
1707+
TypeDef(name, rhs.bounds)
1708+
case other => other
1709+
17021710
private def makeKindProjectorTypeDef(name: TypeName): TypeDef = {
17031711
val isVarianceAnnotated = name.startsWith("+") || name.startsWith("-")
17041712
// We remove the variance marker from the name without passing along the specified variance at all
@@ -3267,7 +3275,8 @@ object Parsers {
32673275
* TypTypeParam ::= {Annotation} id [HkTypePamClause] TypeBounds
32683276
*
32693277
* HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} ‘]’
3270-
* HkTypeParam ::= {Annotation} [‘+’ | ‘-’] (id [HkTypePamClause] | ‘_’) TypeBounds
3278+
* HkTypeParam ::= {Annotation} [‘+’ | ‘-’]
3279+
* (id | ‘_’) [HkTypeParamClause] TypeBounds
32713280
*/
32723281
def typeParamClause(paramOwner: ParamOwner): List[TypeDef] = inBracketsWithCommas {
32733282

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)