Skip to content

Cannot use type members of type parameters leaves in method signatures #18898

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
kyouko-taiga opened this issue Nov 10, 2023 · 2 comments
Closed

Comments

@kyouko-taiga
Copy link
Contributor

Compiler version

Scala compiler version 3.3.1 -- Copyright 2002-2023, LAMP/EPFL

Minimized example

import scala.collection.mutable

/// A parser combinator producing instances of `E`.
trait Combinator[E] {
  type Context
  def parse(context: Context): Option[E]
}

/// A parser combinator producing elements by applying `action`.
final case class Apply[C, E](val action: C => Option[E]) extends Combinator[E] {
  type Context = C
  def parse(context: Context): Option[E] = action(context)
}

/// A combinator combining the result of other combinators.
final case class Combine[A, B](
    val first: Combinator[A],
    val second: Combinator[B]
)(using
    first.Context =:= second.Context
) extends Combinator[(A, B)] {
  type Context = first.Context
  def parse(context: Context): Option[(A, B)] = ???
}

@main def hello: Unit = {
  val source = (0 to 10).toList.to(mutable.ListBuffer)
  val n: Combinator[Int] = Apply[mutable.ListBuffer[Int], Int](s => { val h = s.head; s.remove(0); Some(h) })
  val parser = Combine(n, n)
  
  parser.parse(source)
}

Output

[error] -- [E007] Type Mismatch Error
[error] 27 |  val r = parser.parse(source)
[error]    |                       ^^^^^^
[error]    |           Found:    (source : scala.collection.mutable.ListBuffer[Int])
[error]    |           Required: parser.Context

Expectation

The compiler should figure out that parser.Context is indeed scala.collection.mutable.ListBuffer[Int].

@kyouko-taiga kyouko-taiga added the stat:needs triage Every issue needs to have an "area" and "itype" label label Nov 10, 2023
@nicolasstucki nicolasstucki added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 10, 2023
@odersky
Copy link
Contributor

odersky commented Nov 10, 2023

This is not a simple bug fix. It means we have to go much further into dependent types territory, which is at least a SIP, and might be a whole new research project. See also #3964

@odersky
Copy link
Contributor

odersky commented Jan 16, 2024

This is fixed by #19395, if one gives the modularity language import.

@odersky odersky closed this as completed Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants