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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3549,11 +3549,17 @@ 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 res = TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
val bounds = typeAndCtxBounds(name)
val res = bounds match
case bounds: ContextBounds if paramOwner.acceptsCtxBounds =>
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
case bounds: ContextBounds if sourceVersion.enablesNewGivens && paramOwner == ParamOwner.Type =>
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
case ContextBounds(bounds, _) =>
report.error(IllegalContextBounds(), bounds.srcPos)
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
case bounds: TypeBoundsTree =>
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
if isCap then
res.pushAttachment(CaptureVar, ())
// putting the attachment here as well makes post-processing in the typer easier
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:12 --------------------------------------------------------------------
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:12 --------------------------------------------------------------------
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