Skip to content

Document usage of extern crate declaration in doctests #30137

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

Closed
contradictioned opened this issue Dec 1, 2015 · 7 comments · Fixed by #30153
Closed

Document usage of extern crate declaration in doctests #30137

contradictioned opened this issue Dec 1, 2015 · 7 comments · Fixed by #30153

Comments

@contradictioned
Copy link
Contributor

When a doctest like this is written:

///
/// ```
/// extern crate mylib;
/// use mylib::something;
/// ...
/// ```

running the tests will produce an error like:

unresolved import `mylib::something`. Did you mean `self::mylib`? [E0432]

since the test code is wrapped inside a fn main() for execution.

This is imho not clear in this algorithm description https://doc.rust-lang.org/stable/book/documentation.html

@jFransham
Copy link
Contributor

This seems like a bug somewhere in rustc/cargo, rather than a problem with the docs, because

///```
///extern crate mylib;
///mylib::foo('bar');
///```

works fine, but (as was mentioned here)

///```
///extern crate mylib;
///use mylib::foo;
///foo('bar');
///```

does not. This seems like wrong behaviour. Also, the suggested

///```
///extern crate mylib;
///use self::mylib::foo;
///foo('bar');
///```

does not work either, I don't know if there is an RFC or issue open about attempting to resolve the names suggested under Did you mean ...? and only showing them if they exist.

EDIT: as mentioned below, this is not a bug with rustc or cargo, it's just that there is no way to refer to the current function scope in a use statement if you import a crate into it.

@durka
Copy link
Contributor

durka commented Dec 1, 2015

@jFransham it's not wrong but it is very misleading. The key is that if fn main() { does not appear in the doc test, then rustdoc encloses the entire test in a main function. This, coupled with the fact that extern crate does not really work inside a function, produces the bad situation in which we find ourselves.

@steveklabnik
Copy link
Member

Right, it's the 'entire' part that's confusing. I myself find it to be so and I wrote the sentence!

@durka
Copy link
Contributor

durka commented Dec 1, 2015

It would be nice if rustdoc used similar heuristics to playbot, where stuff like extern crates and crate attributes get hoisted out of the generated main function.

@jFransham
Copy link
Contributor

Why is extern crate even allowed inside of functions? That seems like a horrible misfeature.

@steveklabnik
Copy link
Member

It's similar to anything else that brings a name into scope, you can do it in whatever scope you'd like.

@durka
Copy link
Contributor

durka commented Dec 1, 2015

I guess the "real" problem is that use has no way to refer to a function/expression scope. self:: doesn't work, making the "did you mean" note less than helpful.

durka added a commit to durka/rust that referenced this issue Dec 1, 2015
I think this fixes rust-lang#30137. I basically just repeated some details that were scattered around other places in this document, and emphasized that you probably don't want an `extern crate` or `mod` statement to end up inside a function.
steveklabnik added a commit to steveklabnik/rust that referenced this issue Dec 2, 2015
I think this fixes rust-lang#30137. I basically just repeated some details that were scattered around other places in this document, and emphasized that you probably don't want an `extern crate` or `mod` statement to end up inside a function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants