Skip to content

Fix eta-expanding guards in adaptType #19960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4281,7 +4281,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer

def adaptType(tp: Type): Tree = {
val tree1 =
if ((pt eq AnyTypeConstructorProto) || tp.typeParamSymbols.isEmpty) tree
if pt eq AnyTypeConstructorProto then tree
else if tp.typeParamSymbols.isEmpty || tp.typeParamSymbols.isEmpty then
// call typeParamSymbols twice, to get the stable results
// (see also note inside typeParamSymbols)
// given `type LifecycleF = [_] =>> Any` in pos/i19942.scala
// with an Ident of LifecycleF, calling typeParams will return:
// 1. [type _] a list of the symbol _ in the type def tree, on the first call
// 2. [+_] a list of a lambda param, afterwards
// we only want to eta-expand if there are real type param symbols, so we check twice
tree
else {
if (ctx.isJava)
// Cook raw type
Expand Down
29 changes: 29 additions & 0 deletions tests/pos/i19942.1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
trait Alternative[F[_]]

opaque type Derived[A] = A
object Derived:
extension [A](derived: Derived[A]) def instance: A = derived
infix type <<<[F[_], G[_]] = [x] =>> F[G[x]]

import Derived.*
import scala.compiletime.summonInline

type DerivedAlternative[F[_]] = Derived[Alternative[F]]
object DerivedAlternative:
inline def apply[F[_]]: Alternative[F] =
import DerivedAlternative.given
summonInline[DerivedAlternative[F]].instance
given nested[F[_], G[_]]: DerivedAlternative[F <<< G] = ???

object auto:
object alternative:
transparent inline given [F[_]]: Alternative[F] = DerivedAlternative[F]

trait Test:
import Test.*
import auto.alternative.given
val fails = summon[Alternative[OptList]]

// Fails if companion object defined AFTER trait
object Test:
type OptList[A] = Option[List[A]]
23 changes: 23 additions & 0 deletions tests/pos/i19942.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type LifecycleF = [_] =>> Any
trait Lifecycle[+F[_], +A]

trait LifecycleTag[R]
object LifecycleTag:
implicit def resourceTag[R <: Lifecycle[F0, A0], F0[_], A0]: LifecycleTag[R] = ???

trait MakeDSL[T]:
final def fromResource[R <: Lifecycle[LifecycleF, T]](implicit tag: LifecycleTag[R]): Any = ???

object distage:
// Cannot be defined in the same scope as rest of code
final type Identity[+A] = A
import distage.*

trait Resource
trait DependentResource() extends Lifecycle[Identity, Resource]

@main def Test = {
val dsl: MakeDSL[Resource] = ???
val fails = dsl.fromResource[DependentResource]
val works = dsl.fromResource[DependentResource](using LifecycleTag.resourceTag[DependentResource, Identity, Resource])
}