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

Rust is confused by variants and structs #19293

Closed
frewsxcv opened this issue Nov 25, 2014 · 2 comments · Fixed by #19317
Closed

Rust is confused by variants and structs #19293

frewsxcv opened this issue Nov 25, 2014 · 2 comments · Fixed by #19317

Comments

@frewsxcv
Copy link
Member

(the title of this issue is terrible, sorry!)

As demonstrated in this repository

lib.rs

pub struct Foo (pub int);
pub enum MyEnum {
    Foo(Foo),
}

test.rs

extern crate meow;
use meow::{Foo, MyEnum};
#[test]
fn test() {
    MyEnum::Foo(Foo(5));
}

Shouldn't this test work? Right now, it thinks the inner Foo is the MyEnum::Foo variant, which is not the case.

/t/m/meow (master|✔) $ cargo build; and cargo test --verbose
   Compiling meow v0.0.1 (file:///private/tmp/meow/meow)
     Running `rustc /private/tmp/meow/meow/src/lib.rs --crate-name meow --crate-type lib -g --test -C metadata=425f6212fa03ed6b -C extra-filename=-425f6212fa03ed6b --out-dir /private/tmp/meow/meow/target --dep-info /private/tmp/meow/meow/target/.fingerprint/meow-df845645e3f32c19/dep-test-lib-meow -L /private/tmp/meow/meow/target -L /private/tmp/meow/meow/target/deps`
     Running `rustc /private/tmp/meow/meow/tests/test.rs --crate-name test --crate-type bin -g --test -C metadata=35d1dbe396721794 -C extra-filename=-35d1dbe396721794 --out-dir /private/tmp/meow/meow/target --dep-info /private/tmp/meow/meow/target/.fingerprint/meow-df845645e3f32c19/dep-test-bin-test -L /private/tmp/meow/meow/target -L /private/tmp/meow/meow/target/deps --extern meow=/private/tmp/meow/meow/target/libmeow-df845645e3f32c19.rlib`
/private/tmp/meow/meow/tests/test.rs:8:21: 8:22 error: mismatched types: expected `meow::Foo`, found `_` (expected struct meow::Foo, found integral variable)
/private/tmp/meow/meow/tests/test.rs:8     MyEnum::Foo(Foo(5));
                                                           ^
/private/tmp/meow/meow/tests/test.rs:8:17: 8:23 error: mismatched types: expected `meow::Foo`, found `meow::MyEnum` (expected struct meow::Foo, found enum meow::MyEnum)
/private/tmp/meow/meow/tests/test.rs:8     MyEnum::Foo(Foo(5));
                                                       ^~~~~~
error: aborting due to 2 previous errors
Build failed, waiting for other jobs to finish...
Could not compile `meow`.

Caused by:
  Process didn't exit successfully: `rustc /private/tmp/meow/meow/tests/test.rs --crate-name test --crate-type bin -g --test -C metadata=35d1dbe396721794 -C extra-filename=-35d1dbe396721794 --out-dir /private/tmp/meow/meow/target --dep-info /private/tmp/meow/meow/target/.fingerprint/meow-df845645e3f32c19/dep-test-bin-test -L /private/tmp/meow/meow/target -L /private/tmp/meow/meow/target/deps --extern meow=/private/tmp/meow/meow/target/libmeow-df845645e3f32c19.rlib` (status=101)

I feel like this is one of those moment where I'm doing something very obviously wrong.

@ftxqxd
Copy link
Contributor

ftxqxd commented Nov 25, 2014

It looks like the underlying problem here is that cross-crate variants are not namespaced under their enum:

lib.rs:

pub enum MyEnum {
    Foo(int),
}

test.rs:

extern crate meow;

#[test]
fn test() {
    // This compiles, but shouldn’t; it should have to be meow::MyEnum::Foo(5)
    let _: meow::MyEnum = meow::Foo(5);
}

If these two files are combined into a single crate, it behaves as expected (i.e., fails to compile).

@ghost ghost added the I-nominated label Nov 25, 2014
@sfackler
Copy link
Member

Should be an easy fix. This chunk of code was deleted at one point but must have come back in a rebase or something: https://github.com/rust-lang/rust/blob/master/src/librustc/metadata/encoder.rs#L509-L516

sfackler added a commit to sfackler/rust that referenced this issue Nov 25, 2014
alexcrichton added a commit to alexcrichton/rust that referenced this issue Nov 27, 2014
The chunk of code in encoder.rs was at one point deleted, but must have come back in a rebase or something :(

Closes rust-lang#19293
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.

3 participants