Skip to content

Commit 84ea7eb

Browse files
authored
Merge pull request #362 from scala/backport-lts-3.3-23015
Backport "Disallow context function types as value-class parameters" to 3.3 LTS
2 parents 74d689a + af06082 commit 84ea7eb

File tree

9 files changed

+20
-122
lines changed

9 files changed

+20
-122
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,10 @@ private sealed trait WarningSettings:
192192
ChoiceWithHelp("linted", "Enable -Wunused:imports,privates,locals,implicits"),
193193
ChoiceWithHelp(
194194
name = "strict-no-implicit-warn",
195-
description = "Same as -Wunused:import, only for imports of explicit named members.\n" +
196-
"NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all"
195+
description = """Same as -Wunused:imports, only for imports of explicit named members.
196+
|NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all""".stripMargin
197197
),
198+
ChoiceWithHelp("unsafe-warn-patvars", "Deprecated alias for `patvars`"),
198199
),
199200
default = Nil
200201
)
@@ -219,7 +220,7 @@ private sealed trait WarningSettings:
219220
def params(using Context) = allOr("params")
220221
def privates(using Context) =
221222
allOr("privates") || allOr("linted")
222-
def patvars(using Context) = allOr("patvars")
223+
def patvars(using Context) = allOr("patvars") || isChoiceSet("unsafe-warn-patvars")
223224
def inlined(using Context) = isChoiceSet("inlined")
224225
def linted(using Context) =
225226
allOr("linted")

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,9 +1677,8 @@ object Parsers {
16771677
val start = in.offset
16781678
val tparams = typeParamClause(ParamOwner.TypeParam)
16791679
if in.token == TLARROW then
1680-
// Filter illegal context bounds and report syntax error
16811680
atSpan(start, in.skipToken()):
1682-
LambdaTypeTree(tparams.mapConserve(stripContextBounds("type lambdas")), toplevelTyp())
1681+
LambdaTypeTree(tparams, toplevelTyp())
16831682
else if in.token == ARROW || isPureArrow(nme.PUREARROW) then
16841683
val arrowOffset = in.skipToken()
16851684
val body = toplevelTyp()
@@ -1700,13 +1699,6 @@ object Parsers {
17001699
typeRest(infixType())
17011700
end typ
17021701

1703-
/** Removes context bounds from TypeDefs and returns a syntax error. */
1704-
private def stripContextBounds(in: String)(tparam: TypeDef) = tparam match
1705-
case TypeDef(name, rhs: ContextBounds) =>
1706-
syntaxError(em"context bounds are not allowed in $in", rhs.span)
1707-
TypeDef(name, rhs.bounds)
1708-
case other => other
1709-
17101702
private def makeKindProjectorTypeDef(name: TypeName): TypeDef = {
17111703
val isVarianceAnnotated = name.startsWith("+") || name.startsWith("-")
17121704
// We remove the variance marker from the name without passing along the specified variance at all
@@ -3275,8 +3267,7 @@ object Parsers {
32753267
* TypTypeParam ::= {Annotation} id [HkTypePamClause] TypeBounds
32763268
*
32773269
* HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} ‘]’
3278-
* HkTypeParam ::= {Annotation} [‘+’ | ‘-’]
3279-
* (id | ‘_’) [HkTypeParamClause] TypeBounds
3270+
* HkTypeParam ::= {Annotation} [‘+’ | ‘-’] (id [HkTypePamClause] | ‘_’) TypeBounds
32803271
*/
32813272
def typeParamClause(paramOwner: ParamOwner): List[TypeDef] = inBracketsWithCommas {
32823273

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ object Checking {
755755
}
756756
clParamAccessors match {
757757
case param :: params =>
758+
if (defn.isContextFunctionType(param.info))
759+
report.error("value classes are not allowed for context function types", param.srcPos)
758760
if (param.is(Mutable))
759761
report.error(ValueClassParameterMayNotBeAVar(clazz, param), param.srcPos)
760762
if (param.info.isInstanceOf[ExprType])

tests/neg/i22552.check

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

tests/neg/i22552.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ trait Foo:
22
type TC[T]
33
type A[X: TC] // error
44
type C = [X: TC] =>> List[X] // error
5-
type D = [X: TC] => () => List[X] // allowed context bound
5+
type D = [X: TC] => () => List[X] // error

tests/neg/i22752.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Inner(body: Int ?=> Int) extends AnyVal: // error
2+
def rescue: Int ?=> Int = ???
3+
4+
class Inner2(body: Int => Int) extends AnyVal // ok

tests/pos/i22643.scala

Lines changed: 0 additions & 48 deletions
This file was deleted.

tests/pos/i22645a.scala

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/pos/i22645b.scala

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)