Skip to content

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

Closed
@chris-morgan

Description

@chris-morgan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions