Skip to content

Commit 6896fe3

Browse files
authored
chore: have a better error message when context bounds are not allowed (#23190)
Closes #22660
2 parents 797bbb7 + 7294607 commit 6896fe3

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ object Parsers {
6868
this == Given || this == ExtensionFollow
6969
def acceptsVariance =
7070
this == Class || this == CaseClass || this == Hk
71-
def acceptsCtxBounds =
72-
!(this == Type || this == Hk)
71+
def acceptsCtxBounds(using Context) =
72+
!(this == Type || this == Hk) || (sourceVersion.enablesNewGivens && this == Type)
7373
def acceptsWildcard =
7474
this == Type || this == Hk
7575

@@ -3549,10 +3549,12 @@ object Parsers {
35493549
else ident().toTypeName
35503550
val isCap = gobbleHat()
35513551
val hkparams = typeParamClauseOpt(ParamOwner.Hk)
3552-
val bounds =
3553-
if paramOwner.acceptsCtxBounds then typeAndCtxBounds(name)
3554-
else if sourceVersion.enablesNewGivens && paramOwner == ParamOwner.Type then typeAndCtxBounds(name)
3555-
else typeBounds()
3552+
val bounds = typeAndCtxBounds(name) match
3553+
case bounds: TypeBoundsTree => bounds
3554+
case bounds: ContextBounds if paramOwner.acceptsCtxBounds => bounds
3555+
case ContextBounds(bounds, cxBounds) =>
3556+
for cbound <- cxBounds do report.error(IllegalContextBounds(), cbound.srcPos)
3557+
bounds
35563558
val res = TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
35573559
if isCap then
35583560
res.pushAttachment(CaptureVar, ())

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
227227
case MatchIsNotPartialFunctionID // errorNumber: 211
228228
case OnlyFullyDependentAppliedConstructorTypeID // errorNumber: 212
229229
case PointlessAppliedConstructorTypeID // errorNumber: 213
230+
case IllegalContextBoundsID // errorNumber: 214
230231

231232
def errorNumber = ordinal - 1
232233

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3516,3 +3516,11 @@ final class OnlyFullyDependentAppliedConstructorType()(using Context)
35163516
i"Applied constructor type can only be used with classes where all parameters in the first parameter list are tracked"
35173517

35183518
override protected def explain(using Context): String = ""
3519+
3520+
final class IllegalContextBounds(using Context) extends SyntaxMsg(IllegalContextBoundsID):
3521+
override protected def msg(using Context): String =
3522+
i"Context bounds are not allowed in this position"
3523+
3524+
override protected def explain(using Context): String = ""
3525+
3526+
end IllegalContextBounds

tests/neg/i22552.check

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
-- [E040] Syntax Error: tests/neg/i22552.scala:3:10 --------------------------------------------------------------------
1+
-- [E214] Syntax Error: tests/neg/i22552.scala:3:14 --------------------------------------------------------------------
22
3 | type A[X: TC] // error
3-
| ^
4-
| ']' expected, but ':' found
3+
| ^^
4+
| Context bounds are not allowed in this position
55
-- Error: tests/neg/i22552.scala:4:15 ----------------------------------------------------------------------------------
66
4 | type C = [X: TC] =>> List[X] // error
77
| ^^

tests/neg/i22660.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- [E214] Syntax Error: tests/neg/i22660.scala:2:21 --------------------------------------------------------------------
2+
2 |type Foo[T: Equatable] // error
3+
| ^^^^^^^^^
4+
| Context bounds are not allowed in this position

tests/neg/i22660.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
trait Equatable[T]
2+
type Foo[T: Equatable] // error

0 commit comments

Comments
 (0)