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

Adjust #[macro_export]/doctest help suggestion for non_local_defs lint #124568

Merged
merged 1 commit into from
May 2, 2024

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented Apr 30, 2024

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) #124534

@rustbot
Copy link
Collaborator

rustbot commented Apr 30, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Apr 30, 2024
Comment on lines 235 to 239
// 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();
Copy link
Member

@fmease fmease May 1, 2024

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?

Copy link
Member Author

@Urgau Urgau May 1, 2024

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.

Copy link
Member

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. ^^'

Copy link
Member Author

@Urgau Urgau May 1, 2024

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.

Copy link
Member

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.

@fee1-dead
Copy link
Member

r? compiler

@rustbot rustbot assigned michaelwoerister and unassigned fee1-dead May 1, 2024
@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented May 1, 2024

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.

Copy link
Member

@GuillaumeGomez GuillaumeGomez left a 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!

@michaelwoerister
Copy link
Member

So there is a path forward to getting rid of the UNSTABLE_RUSTDOC_TEST_PATH env var check?

@Urgau
Copy link
Member Author

Urgau commented May 2, 2024

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 rustc crates a special FileName::DocTest for doctest when given UNSTABLE_RUSTDOC_TEST_PATH:

if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
);
let line = isize::from_str_radix(&line, 10)
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
Ok(Some(Input::Str { name: file_name, input: src }))

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

@GuillaumeGomez
Copy link
Member

Or we could right now use the fact that rustc crates a special FileName::DocTest for doctest when given UNSTABLE_RUSTDOC_TEST_PATH:

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...

@fmease
Copy link
Member

fmease commented May 2, 2024

Not something we can rely on so better not.

Why not? File::DocTest resides in rustc just like the lint. Its removal would trigger a build error if used in the lint.

@michaelwoerister
Copy link
Member

I like the FileName:::DocTest idea better than using an env var.

@GuillaumeGomez
Copy link
Member

Because it might be changed in the future when #123974 is merged (you can see the change here).

@michaelwoerister
Copy link
Member

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.

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!

@GuillaumeGomez
Copy link
Member

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

@bors
Copy link
Contributor

bors commented May 2, 2024

📌 Commit 712560c has been approved by michaelwoerister,GuillaumeGomez

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 2, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request May 2, 2024
…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
@fmease
Copy link
Member

fmease commented May 2, 2024

Nominating for beta-backport (well, the T-compiler meeting is happening as we speak).
Removing T-rustdoc to avoid triggering the triagebot nomination posting on Zulip.

@fmease fmease removed the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label May 2, 2024
@fmease fmease added the beta-nominated Nominated for backporting to the compiler in the beta channel. label May 2, 2024
@bors bors merged commit df9f8d0 into rust-lang:master May 2, 2024
6 checks passed
@rustbot rustbot added this to the 1.80.0 milestone May 2, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 2, 2024
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
@apiraino
Copy link
Contributor

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

@rustbot rustbot removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label May 10, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jun 18, 2024
…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`
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jun 19, 2024
…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``
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jun 19, 2024
…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```
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jun 19, 2024
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```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants