<p dir="auto">Compilation of:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="mod privmod {
    pub struct HiddenType;

    impl HiddenType {
        pub fn hidden_method(&amp;self) {

        }
    }
}

pub mod pubmod {
    use privmod;

    pub fn expose() -&gt; privmod::HiddenType {
        privmod::HiddenType
    }
}

pub use pubmod::expose;"><pre class="notranslate"><span class="pl-k">mod</span> privmod <span class="pl-kos">{</span>
    <span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">HiddenType</span><span class="pl-kos">;</span>

    <span class="pl-k">impl</span> <span class="pl-smi">HiddenType</span> <span class="pl-kos">{</span>
        <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">hidden_method</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-smi">self</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-k">pub</span> <span class="pl-k">mod</span> pubmod <span class="pl-kos">{</span>
    <span class="pl-k">use</span> privmod<span class="pl-kos">;</span>

    <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">expose</span><span class="pl-kos">(</span><span class="pl-kos">)</span> -&gt; privmod<span class="pl-kos">::</span><span class="pl-smi">HiddenType</span> <span class="pl-kos">{</span>
        privmod<span class="pl-kos">::</span><span class="pl-v">HiddenType</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-k">pub</span> <span class="pl-k">use</span> pubmod<span class="pl-kos">::</span>expose<span class="pl-kos">;</span></pre></div>
<p dir="auto">produces the warning:</p>
<blockquote>
<p dir="auto">warning: method is never used: <code class="notranslate">hidden_method</code>, #[warn(dead_code)] on by default</p>
</blockquote>
<p dir="auto">I think it's not correct, because <code class="notranslate">hidden_method</code> can be called from outside the crate by getting an instance of the type via <code class="notranslate">pubmod::expose</code>.</p>
<p dir="auto">I'd expect Rust to either stop with an error that a private type (private due to being in a private module) is exposed, or figure out that the method is exposed indirectly and assume it's used.</p>
<p dir="auto">This problem creates another when linking across crates. To reproduce the problem: take the example code above and put it in <code class="notranslate">src/lib.rs</code> of a crate called "library", and then create a crate "tool" with it as a dependency:</p>
<p dir="auto"><code class="notranslate">tool/Cargo.toml</code> +=</p>
<div class="highlight highlight-source-toml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[dependencies.library]
path = &quot;../library&quot;"><pre class="notranslate">[<span class="pl-en">dependencies</span>.<span class="pl-en">library</span>]
<span class="pl-smi">path</span> = <span class="pl-s"><span class="pl-pds">"</span>../library<span class="pl-pds">"</span></span></pre></div>
<p dir="auto">and <code class="notranslate">src/bin/tool.rs</code> as follows:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="extern crate library;

fn main() {
    library::expose().hidden_method();
}"><pre class="notranslate"><span class="pl-k">extern</span> <span class="pl-k">crate</span> library<span class="pl-kos">;</span>

<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    library<span class="pl-kos">::</span><span class="pl-en">expose</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">hidden_method</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><code class="notranslate">cargo run --bin tool</code> will compile, but won't link:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Undefined symbols for architecture x86_64:
  &quot;privmod::HiddenType::hidden_method::h4e1e7549e65f404ckaa&quot;, referenced from:
      main::hb631601ec0f76d56faa in tool.0.o"><pre class="notranslate"><code class="notranslate">Undefined symbols for architecture x86_64:
  "privmod::HiddenType::hidden_method::h4e1e7549e65f404ckaa", referenced from:
      main::hb631601ec0f76d56faa in tool.0.o
</code></pre></div>
<p dir="auto">I'd prefer this to either work, or fail at compilation stage, because linker errors are scary.</p>
<hr>
<p dir="auto">rustc 1.4.0-nightly (<a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/rust-lang/rust/commit/8f1b0aa32552f2e694aa8702ff2cd6d9a0e894f1/hovercard" href="https://github.com/rust-lang/rust/commit/8f1b0aa32552f2e694aa8702ff2cd6d9a0e894f1"><tt>8f1b0aa</tt></a> 2015-08-21); OS X 11.11</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// foo.rs
pub use foo::Bar;

pub fn test() -&gt; Bar&lt;i32&gt; {
    loop {}
}

mod foo {
    pub type Bar&lt;T&gt; = Result&lt;T, Baz&gt;;

    pub struct Baz;

    impl Baz {
        pub fn foo(&amp;self) {}
    }
}"><pre class="notranslate"><span class="pl-c">// foo.rs</span>
<span class="pl-k">pub</span> <span class="pl-k">use</span> foo<span class="pl-kos">::</span><span class="pl-v">Bar</span><span class="pl-kos">;</span>

<span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">Bar</span><span class="pl-kos">&lt;</span><span class="pl-smi">i32</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">loop</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-k">mod</span> foo <span class="pl-kos">{</span>
    <span class="pl-k">pub</span> <span class="pl-k">type</span> <span class="pl-smi">Bar</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span> = <span class="pl-smi">Result</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">,</span> <span class="pl-smi">Baz</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>

    <span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">Baz</span><span class="pl-kos">;</span>

    <span class="pl-k">impl</span> <span class="pl-smi">Baz</span> <span class="pl-kos">{</span>
        <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-smi">self</span><span class="pl-kos">)</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// bar.rs
extern crate foo;

fn main() {
    match foo::test() {
        Ok(..) =&gt; {}
        Err(e) =&gt; e.foo()
    }
}"><pre class="notranslate"><span class="pl-c">// bar.rs</span>
<span class="pl-k">extern</span> <span class="pl-k">crate</span> foo<span class="pl-kos">;</span>

<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">match</span> foo<span class="pl-kos">::</span><span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-v">Ok</span><span class="pl-kos">(</span>..<span class="pl-kos">)</span> =&gt; <span class="pl-kos">{</span><span class="pl-kos">}</span>
        <span class="pl-v">Err</span><span class="pl-kos">(</span>e<span class="pl-kos">)</span> =&gt; e<span class="pl-kos">.</span><span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ rustc --crate-type lib foo.rs      
foo.rs:13:9: 13:29 warning: code is never used: `foo`, #[warn(dead_code)] on by default
foo.rs:13         pub fn foo(&amp;self) {}
                  ^~~~~~~~~~~~~~~~~~~~
$ rustc bar.rs -L.    
error: linking with `cc` failed: exit code: 1
note: cc '-m64' '-L' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib' '-o' 'bar' 'bar.o' '-Wl,--whole-archive' '-lmorestack' '-Wl,--no-whole-archive' '-nodefaultlibs' '-Wl,--gc-sections' '-pie' '-Wl,--as-needed' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libnative-4e7c5e5c.rlib' '/home/alex/libfoo.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/librand-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/liballoc-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/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: bar.o: In function `main::hbeb79415393a4115faa':
bar.rs:(.text._ZN4main20hbeb79415393a4115faaE+0x45): undefined reference to `foo::Baz::foo::h3d69757c908833a5Yaa'
collect2: error: ld returned 1 exit status

error: aborting due to previous error
"><pre class="notranslate"><code class="notranslate">$ rustc --crate-type lib foo.rs      
foo.rs:13:9: 13:29 warning: code is never used: `foo`, #[warn(dead_code)] on by default
foo.rs:13         pub fn foo(&amp;self) {}
                  ^~~~~~~~~~~~~~~~~~~~
$ rustc bar.rs -L.    
error: linking with `cc` failed: exit code: 1
note: cc '-m64' '-L' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib' '-o' 'bar' 'bar.o' '-Wl,--whole-archive' '-lmorestack' '-Wl,--no-whole-archive' '-nodefaultlibs' '-Wl,--gc-sections' '-pie' '-Wl,--as-needed' '/home/alex/code/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libnative-4e7c5e5c.rlib' '/home/alex/libfoo.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/librand-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/liballoc-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/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: bar.o: In function `main::hbeb79415393a4115faa':
bar.rs:(.text._ZN4main20hbeb79415393a4115faaE+0x45): undefined reference to `foo::Baz::foo::h3d69757c908833a5Yaa'
collect2: error: ld returned 1 exit status

error: aborting due to previous error

</code></pre></div>
<p dir="auto">There are a number of problems with this, and they may likely all be inter-related.</p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> linker error</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> dead code which is not actually dead code</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> it should be an error that <code class="notranslate">Baz</code> is a publicly visible private type.</li>
</ul>
<p dir="auto">Nominating due to the publicly visible private type not generating an error.</p>