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.

Compiler version

Tested with 3.0.2 and 3.1.0-RC2

Minimized code

https://github.com/vigoo/scala3-type-infer-bug

Contains a minimal example (the issue originally came up in a parser library under development) and two files from ZIO, Zippable and Unzippable as the issue seems to be related to these specific implicits.

Output

[error] 26 |    val _ = assertSameType(xy2)
[error]    |                           ^^^
[error]    |                      Found:    (Main.xy2 : Example[Int, (AnyVal, Int)])
[error]    |                      Required: Example[T, T]
[error]    |
[error]    |                      where:    T is a type variable

Expectation

The

 val x = pure(1)
 val y = pure(2)
 val xy1 = x zip y

and

val xy2 = pure(1) zip pure(2)

should both be inferred to the same result, Example[(Int, Int), (Int, Int)].

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Oct 12, 2021

In fact, taking a closer look at https://github.com/lampepfl/dotty/issues/13646#issuecomment-933551826, we do have I qualifying to be instantiated in the zip example, but it doesn’t get instantiated because of another heuristic: https://github.com/lampepfl/dotty/blob/1ed25ce458d36e773f732267975fbddb0a0be26c/compiler/src/dotty/tools/dotc/typer/Inferencing.scala#L184 which relies on: https://github.com/lampepfl/dotty/blob/1ed25ce458d36e773f732267975fbddb0a0be26c/compiler/src/dotty/tools/dotc/core/Types.scala#L4625-L4630

But I does have a more precise upper-bound than Any in fact, it’s upper-bounded by a type variable A coming from the pure method. hasNonWildcardUpperBound returns false anyway because it relies on currentEntry which only returns the non-variable bounds, if we instead do something like:

diff --git compiler/src/dotty/tools/dotc/core/Types.scala compiler/src/dotty/tools/dotc/core/Types.scala
index 1d36efcb2a0..ce84ad92840 100644
--- compiler/src/dotty/tools/dotc/core/Types.scala
+++ compiler/src/dotty/tools/dotc/core/Types.scala
@@ -4626,7 +4626,7 @@ object Types {
      *  does it not contain wildcard types?
      */
     def hasNonWildcardUpperBound(using Context): Boolean =
-      val hi = currentEntry.hiBound
+      val hi = TypeComparer.fullUpperBound(origin).orElse(currentEntry.hiBound)
       !hi.isRef(defn.AnyClass) && !hi.containsWildcardTypes

     /** Unwrap to instance (if instantiated) or origin (if not), until result

… then the test case pass. So, probably something worth digging into more.

0reactions
SethTisuecommented, Sep 6, 2022

Could someone suggest a better title for the ticket, to make it easier to find in the future?

Read more comments on GitHub >

github_iconTop Results From Across the Web

List of all issue types - Infer Static Analyzer
Here is an overview of the issue types currently reported by Infer.
Read more >
Argument type infer not work correctly without specify ... - GitHub
A context sensitive function is not fully annotated and so the compiler must use contextual typing to infer something about its type. According ......
Read more >
Type inference has usability problems - Austin Z. Henley
People have argued that type inference actually improves readability by removing redundant information from the code that gets in the way.
Read more >
Kotlin requires type inference where Java doesn't : KT-5464
I have another issue raised on Spring Framework side due to Kotlin requiring type inference where Java doesn't. Not 100% sure this is...
Read more >
Why cannot Typescript infer the type of the function argument ...
But this extra bit of processing after compilation has completed doesn't fix the inference problem. That has already failed, and will only ...
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