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

Allow generics in fn definition #18

Closed
ErichDonGubler opened this issue Jul 13, 2017 · 10 comments
Closed

Allow generics in fn definition #18

ErichDonGubler opened this issue Jul 13, 2017 · 10 comments

Comments

@ErichDonGubler
Copy link
Owner

No description provided.

@ErichDonGubler ErichDonGubler added this to the 0.2.0 milestone Jul 13, 2017
@ErichDonGubler ErichDonGubler self-assigned this Jul 13, 2017
@ErichDonGubler ErichDonGubler modified the milestones: 0.2.0, Generics support, 1.0 Jul 18, 2017
@ErichDonGubler
Copy link
Owner Author

@DanielKeep: The problem actually isn't using the information for generics -- I just need to forward that text along into the new fn definition. The heart of this issue is simply figuring out how to write a macro that will actually accept any form of generics in the possible positions, starting here.

Any suggestions? I have looked at the source you linked, and it seems that you depend on procedural macros. I could be wrong, since I'm very new to writing macros, though.

@DanielKeep
Copy link

There's an implementation of those macros as a compiler plugin, but that link is to the macro_rules!-only version. The plugin stuff there is so that it can fall back to using the plugin if desired, although the plugin almost certainly doesn't work any more. The stable macros should still work.

As for how you're trying to do it, there is no way to match generic arguments directly. The grammar is simply too complex for macro_rules! to handle.

The way you use them is to capture everything up to where the generic parameter list starts. You capture everything left as tts, then feed all of it to the parse_generics! macro along with a callback. parse_generics! will invoke your callback with the parsed generics, plus everything that came after so you can continue your own parsing.

The parse-macros sibling package shows some examples of using the base parsing macros to parse things like structs and enums.

@ErichDonGubler
Copy link
Owner Author

ErichDonGubler commented Aug 10, 2017

@DanielKeep: Excellent! I'm starting to understand your examples better now. Macros by example are a new ball game to me, and I'm definitely learning a lot.

So, I'm trying to make the parse_generics! macro available in my crate, and I'm not sure exactly how to do it. Here's what I've got so far:

  • cargo.toml:
    [dependencies]
    parse-generics-shim = "0.1.1"
  • src/lib.rs
    #[macro_use]
    extern crate parse_generics_shim;

...but I'm definitely missing something, since I can't try using it and compile yet. What steps do I need for setup? I'm sure that I'm simply not used to setting up macro-heavy dependencies like this.

EDIT: As a note, I think I understand the mechanism for using the parse-generics-poc crate, but I'd rather have a consistent mechanism across nightly AND stable right now. :) I'm just trying to get an MVP, so...

@ErichDonGubler
Copy link
Owner Author

ErichDonGubler commented Aug 10, 2017

@DanielKeep: Ah, I was making a rookie mistake -- there's still no good way to re-export macros, so I need to have the extern crate in any tests I have to.

Alright, excited to try this! Not excited to force users to extern another crate, but...hey, it's better than having no generics!

@DanielKeep
Copy link

[...] I'd rather have a consistent mechanism across nightly AND stable right now.

Well the plugin version likely doesn't work any more, so I'd just remove it. The MBE macros should work fine in nightly.

Not excited to force users to extern another crate, [...]

I'd recommend just copy+pasting the macros into your own crate under different names. Until re-exporting is added, it's about the only way to not drive your users mad.

@ErichDonGubler
Copy link
Owner Author

@DanielKeep: Excellent news! I've managed to successfully implement generics support in a feature branch here. My only concern with how I've got it working right now, however, is that right now the doctests are failing in the parse_generics_shim_util module, so in order to preserve the sanity of cargo test I've removed them.

That doesn't feel good, though, because I'm sure you had valid doctests at some point. Does the markdown simply need to be corrected a la the solution to the markdown parsing bug in Rustdoc here? I'm not sure, since I didn't write that code, and the docs are pretty intense. Kudos for the extensive documentation, by the way!

@DanielKeep
Copy link

At a casual glance, it looks like the problem is that the doc tests are still trying to use the original crates. If you've moved the macros into your own crate, you need to adjust the tests since the crate names will have changed.

@ErichDonGubler
Copy link
Owner Author

Strange, for some reason it seems that cargo doc is crapping out:

Documenting adhesion v0.3.0 (file:///.../adhesion-rs)
error: unknown start of token: `
 --> <stdin>:1:1
  |
1 | ```rust
  | ^

error: Could not document `adhesion`.

I've saved off the WIP at the feature/generics.doctest branch. If you have anything that can help, I'll gladly take it, but you've already done so much! I'll research this later. At least the hard part is done! :) Thank you so much for you help!

@ErichDonGubler
Copy link
Owner Author

@DanielKeep: I've created an issue specifically regarding my concerns with doctests for parse-generics-shim -- I see no reason to block this issue getting closed on the details that that issue is intended to handle, so I will move the discussion we've had from here to there should it need to continue.

Again, thanks so much for the help! You're awesome. :)

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

No branches or pull requests

2 participants