rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.06k stars 12.54k forks source link

error: linking with `cc` failed: exit status: 1 #88562

Open steffahn opened 3 years ago

steffahn commented 3 years ago
fn main() {
    let x = Delegate(|_: &String| ());
    dbg!(x);
}

pub struct Delegate<T>(fn(Data: &T));

use std::fmt;

impl<T> fmt::Debug for Delegate<T> {
    fn fmt<'a>(&'a self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Delegate")
            .field(&self.0 as &fn(&'a T))
            .finish()
    }
}
   Compiling playground v0.0.1 (/playground)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/playground/target/debug/deps/playground-4c173013dcdff931.playground.8c85a93c-cgu.0.rcgu.o" "/playground/target/debug/deps/playground-4c173013dcdff931.12vh8z6qs538xs91.rcgu.o" "-Wl,--as-needed" "-L" "/playground/target/debug/deps" "-L" "/playground/target/debug/build/libsqlite3-sys-0d351e27eaa1e97b/out" "-L" "/playground/target/debug/build/ring-38bf9793e960dc3f/out" "-L" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-d31eae41cd792517.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-4695c0f6311791db.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-1a282f8b292d9e3f.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-a54ae5159230894d.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-df56b38eda5fdb84.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-7509adad918d4b3b.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-66d0be1f7efaf5ca.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-41443b46a1557b2c.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-60812e99072283ff.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-412d545269c27063.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-0dbc7e011696d844.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-78b2343cc72ff57a.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-65fa19680bfb5ee4.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-72cab8079f9b3b1e.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-32d13a5363efe47b.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-9ee1d5d15e6abbeb.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-52d5241975807511.rlib" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9924c22ae1efcf66.rlib" "-Wl,--end-group" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-96219fb718f2f3e8.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/playground/target/debug/deps/playground-4c173013dcdff931" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs"
  = note: /usr/bin/ld: /playground/target/debug/deps/playground-4c173013dcdff931.playground.8c85a93c-cgu.0.rcgu.o:(.data.rel.ro..L__unnamed_2+0x0): undefined reference to `core::ptr::drop_in_place<fn(&alloc::string::String)>'
          /usr/bin/ld: /playground/target/debug/deps/playground-4c173013dcdff931: hidden symbol `_ZN4core3ptr56drop_in_place$LT$fn$LP$$RF$alloc..string..String$RP$$GT$17hd611700673430a9aE' isn't defined
          /usr/bin/ld: final link failed: bad value
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `playground` due to previous error

(playground)

@rustbot label T-compiler


Edit: In case that isn’t clear: I’m expecting the code to compile. The context is this thread on URLO.

steffahn commented 3 years ago

In this form

fn main() {
    let x = Delegate(foo);
    println!("{:?}", x);
}

fn foo(_: &String) {}

pub struct Delegate<T> (fn(Data: &T));

use std::fmt;

impl<T> fmt::Debug for Delegate<T> {
    fn fmt<'a, 'b>(&'a self, f: &mut fmt::Formatter<'b>) -> std::fmt::Result {
        f.debug_tuple("Delegate").field(&self.0 as &fn(&'a T)).finish()
    }
}

this is a regression. The code does

steffahn commented 3 years ago

IMO arguable even two regressions: in 1.10 it stops to compile (that one’s old, admitted), and in 1.46 it stops producing an ICE (I prefer an ICE over a linking error: with an ICE the compiler at least admits that it’s doing something wrong and doesn’t pass invalid code to LLVM, and ultimately the linker).

@rustbot label regression-from-stable-to-stable, A-codegen

JamRemco commented 3 years ago

I am not sure if it adds any value, but when you 'forget' the & in the struct def. like this pub struct Delegate<T>(fn(Data: T)); the linker does not crash. and program runs.

problem seems to be the Custom impl for debug

Working examples: https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=638ead9cbca8701ab3e5f73f3b84c6f0

Regards.

Remco

steffahn commented 3 years ago

Using &(self.0 as fn(&'a T)) seems to get rid of the error, i.e.:

impl<T> fmt::Debug for Delegate<T> {
    fn fmt<'a>(&'a self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Delegate")
            .field(&(self.0 as fn(&'a T)))
            .finish()
    }
}
apiraino commented 3 years ago

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-medium

steffahn commented 3 years ago

Reduced example:

trait Trait {
    fn method(&self);
}

impl<'a> Trait for fn(&'a ()) {
    fn method(&self) {}
}

struct Struct(fn(&()));

impl Trait for Struct {
    fn method<'a>(&'a self) {
        let _x = &self.0 as &fn(&'a ()) as &dyn Trait;
    }
}

fn main() {
    let _x = Trait::method as fn(&Struct);
}
steffahn commented 3 years ago

Further reduced:

trait Trait {}

impl Trait for fn(&'static ()) {}

fn main() {
    let f: fn(&()) = |_| ();
    let _x = &f as &fn(&'static ()) as &dyn Trait;
}