Skip to content

reachability/dead_code doesn't visit some type parameters #11115

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

Closed
emberian opened this issue Dec 22, 2013 · 4 comments
Closed

reachability/dead_code doesn't visit some type parameters #11115

emberian opened this issue Dec 22, 2013 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@emberian
Copy link
Member

#[crate_type="lib"];

pub struct Foo {
    x: Option<Bar>
}

impl Foo {
    pub fn new() -> Foo {
        Foo { x: Some(Bar) }
    }
}

struct Bar;

impl Bar {
    pub fn bar(&self) {
        println("onoez");
    }
}

warns:

foo.rs:16:4: 18:5 warning: code is never used: `bar`, #[warn(dead_code)] on by default
foo.rs:16     pub fn bar(&self) {
foo.rs:17         println("onoez");
foo.rs:18     }

But it could be used, since it's exported through Foo which is public.

When trying to use the method with:

extern mod foo;

fn main() {
    let f = foo::Foo::new();
    match f.x {
        Some(b) => b.bar(),
        None => println("oh yes!")
    }
}

there's a link failure:

[9:53:19]/tmp> rustc -L . bar.rs
error: linking with `cc` failed: exit code: 1
note: cc arguments: '-m64' '-L/usr/lib/rustc/x86_64-unknown-linux-gnu/lib' '-o' 'bar' 'bar.o' '-Wl,--as-needed' '-L.' '-L/home/cmr/.rustpkg' '-L/tmp/.rust' '-L/tmp' '-L/home/cmr/.rust' '-L/usr/lib/rustc/x86_64-unknown-linux-gnu/lib' '-lstd-04ff901e-0.9-pre' '-L/usr/lib/rustc/x86_64-unknown-linux-gnu/lib' '-lrustuv-7945354c-0.9-pre' '-L.' '-lfoo-d4ab21d1-0.0' '-lrt' '-ldl' '-lm' '-lpthread' '-lstdc++' '-lpthread' '-lmorestack' '-Wl,-rpath,$ORIGIN/../usr/lib/rustc/x86_64-unknown-linux-gnu/lib' '-Wl,-rpath,$ORIGIN/.' '-Wl,-rpath,/usr/lib/rustc/x86_64-unknown-linux-gnu/lib' '-Wl,-rpath,/tmp'
note: bar.o: In function `main::hde257f2ef387233e6af2bb9c9b95cdd6459006df08d7a15d23b81ac957cba2bcah::v0.0':
bar.rc:(.text+0x61): undefined reference to `Bar::bar::he61fdf95355f1b5d3bd22fb7908295d1daf3feeb6b1a6a3f97bd8d5fa0989316Q3a3::v0.0'
collect2: error: ld returned 1 exit status

error: aborting due to previous error

cc @ktt3ja @alexcrichton

@alexcrichton
Copy link
Member

This is vaguely along the lines of #10573 as well. There's a question as to whether we want to allow this at all, but it definitely seems bad that there's a link error today.

@pnkfelix
Copy link
Member

pnkfelix commented Jan 9, 2014

Just a bug, denominating.

@eddyb
Copy link
Member

eddyb commented Aug 4, 2014

Doesn't seem to have anything to do with type parameters, neither fields nor methods are taken into consideration (probably assuming that a field or a method can't expose a private type with public methods):

#![crate_type="lib"]

pub struct Foo {
    pub x: Bar
}

impl Foo {
    pub fn new() -> Foo {
        Foo { x: Bar}
    }
    pub fn get_x(&self) -> Bar {
        self.x
    }
}

struct Bar;

impl Bar {
    pub fn bar(&self) {
        println!("onoez");
    }
}
<anon>:4:12: 4:15 warning: private type in exported type signature, #[warn(visible_private_types)] on by default
<anon>:4     pub x: Bar
                    ^~~
<anon>:11:28: 11:31 warning: private type in exported type signature, #[warn(visible_private_types)] on by default
<anon>:11     pub fn get_x(&self) -> Bar {
                                     ^~~
<anon>:19:5: 21:6 warning: code is never used: `bar`, #[warn(dead_code)] on by default
<anon>:19     pub fn bar(&self) {
<anon>:20         println!("onoez");
<anon>:21     }

The link error is still there, using either the field or the method to get a Bar value.
AFAICT, the lint reports the results of a reachability analysis that is also used in determining the visibility of symbols.
objdump reports the presence of a Bar::bar function in the resulting libfoo.rlib/foo.o object file, whether or not it is called in the foo crate, however, it is always a local symbol.

@sanxiyn sanxiyn added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-linkage Area: linking into static, shared libraries and binaries labels Jan 25, 2015
@huonw
Copy link
Member

huonw commented Mar 1, 2015

Since this bug was filed, struct fields have switched from public by default to private by default, and, private types in public position have been outlawed, meaning (a) the external crate crate in @cmr's original example fails to compile before hitting linking errors (f.x is inaccessible) and (b) @eddyb's updated example also fails to compile with

<anon>:4:12: 4:15 error: private type in exported type signature
<anon>:4     pub x: Bar
                    ^~~
<anon>:11:28: 11:31 error: private type in exported type signature
<anon>:11     pub fn get_x(&self) -> Bar {
                                     ^~~

Closing.

@huonw huonw closed this as completed Mar 1, 2015
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 31, 2023
New lint [`filter_map_bool_then`]

Closes rust-lang#9098

changelog: New lint [`filter_map_bool_then`]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

6 participants