Skip to content

Scalac outputs least informative error #10234

Open
@scabug

Description

@scabug
class Foo[T <: String]
class Bar[T]
def foo[T](implicit bar: Bar[T]) = bar
implicit def mkFooBar[A, T[x] <: Foo[x]]: Bar[T[A]] = new Bar[T[A]]

foo[Foo[String]]

If you compile this code in one go, you get the following error:

error: could not find implicit value for parameter bar: Bar[Foo[String]]
       foo[Foo[String]]
          ^

This error is actually caused by another error:

error: type arguments [x] do not conform to class Foo's type parameter bounds [T <: String]
       implicit def mkFooBar[A, T[x] <: Foo[x]]: Bar[T[A]] = new Bar[T[A]]
                                        ^

This is the error you actually have to fix to get the code working, but the compiler doesn't show it and leaves you guessing what the problem is.

Activity

scabug

scabug commented on Mar 17, 2017

@scabug
Author

Imported From: https://issues.scala-lang.org/browse/SI-10234?orig=1
Reporter: Jasper-M
Affected Versions: 2.12.1

atiqsayyed

atiqsayyed commented on Aug 17, 2017

@atiqsayyed

Can I pick this Issue?? If somebody can provide the details it'd be great help

SethTisue

SethTisue commented on Sep 11, 2017

@SethTisue
Member

@atiqsayyed I'd suggest starting by finding the place in the code where the error is coming from.

I'm not certain this is actually a "quick" fix — implicit search is pretty hairy stuff. if you get into it and find it's too hard, it would still be valuable even just to record your investigation here as a starting point for the next person.

som-snytt

som-snytt commented on Jul 29, 2019

@som-snytt

I don't know why scalacenter doesn't run a contest for least informative error.

$ scala -Vimplicits -language:_
Welcome to Scala 2.13.0 (OpenJDK 64-Bit Server VM, Java 11.0.3).
Type in expressions for evaluation. Or try :help.

scala> :pa
// Entering paste mode (ctrl-D to finish)

class Foo[T <: String]
class Bar[T]
def foo[T](implicit bar: Bar[T]) = bar
implicit def mkFooBar[A, T[x] <: Foo[x]]: Bar[T[A]] = new Bar[T[A]]


// Exiting paste mode, now interpreting.

       implicit def mkFooBar[A, T[x] <: Foo[x]]: Bar[T[A]] = new Bar[T[A]]
                                        ^
On line 4: error: type arguments [x] do not conform to class Foo's type parameter bounds [T <: String]

scala> :pa
// Entering paste mode (ctrl-D to finish)

class Foo[T <: String]
class Bar[T]
def foo[T](implicit bar: Bar[T]) = bar
implicit def mkFooBar[A, T[x] <: Foo[x]]: Bar[T[A]] = new Bar[T[A]]

foo[Foo[String]]

// Exiting paste mode, now interpreting.


foo[Foo[String]]
   ^
<pastie>:6: mkFooBar is not a valid implicit value for Bar[Foo[String]] because:
typing TypeApply reported errors for the implicit tree: kinds of the type arguments (String,Foo) do not conform to the expected kinds of the type parameters (type A,type T).
Foo's type parameters do not match type T's expected parameters:
type T's bounds <: String are stricter than type x's declared bounds >: Nothing <: Any

foo[Foo[String]]
   ^
<pastie>:6: this.mkFooBar is not a valid implicit value for Bar[Foo[String]] because:
typing TypeApply reported errors for the implicit tree: kinds of the type arguments (String,Foo) do not conform to the expected kinds of the type parameters (type A,type T).
Foo's type parameters do not match type T's expected parameters:
type T's bounds <: String are stricter than type x's declared bounds >: Nothing <: Any

foo[Foo[String]]
   ^
<pastie>:6: error: could not find implicit value for parameter bar: Bar[Foo[String]]
added this to the Backlog milestone on Jul 29, 2019
som-snytt

som-snytt commented on Dec 23, 2022

@som-snytt
-- Error: t10234.scala:9:29 --------------------------------------------------------------------------------------------
9 |  def test = foo[Foo[String]]
  |                             ^
  |                  No given instance of type Bar[Foo[String]] was found for parameter bar of method foo in class C.
  |                  I found:
  |
  |                      this.mkFooBar[A, T]
  |
  |                  But method mkFooBar in class C does not match type Bar[Foo[String]].
1 error found

The ticket title reads like an Onion headline!

Jasper-M

Jasper-M commented on Dec 23, 2022

@Jasper-M

You won't BELIEVE these 27 errors that the scala compiler is hiding from you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @SethTisue@som-snytt@gourlaysama@atiqsayyed@Jasper-M

        Issue actions

          Scalac outputs least informative error · Issue #10234 · scala/bug