Skip to content

Commit be01462

Browse files
authored
Merge pull request #432 from scala/backport-lts-3.3-23190
Backport "chore: have a better error message when context bounds are not allowed" to 3.3 LTS
2 parents 6e14664 + 0a3abee commit be01462

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ object Parsers {
6767
this == Given || this == ExtensionFollow
6868
def acceptsVariance =
6969
this == Class || this == CaseClass || this == Type
70-
7170
end ParamOwner
7271

7372
enum ParseKind:
@@ -3283,7 +3282,7 @@ object Parsers {
32833282
ok
32843283

32853284
def typeParam(): TypeDef = {
3286-
val isAbstractOwner = paramOwner == ParamOwner.Type || paramOwner == ParamOwner.TypeParam
3285+
val isAbstractOwner = (paramOwner == ParamOwner.Type || paramOwner == ParamOwner.TypeParam)
32873286
val start = in.offset
32883287
var mods = annotsAsMods() | Param
32893288
if paramOwner == ParamOwner.Class || paramOwner == ParamOwner.CaseClass then
@@ -3304,7 +3303,13 @@ object Parsers {
33043303
}
33053304
else ident().toTypeName
33063305
val hkparams = typeParamClauseOpt(ParamOwner.Type)
3307-
val bounds = if (isAbstractOwner) typeBounds() else typeParamBounds(name)
3306+
// val bounds = if (isAbstractOwner) typeBounds() else typeParamBounds(name)
3307+
val bounds = typeParamBounds(name) match
3308+
case bounds: TypeBoundsTree => bounds
3309+
case bounds: ContextBounds if !isAbstractOwner => bounds
3310+
case ContextBounds(bounds, cxBounds) =>
3311+
for cbound <- cxBounds do report.error(IllegalContextBounds(), cbound.srcPos)
3312+
bounds
33083313
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
33093314
}
33103315
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
225225
case FormatInterpolationErrorID // errorNumber: 209
226226
case ValueClassCannotExtendAliasOfAnyValID // errorNumber: 210
227227
case MatchIsNotPartialFunctionID // errorNumber: 211
228+
case OnlyFullyDependentAppliedConstructorTypeID // errorNumber: 212
229+
case PointlessAppliedConstructorTypeID // errorNumber: 213
230+
case IllegalContextBoundsID // errorNumber: 214
228231

229232
def errorNumber = ordinal - 1
230233

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,3 +3255,11 @@ class MatchIsNotPartialFunction(using Context) extends SyntaxMsg(MatchIsNotParti
32553255
|
32563256
|Efficient operations will use `applyOrElse` to avoid computing the match twice,
32573257
|but the `apply` body would be executed "per element" in the example."""
3258+
3259+
final class IllegalContextBounds(using Context) extends SyntaxMsg(IllegalContextBoundsID):
3260+
override protected def msg(using Context): String =
3261+
i"Context bounds are not allowed in this position"
3262+
3263+
override protected def explain(using Context): String = ""
3264+
3265+
end IllegalContextBounds

tests/neg/i22552.check

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

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:10 --------------------------------------------------------------------
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)