-
Notifications
You must be signed in to change notification settings - Fork 13.3k
rustdoc: inject #[macro_use] extern crate
in doctests for macros
#33511
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Thanks! Could you elaborate a bit on why the added test wouldn't work? What did the error message look like? Also, perhaps macros could always be imported? I could imagine a doctest for a normal function may also want to use the crate's macros as it's part of the API |
The external crate isn't found. This is not specific to the macro issue, but apparently there was no need for such a test yet (all test cases that use
That can be done as well; this was the more backwards compatible option (although I admit that the breakage is pretty hard to imagine). Should the "does it contain the crate name" check be removed too then? |
Oh this is actually a case where the test harness isn't quite up to the task. Right now it only runs I'd leave in the crate name check for now, although in retrospect we probably should have just always added it. I suspect though that the addition of |
I think Either way, please also update the docs here which explain how test code gets wrapped in |
@durka good point, done. |
<mycrate>;` is inserted (note the lack of `#[macro_use]`). | ||
3. If the example does not contain `extern crate`, then `#[macro_use] | ||
extern crate <mycrate>;` is inserted if the example is either for | ||
a macro definition, or contains the crate name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[macro_use]
is also mentioned in the paragraph below, and in the section "Documenting macros" -- both obsolete with this PR I believe.
/// ```ignore | ||
/// fn foo() { | ||
/// ``` | ||
# fn foo() {} | ||
``` | ||
|
||
The `ignore` directive tells Rust to ignore your code. This is almost never | ||
what you want, as it's the most generic. Instead, consider annotating it | ||
with `text` if it's not code, or using `#`s to get a working example that | ||
only shows the part you care about. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to be ignoring this advice in this very file with the second commit...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That wasn't supposed to be part of this PR anyway.
Yeah, I would be a fan of doing this. |
All looks good to me, thanks @birkenfeld! |
Was there a resolution on whether macros should be imported when documenting a non-macro item? |
(If not, and this stays r+ed, I'll open a followup PR after this is merged so we can discuss there.) |
@durka for backwards compatibility we may not be able to do that, although in retrospect we should always have injected |
I don't see how adding |
These tests are pretty fragile as they're syntactically creating a program, and if we didn't previously inject |
If we're really worried about backcompat here and the justification for this PR as implemented is only that it won't affect a large number of tests... I think we shouldn't change it at all, unfortunately. I'm not convinced that complicating the test runner is worth it. Yes, it works great in simple cases. But as soon as you get a weird resolve error and you have to comb through the docs trying to understand how it mangles the doctests, it backfires. If you're testing a non-macro item and you want to use crate macros, now you need to add the manual |
@bors: r- Ok, let's see what others think. cc @rust-lang/tools |
Sorry for being difficult :( I remember being really frustrated when first learning how to document macros. This makes it easier, but harder to discover the fix when it breaks (which I believe it will, in crates that mix macro and non-macro items). So I'm torn. |
☔ The latest upstream changes (presumably #33742) made this pull request unmergeable. Please resolve the merge conflicts. |
@durka ok, thinking more on this, I don't think I really see a reason to not land this. The way doc-blocks are tested is already voodoo, so that's not really the issue at hand here. Do you have a concrete use case where this goes awry? I think that this is fully backwards-compatible otherwise. |
I'm fine with it landing. Just two things:
|
Yeah this perhaps could happen, but there's no way that would actually work out well in practice (the macro/crate would be unusable anyway), so I doubt that's actually happening anywhere. I couldn't actually think of any backcompat hazard, which is why I think it's ok to land this. |
But then why don't we put |
As I commented before
There's also nothing to get around here, all this patch does is inject |
The case where you still need to "get around" the voodoo is if you are not doctesting a macro but you do want to invoke a macro. |
Again I'm sorry to be a stick in the mud here but I don't want this to get any harder to figure out than it already is. Just now coaching a user on IRC who was frustrated with doctests to the point of not wanting to mark them as code. |
@birkenfeld do you want to rebase this and perhaps exclude the check for |
Closing due to inactivity, but feel free to resubmit with a rebase! |
Fixes: #29286
Note: I tried to add a test to
src/test/rustdoc
, but the way in which these tests are called doesn't seem to allowextern crate testname;
to work at all...