Discussion:
[rust-dev] Decodable trait cannot be found when used in a module
Oldřich Vetešník
2014-09-05 15:41:51 UTC
Permalink
Hello all,

I?m having some trouble when using external crates in a module. For example the following code won?t compile.
But if I put everything in main.rs it will compile.
(Note: the code is also here https://gist.github.com/ollie/90e266f4cbfcad21501d if it gets mangled along the way.)

main.rs:

use lib::decode_json_file;

mod lib;

fn main() {
decode_json_file();
}


lib.rs:

extern crate serialize;

use self::serialize::json;
// This has no effect:
// use self::serialize::{Decodable, Decoder};

#[deriving(Show, Decodable)]
struct Foo {
foo: u8,
}

pub fn decode_json_file() {
let raw_json = "{ \"foo\": 1 }";

let foo: Foo = json::decode(raw_json).unwrap();

println!("{}", foo);
}


When I run rustc main.rs, it prints this:

lib.rs:7:18: 7:27 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: attempt to bound type parameter with a nonexistent trait `serialize::Decoder`
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: attempt to implement a nonexistent trait `serialize::Decodable`
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean `self::serialize::Decodable`?
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: unresolved name `serialize::Decodable::decode`.
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
error: aborting due to 6 previous errors


Am I doing something wrong?

Thank you and have a nice day,
Ollie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140905/d1a20b44/attachment.html>
Erick Tryzelaar
2014-09-05 16:05:07 UTC
Permalink
Woah, this is unexpected behavior. I've slimmed down this error to:

lib.rs:
```
extern crate serialize;

struct Foo;

impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E> for Foo {
fn decode(_d: &mut D) -> Result<Foo, E> {
fail!()
}
}
```

main.rs:
```
mod lib;
fn main() {}
```


Which errors with:

```
lib.rs:5:10: 5:19 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:5 impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E>
for Foo {
^~~~~~~~~
lib.rs:5:10: 5:33 error: attempt to bound type parameter with a nonexistent
trait `serialize::Decoder`
lib.rs:5 impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E>
for Foo {
^~~~~~~~~~~~~~~~~~~~~~~
lib.rs:5:38: 5:47 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:5 impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E>
for Foo {
^~~~~~~~~
lib.rs:5:38: 5:66 error: attempt to implement a nonexistent trait
`serialize::Decodable`
lib.rs:5 impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E>
for Foo {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 4 previous errors
```

However, if instead we use a relative path to `serialize::Decoder` and etc,
it works:

```
extern crate serialize;

struct Foo;

impl <D: serialize::Decoder<E>, E> serialize::Decodable<D, E> for Foo {
fn decode(_d: &mut D) -> Result<Foo, E> {
fail!()
}
}
```

It also works if we move the `extern crate serialize` into `main.rs`. Since
almost everyone puts the `extern crate` in the lib.rs/main.rs of a project,
we tend not to run into this. I thought this used to work. Has anything
recently changed in name resolution that would have changed how how `extern
crate` works with submodules?



On Fri, Sep 5, 2014 at 8:41 AM, Old?ich Vete?n?k <oldrich.vetesnik at gmail.com
Post by Oldřich Vetešník
Hello all,
I?m having some trouble when using external crates in a module. For
example the following code won?t compile.
But if I put everything in main.rs it will compile.
(Note: the code is also here
https://gist.github.com/ollie/90e266f4cbfcad21501d if it gets mangled
along the way.)
use lib::decode_json_file;
mod lib;
fn main() {
decode_json_file();
}
extern crate serialize;
use self::serialize::json;
// use self::serialize::{Decodable, Decoder};
#[deriving(Show, Decodable)]
struct Foo {
foo: u8,
}
pub fn decode_json_file() {
let raw_json = "{ \"foo\": 1 }";
let foo: Foo = json::decode(raw_json).unwrap();
println!("{}", foo);
}
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean
`self::serialize`?
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: attempt to bound type parameter with a
nonexistent trait `serialize::Decoder`
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean
`self::serialize`?
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: attempt to implement a nonexistent trait `serialize::Decodable`
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean
`self::serialize::Decodable`?
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: unresolved name `serialize::Decodable::decode`.
lib.rs:7 #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
error: aborting due to 6 previous errors
Am I doing something wrong?
Thank you and have a nice day,
Ollie
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140905/d9dcaa1f/attachment.html>
Oldřich Vetešník
2014-09-05 20:28:24 UTC
Permalink
Ha, you?re right, if I put the `extern crate` in main.rs and drop the `self::`, it works.
Anyway should I make an issue on github?
Post by Oldřich Vetešník
```
extern crate serialize;
struct Foo;
impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E> for Foo {
fn decode(_d: &mut D) -> Result<Foo, E> {
fail!()
}
}
```
```
mod lib;
fn main() {}
```
```
lib.rs:5:10: 5:19 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:5 (http://lib.rs:5) impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E> for Foo {
^~~~~~~~~
lib.rs:5:10: 5:33 error: attempt to bound type parameter with a nonexistent trait `serialize::Decoder`
lib.rs:5 (http://lib.rs:5) impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E> for Foo {
^~~~~~~~~~~~~~~~~~~~~~~
lib.rs:5:38: 5:47 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:5 (http://lib.rs:5) impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E> for Foo {
^~~~~~~~~
lib.rs:5:38: 5:66 error: attempt to implement a nonexistent trait `serialize::Decodable`
lib.rs:5 (http://lib.rs:5) impl <D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E> for Foo {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 4 previous errors
```
```
extern crate serialize;
struct Foo;
impl <D: serialize::Decoder<E>, E> serialize::Decodable<D, E> for Foo {
fn decode(_d: &mut D) -> Result<Foo, E> {
fail!()
}
}
```
It also works if we move the `extern crate serialize` into `main.rs (http://main.rs)`. Since almost everyone puts the `extern crate` in the lib.rs/main.rs (http://lib.rs/main.rs) of a project, we tend not to run into this. I thought this used to work. Has anything recently changed in name resolution that would have changed how how `extern crate` works with submodules?
Post by Oldřich Vetešník
Hello all,
I?m having some trouble when using external crates in a module. For example the following code won?t compile.
But if I put everything in main.rs (http://main.rs) it will compile.
(Note: the code is also here https://gist.github.com/ollie/90e266f4cbfcad21501d if it gets mangled along the way.)
use lib::decode_json_file;
mod lib;
fn main() {
decode_json_file();
}
extern crate serialize;
use self::serialize::json;
// use self::serialize::{Decodable, Decoder};
#[deriving(Show, Decodable)]
struct Foo {
foo: u8,
}
pub fn decode_json_file() {
let raw_json = "{ \"foo\": 1 }";
let foo: Foo = json::decode(raw_json).unwrap();
println!("{}", foo);
}
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:7 (http://lib.rs:7) #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: attempt to bound type parameter with a nonexistent trait `serialize::Decoder`
lib.rs:7 (http://lib.rs:7) #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean `self::serialize`?
lib.rs:7 (http://lib.rs:7) #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: attempt to implement a nonexistent trait `serialize::Decodable`
lib.rs:7 (http://lib.rs:7) #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: failed to resolve. Did you mean `self::serialize::Decodable`?
lib.rs:7 (http://lib.rs:7) #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
lib.rs:7:18: 7:27 error: unresolved name `serialize::Decodable::decode`.
lib.rs:7 (http://lib.rs:7) #[deriving(Show, Decodable)]
^~~~~~~~~
note: in expansion of #[deriving]
lib.rs:7:1: 7:29 note: expansion site
error: aborting due to 6 previous errors
Am I doing something wrong?
Thank you and have a nice day,
Ollie
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org (mailto:Rust-dev at mozilla.org)
https://mail.mozilla.org/listinfo/rust-dev
Loading...