Skip to content

Commit 7f01a79

Browse files
committed
Merge pull request #788 from dotty-staging/fix-#781-context-bounds
Fix #781 context bounds
2 parents 03f860b + 6235c45 commit 7f01a79

File tree

6 files changed

+658
-9
lines changed

6 files changed

+658
-9
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ object desugar {
118118
*
119119
* Expand default arguments to default getters. E.g,
120120
*
121-
* def f(x: Int = 1)(y: String = x + "m") = ...
121+
* def f[T: B](x: Int = 1)(y: String = x + "m") = ...
122122
* ==>
123-
* def f(x: Int)(y: String) = ...
124-
* def f$default$1 = 1
125-
* def f$default$2(x: Int) = x + "m"
123+
* def f[T](x: Int)(y: String)(implicit evidence$0: B[T]) = ...
124+
* def f$default$1[T] = 1
125+
* def f$default$2[T](x: Int) = x + "m"
126126
*/
127127
def defDef(meth: DefDef, isPrimaryConstructor: Boolean = false)(implicit ctx: Context): Tree = {
128128
val DefDef(name, tparams, vparamss, tpt, rhs) = meth
@@ -162,15 +162,20 @@ object desugar {
162162
Nil
163163
}
164164

165-
def normalizedVparamss = vparamss map (_ map (vparam =>
165+
def normalizedVparamss = meth1.vparamss map (_ map (vparam =>
166166
cpy.ValDef(vparam)(rhs = EmptyTree)))
167167

168+
def dropContextBound(tparam: TypeDef) = tparam.rhs match {
169+
case ContextBounds(tbounds, _) => cpy.TypeDef(tparam)(rhs = tbounds)
170+
case _ => tparam
171+
}
172+
168173
def defaultGetters(vparamss: List[List[ValDef]], n: Int): List[DefDef] = vparamss match {
169174
case (vparam :: vparams) :: vparamss1 =>
170175
def defaultGetter: DefDef =
171176
DefDef(
172177
name = meth.name.defaultGetterName(n),
173-
tparams = meth.tparams map toDefParam,
178+
tparams = meth.tparams.map(tparam => dropContextBound(toDefParam(tparam))),
174179
vparamss = takeUpTo(normalizedVparamss, n),
175180
tpt = TypeTree(),
176181
rhs = vparam.rhs

src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ object StdNames {
332332
val Ref: N = "Ref"
333333
val RootPackage: N = "RootPackage"
334334
val RootClass: N = "RootClass"
335+
val Scala2: N = "Scala2"
335336
val Select: N = "Select"
336337
val StringContext: N = "StringContext"
337338
val This: N = "This"

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ object Parsers {
258258
} finally inFunReturnType = saved
259259
}
260260

261+
/** Cannot use ctx.featureEnabled because accessing the context would force too much */
262+
private def scala2mode = ctx.settings.language.value.contains(nme.Scala2.toString)
263+
261264
/* ---------- TREE CONSTRUCTION ------------------------------------------- */
262265

263266
/** Convert tree to formal parameter list
@@ -1713,22 +1716,23 @@ object Parsers {
17131716
* DefSig ::= id [DefTypeParamClause] ParamClauses
17141717
*/
17151718
def defDefOrDcl(mods: Modifiers): Tree = atPos(tokenRange) {
1719+
def atScala2Brace = scala2mode && in.token == LBRACE
17161720
if (in.token == THIS) {
17171721
in.nextToken()
17181722
val vparamss = paramClauses(nme.CONSTRUCTOR)
17191723
val rhs = {
1720-
accept(EQUALS)
1724+
if (!atScala2Brace) accept(EQUALS)
17211725
atPos(in.offset) { constrExpr() }
17221726
}
17231727
makeConstructor(Nil, vparamss, rhs).withMods(mods)
17241728
} else {
17251729
val name = ident()
17261730
val tparams = typeParamClauseOpt(ParamOwner.Def)
17271731
val vparamss = paramClauses(name)
1728-
val tpt = fromWithinReturnType(typedOpt())
1732+
var tpt = fromWithinReturnType(typedOpt())
17291733
val rhs =
17301734
if (tpt.isEmpty || in.token == EQUALS) {
1731-
accept(EQUALS)
1735+
if (atScala2Brace) tpt = scalaUnit else accept(EQUALS)
17321736
expr()
17331737
} else EmptyTree
17341738
DefDef(name, tparams, vparamss, tpt, rhs).withMods(mods | Method)

0 commit comments

Comments
 (0)