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

Defining a public method on a private type alias causes linking failures #27448

Closed
chris-morgan opened this issue Aug 1, 2015 · 1 comment
Closed

Comments

@chris-morgan
Copy link
Member

Suppose we have a crate named c.

src/lib.rs:

pub struct A<T>(T);

type B = A<()>;
impl B {
    pub fn new() -> Self {
        A(())
    }
}
src/lib.rs:5:5: 7:6 warning: method is never used: `new`, #[warn(dead_code)] on by default
src/lib.rs:5     pub fn new() -> Self {
src/lib.rs:6         A(())
src/lib.rs:7     }

Given that type aliases are supposed to be purely aliases, new should not be considered dead code. The fact that B is not public should be immaterial.

Then we try to use it in src/main.rs:

extern crate c;

fn main() {
    let _ = c::A::new();
}
error: linking with `cc` failed: exit code: 1
note: "cc" "-Wl,--as-needed" "-m64" "-L" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/tmp/c/target/debug/c.0.o" "-o" "/tmp/c/target/debug/c" "-Wl,--whole-archive" "-l" "morestack" "-Wl,--no-whole-archive" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "/tmp/c/target/debug/libc.rlib" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-74fa456f.rlib" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-74fa456f.rlib" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_unicode-74fa456f.rlib" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-74fa456f.rlib" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-74fa456f.rlib" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-74fa456f.rlib" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-74fa456f.rlib" "-L" "/tmp/c/target/debug" "-L" "/tmp/c/target/debug/deps" "-L" "/home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "/tmp/c/.rust/lib/x86_64-unknown-linux-gnu" "-L" "/tmp/c/lib/x86_64-unknown-linux-gnu" "-Wl,-Bstatic" "-Wl,-Bdynamic" "-l" "dl" "-l" "pthread" "-l" "rt" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-Wl,-rpath,$ORIGIN/../../../../home/chris/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-rpath,/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-l" "compiler-rt"
note: /tmp/c/target/debug/c.0.o: In function `c::main':
/tmp/c/src/main.rs:4: undefined reference to `B::new::h8f30536929b2bf42paa'
collect2: error: ld returned 1 exit status

So the Rust compiler thinks it exists, but hasn’t gone creating the symbol for it and so it fails in linking.

@chris-morgan chris-morgan changed the title Defining a public method on a private type alias cause linking failures Defining a public method on a private type alias causes linking failures Aug 1, 2015
@alexcrichton
Copy link
Member

I believe this is a dupe of #16734, so closing in favor of that.

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

No branches or pull requests

2 participants