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

Fail Seed with (0, 0, 0, 0) #674

Merged
merged 2 commits into from
Aug 13, 2020
Merged

Fail Seed with (0, 0, 0, 0) #674

merged 2 commits into from
Aug 13, 2020

Conversation

nevillelyh
Copy link
Contributor

Seed(0, 0, 0, 0) would always produce the same next value

Seed(0, 0, 0, 0) would always produce the same next value
@nevillelyh
Copy link
Contributor Author

All builds passed except this one due to a time out. Can someone with admin rights re-trigger?
https://travis-ci.org/github/typelevel/scalacheck/jobs/714591093

@SethTisue
Copy link
Member

green now. (but someone else will have to review the content of the PR)

@@ -99,6 +99,9 @@ sealed abstract class Seed extends Serializable {
object Seed {

private case class apply(a: Long, b: Long, c: Long, d: Long) extends Seed {
if (a == 0 && b == 0 && c == 0 && d == 0) {
throw new IllegalArgumentException("illegal Seed(0, 0, 0, 0)")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not check this in fromLongs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually discovered this via with a magnolia derivation of Arbitrary[T]. Turns out magnolia can derive the sealed abstract class Seed by calling its private case class apply(...).

Derivation code:
https://github.com/spotify/magnolify/blob/master/scalacheck/src/main/scala/magnolify/scalacheck/semiauto/ArbitraryDerivation.scala#L25

This calls apply()

implicitly[Arbitrary[Seed]].arbitrary.sample

Checking in apply would prevent these macro libraries from accidentally deriving a bad seed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it calling private case class apply or its own subclass of Seed?

It shouldn't be able to do either, as Seed is sealed and apply is private, so I'd argue that particular behaviour is a problem that needs resolving in Magnolia. (But I think this PR has merit regardless.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah looks like magnolia is able to call the private case class thru macros.

I admit that doing the check here might add a bit of overhead, since Seed#apply(s: Long) does this while (i < 20) { seed = seed.next; i += 1 } which calls apply().
I can probably override the implicit in my library, but such a check is still nice to have, maybe in fromLongs as you suggested?

dbg

Copy link
Contributor

Choose a reason for hiding this comment

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

So, the intention was for this constructor to be private -- the issue of an all-zero seed is known and the code was being defensive to avoid it.

I'm fine adding this check to fromLongs but I'd rather not require every (internal) use of apply to do the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, my thought as well. Will move it to fromLongs and fix it on my end to avoid apply(0, 0, 0, 0).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@non moved to fromLongs PTAL thanks :)

@non
Copy link
Contributor

non commented Aug 13, 2020

LGTM 👍

@non non merged commit 15d7eb6 into typelevel:master Aug 13, 2020
@ashawley ashawley added this to the 1.15.0 milestone Sep 22, 2020
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.

5 participants