CrystalGamma
CrystalGamma

Reputation: 13

Source trait is inaccessible

The situation is (severely simplified) this (playpen):

mod tokentree {
    pub struct TokenTree;
    mod serialize {
        use std::collections::BTreeMap;
        use super::TokenTree;
        #[derive(Debug)]
        pub struct InternStr;
        pub trait InternStrsExt {
            fn intern_strs(&self) -> BTreeMap<&str, InternStr>;
        }
        impl InternStrsExt for [TokenTree] {
            fn intern_strs(&self) -> BTreeMap<&str, InternStr> { BTreeMap::new() }
        }
    }
    pub use self::serialize::{InternStrsExt, InternStr};
}
use tokentree::*;

fn main() {
    println!("{:?}", [TokenTree].intern_strs());
}

I get the following error (both on nightly and beta):

<anon>:20:22: 20:47 error: source trait is inaccessible
<anon>:20     println!("{:?}", [TokenTree].intern_strs());
                               ^~~~~~~~~~~~~~~~~~~~~~~~~

My problem is that I don't even know what this is supposed to mean.

Upvotes: 1

Views: 1072

Answers (1)

Daniel Fath
Daniel Fath

Reputation: 18109

It needs a pub declaration. Also your declarations are all over the place. Recommended form is to stick your pub mod declarations first, then, use.

Here is the working example.

mod tokentree {
    pub struct TokenTree;
    pub mod serialize {
        use std::collections::BTreeMap;
        use super::TokenTree;
        #[derive(Debug)]
        pub struct InternStr;
        pub trait InternStrsExt {
            fn intern_strs(&self) -> BTreeMap<&str, InternStr>;
        }
        impl InternStrsExt for [TokenTree] {
            fn intern_strs(&self) -> BTreeMap<&str, InternStr> { BTreeMap::new() }
        }
    }
    pub use self::serialize::{InternStrsExt, InternStr};
}
pub use tokentree::*;

fn main() {
    println!("{:?}", [TokenTree].intern_strs());
}

(playpen)

What happened here is that you stumbled upon following glitches:

You can't export your traits from a private module. That's why you need to change mod serialize into pub mod serialize. For example this playpen example demonstrates that exporting struct Export works, but un-commenting the println! will make it stop compiling, because we used a trait.

Tip: One thing that helps me with the visibility rules is to generate doc files and see which doc files are visible.

Upvotes: 1

Related Questions