Skip to content

Associated types lets you expose private types in public signatures (?) #22912

Closed
@japaric

Description

@japaric

STR

#![crate_type = "lib"]

pub struct Public;
struct Private;

impl Iterator for Public {
    type Item = Private;

    fn next(&mut self) -> Option<Private> {  // OK(???) `Private` type in public method 
        unimplemented!();
    }
}

// an inherent method rejects the same pattern
impl Public {
    pub fn my_next(&mut self) -> Option<Private> {  //~ error: private type in exported type signature
        unimplemented!();
    }
}

pub trait MyIterator<Item> {
    fn next(&mut self) -> Option<Item>;
}

// the old version of `Iterator` doesn't allow returning `Private` from `next()` either
impl MyIterator<Private> for Public {  //~ error: private type in exported type signature
    fn next(&mut self) -> Option<Private> {
        unimplemented!();
    }
}

AFAIK, you can't put private types in "public" items (public as in marked with pub). Iterator::next is not explicitly marked with pub, but for all intent and purposes all the methods in a trait are public if the trait is public. So, I think the impl Iterator for Public { type Item = Private; .. } should be rejected by the privacy checker. (Or, I could be wrong and this is "correct" behavior - and this wouldn't be the first time that the privacy/module system allows things that I find unreasonable)

Version

rustc 1.0.0-nightly (e233987ce 2015-02-27) (built 2015-02-28)

cc @alexcrichton @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions