Skip to content

chore: have a better error message when context bounds are not allowed #23190

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 1 commit into from
May 22, 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
14 changes: 8 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ object Parsers {
this == Given || this == ExtensionFollow
def acceptsVariance =
this == Class || this == CaseClass || this == Hk
def acceptsCtxBounds =
!(this == Type || this == Hk)
def acceptsCtxBounds(using Context) =
!(this == Type || this == Hk) || (sourceVersion.enablesNewGivens && this == Type)
def acceptsWildcard =
this == Type || this == Hk

Expand Down Expand Up @@ -3549,10 +3549,12 @@ object Parsers {
else ident().toTypeName
val isCap = gobbleHat()
val hkparams = typeParamClauseOpt(ParamOwner.Hk)
val bounds =
if paramOwner.acceptsCtxBounds then typeAndCtxBounds(name)
else if sourceVersion.enablesNewGivens && paramOwner == ParamOwner.Type then typeAndCtxBounds(name)
else typeBounds()
val bounds = typeAndCtxBounds(name) match
case bounds: TypeBoundsTree => bounds
case bounds: ContextBounds if paramOwner.acceptsCtxBounds => bounds
case ContextBounds(bounds, cxBounds) =>
for cbound <- cxBounds do report.error(IllegalContextBounds(), cbound.srcPos)
bounds
val res = TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
if isCap then
res.pushAttachment(CaptureVar, ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case MatchIsNotPartialFunctionID // errorNumber: 211
case OnlyFullyDependentAppliedConstructorTypeID // errorNumber: 212
case PointlessAppliedConstructorTypeID // errorNumber: 213
case IllegalContextBoundsID // errorNumber: 214

def errorNumber = ordinal - 1

Expand Down
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3510,3 +3510,11 @@ final class OnlyFullyDependentAppliedConstructorType()(using Context)
i"Applied constructor type can only be used with classes where all parameters in the first parameter list are tracked"

override protected def explain(using Context): String = ""

final class IllegalContextBounds(using Context) extends SyntaxMsg(IllegalContextBoundsID):
override protected def msg(using Context): String =
i"Context bounds are not allowed in this position"

override protected def explain(using Context): String = ""

end IllegalContextBounds
6 changes: 3 additions & 3 deletions tests/neg/i22552.check
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- [E040] Syntax Error: tests/neg/i22552.scala:3:10 --------------------------------------------------------------------
-- [E214] Syntax Error: tests/neg/i22552.scala:3:14 --------------------------------------------------------------------
3 | type A[X: TC] // error
| ^
| ']' expected, but ':' found
| ^^
| Context bounds are not allowed in this position
-- Error: tests/neg/i22552.scala:4:15 ----------------------------------------------------------------------------------
4 | type C = [X: TC] =>> List[X] // error
| ^^
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i22660.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- [E214] Syntax Error: tests/neg/i22660.scala:2:21 --------------------------------------------------------------------
2 |type Foo[T: Equatable] // error
| ^^^^^^^^^
| Context bounds are not allowed in this position
2 changes: 2 additions & 0 deletions tests/neg/i22660.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trait Equatable[T]
type Foo[T: Equatable] // error
Loading