Skip to content

Strange error message for failing an Ordered[T] type bound #14363

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
spamegg1 opened this issue Jan 26, 2022 · 2 comments · Fixed by #14723
Closed

Strange error message for failing an Ordered[T] type bound #14363

spamegg1 opened this issue Jan 26, 2022 · 2 comments · Fixed by #14723
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug
Milestone

Comments

@spamegg1
Copy link

Compiler version

3.1.0

Minimized code

def f[T <: Ordered[T]](t: T): T = t
f(1)

Output

Found:    (1 : Int)
Required: Ordered[
  Ordered[
    Ordered[
      Ordered[
        Ordered[
          Ordered[
            Ordered[
              Ordered[
                Ordered[
                  Ordered[Ordered[Ordered[Ordered[Ordered[Ordered[...[...]]]]]]]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

One of the following imports might fix the problem:

  import math.BigDecimal.int2bigDecimal
  import math.BigInt.int2bigInt

Expectation

// this is also what Scala 2 gives:
inferred type arguments [Int] do not conform to method f's type parameter bounds [T <: Ordered[T]]

Info

Hello all, I accidentally came across this while reading Chapter 18 "Type Parameterization" of "Programming in Scala, 5th Edition". At the end of the chapter there is an example of

def orderedMergeSort[T <: Ordered[T]](xs: List[T]): List[T] = ...

then the book demonstrates why this cannot be used to sort a list of integers because Int is not a subtype of Ordered[Int]:

// this is what the book says should happen (also what Scala 2 gives):
scala> val wontCompile = orderedMergeSort(List(3, 2, 1))
<console>:5: error: inferred type arguments [Int] do
   not conform to method orderedMergeSort's type
     parameter bounds [T <: Ordered[T]]
       val wontCompile = orderedMergeSort(List(3, 2, 1))
                         ^

I thought the error message looked really strange, and obviously different from the book. So I minimized it.

Providing a type parameter gives a much better message:

scala> f[Int](1)
-- Error:
1 |f[Int](1)
  |  ^
  |  Type argument Int does not conform to upper bound Ordered[Int]

Also doing one of the mentioned imports fixes the problem too.

Wasn't sure if it is a bug. I asked in Discussions, @SethTisue encouraged me to ask in Scala Users, where @smarter encouraged me to report it.

@spamegg1 spamegg1 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 26, 2022
@KacperFKorban KacperFKorban added area:reporting Error reporting including formatting, implicit suggestions, etc and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 27, 2022
@nicolasstucki
Copy link
Contributor

Scala 2 shows this error

On line 2: error: inferred type arguments [Int] do not conform to method f's type parameter bounds [T <: Ordered[T]]
       f(1)
         ^
On line 2: error: type mismatch;
        found   : Int(1)
        required: T

@spamegg1
Copy link
Author

Noticed a similar issue with other traits with type parameters as well:

  scala
Welcome to Scala 3.1.0 (11.0.13, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                       
scala> trait Spam[T]
// defined trait Spam
                                                                                                                                       
scala> def f[T <: Spam[T]](t: T): T = t
def f[T <: Spam[T]](t: T): T
                                                                                                                                       
scala> f('a')
-- Error:
1 |f('a')
  |  ^^^
  |  Found:    ('a' : Char)
  |  Required: Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[Spam[...[...]]]]]]]]]]]]]]]]
  |
  |  The following import might make progress towards fixing the problem:
  |
  |    import collection.Searching.search
  |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants