Skip to content

Commit

Permalink
Handle named context bounds in poly function context bound desugaring
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Nov 14, 2024
1 parent a76470f commit 8dc68c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1230,15 +1230,19 @@ object desugar {
case td @ TypeDef(name, cb @ ContextBounds(bounds, ctxBounds)) =>
TypeDef(name, ContextBounds(bounds, List.empty))
}
var idx = -1
var idx = 0
val collecedContextBounds = tparams.collect {
case td @ TypeDef(name, cb @ ContextBounds(bounds, ctxBounds)) if ctxBounds.nonEmpty =>
// TOOD(kπ) Should we handle non empty normal bounds here?
name -> ctxBounds
}.flatMap { case (name, ctxBounds) =>
ctxBounds.map { ctxBound =>
idx = idx + 1
makeSyntheticParameter(idx, ctxBound).withAddedFlags(Given)
ctxBound match
case ContextBoundTypeTree(_, _, ownName) =>
ValDef(ownName, ctxBound, EmptyTree).withFlags(TermParam | Given)
case _ =>
makeSyntheticParameter(idx, ctxBound).withAddedFlags(Given)
}
}
val contextFunctionResult =
Expand Down
6 changes: 4 additions & 2 deletions tests/pos/contextbounds-for-poly-functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ trait Ord[X]:

val less1 = [X: Ord] => (x: X, y: X) => summon[Ord[X]].compare(x, y) < 0

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

// type Comparer = [X: Ord] => (x: X, y: X) => Boolean
// val less2: Comparer = [X: Ord as ord] => (x: X, y: X) => ord.compare(x, y) < 0
// val less3: Comparer = [X: Ord as ord] => (x: X, y: X) => ord.compare(x, y) < 0

// type Cmp[X] = (x: X, y: X) => Boolean
// type Comparer2 = [X: Ord] => Cmp[X]
// val less3: Comparer2 = [X: Ord] => (x: X, y: X) => summon[Ord[X]].compare(x, y) < 0
// val less4: Comparer2 = [X: Ord] => (x: X, y: X) => summon[Ord[X]].compare(x, y) < 0

0 comments on commit 8dc68c3

Please sign in to comment.