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: strange issues with no_std #38129

Closed
phil-opp opened this issue Dec 2, 2016 · 7 comments
Closed

Rustdoc: strange issues with no_std #38129

phil-opp opened this issue Dec 2, 2016 · 7 comments
Labels
T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@phil-opp
Copy link
Contributor

phil-opp commented Dec 2, 2016

The following doc test fails:

#![no_std]

use embedded::components::gpio::stm32f7;

fn main() {}

with the error

error: an inner attribute is not permitted in this context
 --> <anon>:2:3
  |
2 | #![no_std]
  |   ^
  |
  = note: inner attributes and doc comments, like `#![no_std]` or `//! My crate`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes and doc comments, like `#[test]` and
                                   `/// My function`, annotate the item following them.

So rustdoc doesn't recognize the no_std attribute.

However, when I add the extern crate definition manually, it works:

#![no_std]

use embedded::components::gpio::stm32f7;

extern crate embedded; // add the import manually

fn main() {}

It also works for imports from core:

#![no_std]

use core::sync; // import something from core (instead of importing from my own crate)

fn main() {}

The strange thing is that it also works if I add a comment containing extern crate:

#![no_std]

use embedded::components::gpio::stm32f7;

// extern crate

fn main() {}

(With "it works" I mean that the no_std attribute is recognized).


Edit: I added an example project in at phil-opp/issue-38129 (see the comment below).

@steveklabnik
Copy link
Member

So, https://doc.rust-lang.org/stable/book/documentation.html#documentation-as-tests

Any leading #![foo] attributes are left intact as crate attributes.

So I would expect that this error wouldn't happen.

What's truly strange is that trying this out myself, I get the right kind of error:

---- _0 stdout ----
        error[E0432]: unresolved import `foo::bar`
 --> <anon>:2:5
  |
2 | use foo::bar;
  |     ^^^^^^^^ Maybe a missing `extern crate foo`?

error: language item required, but not found: `panic_fmt`

error: language item required, but not found: `eh_personality`

error: language item required, but not found: `eh_unwind_resume`

@steveklabnik steveklabnik added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Dec 2, 2016
@phil-opp
Copy link
Contributor Author

phil-opp commented Dec 2, 2016

trying this out myself, I get the right kind of error:

These errors are fine, since we're creating a no_std executable. It does mean that the no_std attribute is recognized.

@phil-opp
Copy link
Contributor Author

phil-opp commented Dec 2, 2016

I created an example project for this issue at phil-opp/issue-38129. When you run cargo test on it, all 4 tests will fail. However, only the foo test will reject the no_std attribute (the other tests fail because various lang_items are missing in no_std mode).

@durka
Copy link
Contributor

durka commented Dec 2, 2016

The heuristic for whether to automatically insert extern crate is pretty dumb, it just does a substring search.

@durka
Copy link
Contributor

durka commented Dec 2, 2016

And it looks like the book

Any leading #![foo] attributes are left intact as crate attributes.

is lying: it only leaves attributes of the form #![feature(foo)]. Ugh, this heuristic is so leaky. I'm not sure whether we should change the docs or change the code.

@durka
Copy link
Contributor

durka commented Dec 4, 2016

PR submitted.

bors added a commit that referenced this issue Feb 5, 2017
rustdoc: fix doctests with non-feature crate attrs

Fixes #38129.

The book says that any top-level crate attributes at the beginning of a doctest are moved outside the generated `fn main`, but it was only checking for `#![feature`, not `#![`.

These attributes previously caused warnings but were then ignored, so in theory this could change the behavior of doctests in the wild.
bors added a commit that referenced this issue Feb 5, 2017
rustdoc: fix doctests with non-feature crate attrs

Fixes #38129.

The book says that any top-level crate attributes at the beginning of a doctest are moved outside the generated `fn main`, but it was only checking for `#![feature`, not `#![`.

These attributes previously caused warnings but were then ignored, so in theory this could change the behavior of doctests in the wild.
@Zildj1an
Copy link

So what is the workaround if I get this error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants