-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Adjust #[macro_export]
/doctest help suggestion for non_local_defs lint
#124568
Conversation
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
2aad418
to
33a3cbc
Compare
// this condition is very much a hack but it's the best we can do for now | ||
let is_at_toplevel_doctest = self.body_depth == 2 | ||
&& parent_opt_item_name | ||
.map(|s| s.as_str().starts_with("_doctest")) | ||
.unwrap_or_default(); |
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.
What if rustdoc added an internal #[rustc_*]
attribute to the generated doc test fns which we would then look for here?
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 would be the best and that's partially what #123974 is trying to do by making doctest regular #[test]
fn, but there are many subtleties.
When that PR lands we could look at improving this checks. In the meantime I would like to stay with this minimal approach so it can be backported easily.
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.
Yes totally agree. Let's not add a hack to try to guess if we're inside a doctest or not please. ^^'
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.
In order to guess less, I changed the PR to use the UNSTABLE_RUSTDOC_TEST_PATH
env, which is already used for diagnostic code.
It's not as great as a proper attribute but much better than looking a function names.
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.
Nah it's fine. Much simpler than a brand new attribute.
r? compiler |
Wouldn't this better to make it a rustdoc lint instead? EDIT: nevermind, got confused when reading "doctest" in the title. So from what I saw, the current code is a hack to detect whether it's a doctest or not. @fmease suggested to instead add a new crate level attribute when generating doctests so we're sure it's a doctest instead of trying to guess what it is, which I think it's a great idea. |
33a3cbc
to
712560c
Compare
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.
Looks good to me. Great idea to use an environment variable set by rustdoc when running doctests!
So there is a path forward to getting rid of the UNSTABLE_RUSTDOC_TEST_PATH env var check? |
I think in the longer term we could a custom attribute. Or we could right now use the fact that rust/compiler/rustc_driver_impl/src/lib.rs Lines 506 to 514 in f5efc3c
and then in the code instead of looking for the env we could do something like this let is_from_doctest = matches!(cx
.tcx
.sess
.source_map()
.span_to_filename(span), FileName:::DocTest(..)); // untested, but I think it would work |
Not something we can rely on so better not. I think using the environment variable is good enough. Not sure if it's worth adding a new attribute for handling this case though... |
Why not? |
I like the |
OK, I don't think we should block this PR on the env var issue. It looks like #123974 is going to change this quite a bit. Feel free to r=me (unless you have a sudden insight on how to improve this 🙂) Thanks for the review, @GuillaumeGomez! |
We can always come back on this later on, nothing's ever definitive in compiler-land. 😺 And since it's not something users can directly use, we can make other changes easily. Anyway, since we got both rustdoc and compiler team approving this, let's go! Thanks everyone and thanks @Urgau for this improvement! @bors r=michaelwoerister,GuillaumeGomez rollup |
…llaumeGomez Rollup of 3 pull requests Successful merges: - rust-lang#124568 (Adjust `#[macro_export]`/doctest help suggestion for non_local_defs lint) - rust-lang#124582 (always print nice 'std not found' error when std is not found) - rust-lang#124597 (Use an explicit x86-64 cpu in tests that are sensitive to it) r? `@ghost` `@rustbot` modify labels: rollup
Nominating for beta-backport (well, the T-compiler meeting is happening as we speak). |
Rollup merge of rust-lang#124568 - Urgau:non-local-defs-doctest, r=michaelwoerister,GuillaumeGomez Adjust `#[macro_export]`/doctest help suggestion for non_local_defs lint This PR adjust the help suggestion of the `non_local_definitions` lint when encountering a `#[macro_export]` at top-level doctest. So instead of a non-sentential help suggestion to move the `macro_rules!` up above the `rustdoc`-generated function. We now suggest users to declare their own function. Fixes *(partially, needs backport)* rust-lang#124534
Beta backport declined as per compiler team on Zulip. Rather than backporting this (which seems to still a few open points, example about the wording), this should be reverted on nightly and then revert that. (cc @Urgau @wesleywiser did I understand correctly the discussion?) @rustbot label -beta-nominated |
…r=fmease Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in rust-lang#124568. Fixes rust-lang#126339 r? `@fmease`
…r=fmease Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in rust-lang#124568. Fixes rust-lang#126339 r? ``@fmease``
…r=fmease Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in rust-lang#124568. Fixes rust-lang#126339 r? ```@fmease```
Rollup merge of rust-lang#126422 - Urgau:doctest-impl-non-local-def, r=fmease Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in rust-lang#124568. Fixes rust-lang#126339 r? ```@fmease```
This PR adjust the help suggestion of the
non_local_definitions
lint when encountering a#[macro_export]
at top-level doctest.So instead of a non-sentential help suggestion to move the
macro_rules!
up above therustdoc
-generated function. We now suggest users to declare their own function.Fixes (partially, needs backport) #124534