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

Linker error with u32::to_ne_bytes and map #88549

Closed
ejmount opened this issue Aug 31, 2021 · 3 comments
Closed

Linker error with u32::to_ne_bytes and map #88549

ejmount opened this issue Aug 31, 2021 · 3 comments
Labels
A-const-generics Area: const generics (parameters and arguments) A-linkage Area: linking into static, shared libraries and binaries A-monomorphization Area: Monomorphization C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ejmount
Copy link

ejmount commented Aug 31, 2021

I tried this code (playground example)

#![allow(incomplete_features)]
#![feature(const_generics)]

pub fn main() {
    // this causes a linker error if const_generics is enabled
    let _ = [0; 1].map(u32::to_ne_bytes);
    
    // but this does not!
    //let _ = [0; 1].map(|x| u32::to_ne_bytes(x));
}

I expected to see this happen: a compile error. And also, I particularly expect map(foo) and map(|x| foo(x)) to behave similarly.

Instead, this happened: the linker falls over:

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-03b16363306a53dd.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-13671d003abcccd5.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: in function `core::array::collect_into_array':
          /rustc/5d6804469d80aaf26f98090ae016af45e267f58f/library/core/src/array/mod.rs:506: undefined reference to `core::mem::maybe_uninit::MaybeUninit<T>::uninit_array'
          /usr/bin/ld: /rustc/5d6804469d80aaf26f98090ae016af45e267f58f/library/core/src/array/mod.rs:485: undefined reference to `core::mem::zeroed'
          /usr/bin/ld: /rustc/5d6804469d80aaf26f98090ae016af45e267f58f/library/core/src/array/mod.rs:508: undefined reference to `core::mem::maybe_uninit::MaybeUninit<T>::slice_as_mut_ptr'
          /usr/bin/ld: /rustc/5d6804469d80aaf26f98090ae016af45e267f58f/library/core/src/array/mod.rs:515: undefined reference to `core::slice::<impl [T]>::get_unchecked_mut'
          /usr/bin/ld: /rustc/5d6804469d80aaf26f98090ae016af45e267f58f/library/core/src/array/mod.rs:515: undefined reference to `core::mem::maybe_uninit::MaybeUninit<T>::write'
          /usr/bin/ld: /rustc/5d6804469d80aaf26f98090ae016af45e267f58f/library/core/src/array/mod.rs:521: undefined reference to `core::mem::forget'
          /usr/bin/ld: /rustc/5d6804469d80aaf26f98090ae016af45e267f58f/library/core/src/array/mod.rs:525: undefined reference to `core::mem::maybe_uninit::MaybeUninit<T>::array_assume_init'
          /usr/bin/ld: /playground/target/debug/deps/playground-4c173013dcdff931: hidden symbol `_ZN4core5slice29_$LT$impl$u20$$u5b$T$u5d$$GT$17get_unchecked_mut17hab68e22870af8369E' 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)
@ejmount ejmount added the C-bug Category: This is a bug. label Aug 31, 2021
@hellow554
Copy link
Contributor

hellow554 commented Sep 1, 2021

It looks like you are using an oder version of rustc? Can you post your version please (rustc -vV). In the meantime, use rustup update to update your compiler.

I can't see the error anymore on the latest nightly

Wow, this is funny!

So feature(const_generics) is now "removed" (in #88369) and replaced by either adt_const_params or generic_const_exprs. If you choose the first, the program will compile fine and gives no warning and even runs as intended.

But if you choose the later, the program will error out with the exact same message as @ejmount described.

Second fun fact, if the second map isn't commeted out (and the still one is still in place) the program will compile fine in both configurations.

@Rain336
Copy link

Rain336 commented Sep 2, 2021

Sounds to me like a monomorpization problem! It seams like the first example doesn't create a monomorpized version of the u32::to_ne_bytes function, but still references it! That would also explain why it would work if the second example is enabled, since that one would correctly generate a monomorpized version of u32::to_ne_bytes, which can then be used by the first example!

@fmease fmease added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-generics Area: const generics (parameters and arguments) A-monomorphization Area: Monomorphization and removed needs-triage-legacy labels Jan 26, 2024
@fmease
Copy link
Member

fmease commented Jan 26, 2024

I can no longer reproduce this even if I follow

So feature(const_generics) is now "removed" (in #88369) and replaced by either adt_const_params or generic_const_exprs. If you choose the first, the program will compile fine and gives no warning and even runs as intended.

Closing as fixed. Please ping me if I missed anything.

@fmease fmease closed this as completed Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-linkage Area: linking into static, shared libraries and binaries A-monomorphization Area: Monomorphization C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants