Skip to content

Disallow context bounds in type lambdas #22659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1798,8 +1798,9 @@ object Parsers {
val start = in.offset
val tparams = typeParamClause(ParamOwner.Type)
if in.token == TLARROW then
// Filter illegal context bounds and report syntax error
atSpan(start, in.skipToken()):
LambdaTypeTree(tparams, toplevelTyp())
LambdaTypeTree(tparams.mapConserve(stripContextBounds("type lambdas")), toplevelTyp())
else if in.token == ARROW || isPureArrow(nme.PUREARROW) then
val arrowOffset = in.skipToken()
val body = toplevelTyp(nestedIntoOK(in.token))
Expand All @@ -1815,6 +1816,13 @@ object Parsers {
typeRest(infixType(inContextBound))
end typ

/** Removes context bounds from TypeDefs and returns a syntax error. */
private def stripContextBounds(in: String)(tparam: TypeDef) = tparam match
case TypeDef(name, rhs: ContextBounds) =>
syntaxError(em"context bounds are not allowed in $in", rhs.span)
TypeDef(name, rhs.bounds)
case other => other

private def makeKindProjectorTypeDef(name: TypeName): TypeDef = {
val isVarianceAnnotated = name.startsWith("+") || name.startsWith("-")
// We remove the variance marker from the name without passing along the specified variance at all
Expand Down Expand Up @@ -3475,7 +3483,7 @@ object Parsers {
*
* HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} ‘]’
* HkTypeParam ::= {Annotation} [‘+’ | ‘-’]
* (id | ‘_’) [HkTypePamClause] TypeBounds
* (id | ‘_’) [HkTypeParamClause] TypeBounds
*/
def typeParamClause(paramOwner: ParamOwner): List[TypeDef] = inBracketsWithCommas {

Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i22552.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- [E040] Syntax Error: tests/neg/i22552.scala:3:10 --------------------------------------------------------------------
3 | type A[X: TC] // error
| ^
| ']' expected, but ':' found
-- Error: tests/neg/i22552.scala:4:15 ----------------------------------------------------------------------------------
4 | type C = [X: TC] =>> List[X] // error
| ^^
| context bounds are not allowed in type lambdas
5 changes: 5 additions & 0 deletions tests/neg/i22552.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait Foo:
type TC[T]
type A[X: TC] // error
type C = [X: TC] =>> List[X] // error
type D = [X: TC] => () => List[X] // allowed context bound
Loading