5

I have two files :

  • json.rs in which I call Builder, a struct defined in builder.rs
  • builder.rs in which I call Json, a struct defined in json.rs

As you can see there is a circular dependency between these two modules. It seems like sometimes Rust supports circular dependencies, but for this case, the compiler throws errors:

src/json/mod.rs:1:25: 1:31 error: unresolved import (maybe you meant `ToJson::*`?)
src/json/mod.rs:1 pub use self::to_json::{ToJson};
                                          ^~~~~~
src/json/builder.rs:2:18: 2:22 error: unresolved import (maybe you meant `Json::*`?)
src/json/builder.rs:2 use json::json::{Json, JsonEvent, Array, Object}; //commenting this line solve the dependency error

[...]

The code is here in the branch json_mod. I tried to reproduce the problem in fewer lines of code, but the circular dependencies I created compiled correctly.

After the debugging, something like 400 errors are left — this is normal as I am in the process of a huge code refactoring (splitting one file of ~= 4000 line file into many files) and I still have a lot of work to do before making it work.

1
  • 2
    I can't speak to this specific issue, but I would definitely recommend trying to find a refactoring approach that lets you use smaller steps. You'd have to make a lot of changes between working compilations to fix 400+ errors... Commented May 2, 2015 at 18:17

1 Answer 1

5

Edit: Glorious news, the bug mentioned below is fixed! It's fixed in Rust 1.4 or later.

Glob imports (use foo::*) have a known bug where they are incompatible with cyclic imports. I would try removing the glob imports in both affected modules.

Sign up to request clarification or add additional context in comments.

3 Comments

I thought I saw use std::io::prelude::* in both modules. I'm not 100% it will work, but this is the hunch I have.
Link to the rust issue describing it : github.com/rust-lang/rust/issues/4865
great! Thanks for the link, I didn't find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.