Skip to content

Commit 1c36605

Browse files
committed
Correctly-ish desugar poly function context bounds in function types
1 parent 4060cf2 commit 1c36605

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,31 +1214,36 @@ object desugar {
12141214
*/
12151215
def expandPolyFunctionContextBounds(tree: PolyFunction)(using Context): PolyFunction =
12161216
val PolyFunction(tparams: List[untpd.TypeDef] @unchecked, fun @ Function(vparamTypes, res)) = tree: @unchecked
1217-
val newTParams = tparams.map {
1217+
val newTParams = tparams.mapConserve {
12181218
case td @ TypeDef(name, cb @ ContextBounds(bounds, ctxBounds)) =>
12191219
TypeDef(name, ContextBounds(bounds, List.empty))
1220+
case t => t
12201221
}
12211222
var idx = 0
1222-
val collecedContextBounds = tparams.collect {
1223+
val collectedContextBounds = tparams.collect {
12231224
case td @ TypeDef(name, cb @ ContextBounds(bounds, ctxBounds)) if ctxBounds.nonEmpty =>
1224-
// TOOD(kπ) Should we handle non empty normal bounds here?
12251225
name -> ctxBounds
12261226
}.flatMap { case (name, ctxBounds) =>
12271227
ctxBounds.map { ctxBound =>
12281228
idx = idx + 1
12291229
ctxBound match
1230-
case ContextBoundTypeTree(_, _, ownName) =>
1231-
ValDef(ownName, ctxBound, EmptyTree).withFlags(TermParam | Given)
1230+
case ctxBound @ ContextBoundTypeTree(tycon, paramName, ownName) =>
1231+
if tree.isTerm then
1232+
ValDef(ownName, ctxBound, EmptyTree).withFlags(TermParam | Given)
1233+
else
1234+
ContextBoundTypeTree(tycon, paramName, EmptyTermName) // this has to be handled in Typer#typedFunctionType
12321235
case _ =>
12331236
makeSyntheticParameter(idx, ctxBound).withAddedFlags(Given)
12341237
}
12351238
}
12361239
val contextFunctionResult =
1237-
if collecedContextBounds.isEmpty then
1238-
fun
1240+
if collectedContextBounds.isEmpty then fun
12391241
else
1240-
Function(vparamTypes, Function(collecedContextBounds, res)).withSpan(fun.span)
1241-
PolyFunction(newTParams, contextFunctionResult).withSpan(tree.span)
1242+
val mods = EmptyModifiers.withFlags(Given)
1243+
val erasedParams = collectedContextBounds.map(_ => false)
1244+
Function(vparamTypes, FunctionWithMods(collectedContextBounds, res, mods, erasedParams)).withSpan(fun.span)
1245+
if collectedContextBounds.isEmpty then tree
1246+
else PolyFunction(newTParams, contextFunctionResult).withSpan(tree.span)
12421247

12431248
/** Desugar [T_1, ..., T_M] => (P_1, ..., P_N) => R
12441249
* Into scala.PolyFunction { def apply[T_1, ..., T_M](x$1: P_1, ..., x$N: P_N): R }

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import annotation.tailrec
4040
import Implicits.*
4141
import util.Stats.record
4242
import config.Printers.{gadts, typr}
43-
import config.Feature, Feature.{migrateTo3, modularity, sourceVersion, warnOnMigration}
43+
import config.Feature, Feature.{migrateTo3, sourceVersion, warnOnMigration}
4444
import config.SourceVersion.*
4545
import rewrites.Rewrites, Rewrites.patch
4646
import staging.StagingLevel
@@ -1142,7 +1142,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
11421142
if templ1.parents.isEmpty
11431143
&& isFullyDefined(pt, ForceDegree.flipBottom)
11441144
&& isSkolemFree(pt)
1145-
&& isEligible(pt.underlyingClassRef(refinementOK = Feature.enabled(modularity)))
1145+
&& isEligible(pt.underlyingClassRef(refinementOK = Feature.enabled(Feature.modularity)))
11461146
then
11471147
templ1 = cpy.Template(templ)(parents = untpd.TypeTree(pt) :: Nil)
11481148
for case parent: RefTree <- templ1.parents do
@@ -1717,7 +1717,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
17171717
typedFunctionType(desugar.makeFunctionWithValDefs(tree, pt), pt)
17181718
else
17191719
val funSym = defn.FunctionSymbol(numArgs, isContextual, isImpure)
1720-
val result = typed(cpy.AppliedTypeTree(tree)(untpd.TypeTree(funSym.typeRef), args :+ body), pt)
1720+
val args1 = args.mapConserve {
1721+
case cb: untpd.ContextBoundTypeTree => typed(cb)
1722+
case t => t
1723+
}
1724+
val result = typed(cpy.AppliedTypeTree(tree)(untpd.TypeTree(funSym.typeRef), args1 :+ body), pt)
17211725
// if there are any erased classes, we need to re-do the typecheck.
17221726
result match
17231727
case r: AppliedTypeTree if r.args.exists(_.tpe.isErasedClass) =>
@@ -2458,12 +2462,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
24582462
if tycon.tpe.typeParams.nonEmpty then
24592463
val tycon0 = tycon.withType(tycon.tpe.etaCollapse)
24602464
typed(untpd.AppliedTypeTree(spliced(tycon0), tparam :: Nil))
2461-
else if Feature.enabled(modularity) && tycon.tpe.member(tpnme.Self).symbol.isAbstractOrParamType then
2465+
else if Feature.enabled(Feature.modularity) && tycon.tpe.member(tpnme.Self).symbol.isAbstractOrParamType then
24622466
val tparamSplice = untpd.TypedSplice(typedExpr(tparam))
24632467
typed(untpd.RefinedTypeTree(spliced(tycon), List(untpd.TypeDef(tpnme.Self, tparamSplice))))
24642468
else
24652469
def selfNote =
2466-
if Feature.enabled(modularity) then
2470+
if Feature.enabled(Feature.modularity) then
24672471
" and\ndoes not have an abstract type member named `Self` either"
24682472
else ""
24692473
errorTree(tree,
@@ -2482,7 +2486,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
24822486
val TypeDef(_, impl: Template) = typed(refineClsDef): @unchecked
24832487
val refinements1 = impl.body
24842488
val seen = mutable.Set[Symbol]()
2485-
for (refinement <- refinements1) { // TODO: get clarity whether we want to enforce these conditions
2489+
for refinement <- refinements1 do // TODO: get clarity whether we want to enforce these conditions
24862490
typr.println(s"adding refinement $refinement")
24872491
checkRefinementNonCyclic(refinement, refineCls, seen)
24882492
val rsym = refinement.symbol
@@ -2496,7 +2500,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
24962500
val member = refineCls.info.member(rsym.name)
24972501
if (member.isOverloaded)
24982502
report.error(OverloadInRefinement(rsym), refinement.srcPos)
2499-
}
25002503
assignType(cpy.RefinedTypeTree(tree)(tpt1, refinements1), tpt1, refinements1, refineCls)
25012504
}
25022505

@@ -4693,7 +4696,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
46934696
cpy.Ident(qual)(qual.symbol.name.sourceModuleName.toTypeName)
46944697
case _ =>
46954698
errorTree(tree, em"cannot convert from $tree to an instance creation expression")
4696-
val tycon = ctorResultType.underlyingClassRef(refinementOK = Feature.enabled(modularity))
4699+
val tycon = ctorResultType.underlyingClassRef(refinementOK = Feature.enabled(Feature.modularity))
46974700
typed(
46984701
untpd.Select(
46994702
untpd.New(untpd.TypedSplice(tpt.withType(tycon))),

tests/pos/contextbounds-for-poly-functions.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import scala.language.future
55
trait Ord[X]:
66
def compare(x: X, y: X): Int
77

8-
val less1 = [X: Ord] => (x: X, y: X) => summon[Ord[X]].compare(x, y) < 0
8+
// val less1 = [X: Ord] => (x: X, y: X) => summon[Ord[X]].compare(x, y) < 0
99

10-
val less2 = [X: Ord as ord] => (x: X, y: X) => ord.compare(x, y) < 0
10+
// val less2 = [X: Ord as ord] => (x: X, y: X) => ord.compare(x, y) < 0
1111

12-
// type Comparer = [X: Ord] => (x: X, y: X) => Boolean
13-
// val less3: Comparer = [X: Ord as ord] => (x: X, y: X) => ord.compare(x, y) < 0
12+
type ComparerRef = [X] => (x: X, y: X) => Ord[X] ?=> Boolean
13+
type Comparer = [X: Ord] => (x: X, y: X) => Boolean
14+
val less3: Comparer = [X: Ord as ord] => (x: X, y: X) => ord.compare(x, y) < 0
1415

1516
// type Cmp[X] = (x: X, y: X) => Boolean
1617
// type Comparer2 = [X: Ord] => Cmp[X]

0 commit comments

Comments
 (0)