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

Add lifetime parameter to Arbitrary trait. Remove shrinking functionality. Implement Arbitrary for &str. #63

Merged
merged 11 commits into from Nov 26, 2020

Conversation

frewsxcv
Copy link
Member

@frewsxcv frewsxcv commented Nov 22, 2020

Fixes #43

Note: This merges into a new branch staging-1.0 where we can put our changes prepping for a 1.0 release

To-do:

  • Remove Shrinkable
  • Fix Cow lifetime
  • Remove shrinking from README
  • Save shrinking code to a gist

@frewsxcv frewsxcv changed the title Add lifetime parameter to Arbitrary trait Add lifetime parameter to Arbitrary trait. Extract shrinking functionality into new trait. Nov 22, 2020
@frewsxcv
Copy link
Member Author

@Manishearth @fitzgen thoughts on this so far? should we create a separate crate for the shrinking behavior?

@Manishearth
Copy link
Member

I think we should have a pre-1.0 release before going 1.0 fwiw. But we can name the branch whatever

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

I'm not sure the shrinking code is worth maintaining? AFAIK, no one is using it at all. It isn't used with cargo fuzz, so someone would have to be implementing their own fuzzing engine or driver to use it, which AFAIK, no one is doing. As the person who wrote the shrinking code initially, I'd like to hear someone else champion it and articulate why we should keep it around.

In any case, if we keep it, then we should name the trait Shrink instead of Shrinkable, the same way std::iter::Extend is not named Extendable.

Finally, we should also add a impl<'a> Arbitrary<'a> for &'a str implementation, now that it's possible.

@fitzgen
Copy link
Member

fitzgen commented Nov 23, 2020

Also: thanks for driving this work @frewsxcv! :)

@frewsxcv
Copy link
Member Author

I'm not using the shrinking code, and not planning to use it. Just moved the code to a separate module in case someone did have a use for it! But if none of us do, then yeah let's just get rid of it

@frewsxcv
Copy link
Member Author

to anyone in the future who wants the shrinking code: https://gist.github.com/frewsxcv/390976eb39c7e2a065c0b2d2731a3eb3

@frewsxcv
Copy link
Member Author

currently trying to figure out what the right behavior should be for the new lifetime when deriving Arbitrary. i.e. should we always create a lifetime named 'a? will that cause issues?

@fitzgen
Copy link
Member

fitzgen commented Nov 23, 2020

Do you mean the actual name of the lifetime we emit in the generated code?

The only way I can imagine running into issues is if the type requires multiple type parameters, but in such a case I don't think our derive can support such types anyways, because we wouldn't know what to do with any extra lifetimes.

But I'm also not a master macrologist, so I'm not 100% sure here...

@frewsxcv frewsxcv mentioned this pull request Nov 23, 2020
@frewsxcv frewsxcv marked this pull request as ready for review November 23, 2020 23:27
@frewsxcv
Copy link
Member Author

This is ready for review! I will add a &str implementation in a follow-up PR

@frewsxcv
Copy link
Member Author

Squashed

@frewsxcv frewsxcv changed the title Add lifetime parameter to Arbitrary trait. Extract shrinking functionality into new trait. Add lifetime parameter to Arbitrary trait. Remove shrinking functionality. Nov 23, 2020
@frewsxcv
Copy link
Member Author

In the latest force push, I renamed our derive codegen to generate a lifetime named 'arbitrary instead of 'a. To prevent collisions if the user has a lifetime named 'a.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Nice!

Can we also have a type with a lifetime parameter that we derive Arbitrary for in the tests/derive.rs file?

With that, I think we can release a 1.0-rc1.

@fitzgen
Copy link
Member

fitzgen commented Nov 23, 2020

In the latest force push, I renamed our derive codegen to generate a lifetime named 'arbitrary instead of 'a. To prevent collisions if the user has a lifetime named 'a.

Oh I just remembered the other thing we can do: try and grab their existing lifetime from their type and use that instead of coming up with our own and replacing theirs. Sort of something like get_existing_lifetime(input).unwrap_or(default_lifetime).

@frewsxcv
Copy link
Member Author

In the latest force push, I renamed our derive codegen to generate a lifetime named 'arbitrary instead of 'a. To prevent collisions if the user has a lifetime named 'a.

Oh I just remembered the other thing we can do: try and grab their existing lifetime from their type and use that instead of coming up with our own and replacing theirs. Sort of something like get_existing_lifetime(input).unwrap_or(default_lifetime).

I think you're right, we do want to be smarter about lifetimes than just using a new one. In particular, I created this struct for a test:

#[derive(Arbitrary, Debug)]
struct Lifetime<'a, 'b> {
    alpha: &'a str,
    beta: &'b str,
}

This results in an error as currently implemented:

Screen Shot 2020-11-23 at 8 28 09 PM

The fix here is to replace:

impl<'a, 'b, 'arbitrary> arbitrary::Arbitrary<'arbitrary> for Lifetime<'a, 'b> 

With:

impl<'a, 'b, 'arbitrary: 'a + 'b> arbitrary::Arbitrary<'arbitrary> for Lifetime<'a, 'b> 

So that our lifetime is guaranteed to last as long as the existing lifetimes

@frewsxcv
Copy link
Member Author

Also good idea to add a test :)

@frewsxcv frewsxcv changed the title Add lifetime parameter to Arbitrary trait. Remove shrinking functionality. Add lifetime parameter to Arbitrary trait. Remove shrinking functionality. Implement Arbitrary for &str. Nov 24, 2020
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

LGTM!

A couple inline suggestions, just little things.

With this merged, I think we can release 1.0.0-rc1, unless I'm overlooking something. @Manishearth @nagisa anything you can think of holding us back from a 1.0.0-rc1 release?

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
tests/derive.rs Show resolved Hide resolved
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

lgtm modulo nick's comments

frewsxcv and others added 4 commits November 24, 2020 16:45
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
@frewsxcv frewsxcv merged commit 8f3c71a into staging-1.0 Nov 26, 2020
@frewsxcv frewsxcv deleted the lifetime-changing branch November 26, 2020 03:12
@frewsxcv
Copy link
Member Author

1.0.0-rc1 has been published!

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

3 participants