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

#[deriving(ToStr)] handles generics in a surprising manner. #7180

Closed
huonw opened this issue Jun 16, 2013 · 1 comment · Fixed by #7204
Closed

#[deriving(ToStr)] handles generics in a surprising manner. #7180

huonw opened this issue Jun 16, 2013 · 1 comment · Fixed by #7204

Comments

@huonw
Copy link
Member

huonw commented Jun 16, 2013

#[deriving(ToStr)]
struct Foo<A>(A);

struct Bar;
impl ToStr for Bar {
    fn to_str(&self) -> ~str {
        ~"some string"
    }
}

fn main() {
    println(Bar.to_str());
    println(Foo(Bar).to_str());
}

prints:

some string
{__field__: {}}

Ignoring the strange struct printing, it should probably print:

some string
{__field__: some string}

i.e. use the ToStr impl of any generics.

@alexcrichton
Copy link
Member

Edit: disregard all this, I'm working on a fix

This may also be a problem with fmt!. With deriving(ToStr), you can pass rustc --pretty expanded to get what it expands to:

#[deriving(ToStr)]
struct Foo<A>(A);
#[doc = "Automatically derived."]
pub impl <A: ::std::to_str::ToStr> ::std::to_str::ToStr for Foo<A> {
    pub fn to_str(&self) -> ~str {
        match *self { Foo(ref __self_0_0) => ::std::sys::log_str(&*self) }
    }
}

Which is basically invoking fmt("%?", self). And, as expected, the program

#[deriving(ToStr)]
struct Foo<A>(A);

struct Bar;
impl ToStr for Bar {
    fn to_str(&self) -> ~str {
        ~"some string"
    }
}

fn main() {
    println(Bar.to_str());
    println(Foo(Bar).to_str());
    println(fmt!("%?", Foo(Bar)));
}

also prints out

some string
{__field__: {}}
{__field__: {}}

@bors bors closed this as completed in fc83d82 Jun 23, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue May 20, 2021
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.

2 participants