Skip to content

libc crate triggers warnings when compiled with default-features = false, features = ['rustc-dep-of-std'] #3789

Open
@felixwrt

Description

@felixwrt

Summary

  • Trigger: I wanted to make some changes to the libc used in rustc and therefore patched rustc to use libc from a local path. This made the compilation fail.
  • Root cause:
    • building the libc crate for x86_64-unknown-linux-gnu (and probably other targets) produces warnings when using default-features = false, features = ['rustc-dep-of-std'] (like for example the rust standard library does)
    • rustc usually pulls libc from crates.io and a heuristic in Cargo suppresses warnings from it in that case
    • when I changed the libc dependency from crates.io to a local folder, the warnings weren't suppressed anymore
    • rustc is compiled using -D warnings which makes the warnings in the libc crate fail the entire build

Steps to reproduce

Clone Rust and libc:

# clone rust
git clone https://github.com/rust-lang/rust.git --depth 1
# clone libc 0.2.155 (latest released version)
git clone --branch 0.2.155 https://github.com/rust-lang/libc.git

Add a path override for libc in rustc's Cargo workspace:

diff --git a/Cargo.toml b/Cargo.toml
index 178a5ab9..51b4664e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -122,6 +122,8 @@ codegen-units = 1
 # lto = true

 [patch.crates-io]
+libc = { path = "../libc" }
+
 # See comments in `library/rustc-std-workspace-core/README.md` for what's going on
 # here
 rustc-std-workspace-core = { path = 'library/rustc-std-workspace-core' }

Try building the compiler (stage 0 std is enough to trigger the problem):

./configure
./x.py build --stage 0 library/std

Compilation fails with the following output:

...
Building stage0 library artifacts {std} (x86_64-unknown-linux-gnu)
    Updating crates.io index
    Updating crates.io index
     Locking 1 package to latest compatible version
      Adding libc v0.2.155 (/home/me/pg/warnings_issue_tmp/libc)
  Downloaded compiler_builtins v0.1.114
  Downloaded object v0.36.2
  Downloaded 2 crates (516.4 KB) in 2.27s
   Compiling cc v1.0.99
   Compiling core v0.0.0 (/home/me/pg/warnings_issue_tmp/rust/library/core)
   Compiling libc v0.2.155 (/home/me/pg/warnings_issue_tmp/libc)
   Compiling memchr v2.5.0
   Compiling std v0.0.0 (/home/me/pg/warnings_issue_tmp/rust/library/std)
   Compiling compiler_builtins v0.1.114
   Compiling rustc-std-workspace-core v1.99.0 (/home/me/pg/warnings_issue_tmp/rust/library/rustc-std-workspace-core)
error: private item shadows public glob re-export
   --> /home/me/pg/warnings_issue_tmp/libc/src/lib.rs:38:13
    |
38  |         use core::option;
    |             ^^^^^^^^^^^^
    |
note: the name `option` in the type namespace is supposed to be publicly re-exported here
   --> /home/me/pg/warnings_issue_tmp/libc/src/lib.rs:131:17
    |
131 |         pub use unix::*;
    |                 ^^^^^^^
note: but the private item here shadows it
   --> /home/me/pg/warnings_issue_tmp/libc/src/lib.rs:38:13
    |
38  |         use core::option;
    |             ^^^^^^^^^^^^
    = note: `-D hidden-glob-reexports` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(hidden_glob_reexports)]`

error: struct `in6_rtmsg` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/mod.rs:167:16
    |
14  | /   s! {
15  | |       pub struct in_addr {
16  | |           pub s_addr: ::in_addr_t,
17  | |       }
...   |
167 | |       pub struct in6_rtmsg {
    | |                  ^^^^^^^^^
...   |
206 | |       }
207 | |   }
    | |___- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:64:1
    |
64  |   / macro_rules! s {
65  |   |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
66  |   |         s!(it: $(#[$attr])* pub $t $i { $($field)* });
    |   |         --------------------------------------------- in this macro invocation (#2)
67  |   |     )*);
...     |
85  |   |     );
86  |   | }
    |   | -
    |   | |
    |   |_in this expansion of `s!` (#1)
    |     in this expansion of `s!` (#2)
    |
    = note: `-D dead-code` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(dead_code)]`

error: struct `sockaddr_storage` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/mod.rs:228:16
    |
209 | / s_no_extra_traits! {
210 | |     #[cfg_attr(
211 | |         any(
212 | |             all(
...   |
228 | |     pub struct sockaddr_storage {
    | |                ^^^^^^^^^^^^^^^^
...   |
257 | |     }
258 | | }
    | |_- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:88:1
    |
88  | / macro_rules! s_no_extra_traits {
89  | |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
90  | |         s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
    | |         ------------------------------------------------------------- in this macro invocation (#2)
91  | |     )*);
...   |
120 | |     );
121 | | }
    | | -
    | | |
    | |_in this expansion of `s_no_extra_traits!` (#1)
    |   in this expansion of `s_no_extra_traits!` (#2)

error: struct `signalfd_siginfo` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/linux/mod.rs:109:16
    |
61  | /   s! {
62  | |       pub struct glob_t {
63  | |           pub gl_pathc: ::size_t,
64  | |           pub gl_pathv: *mut *mut c_char,
...   |
109 | |       pub struct signalfd_siginfo {
    | |                  ^^^^^^^^^^^^^^^^
...   |
744 | |       }
745 | |   }
    | |___- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:64:1
    |
64  |   / macro_rules! s {
65  |   |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
66  |   |         s!(it: $(#[$attr])* pub $t $i { $($field)* });
    |   |         --------------------------------------------- in this macro invocation (#2)
67  |   |     )*);
...     |
85  |   |     );
86  |   | }
    |   | -
    |   | |
    |   |_in this expansion of `s!` (#1)
    |     in this expansion of `s!` (#2)

error: struct `sockaddr_nl` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/linux/mod.rs:748:16
    |
747 | / s_no_extra_traits! {
748 | |     pub struct sockaddr_nl {
    | |                ^^^^^^^^^^^
749 | |         pub nl_family: ::sa_family_t,
750 | |         nl_pad: ::c_ushort,
...   |
891 | |     }
892 | | }
    | |_- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:88:1
    |
88  | / macro_rules! s_no_extra_traits {
89  | |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
90  | |         s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
    | |         ------------------------------------------------------------- in this macro invocation (#2)
91  | |     )*);
...   |
120 | |     );
121 | | }
    | | -
    | | |
    | |_in this expansion of `s_no_extra_traits!` (#1)
    |   in this expansion of `s_no_extra_traits!` (#2)

error: struct `iocb` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/linux/gnu/mod.rs:437:16
    |
18  | /   s! {
19  | |       pub struct statx {
20  | |           pub stx_mask: u32,
21  | |           pub stx_blksize: u32,
...   |
437 | |       pub struct iocb {
    | |                  ^^^^
...   |
456 | |       }
457 | |   }
    | |___- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:64:1
    |
64  |   / macro_rules! s {
65  |   |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
66  |   |         s!(it: $(#[$attr])* pub $t $i { $($field)* });
    |   |         --------------------------------------------- in this macro invocation (#2)
67  |   |     )*);
...     |
85  |   |     );
86  |   | }
    |   | -
    |   | |
    |   |_in this expansion of `s!` (#1)
    |     in this expansion of `s!` (#2)

error: struct `semid_ds` is never constructed
  --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/linux/gnu/b64/mod.rs:68:16
   |
29 | /   s! {
30 | |       pub struct sigset_t {
31 | |           #[cfg(target_pointer_width = "32")]
32 | |           __val: [u32; 32],
...  |
68 | |       pub struct semid_ds {
   | |                  ^^^^^^^^
...  |
93 | |       }
94 | |   }
   | |___- in this macro invocation (#1)
   |
  ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:64:1
   |
64 |   / macro_rules! s {
65 |   |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
66 |   |         s!(it: $(#[$attr])* pub $t $i { $($field)* });
   |   |         --------------------------------------------- in this macro invocation (#2)
67 |   |     )*);
...    |
85 |   |     );
86 |   | }
   |   | -
   |   | |
   |   |_in this expansion of `s!` (#1)
   |     in this expansion of `s!` (#2)

error: struct `user` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/linux/gnu/b64/x86_64/mod.rs:208:16
    |
12  | /   s! {
13  | |       pub struct sigaction {
14  | |           pub sa_sigaction: ::sighandler_t,
15  | |           pub sa_mask: ::sigset_t,
...   |
208 | |       pub struct user {
    | |                  ^^^^
...   |
269 | |       }
270 | |   }
    | |___- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:64:1
    |
64  |   / macro_rules! s {
65  |   |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
66  |   |         s!(it: $(#[$attr])* pub $t $i { $($field)* });
    |   |         --------------------------------------------- in this macro invocation (#2)
67  |   |     )*);
...     |
85  |   |     );
86  |   | }
    |   | -
    |   | |
    |   |_in this expansion of `s!` (#1)
    |     in this expansion of `s!` (#2)

error: struct `user_fpregs_struct` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/linux/gnu/b64/x86_64/mod.rs:273:16
    |
272 | / s_no_extra_traits! {
273 | |     pub struct user_fpregs_struct {
    | |                ^^^^^^^^^^^^^^^^^^
274 | |         pub cwd: ::c_ushort,
275 | |         pub swd: ::c_ushort,
...   |
299 | |     }
300 | | }
    | |_- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:88:1
    |
88  | / macro_rules! s_no_extra_traits {
89  | |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
90  | |         s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
    | |         ------------------------------------------------------------- in this macro invocation (#2)
91  | |     )*);
...   |
120 | |     );
121 | | }
    | | -
    | | |
    | |_in this expansion of `s_no_extra_traits!` (#1)
    |   in this expansion of `s_no_extra_traits!` (#2)

error: struct `max_align_t` is never constructed
   --> /home/me/pg/warnings_issue_tmp/libc/src/unix/linux_like/linux/gnu/b64/x86_64/align.rs:4:16
    |
1   | / s_no_extra_traits! {
2   | |     #[allow(missing_debug_implementations)]
3   | |     #[repr(align(16))]
4   | |     pub struct max_align_t {
    | |                ^^^^^^^^^^^
5   | |         priv_: [f64; 4]
6   | |     }
7   | | }
    | |_- in this macro invocation (#1)
    |
   ::: /home/me/pg/warnings_issue_tmp/libc/src/macros.rs:88:1
    |
88  | / macro_rules! s_no_extra_traits {
89  | |     ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
90  | |         s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
    | |         ------------------------------------------------------------- in this macro invocation (#2)
91  | |     )*);
...   |
120 | |     );
121 | | }
    | | -
    | |_|
    | |_in this expansion of `s_no_extra_traits!` (#1)
    |   in this expansion of `s_no_extra_traits!` (#2)

error: could not compile `libc` (lib) due to 10 previous errors
...

What should be fixed?

I think there's two parts to this:

  1. Fix the warnings that are currently present in libc when using it like rustc does. Note: there might be more warnings than what I've pasted above.
  2. Expand CI coverage to make sure that there are no warnings when building with the compiler-specific features.

Feel free to ask questions about this issue. Thanks in advance for taking a look at it and let me know if there's anything I can do to fix the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions