Skip to content

Backport "chore: have a better error message when context bounds are not allowed" to 3.3 LTS #432

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
May 27, 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
11 changes: 8 additions & 3 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ object Parsers {
this == Given || this == ExtensionFollow
def acceptsVariance =
this == Class || this == CaseClass || this == Type

end ParamOwner

enum ParseKind:
Expand Down Expand Up @@ -3283,7 +3282,7 @@ object Parsers {
ok

def typeParam(): TypeDef = {
val isAbstractOwner = paramOwner == ParamOwner.Type || paramOwner == ParamOwner.TypeParam
val isAbstractOwner = (paramOwner == ParamOwner.Type || paramOwner == ParamOwner.TypeParam)
val start = in.offset
var mods = annotsAsMods() | Param
if paramOwner == ParamOwner.Class || paramOwner == ParamOwner.CaseClass then
Expand All @@ -3304,7 +3303,13 @@ object Parsers {
}
else ident().toTypeName
val hkparams = typeParamClauseOpt(ParamOwner.Type)
val bounds = if (isAbstractOwner) typeBounds() else typeParamBounds(name)
// val bounds = if (isAbstractOwner) typeBounds() else typeParamBounds(name)
val bounds = typeParamBounds(name) match
case bounds: TypeBoundsTree => bounds
case bounds: ContextBounds if !isAbstractOwner => bounds
case ContextBounds(bounds, cxBounds) =>
for cbound <- cxBounds do report.error(IllegalContextBounds(), cbound.srcPos)
bounds
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case FormatInterpolationErrorID // errorNumber: 209
case ValueClassCannotExtendAliasOfAnyValID // errorNumber: 210
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 @@ -3255,3 +3255,11 @@ class MatchIsNotPartialFunction(using Context) extends SyntaxMsg(MatchIsNotParti
|
|Efficient operations will use `applyOrElse` to avoid computing the match twice,
|but the `apply` body would be executed "per element" in the example."""

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
18 changes: 9 additions & 9 deletions tests/neg/i22552.check
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- [E040] Syntax Error: tests/neg/i22552.scala:3:10 --------------------------------------------------------------------
-- [E214] Syntax Error: tests/neg/i22552.scala:3:10 --------------------------------------------------------------------
3 | type A[X: TC] // error
| ^
| ']' expected, but ':' found
-- [E040] Syntax Error: tests/neg/i22552.scala:4:13 --------------------------------------------------------------------
| ^^^^
| Context bounds are not allowed in this position
-- [E214] Syntax Error: tests/neg/i22552.scala:4:13 --------------------------------------------------------------------
4 | type C = [X: TC] =>> List[X] // error
| ^
| ']' expected, but ':' found
-- [E040] Syntax Error: tests/neg/i22552.scala:5:13 --------------------------------------------------------------------
| ^^^^
| Context bounds are not allowed in this position
-- [E214] Syntax Error: tests/neg/i22552.scala:5:13 --------------------------------------------------------------------
5 | type D = [X: TC] => () => List[X] // error
| ^
| ']' expected, but ':' found
| ^^^^
| Context bounds are not allowed in this position
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:10 --------------------------------------------------------------------
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