Skip to content

Commit 5b4b5c2

Browse files
Disallow context bounds in type lambdas (#22659)
Fixes #22552 --------- Co-authored-by: Natsu Kagami <[email protected]>
1 parent 7d2c322 commit 5b4b5c2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1819,8 +1819,9 @@ object Parsers {
18191819
val start = in.offset
18201820
val tparams = typeParamClause(ParamOwner.Type)
18211821
if in.token == TLARROW then
1822+
// Filter illegal context bounds and report syntax error
18221823
atSpan(start, in.skipToken()):
1823-
LambdaTypeTree(tparams, toplevelTyp())
1824+
LambdaTypeTree(tparams.mapConserve(stripContextBounds("type lambdas")), toplevelTyp())
18241825
else if in.token == ARROW || isPureArrow(nme.PUREARROW) then
18251826
val arrowOffset = in.skipToken()
18261827
val body = toplevelTyp(nestedIntoOK(in.token))
@@ -1836,6 +1837,13 @@ object Parsers {
18361837
typeRest(infixType(inContextBound))
18371838
end typ
18381839

1840+
/** Removes context bounds from TypeDefs and returns a syntax error. */
1841+
private def stripContextBounds(in: String)(tparam: TypeDef) = tparam match
1842+
case TypeDef(name, rhs: ContextBounds) =>
1843+
syntaxError(em"context bounds are not allowed in $in", rhs.span)
1844+
TypeDef(name, rhs.bounds)
1845+
case other => other
1846+
18391847
private def makeKindProjectorTypeDef(name: TypeName): TypeDef = {
18401848
val isVarianceAnnotated = name.startsWith("+") || name.startsWith("-")
18411849
// We remove the variance marker from the name without passing along the specified variance at all
@@ -3498,7 +3506,7 @@ object Parsers {
34983506
*
34993507
* HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} ‘]’
35003508
* HkTypeParam ::= {Annotation} [‘+’ | ‘-’]
3501-
* (id | ‘_’) [HkTypePamClause] TypeBounds
3509+
* (id | ‘_’) [HkTypeParamClause] TypeBounds
35023510
*/
35033511
def typeParamClause(paramOwner: ParamOwner): List[TypeDef] = inBracketsWithCommas {
35043512

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)