arjenpdevries / capitalizer

A demonstration on a potentially misleading warning in Rust 1.33 stable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Capitalizer (Double Mount Bug Demo)

This repo's sole purpose is to demonstrate a small issue with Rust's compiler warnings.

The warning we get

If you clone down this repo and run cargo build or (cargo clippy) -- using rustc 1.33.0 stable -- , you should get this warning:

Compiling capitalizer v0.1.0 (/home/sschlinkert/code/capitalizer)
warning: function is never used: `downcase_this`
--> src/downcase.rs:1:1
|
1 | pub fn downcase_this(s: String) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(dead_code)] on by default

 Finished dev [unoptimized + debuginfo] target(s) in 0.37s

What's strange about this is that that function, downcase_this, is used, both in src/lib.rs and in src/main.rs.

A guess at why we get this warning

The downcase module is "mounted" in both main.rs and in lib.rs. Strangely, only one version on downcase is used in both main.rs and lib.rs, and the other version is unused.

Calling mod downcase and use capitalizer::downcase::downcase_this in main.rs, while calling use downcase::downcase_this in lib.rs seems to cause the issue.

As Graydon explained to me when I showed him a different project experiencing a similar issue:

What's going on here is cargo is seeing lib.rs and main.rs and compiling two crates (one library, one executable that uses the library) but you're also mounting entries.rs as a sub module of main.rs. One you don't use directly.

It's only giving you the warning when compiling the second (executable == main.rs) crate. It just does them both back to back and you get the warnings from both.

Thus I'm, at least informally, going to refer to this as "double mounting."

Ways to avoid this warning

Admittedly there are some easy ways to not get the warning, many of which developers may use in the first place. Here are a few easy ones I've found.

  1. If we have use crate::downcase::downcase_this in both main.rs and lib.rs, we don't get the warning.

  2. If we have use downcase::downcase_this in both main.rs and lib.rs, we don't get the warning.

  3. If you remove mod downcase from main.rs, the warning also goes away. So:

// main.rs
use capitalizer::downcase::downcase_this;
// lib.rs
pub mod downcase;
use crate::downcase::downcase_this;

Interestingly, doing the equivalent of solution #1 or #2 in my larger project did not work as well as solution #3.

A guess at what the warning should say

Ideally the compiler would give a more accurate warning here, explaining to the developer they've "double mounted" a module or function from a module.

However I'm not sure if it's a bad thing to "double mount". If it's not a bad thing, maybe no warning should be given at all.

Conversely, there may be real issues with double-mounting that I'm unable to understand.

More about my system

> rustc --version
rustc 1.33.0 (2aa4c46cf 2019-02-28)

> cargo --version
cargo 1.33.0 (f099fe94b 2019-02-12)

> uname -a
Linux sschlinkert 4.18.0-18-generic #19~18.04.1-Ubuntu SMP Fri Apr 5 10:22:13 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

About

A demonstration on a potentially misleading warning in Rust 1.33 stable


Languages

Language:Rust 100.0%