Skip to content
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

Incorrect inference of dependent types in classes #15840

Open
jchyb opened this issue Aug 10, 2022 · 0 comments
Open

Incorrect inference of dependent types in classes #15840

jchyb opened this issue Aug 10, 2022 · 0 comments

Comments

@jchyb
Copy link
Contributor

jchyb commented Aug 10, 2022

Inspired by the shapeless-2 codebase, as I was looking what still remains to be done there and run into problems with type-level natural numbers implementation there.

Compiler version

3.2.1-RC1-bin-20220805-e560c2d-NIGHTLY, previous stable versions as well

Minimized code

//> using scala "3.nightly"

trait Nat { type N <: Nat }

class _0 extends Nat {
  type N = _0  // source of our issues 
}

class NatOps[N <: Nat](val n: N) {
  // does not work, ToInt[n.N] becomes Nothing
  def toInt(implicit toIntN: ToInt[n.N]): Int = toIntN()
}

// works
def toInt[N <: Nat](n: N)(implicit toIntN: ToInt[n.N]) = toIntN()

sealed abstract class ToInt[N <: Nat] {
  def apply(): Int
}

object ToInt {
  implicit val toInt0: ToInt[_0] = new ToInt {
    def apply(): Int = 0
  }
}

@main def main() =
  println(toInt(new _0)) // works
  println(NatOps(new _0).toInt) // does not work, but NatOps[_0](new _0).toInt works

Output

[error] ./ShapelessNatBug.scala:28:31: No given instance of type Nothing was found for parameter toIntN of method toInt in class NatOps
[error]   println(NatOps(new _0).toInt) // does not work, but NatOps[_0](new _0).toInt works
[error]       

Expectation

Should compile and print "0", like the method version (def toInt) does. Very similar to #10929, but here the dependent types aren't part of the type classes, ToInt[n.N] simply cannot be found because of inference caused by constructor of a class, which I hope makes for a simpler minimization. If the issues are indeed more similar than I though I can move this post there.

@jchyb jchyb added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 10, 2022
@KacperFKorban KacperFKorban added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 10, 2022
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 8, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 11, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 28, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 30, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Feb 2, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Feb 24, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Feb 26, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Feb 29, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Mar 8, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Mar 29, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
odersky added a commit to dotty-staging/dotty that referenced this issue Mar 29, 2024
…5840

Faced with

    given C[T]:
      ...

(with a new line after `:`) we now classify this as new given syntax, and assume ... is a template body.
If one wants to use old syntax, one can still write

    given C[T]
      : ImplementedType ...

# Conflicts:
#	compiler/src/dotty/tools/dotc/parsing/Parsers.scala
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

2 participants