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

Modules in doc-tests can not access types in super/crate module #79769

Closed
MauriceKayser opened this issue Dec 6, 2020 · 1 comment
Closed
Labels
C-bug Category: This is a bug.

Comments

@MauriceKayser
Copy link

I probably am just using doc-tests wrongly, but I have come across a "type not found" problem using modules in doc-tests that seems rather odd to me, because it compiles in normal code without complaining:

type MyType = u8;

// Without extra module.
fn my_func(_: MyType) {}

fn main() {
    my_func(0);
}
type MyType = u8;

// With extra module.
mod my_mod { pub(super) fn my_func(_: super::MyType) {} }

fn main() {
    my_mod::my_func(0);
}

When using the same code (without main) in doc-tests with cargo test, only the first snippet passes the test:

//! ```rust
//! type MyType = u8;
//!
//! fn my_func(_: MyType) {}
//!
//! my_func(0);
//! ```
//! ```rust
//! type MyType = u8;
//!
//! mod my_mod { pub(super) fn my_func(_: super::MyType) {} }
//!
//! my_mod::my_func(0);
//! ```

The second snippet fails with the following message:

error[E0412]: cannot find type `MyType` in module `super`
 --> src\lib.rs:4:46
  |
5 | mod my_mod { pub(super) fn my_func(_: super::MyType) {} }
  |                                              ^^^^^^ not found in `super`

I also tried to use crate::MyType instead of super::MyType but that did not work either.

Meta

rustc --version --verbose:

rustc 1.50.0-nightly (e792288df 2020-12-05)
binary: rustc
commit-hash: e792288df31636ca28108516c63a00ce4267063a
commit-date: 2020-12-05
host: x86_64-pc-windows-msvc
release: 1.50.0-nightly
@MauriceKayser MauriceKayser added the C-bug Category: This is a bug. label Dec 6, 2020
@ehuss
Copy link
Contributor

ehuss commented Dec 6, 2020

It doesn't work because doctests wrap the entire example in fn main() { ... }. Functions themselves don't introduce a new namespace in the module hierarchy, so the super is referring to the module scope outside of the main function. #79260 is a similar issue.

The solution is to add a main function:

//! ```rust
//! type MyType = u8;
//!
//! mod my_mod { pub(super) fn my_func(_: super::MyType) {} }
//!
//!
//! fn main() {
//!   my_mod::my_func(0);
//! }
//! ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants