Skip to content

Commit

Permalink
Fix definition of enclosingExtensionMethod
Browse files Browse the repository at this point in the history
Now also finds enclosing methods outside the current class.

Fixes scala#13075
  • Loading branch information
odersky authored and BarkingBad committed Jul 23, 2021
1 parent 9a41298 commit 9adecd5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,10 @@ object SymDenotations {
else NoSymbol

/** The closest enclosing extension method containing this definition,
* provided the extension method appears in the same class.
* including methods outside the current class.
*/
final def enclosingExtensionMethod(using Context): Symbol =
if this.is(ExtensionMethod) then symbol
else if this.isClass then NoSymbol
else if this.exists then owner.enclosingExtensionMethod
else NoSymbol

Expand Down
40 changes: 40 additions & 0 deletions tests/neg/i13075.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
object Implementing_Tuples:

sealed trait Tup
case class ConsTup[T, H <: Tup](head: T, tail: H) extends Tup
case object EmptyTup extends Tup

val *: = ConsTup // for unapply
type *:[H, T <: Tup] = ConsTup[H, T] // for type matching
type EmptyTup = EmptyTup.type // for type matching

extension [H](head: H)
def *:[T <: Tup](tail: T) = ConsTup(head, tail)

type Fold[T <: Tup, Seed, F[_,_]] = T match
case EmptyTup => Seed
case h *: t => Fold[t, F[Seed, h], F]

extension [T <: Tup](v: T)
def fold[Seed, F[_,_]](seed: Seed)(
fn: [C, Acc] => (C, Acc) => F[C, Acc]
): Fold[T, Seed, F] =
(v match
case EmptyTup => seed
case h *: t => t.fold(fn(h, seed))(fn)
).asInstanceOf[Fold[T, Seed, F]]

extension [T <: Tup](v: T) def reversed: Tup =
v.fold[EmptyTup, [C, Acc] =>> Acc match {
case h *: t => C *: h *: t
}](EmptyTup)(
[C, Acc] => (c: C, acc: Acc) => acc match
case _@(_ *: _) => c *: acc // error
)

@main def testProperFold =
val t = (1 *: '2' *: "foo" *: EmptyTup)
val reversed: (String *: Char *: Int *: EmptyTup) = t.reversed // error
println(reversed)

end Implementing_Tuples

0 comments on commit 9adecd5

Please sign in to comment.