question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type inference bug for type member based on a dependent type

See original GitHub issue

Compiler version

3.0.0

Minimized code

trait X:
  type Out
  def value: Out

class XWrapper[XX <: X](val x: XX):
  type Out = x.Out

object XWrapper:
  type Aux[T, U] = XWrapper[T] { type Out = U }

def getValue[XX <: X, U](y: XWrapper.Aux[XX, U]): U =
  y.x.value

Output

Compilation error:

Found:    y.x.Out
Required: U
    y.x.value

Expectation

The compiler should know that y.x.Out and U are the same type

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Jun 21, 2021

Both X and XWrapper have a member called Out, but they’re not the same member. Let’s give them their own name to make things clearer:

trait X:
  type A
  def value: A

class XWrapper[XX <: X](val x: XX):
  type B = x.A

object XWrapper:
  type Aux[T <: X, U] = XWrapper[T] { type B = U }

  def getValue[XX <: X, U](y: XWrapper.Aux[XX, U]): U =
    y.x.value
-- [E007] Type Mismatch Error: try/i12886.scala:12:8 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
12 |    y.x.value
   |    ^^^^^^^^^
   |    Found:    y.x.A
   |    Required: U

Here I think it’s clear that the issue is that A does not match U, the value of B doesn’t matter since y.x has type y.x.A.

0reactions
smartercommented, Jun 21, 2021

When the compiler checks y.x.A <: U, U isn’t constrained (it’s declared as just U without bounds in def getValue[XX <: X, U]), the fact that U has to be equal to y.x.A is something that can be derived from the fact that it appears as the rhs of B and that B is already defined to be equal to x.A, but that’s a very indirect complex reasoning the compiler isn’t going to perform.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed type inference when path dependent types are used as ...
I don't know if this is the right place to report it, but this code demonstrates an issue when path dependent types are...
Read more >
How to help the Scala 3 compiler infer a path-dependent-type?
To make this compile we could use singleton types class B(val a: a0.type) or add a type parameter class B[_T](val a: A {...
Read more >
Type inference for object definitions - open-std
This feature allows declarations to infer types from the expressions that are used as their initializers. This is part of a series of...
Read more >
Type Inference, Haskell and Dependent Types - Adam Gundry
The first part proposes an approach to unification and type inference, based on information increase in dependency-ordered contexts, and keeping careful ...
Read more >
What is the runtime/time complexity of Coq's (Dependent ...
I was wondering, since Coq allows programs/values themselves to be in the Type somehow my guess would be that type inference is either:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found