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

Extern mod'd shared objects do not respect visibility of structs (linker errors) #11287

Closed
wilkie opened this issue Jan 3, 2014 · 2 comments
Closed

Comments

@wilkie
Copy link

wilkie commented Jan 3, 2014

The following library, innocent enough, contains an allowed visibility conundrum:

// foo.rs
// Compiled with rustc --lib foo.rs
#[link(name = "foo", vers = "1.0", package_id = "foo")];

struct Foo { // ** should be public, but isn't, by human error ** //
  a: i8
}

impl Foo {
  pub fn bar(&self) {
  }
}

The following driver:

// bug.rs
// Compiled by rustc bug.rs -L.
extern mod foo;

fn main() {
  let f = foo::Foo { a: 0 };
  f.bar();
}

The compiler will believe Foo is public, and attempt to link the function bar (visible to the compiler, but not to the linker) causing the following linking error:

error: linking with `cc` failed: exit code: 1
note: cc arguments: '-m64' '-L/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-o' 'bug' 'bug.o' '-Wl,--as-needed' '-L.' '-L/home/wilkie/rust-bug/.rust' '-L/home/wilkie/rust-bug' '-L/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-lstd-6425b930ca146ae9-0.9-pre' '-L/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-lrustuv-a13edc95d75df17-0.9-pre' '-L.' '-lfoo-167086a786d07119-1.0' '-lpthread' '-lrt' '-ldl' '-lm' '-lpthread' '-lstdc++' '-lmorestack' '-Wl,-rpath,$ORIGIN/../../../usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-Wl,-rpath,$ORIGIN/.' '-Wl,-rpath,/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-Wl,-rpath,/home/wilkie/rust-bug'
note: bug.o: In function `main::h7f81f851c09f62ah::v0.0':
bug.rc:(.text+0x3d): undefined reference to `Foo::bar::hbf8f903627fe5f220fad::v1.0'
collect2: error: ld returned 1 exit status

error: aborting due to previous error

If I readelf -Ws libfoo-167086a786d07119-1.0.so, that exact symbol does exist with the correct hash.

If I put 'pub' in front of the struct definition, it works as expected.

I'm compiling a new version from master and will report later, but right now my rust --version is:

rustc 0.9-pre (3cc86d8 2013-12-05 12:32:12 -0800)
host: x86_64-unknown-linux-gnu
@alexcrichton
Copy link
Member

I think you may just need to update your compiler, using today's version I get:

bar.rs:4:12: 4:20 error: struct `Foo` is private                                                                    
bar.rs:4     let f = foo::Foo { a: 0 };                                                                             
                     ^~~~~~~~                                                                                       
error: aborting due to previous error                                                                               

Which is what one would expect.

@wilkie
Copy link
Author

wilkie commented Jan 3, 2014

Yep. Sorry about that. My build finished and it now reports an appropriate error. Must have been fixed recently. Thanks!

matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Mar 21, 2024
[`cast_lossless`]: Suggest type alias instead of the aliased type

Fixes rust-lang#11285

Still an issue with the "from" side, i.e., `I8::from(1) as I64` shows as `i8 to I64`, but this should be ok. Not possible to reliably fix currently anyway.

changelog: [`cast_lossless`]: Suggest type alias instead of the aliased type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants