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

user defined implicits interfere with ammonites internal implicits #221

Closed
cvogt opened this issue Sep 10, 2015 · 3 comments · Fixed by com-lihaoyi/PPrint#72
Closed

user defined implicits interfere with ammonites internal implicits #221

cvogt opened this issue Sep 10, 2015 · 3 comments · Fixed by com-lihaoyi/PPrint#72
Labels

Comments

@cvogt
Copy link
Contributor

cvogt commented Sep 10, 2015

Loading...
Welcome to the Ammonite Repl 0.4.6
(Scala 2.11.7 Java 1.8.0_45)
@ trait Lower {implicit def monad[M[_],T](i: T): M[T] = ???; implicit def preventNothing[T](i: T): Nothing = ??? }
^[[A^[[A^[[Adefined trait Lower
@ object Higher extends Lower{ implicit def value[M[_],T](l: M[T]): T = ??? }
defined object Higher
@ import Higher._
import Higher._
@ List(1,2,3) * List(2,3,4)
scala.NotImplementedError: an implementation is missing
    scala.Predef$.$qmark$qmark$qmark(Predef.scala:225)
    cmd1$Higher$.value(Main.scala:38)
    cmd3$$anonfun$1.apply$mcI$sp(Main.scala:57)
    cmd3$.<init>(Main.scala:58)
    cmd3$.<clinit>(Main.scala:-1)

@ def foo[M[_],T](l: M[T]) = l
defined function foo
@ import scala.util.Try
import scala.util.Try
@ Try(foo(List(1,2,3) * List(2,3,4)))
Compilation Failed
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method preventNothing in trait Lower of type [T](i: T)Nothing
 and method monad in trait Lower of type [M[_], T](i: T)M[T]
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method preventNothing in trait Lower of type [T](i: T)Nothing
 and method monad in trait Lower of type [M[_], T](i: T)M[T]
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method monad in trait Lower of type [M[_], T](i: T)M[T]
 and method preventNothing in trait Lower of type [T](i: T)Nothing
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method monad in trait Lower of type [M[_], T](i: T)M[T]
 and method preventNothing in trait Lower of type [T](i: T)Nothing
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method monad in trait Lower of type [M[_], T](i: T)M[T]
 and method preventNothing in trait Lower of type [T](i: T)Nothing
 are possible conversion functions from Unit to pprint.PPrint[Any]
@lihaoyi
Copy link
Member

lihaoyi commented Jan 31, 2016

Here's a better repro

@ trait Lower {implicit def monad[M[_],T](i: T): M[T] = ???; implicit def preventNothing[T](i: T): No  thing = ??? }
defined trait Lower
@ object Higher extends Lower{ implicit def value[M[_],T](l: M[T]): T = ??? }
defined object Higher
@ ??? : Any
scala.NotImplementedError: an implementation is missing
    scala.Predef$.$qmark$qmark$qmark(Predef.scala:225)
    cmd3$.<init>(Main.scala:47)
    cmd3$.<clinit>(Main.scala:-1)

@  import Higher._
import Higher._
@ ??? :  @Any
Compilation Failed
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method preventNothing in trait Lower of type [T](i: T)Nothing
 and method monad in trait Lower of type [M[_], T](i: T)M[T]
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method preventNothing in trait Lower of type [T](i: T)Nothing
 and method monad in trait Lower of type [M[_], T](i: T)M[T]
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method monad in trait Lower of type [M[_], T](i: T)M[T]
 and method preventNothing in trait Lower of type [T](i: T)Nothing
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method monad in trait Lower of type [M[_], T](i: T)M[T]
 and method preventNothing in trait Lower of type [T](i: T)Nothing
 are possible conversion functions from Unit to pprint.PPrint[Any]
type mismatch;
 found   : Unit
 required: pprint.PPrint[Any]
Note that implicit conversions are not applicable because they are ambiguous:
 both method monad in trait Lower of type [M[_], T](i: T)M[T]
 and method preventNothing in trait Lower of type [T](i: T)Nothing
 are possible conversion functions from Unit to pprint.PPrint[Any]

@lihaoyi
Copy link
Member

lihaoyi commented May 22, 2017

Still fails on the 0.9.0, but with a different error

@
trait Lower {implicit def monad[M[_],T](i: T): M[T] = ???; implicit def preventNothing[T](i: T): Nothing = ??? }
defined trait Lower
@  object Higher extends Lower{ implicit def value[M[_],T](l: M[T]): T = ??? }
defined object Higher
@ ??? : Any
scala.NotImplementedError: an implementation is missing
  scala.Predef$.$qmark$qmark$qmark(Predef.scala:284)
  $sess.cmd2$.<init>(cmd2.sc:1)
  $sess.cmd2$.<clinit>(cmd2.sc:-1)

@ import Higher._
import Higher._
@ ??? : Any
cmd4.sc:8: type mismatch;
 found   : String("Any")
 required: fansi.Str
Note that implicit conversions are not applicable because they are ambiguous:
 both method preventNothing in trait Lower of type [T](i: T)Nothing
 and method monad in trait Lower of type [M[_], T](i: T)M[T]
 are possible conversion functions from String("Any") to fansi.Str
            .print(res4, "res4", _root_.scala.None)
                  ^
Compilation Failed
@

@lihaoyi
Copy link
Member

lihaoyi commented May 22, 2017

A self-contained example

@ {{
  trait Lower {
    implicit def monad[M[_],T](i: T): M[T] = ???
    implicit def preventNothing[T](i: T): Nothing = ???
  }
  object Higher extends Lower{
    implicit def value[M[_],T](l: M[T]): T = ???
  }
  import Higher._

  def print[T: pprint.TPrint](value: => T) = ???
  print(1)
  }}
cmd2.sc:12: type mismatch;
 found   : String("Int")
 required: fansi.Str
Note that implicit conversions are not applicable because they are ambiguous:
 both method monad in trait Lower of type [M[_], T](i: T)M[T]
 and method preventNothing in trait Lower of type [T](i: T)Nothing
 are possible conversion functions from String("Int") to fansi.Str
print(1)
     ^
Compilation Failed

Seems the pprint.TPrint implicit is misbehaving

lihaoyi added a commit to com-lihaoyi/PPrint that referenced this issue Dec 6, 2021
Fixes com-lihaoyi/Ammonite#221 
Fixes com-lihaoyi/Ammonite#629 
Fixes com-lihaoyi/Ammonite#670 
Fixes #45 
Fixes #44

The old TPrint implementation did a clever thing where it allowed a user to over-ride the TPrinting of a given type by providing an appropriate implicit. While that worked in most cases, it was fiendishly complex, and the intricate nesting of implicit resolution and macro resolution ended up providing and endless source of hard to resolve bugs.

This new implementation is much simpler and less flexible: we simply walk the type data structure in the macro, and spit out a colored `fansi.Str` with the type names hard-coded to `fansi.Green`. The only runtime support necessary is in the `def recolor` function, which parses the incoming `fansi.Str` and replaces the hardcoded `fansi.Green` colors with whatever is specified by the implicit `TPrintColors`. As implicits cannot be used to override tprinting anymore, we now have hardcoded support for tprinting functions and tuples.

While the old macro generated a complex tree of Scala function calls that is evaluated to generate the output `fansi.Str` at runtime, the new macro simply spits out a single `fansi.Str` that is serialized into a `java.lang.String` and deserialized back into a `fansi.Str` for usage at runtime. We propagate a `WrapType` enumeration up the recursion, to help the callers decide if they need to wrap things in parens or not.

This gives up a bit of flexibility, but AFAIK nobody was really using that flexibility anyway. In exchange, we fix a whole bunch of long-standing bugs, and have a drastically simpler implementation.

The fixed bugs are covered by regression unit tests added to `TPrintTests.scala`. All existing tests also pass, so hopefully that'll catch any potential regressions. There's probably more bugs where we're not properly setting or handling the `WrapType`, but exhaustively testing/surfacing/fixing all of those is beyond the scope of this PR. For now, I just kept the current set of tests passing.

Managed to get the Scala3 side working. I didn't realize how half-baked the Scala3 implementation of TPrint is; so much of the Scala2 functionality just isn't implemented and doesn't work. Nevertheless, fixing that is beyond the scope of this PR. I just kept it green with the existing set of green tests passing (except for the custom tprinter test, which is no longer applicable)

Review by @lolgab.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants