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

Replace buildFromAny with buildFromNothing #4815

Merged
merged 2 commits into from Mar 18, 2021

Conversation

joroKr21
Copy link
Contributor

@joroKr21 joroKr21 commented Mar 17, 2021

Instead of generalizing to Any we need to specialize to Nothing.
All other cases are covered by the default BuildFrom.buildFromIterableOps.

Also fixes Intellij highlighting issue.

See scala/bug#12350
Workaround for scala/bug#12104
Followup to #4582

Instead of generalizing to `Any` we need to specialize to `Nothing`.
All other cases are covered by the default `BuildFrom.buildFromIterableOps`.
@joroKr21 joroKr21 changed the title No need for buildFromAny implicit on Scala 2.13+ Replace buildFromAny with buildFromNothing Mar 17, 2021
Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joroKr21 Are you having an issue here? I think that both definitions are sound, though they say slightly different things. At least on previous versions of Scala 2.13 this was necessary for a CanBuildFrom to be in scope for certain operations. I haven't heard of any other problems with this to date.

@joroKr21 joroKr21 marked this pull request as ready for review March 18, 2021 03:24
@joroKr21
Copy link
Contributor Author

@adamgfraser I go have a minor issue actually - Intellij also thinks the instances are ambiguous so it breaks highlighting.

The reason is that there is overlap. Nothing doesn't work when the element type is generic because of the peculiarity that in Scala 2 Nothing is used internally to signal that an implicit was not found. The way to fix it is to provide an instance with the element type specialized to Nothing. All other cases are covered by the default instance.

@@ -6,7 +6,12 @@ private[zio] trait BuildFromCompat {

type BuildFrom[-From, -A, +C] = scala.collection.BuildFrom[From, A, C]

implicit def buildFromAny[Element, Collection[+Element] <: Iterable[Element] with IterableOps[Any, Collection, Any]]
@deprecated("Use BuildFrom.buildFromIterableOps or buildFromNothing instead", "1.0.6")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to change this can we just delete it entirely instead of deprecating it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that be binary incompatible?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binary compatibility checks seem to be okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or was that on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamgfraser oh I see why - we check binary compatibility against 1.0.1 but this was added in 1.0.4-1: https://github.com/zio/zio/releases/tag/v1.0.4-1

Wow, I never really thought about this, but if you don't bump the version for MiMa that means you could accidentally delete something that you introduced in a later version. Amazing 🤯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I bump the version in MimaSettings?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes me think there should be some process or automation to bump this version on release

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Not sure what the best way is to do it but that would definitely be good.

@adamgfraser
Copy link
Contributor

@joroKr21 That's a good reason! I'm good with this change as long as the tests pass. Would prefer to just delete the old instance if possible.

@joroKr21
Copy link
Contributor Author

I will also try to compile with -Xsource:3 and see if there is any difference.

@joroKr21
Copy link
Contributor Author

I will also try to compile with -Xsource:3 and see if there is any difference.

Indeed compiling core-tests with -Xsource:3 fails on master and works with this PR

@adamgfraser
Copy link
Contributor

So how is it possible that it fails if we use X-source:3 but succeeds when we actually compile it on Scala 3?

@joroKr21
Copy link
Contributor Author

So how is it possible that it fails if we use X-source:3 but succeeds when we actually compile it on Scala 3?

I think BuildFromCompat is only present on Scala 2.13 right?

@adamgfraser
Copy link
Contributor

No the 2.13+ files are included on Scala 3. We need them because Scala 3 uses the standard library including the collections library from Scala 2.13 so we couldn't compile without them. That's part of why I am so confused by this.

@joroKr21
Copy link
Contributor Author

No the 2.13+ files are included on Scala 3. We need them because Scala 3 uses the standard library including the collections library from Scala 2.13 so we couldn't compile without them. That's part of why I am so confused by this.

AFAIR in dotty the package object doesn't contribute to implicit scope, so maybe that's the reason.

@adamgfraser
Copy link
Contributor

But it still has the change in implicit resolution for contravariant types that was supposed to be what triggered this issue, right?

And delete filters that are not needed anymore.
@joroKr21
Copy link
Contributor Author

But it still has the change in implicit resolution for contravariant types that was supposed to be what triggered this issue, right?

Yes it does, but if that instance is not in implicit scope it just won't be considered.
And if you import it it takes precedence.

@adamgfraser
Copy link
Contributor

But the instance isn't in the Dotty package, right? It is in the Scala collections which has got to be in implicit scope or everything else with CanBuildFrom would break.

@joroKr21
Copy link
Contributor Author

joroKr21 commented Mar 18, 2021

The buildFromAny instance is in the zio package object which is the package of zio.BuildFrom

@adamgfraser
Copy link
Contributor

Right but that's true on Scala 3 as well. So why would it compile on Scala 3 but not on Scala 2 with implicit resolution rules from Scala 3?

@joroKr21
Copy link
Contributor Author

AFAIR in dotty the package object doesn't contribute to implicit scope, so maybe that's the reason.

It's change number 3. here: https://dotty.epfl.ch/docs/reference/changed-features/implicit-resolution.html

@joroKr21
Copy link
Contributor Author

joroKr21 commented Mar 18, 2021

  1. Package prefixes no longer contribute to the implicit search scope of a type. Example:
  package p

  given a: A = A()

  object o:
     given b: B = B()
     type C

Both a and b are visible as implicits at the point of the definition of type C. However, a reference to p.o.C outside of package p will have only b in its implicit search scope but not a.

I'm a bit confused - so does it include package objects or not?
Only top-level definitions? Only the closest package?

@adamgfraser
Copy link
Contributor

I don't know. I guess what I take from this is that this -XSource:3 flag back ports some changes from Scala 3 but not others so conceptually it makes sense that something could compile on both Scala 2 and Scala 3 but not on Scala 2 with some subset of Scala 3 rules.

@adamgfraser
Copy link
Contributor

Kind of raises an interesting question for library authors of whether we need to have this in our build matrix too. As we're seeing it is essentially a different version with different semantics of what compiles or not in some cases. If we did run against this in CI we would have caught before the PR adding this got merged.

@joroKr21
Copy link
Contributor Author

joroKr21 commented Mar 18, 2021

Indeed that's a good question. I don't think it's possible to port everything from Scala 3. On the other hand -Xsource:3 is useful if you plan to migrate to Scala 3 but can't yet - it would also show you deprecated features you are using.

Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work on this! 🙏

@adamgfraser adamgfraser merged commit a845272 into zio:master Mar 18, 2021
@joroKr21 joroKr21 deleted the build-from-any branch March 18, 2021 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants