Skip to content

Commit 744ab30

Browse files
committed
Clarify migration of context bounds
1 parent cdc785f commit 744ab30

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ object desugar {
222222
val epbuf = ListBuffer[ValDef]()
223223
def desugarContextBounds(rhs: Tree): Tree = rhs match {
224224
case ContextBounds(tbounds, cxbounds) =>
225-
epbuf ++= makeImplicitParameters(cxbounds, Implicit, forPrimaryConstructor = isPrimaryConstructor)
225+
val iflag = if ctx.settings.strict.value then Given else Implicit
226+
epbuf ++= makeImplicitParameters(cxbounds, iflag, forPrimaryConstructor = isPrimaryConstructor)
226227
tbounds
227228
case LambdaTypeTree(tparams, body) =>
228229
cpy.LambdaTypeTree(rhs)(tparams, desugarContextBounds(body))

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,21 @@ class Typer extends Namer
26542654
else
26552655
tree
26562656
else if (wtp.isContextualMethod)
2657-
adaptNoArgs(wtp) // insert arguments implicitly
2657+
def isContextBoundParams = wtp.stripPoly match
2658+
case MethodType(EvidenceParamName(_) :: _) => true
2659+
case _ => false
2660+
if ctx.settings.migration.value && ctx.settings.strict.value
2661+
&& isContextBoundParams
2662+
then // Under 3.1 and -migration, don't infer implicit arguments yet for parameters
2663+
// coming from context bounds. Issue a warning instead and offer a patch.
2664+
ctx.migrationWarning(
2665+
em"""Context bounds will map to context parameters.
2666+
|A `with` clause is needed to pass explicit arguments to them.
2667+
|This code can be rewritten automatically using -rewrite""", tree.sourcePos)
2668+
patch(Span(tree.span.end), ".with")
2669+
tree
2670+
else
2671+
adaptNoArgs(wtp) // insert arguments implicitly
26582672
else if (tree.symbol.isPrimaryConstructor && tree.symbol.info.firstParamTypes.isEmpty)
26592673
readapt(tree.appliedToNone) // insert () to primary constructors
26602674
else

docs/docs/reference/contextual/context-bounds-new.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ Context bounds can be combined with subtype bounds. If both are present, subtype
2222
def g[T <: B : C](x: T): R = ...
2323
```
2424

25-
## Syntax
25+
### Migration
26+
27+
To ease migration, context bounds in Dotty map in Scala 3.0 to old-style implicit parameters
28+
for which arguments can be passed either using `.with(...)` or using a normal application.
29+
From Scala 3.1 on, they will map to context parameters instead, as is described above.
30+
31+
If the source version is `3.1` and the `-migration` command-line option is set, any pairing of an evidence
32+
context parameter stemming from a context bound with a normal argument will give a migration
33+
warning. The warning indicates that a `.with(...)` clause should be used instead. The rewrite can be
34+
done automatically under `-rewrite`.
35+
36+
### Syntax
2637

2738
```
2839
TypeParamBounds ::= [SubtypeBounds] {ContextBound}

docs/docs/reference/contextual/relationship-implicits.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ asked for.
105105

106106
### Context Bounds
107107

108-
Context bounds are the same in both language versions. They expand to the respective forms of implicit parameters.
109-
110-
**Note:** To ease migration, context bounds in Dotty map for a limited time to old-style implicit parameters for which arguments can be passed either with `given` or
111-
with a normal application. Once old-style implicits are deprecated, context bounds
112-
will map to given clauses instead.
108+
Context bounds are the same in both language versions.
109+
They expand to `implicit` parameters in Scala 2 and also in Scala 3.0.
110+
They will expand to context parameters from Scala 3.1 on.
113111

114112
### Extension Methods
115113

0 commit comments

Comments
 (0)