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

Rustdoc creates doctests with extern crate mod; #79771

Closed
Swatinem opened this issue Dec 6, 2020 · 3 comments · Fixed by #79775
Closed

Rustdoc creates doctests with extern crate mod; #79771

Swatinem opened this issue Dec 6, 2020 · 3 comments · Fixed by #79775
Assignees
Labels
A-doctests Area: Documentation tests, run by rustdoc C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@Swatinem
Copy link
Contributor

Swatinem commented Dec 6, 2020

In #79770 I tried to create a new rustdoc-ui testcase that runs doctests. However having the mod substring in a doctest comment injected an invalid extern crate mod; into the code generated for the doctest.

Turns out this here is a bit too aggressive:

if s.contains(cratename) {
prog.push_str(&format!("extern crate {};\n", cratename));

It looks like compiletest actually gave the crate the name mod, perhaps because it was defined in a issue-XXX/mod.rs file, and having a doctest such as this triggers the this auto-injection, resulting in a failed doctest run:

/// ```
/// // ^ mod.rs line 10
/// assert_eq!(1, 1);
/// ```
@camelid camelid added A-doctests Area: Documentation tests, run by rustdoc C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Dec 6, 2020
@jyn514 jyn514 changed the title Doctest inject_crate too aggressive for rustdoc-ui testsuite tests Rustdoc creates doctests with extern crate mod; Dec 6, 2020
@jyn514
Copy link
Member

jyn514 commented Dec 6, 2020

I think rustdoc needs to check if the crate name is a keyword and add r# if so. Actually it's probably enough to unconditionally add r# since it works for things that aren't keywords.

@Swatinem
Copy link
Contributor Author

Actually it's probably enough to unconditionally add r# since it works for things that aren't keywords.

there is actually two bugs here. mod not being properly escaped is one thing, the other is probably the way that rustdoc is invoked, or not quite sure. Basically it infers the crate name from the file name if not explicitly given, and then just assumes that a crate with that name exists, which it doesn’t.

@jyn514
Copy link
Member

jyn514 commented Dec 10, 2020

@Swatinem that's an issue with compiletest, not rustdoc - it needs to compile the crate itself before running rustdoc on it: #79775 (comment). I don't know how to fix that, I think compiletest needs to add support for this maybe?

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 12, 2021
Fix injected errors when running doctests on a crate named after a keyword

Closes rust-lang#79771
@bors bors closed this as completed in ab3f4f0 Feb 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-doctests Area: Documentation tests, run by rustdoc C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants