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

Const generics with function pointers produces linker error. #62395

Closed
Mr-Byte opened this issue Jul 5, 2019 · 1 comment · Fixed by #64986
Closed

Const generics with function pointers produces linker error. #62395

Mr-Byte opened this issue Jul 5, 2019 · 1 comment · Fixed by #64986
Labels
A-const-generics Area: const generics (parameters and arguments) A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Mr-Byte
Copy link

Mr-Byte commented Jul 5, 2019

When attempting to compile a program using const generic function pointers, I get the following linker error:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/playground/target/debug/deps/playground-343407c725b154da.playground.9gij2ntw-cgu.0.rcgu.o" "-o" "/playground/target/debug/deps/playground-343407c725b154da" "/playground/target/debug/deps/playground-343407c725b154da.3lfkt68d9bwnd0w.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/playground/target/debug/deps" "-L" "/playground/target/debug/build/backtrace-sys-0bad857f246b8d46/out" "-L" "/playground/target/debug/build/miniz-sys-cc52677d966ce23b/out" "-L" "/playground/target/debug/build/ring-3ec1d93b15dca95a/out" "-L" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-3155131507cc46c3.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-d295083df0b8f9bc.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace-ee721caa5e399a5b.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace_sys-f522ac12e8b57785.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-fde635d685e9d5b4.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-400b98aa42a3327b.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-8d4d01f94c0ba00e.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-bbe52d74e07e8ddb.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-7809a3c56dc793e3.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-eb4f3199c7861934.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-33ebcd88f3886c16.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-a2ceb2a4c1b13237.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-c19151a56f15ba15.rlib" "-Wl,--end-group" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-0ce4526441314674.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil"
  = note: /playground/target/debug/deps/playground-343407c725b154da.playground.9gij2ntw-cgu.0.rcgu.o: In function `playground::Wrapper<_>::call':
          /playground/src/main.rs:11: undefined reference to `playground::hello_world'
          collect2: error: ld returned 1 exit status

Source:

#![feature(const_generics)]

fn hello_world() {
    println!("Hello, world!");
}

struct Wrapper<const F: fn() -> ()>;

impl<const F: fn() -> ()> Wrapper<{F}> {
    fn call() {
        F();
    }
}

fn main() {
    Wrapper::<{hello_world}>::call();
}

I'm not sure what the intended behavior should be here, but I was expecting to be able to call the function that I passed as a const generic parameter, which the compiler seems to accept as valid, but is failing during the linking step.

@Centril Centril added A-const-generics Area: const generics (parameters and arguments) 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. labels Jul 5, 2019
@varkor varkor removed their assignment Jul 5, 2019
@Aaron1011
Copy link
Member

Possibly related: The following code:

#![feature(const_generics)]
struct Checked<const F: fn(usize) -> bool>;

fn not_one(val: usize) -> bool { val != 1 }

fn new() {
    let a: Option<Checked<{not_one}>> = None;
}

produces some strange errors:

error[E0308]: mismatched types
  --> src/lib.rs:36:41
   |
36 |     let a: Option<Checked<{not_one}>> = None;
   |                                         ^^^^ expected `Scalar(AllocId(1).0x0) : fn(usize) -> bool`, found `Scalar(AllocId(1).0x0) : fn(usize) -> bool`
   |
   = note: expected type `std::option::Option<Checked<Scalar(AllocId(1).0x0) : fn(usize) -> bool>>`
              found type `std::option::Option<_>`

error[E0308]: mismatched types
  --> src/lib.rs:36:9
   |
36 |     let a: Option<Checked<{not_one}>> = None;
   |         ^ expected `Scalar(AllocId(1).0x0) : fn(usize) -> bool`, found `Scalar(AllocId(1).0x0) : fn(usize) -> bool`
   |
   = note: expected type `std::option::Option<Checked<Scalar(AllocId(1).0x0) : fn(usize) -> bool>>`
              found type `_`

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. labels Aug 6, 2019
Centril added a commit to Centril/rust that referenced this issue Oct 9, 2019
…varkor

Function pointers as const generic arguments

Makes function pointers as const generic arguments usable.

Fixes rust-lang#62395

r? @varkor
tmandry added a commit to tmandry/rust that referenced this issue Oct 11, 2019
…varkor

Function pointers as const generic arguments

Makes function pointers as const generic arguments usable.

Fixes rust-lang#62395

r? @varkor
tmandry added a commit to tmandry/rust that referenced this issue Oct 11, 2019
…varkor

Function pointers as const generic arguments

Makes function pointers as const generic arguments usable.

Fixes rust-lang#62395

r? @varkor
@bors bors closed this as completed in ea0d155 Oct 12, 2019
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 C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants