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

#[linkage] does not propagate through generic functions #18804

Closed
alexcrichton opened this issue Nov 9, 2014 · 3 comments · Fixed by #52635
Closed

#[linkage] does not propagate through generic functions #18804

alexcrichton opened this issue Nov 9, 2014 · 3 comments · Fixed by #52635
Labels
A-attributes Area: #[attributes(..)] A-codegen Area: Code generation C-bug Category: This is a bug. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alexcrichton
Copy link
Member

For example:

// lib.rs
#![feature(linkage)]

pub fn foo<T>() -> *const () {
    extern {
        #[linkage = "extern_weak"]
        static FOO: *const ();
    }
    FOO
}
// main.rs
extern crate lib;

fn main() {
    lib::foo::<int>();
}
$ rustc lib.rs --crate-type rlib
$ rustc main.rs -L .
error: linking with `cc` failed: exit code: 1
note: cc '-Wl,--as-needed' '-m64' '-L' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib' '-o' 'main' 'main.o' '-Wl,--whole-archive' '-lmorestack' '-Wl,--no-whole-archive' '-fno-lto' '-Wl,--gc-sections' '-pie' '-nodefaultlibs' '/home/alex/liblib.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libnative-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libsync-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustrt-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunicode-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-4e7c5e5c.rlib' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-4e7c5e5c.rlib' '-L' '.' '-L' '/home/alex/.rust' '-L' '/home/alex' '-Wl,--whole-archive' '-Wl,-Bstatic' '-Wl,--no-whole-archive' '-Wl,-Bdynamic' '-ldl' '-lpthread' '-lgcc_s' '-lpthread' '-lc' '-lm' '-lcompiler-rt'
note: main.o:main.0.rs:function foo::h6978205699166201756: error: undefined reference to 'FOO'
collect2: error: ld returned 1 exit status

error: aborting due to previous error

This can be a little more clearly seen with the IR generated for main:

; ModuleID = 'main.0.rs'
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@FOO = external global {}*

...

Note the lack of extern_weak anywhere on FOO.

@alexcrichton alexcrichton added the A-codegen Area: Code generation label Nov 9, 2014
@steveklabnik
Copy link
Member

Triage: this is still true today, though int needs to be updated to i32, and there's now a warning about () not being a valid foreign type, which is irrelevant to this particular issue.

@brson
Copy link
Contributor

brson commented Jun 1, 2017

@Mark-Simulacrum claims it repros still.

@brson brson added I-wrong P-medium Medium priority labels Jun 1, 2017
@brson brson added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 1, 2017
@nagisa nagisa added the A-attributes Area: #[attributes(..)] label Jun 1, 2017
@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. I-wrong labels Jul 22, 2017
@yodaldevoid
Copy link
Contributor

Updated test code:

// lib.rs
#![feature(linkage)]

pub fn foo<T>() -> *const() {
    extern {
        #[linkage = "extern_weak"]
        static FOO: *const();
    }
    unsafe { FOO }
}
// main.rs
extern crate lib;

fn main() {
    lib::foo::<i32>();
}

Still fails to compile with the same errors mentioned in the top post along with the irrelevant invalid foreign type warning mentioned by @steveklabnik.

yodaldevoid added a commit to yodaldevoid/rust that referenced this issue Jul 25, 2018
Signed-off-by: Gabriel Smith <ga29smith@gmail.com>
yodaldevoid added a commit to yodaldevoid/rust that referenced this issue Jul 25, 2018
Fixes issue rust-lang#18804

Signed-off-by: Gabriel Smith <ga29smith@gmail.com>
yodaldevoid added a commit to yodaldevoid/rust that referenced this issue Jul 25, 2018
…smjs

The Emscripten compiler does not support weak symbols at the moment.

Signed-off-by: Gabriel Smith <ga29smith@gmail.com>
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jul 26, 2018
Fix #[linkage] propagation though generic functions

Fixes rust-lang#18804

In the non-local branch of `get_static` (where the fix was implemented) `span_fatal` had to be replaced with `bug!` as we have no span in that case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: #[attributes(..)] A-codegen Area: Code generation C-bug Category: This is a bug. P-medium Medium priority 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.

6 participants