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

Comparing Chunk to Seq causes ClassCastException #8754

Open
andrzejressel opened this issue Apr 16, 2024 · 12 comments
Open

Comparing Chunk to Seq causes ClassCastException #8754

andrzejressel opened this issue Apr 16, 2024 · 12 comments

Comments

@andrzejressel
Copy link
Contributor

andrzejressel commented Apr 16, 2024

Scala 3 (2.13 does not compile with found Seq, require Chunk error)
Both 2.0 and 2.1-RC

https://scastie.scala-lang.org/7CX9Y9D3QYq0v4TNcsIDmw

object Suite extends ZIOSpecDefault {
  override def spec = suite("suite") {
    test("test") {
     assertTrue(Chunk(1, 2, 3) == Seq(1, 2, 3, 4));
    }
  }
}
  - suite�[2m / test
    ✗ ERROR: java.lang.ClassCastException: class scala.collection.immutable.$colon$colon cannot be cast to class zio.Chunk (scala.collection.immutable.$colon$colon is in unnamed module of loader sbt.internal.ScalaLibraryClassLoader @5ad07354; zio.Chunk is in unnamed module of loader sbt.internal.ReverseLookupClassLoader @3094bb)
      zio.test.diff.DiffInstances.mkSeqDiff$$anonfun$1(Diff.scala:113)
      zio.test.internal.SmartAssertions$.equalTo$$anonfun$1(SmartAssertions.scala:291)
    Chunk(1, 2, 3) == Seq(1, 2, 3, 4)
    �[2m(1, 2, 3) = Chunk(1, 2, 3)
    �[2mChunk = zio.Chunk$@3f018ce
    at /tmp/scastie5353637521019907974/src/main/scala/main.scala:10 
@andrzejressel
Copy link
Contributor Author

andrzejressel commented Apr 16, 2024

I believe it's caused by

      case Unseal(MethodCall(lhs, "==", tpes, Some(List(rhs)))) =>
        val span = getSpan(rhs)
        lhs.tpe.widen.asType match {
          case '[l] =>
            Expr.summon[OptionalImplicit[Diff[l]]] match {
              case Some(optDiff) =>
                '{${transform(lhs.asExpr)} >>> SmartAssertions.equalTo(${rhs.asExpr})($optDiff.asInstanceOf[OptionalImplicit[Diff[Any]]]).span($span)}.asExprOf[TestArrow[Any, A]]
              case _ => throw new Error("OptionalImplicit should be always available")
            }
        }

lhs should be first common class (GCD of class I guess)

@jdegoes
Copy link
Member

jdegoes commented May 8, 2024

/bounty $50

Copy link

algora-pbc bot commented May 8, 2024

💎 $50 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #8754 with your implementation plan
  2. Submit work: Create a pull request including /claim #8754 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @kyri-petrou #8848

@andrzejressel
Copy link
Contributor Author

I was wrong with my initial checks - Chunk and Seq are related. It just required proper mapping logic like in Scala 2.13

@joroKr21
Copy link
Contributor

lhs should be first common class (GCD of class I guess)

LUB and GLB are not exposed in Scala 3 macros. You could make Diff[-A] contravariant and use A | B.

@andrzejressel
Copy link
Contributor Author

andrzejressel commented May 25, 2024 via email

@joroKr21
Copy link
Contributor

I was writing that before i knew how Scala 2 macro work - it should propably work like it.

Not sure what you mean. This is Scala 3.

So one class has to be subtype of another.

Nope, LUB doesn't require one class to be subtype of the other.

@andrzejressel
Copy link
Contributor Author

I meant that assertTrue in Scala 2 does not allow unrelated classes for any comparison. Scala 3 should propably have the same behaviour.

@andrzejressel
Copy link
Contributor Author

S3 unfortunately does allow it and it generate pretty ugly comparisons - I think I would prefer for it to be reported during compilation.

@joroKr21
Copy link
Contributor

They are related though, Chunk extends Seq

@andrzejressel
Copy link
Contributor Author

Yes, this specific issue is about directly related classes (which I didn't know at the beginning) and I want to focus on that.

The issue with non directly related classes is that they are all ultimately related anyway thought Any (which have diff instance). And I like Scala 2 behavior of not allowing comparisons of non directly related classes.

@andrzejressel
Copy link
Contributor Author

Maybe it just my thing - but when I'm comparing A to option A or Seq A, I'd rather have compile error than downcast to Any.

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

Successfully merging a pull request may close this issue.

3 participants