Skip to content

super trait and trait object error with "source trait is inaccessible", but source trait is in scope (via a reexport) #16264

Closed
@japaric

Description

@japaric

STR

use array::ArrayShape;

pub mod array {
    pub use self::traits::ArrayShape;

    mod traits {
        pub trait ArrayShape {
            fn shape(&self) -> (usize, usize) {
                (0, 0)
            }
        }
    }
}

struct Array;

impl ArrayShape for Array {}

fn main() {
    println!("{:?}", Array.shape());  //~ error: source trait is inaccessible
}

trait MatrixShape: ArrayShape {
    fn nrows(&self) -> usize { self.shape().0 }  //~ error: source trait is inaccessible
}

fn show() {
    println!("{:?}", (&Array as &ArrayShape).shape());  //~ error: source trait is inaccessible
}

Version

rustc 1.0.0-nightly (458a6a2f6 2015-01-25 21:20:37 +0000)

Original issue

STR

use array::ArrayShape;

pub mod array {
    pub use self::traits::ArrayShape;

    mod traits {
        pub trait ArrayShape {
            fn shape(&self) -> (uint, uint) {
                (0, 0)
            }
        }
    }
}

struct Array;

// `ArrayShape` is available in this scope, it can be implemented ...
impl ArrayShape for Array {}

fn main() {
    // ... and can also be used
    println!("{}", Array.shape());
}

// But this super trait doesn't work
#[cfg(not(working))]
trait MatrixShape: ArrayShape {
    fn nrows(&self) -> uint { self.shape().val0() }
}

#[cfg(not(working))]
fn bad() {
    // This doesn't work either
    println!("{}", (&Array as &ArrayShape).shape());
}

Output:

trait.rs:28:31: 28:43 error: source trait is inaccessible
trait.rs:28     fn nrows(&self) -> uint { self.shape().val0() }
                                          ^~~~~~~~~~~~
trait.rs:28:31: 28:43 note: module `traits` is private
trait.rs:28     fn nrows(&self) -> uint { self.shape().val0() }
                                          ^~~~~~~~~~~~
trait.rs:34:20: 34:51 error: source trait is inaccessible
trait.rs:34     println!("{}", (&Array as &ArrayShape).shape());
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
trait.rs:34:5: 34:53 note: expansion site
trait.rs:34:20: 34:51 note: module `traits` is private
trait.rs:34     println!("{}", (&Array as &ArrayShape).shape());
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
trait.rs:34:5: 34:53 note: expansion site
error: aborting due to 2 previous errors

I'm no longer sure if this is a bug or is intended? The impl block works, but the super trait and the trait object don't. Do the latter two have different scoping/privacy rules?

Version:

rustc 0.12.0-pre (795f6ae82 2014-08-04 07:01:10 +0000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions