0

I'm trying to build diesel_demo with a stable compiler, not a nightly. Part of the build.rs, modified by me, is:

fn main() {
    extern crate syntex;
    extern crate diesel_codegen_syntex;
    extern crate dotenv_codegen;

    use std::env;
    use std::path::Path;

    let out_dir = env::var_os("OUT_DIR").unwrap();
    let mut registry = syntex::Registry::new();
    diesel_codegen_syntex::register(&mut registry);
    dotenv_codegen::register(&mut registry);

    let src = Path::new("src/lib.in.rs");
    let dst = Path::new(&out_dir).join("lib.rs");

    registry.expand("", &src, &dst).unwrap();
}

When building with Rust 1.11, I get an error:

error: mismatched types [E0308]
    diesel_codegen_syntex::register(&mut registry);
                                    ^~~~~~~~~~~~~
help: run `rustc --explain E0308` to see a detailed explanation
note: expected type `&mut syntex::Registry`
note:    found type `&mut main::syntex::Registry`
error: mismatched types [E0308]
    dotenv_codegen::register(&mut registry);
                             ^~~~~~~~~~~~~
help: run `rustc --explain E0308` to see a detailed explanation
note: expected type `&mut syntex::Registry`
note:    found type `&mut main::syntex::Registry`

What is the main::syntex::Registry type, where did the compiler get it and why it is not the same thing as syntex::Registry?

To reproduce this issue you need change Cargo.toml of disel_demo:

-default = ["nightly"]
+default = ["with-syntex"]

and replace diesel_codegen with diesel_codegen_syntex in build.rs

It looks like there are two syntex::Registry types. I have no idea why, but because diesel_codegen_syntex depends on syntex, there may be two different versions of syntex?

3
  • @kennytm Actually I am insteresting what is going on if extern crate inside the function, because of I didn't meet such construction before.
    – fghj
    Commented Aug 31, 2016 at 9:04
  • Do you get two different versions of syntex in the Cargo.lock file? Commented Aug 31, 2016 at 11:05
  • @ChrisEmerson Yes, two versions of syntex and syntex_something
    – fghj
    Commented Aug 31, 2016 at 11:43

1 Answer 1

3

As I understand it, Cargo allows several versions of a crate in the same project, which can happen if different dependencies require different versions. If so, they will be treated as separate sets of modules and types - otherwise common versions use the same instance.

At time of writing, diesel_codegen_syntex seems to want version syntex 0.42.0, where the build.rs is asking for version 0.38.0. This means that the registry instantiated in build.rs's main() is not compatible with the type expected by diesel_codegen_syntex::register().

The fact that one is main::syntex::Registry is because the crate is imported into main's scope rather than the global module/crate root; it's scoped like use or let. If you move the extern crate syntex; to the top level outside main the types are still different, but confusingly the visible names are the same; the error then says:

build.rs:12:37: 12:50 note: expected type `&mut syntex::Registry`
build.rs:12:37: 12:50 note:    found type `&mut syntex::Registry`

You can get around this problem by changing the version in the [build-dependencies] section of Cargo.toml to match; change:

syntex = { version = "0.38.0", optional = true }

to:

syntex = { version = "0.42.0", optional = true }

That gets build.rs to compile for me (it doesn't get as far as linking because I don't have the postgres libraries installed, though, so I can't guarantee there aren't futher issues).

2
  • Sorry for not clear question. I know how to fix problem. What I do not know, what is going on from language point of view. How extern crate inside function pollete name space, what rules to assign names to the same types with different version?
    – fghj
    Commented Aug 31, 2016 at 12:06
  • I've made an edit which I hope will help. The types are different because they're from different versions/instances of the same crate - nothing to do with "extern crate" being in main. Commented Aug 31, 2016 at 14:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.