Skip to content

Commit 7d9cf12

Browse files
hamzaremmalajafri2001jan-pieter
committed
chore: have a better error message when context bounds are not allowed
Co-authored-by: ajafri2001 <[email protected]> Co-authored-by: jan-pieter <[email protected]>
1 parent d36e423 commit 7d9cf12

File tree

6 files changed

+29
-8
lines changed

6 files changed

+29
-8
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,11 +3549,17 @@ 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()
3556-
val res = TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
3552+
val bounds = typeAndCtxBounds(name)
3553+
val res = bounds match
3554+
case bounds: ContextBounds if paramOwner.acceptsCtxBounds =>
3555+
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
3556+
case bounds: ContextBounds if sourceVersion.enablesNewGivens && paramOwner == ParamOwner.Type =>
3557+
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
3558+
case ContextBounds(bounds, _) =>
3559+
report.error(IllegalContextBounds(), bounds.srcPos)
3560+
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
3561+
case bounds: TypeBoundsTree =>
3562+
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
35573563
if isCap then
35583564
res.pushAttachment(CaptureVar, ())
35593565
// putting the attachment here as well makes post-processing in the typer easier

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
@@ -3510,3 +3510,11 @@ final class OnlyFullyDependentAppliedConstructorType()(using Context)
35103510
i"Applied constructor type can only be used with classes where all parameters in the first parameter list are tracked"
35113511

35123512
override protected def explain(using Context): String = ""
3513+
3514+
final class IllegalContextBounds(using Context) extends SyntaxMsg(IllegalContextBoundsID):
3515+
override protected def msg(using Context): String =
3516+
i"Context bounds are not allowed in this position"
3517+
3518+
override protected def explain(using Context): String = ""
3519+
3520+
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:12 --------------------------------------------------------------------
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:12 --------------------------------------------------------------------
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)