Skip to content

Commit 7bf569a

Browse files
borsgitbot
authored and
gitbot
committed
Auto merge of rust-lang#134286 - Urgau:unreach_pub-std, r=ibraheemdev
Enable `unreachable_pub` lint in core This PR enables the [`unreachable_pub`](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unreachable-pub) as warn in `core`, `rtstartup` and `panic_unwind`. The motivation is similar to the compiler [MCP: Enable deny(unreachable_pub) on `rustc_*` crates](rust-lang/compiler-team#773 (comment)) : > "Where is this thing used?" is a question I ask all the time when reading unfamiliar code. Because of this, I generally find it annoying when things are marked with a more permissive visibility than necessary. "This thing marked pub, which other crates is it used in? Oh, it's not used in any other crates." Another motivation is to help to lint by utilizing it in-tree and seeing it's limitation in more complex scenarios. The diff was mostly generated with `./x.py fix --stage 1 library/core/ -- --broken-code`, as well as manual edits for code in macros, generated code and other targets. r? libs
2 parents 5d8ff70 + 288931b commit 7bf569a

26 files changed

+145
-126
lines changed

core/src/arch.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#![doc = include_str!("../../stdarch/crates/core_arch/src/core_arch_docs.md")]
22

3-
#[allow(unused_imports)]
3+
#[allow(
4+
// some targets don't have anything to reexport, which
5+
// makes the `pub use` unused and unreachable, allow
6+
// both lints as to not have `#[cfg]`s
7+
//
8+
// cf. https://github.com/rust-lang/rust/pull/116033#issuecomment-1760085575
9+
unused_imports,
10+
unreachable_pub
11+
)]
412
#[stable(feature = "simd_arch", since = "1.27.0")]
513
pub use crate::core_arch::arch::*;
614

core/src/array/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ impl<T> Guard<'_, T> {
893893
///
894894
/// No more than N elements must be initialized.
895895
#[inline]
896-
pub unsafe fn push_unchecked(&mut self, item: T) {
896+
pub(crate) unsafe fn push_unchecked(&mut self, item: T) {
897897
// SAFETY: If `initialized` was correct before and the caller does not
898898
// invoke this method more than N times then writes will be in-bounds
899899
// and slots will not be initialized more than once.

core/src/escape.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -163,63 +163,63 @@ pub(crate) struct EscapeIterInner<const N: usize> {
163163
}
164164

165165
impl<const N: usize> EscapeIterInner<N> {
166-
pub const fn backslash(c: ascii::Char) -> Self {
166+
pub(crate) const fn backslash(c: ascii::Char) -> Self {
167167
let (data, range) = backslash(c);
168168
Self { data, alive: range }
169169
}
170170

171-
pub const fn ascii(c: u8) -> Self {
171+
pub(crate) const fn ascii(c: u8) -> Self {
172172
let (data, range) = escape_ascii(c);
173173
Self { data, alive: range }
174174
}
175175

176-
pub const fn unicode(c: char) -> Self {
176+
pub(crate) const fn unicode(c: char) -> Self {
177177
let (data, range) = escape_unicode(c);
178178
Self { data, alive: range }
179179
}
180180

181181
#[inline]
182-
pub const fn empty() -> Self {
182+
pub(crate) const fn empty() -> Self {
183183
Self { data: [ascii::Char::Null; N], alive: 0..0 }
184184
}
185185

186186
#[inline]
187-
pub fn as_ascii(&self) -> &[ascii::Char] {
187+
pub(crate) fn as_ascii(&self) -> &[ascii::Char] {
188188
// SAFETY: `self.alive` is guaranteed to be a valid range for indexing `self.data`.
189189
unsafe {
190190
self.data.get_unchecked(usize::from(self.alive.start)..usize::from(self.alive.end))
191191
}
192192
}
193193

194194
#[inline]
195-
pub fn as_str(&self) -> &str {
195+
pub(crate) fn as_str(&self) -> &str {
196196
self.as_ascii().as_str()
197197
}
198198

199199
#[inline]
200-
pub fn len(&self) -> usize {
200+
pub(crate) fn len(&self) -> usize {
201201
usize::from(self.alive.end - self.alive.start)
202202
}
203203

204-
pub fn next(&mut self) -> Option<u8> {
204+
pub(crate) fn next(&mut self) -> Option<u8> {
205205
let i = self.alive.next()?;
206206

207207
// SAFETY: `i` is guaranteed to be a valid index for `self.data`.
208208
unsafe { Some(self.data.get_unchecked(usize::from(i)).to_u8()) }
209209
}
210210

211-
pub fn next_back(&mut self) -> Option<u8> {
211+
pub(crate) fn next_back(&mut self) -> Option<u8> {
212212
let i = self.alive.next_back()?;
213213

214214
// SAFETY: `i` is guaranteed to be a valid index for `self.data`.
215215
unsafe { Some(self.data.get_unchecked(usize::from(i)).to_u8()) }
216216
}
217217

218-
pub fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
218+
pub(crate) fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
219219
self.alive.advance_by(n)
220220
}
221221

222-
pub fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
222+
pub(crate) fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
223223
self.alive.advance_back_by(n)
224224
}
225225
}

core/src/ffi/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -172,35 +172,35 @@ mod c_char_definition {
172172
target_arch = "xtensa",
173173
)
174174
))] {
175-
pub type c_char = u8;
175+
pub(super) type c_char = u8;
176176
} else {
177177
// On every other target, c_char is signed.
178-
pub type c_char = i8;
178+
pub(super) type c_char = i8;
179179
}
180180
}
181181
}
182182

183183
mod c_int_definition {
184184
cfg_if! {
185185
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
186-
pub type c_int = i16;
187-
pub type c_uint = u16;
186+
pub(super) type c_int = i16;
187+
pub(super) type c_uint = u16;
188188
} else {
189-
pub type c_int = i32;
190-
pub type c_uint = u32;
189+
pub(super) type c_int = i32;
190+
pub(super) type c_uint = u32;
191191
}
192192
}
193193
}
194194

195195
mod c_long_definition {
196196
cfg_if! {
197197
if #[cfg(all(target_pointer_width = "64", not(windows)))] {
198-
pub type c_long = i64;
199-
pub type c_ulong = u64;
198+
pub(super) type c_long = i64;
199+
pub(super) type c_ulong = u64;
200200
} else {
201201
// The minimal size of `long` in the C standard is 32 bits
202-
pub type c_long = i32;
203-
pub type c_ulong = u32;
202+
pub(super) type c_long = i32;
203+
pub(super) type c_ulong = u32;
204204
}
205205
}
206206
}

core/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
#![warn(multiple_supertrait_upcastable)]
102102
#![allow(internal_features)]
103103
#![deny(ffi_unwind_calls)]
104+
#![warn(unreachable_pub)]
104105
// Do not check link redundancy on bootstraping phase
105106
#![allow(rustdoc::redundant_explicit_links)]
106107
#![warn(rustdoc::unescaped_backticks)]
@@ -396,7 +397,8 @@ pub mod primitive;
396397
unused_imports,
397398
unsafe_op_in_unsafe_fn,
398399
ambiguous_glob_reexports,
399-
deprecated_in_future
400+
deprecated_in_future,
401+
unreachable_pub
400402
)]
401403
#[allow(rustdoc::bare_urls)]
402404
mod core_arch;

core/src/net/display_buffer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ use crate::mem::MaybeUninit;
22
use crate::{fmt, str};
33

44
/// Used for slow path in `Display` implementations when alignment is required.
5-
pub struct DisplayBuffer<const SIZE: usize> {
5+
pub(super) struct DisplayBuffer<const SIZE: usize> {
66
buf: [MaybeUninit<u8>; SIZE],
77
len: usize,
88
}
99

1010
impl<const SIZE: usize> DisplayBuffer<SIZE> {
1111
#[inline]
12-
pub const fn new() -> Self {
12+
pub(super) const fn new() -> Self {
1313
Self { buf: [MaybeUninit::uninit(); SIZE], len: 0 }
1414
}
1515

1616
#[inline]
17-
pub fn as_str(&self) -> &str {
17+
pub(super) fn as_str(&self) -> &str {
1818
// SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
1919
// which writes a valid UTF-8 string to `buf` and correctly sets `len`.
2020
unsafe {

core/src/num/dec2flt/decimal.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use crate::num::dec2flt::common::{ByteSlice, is_8digits};
1313

1414
#[derive(Clone)]
15-
pub struct Decimal {
15+
pub(super) struct Decimal {
1616
/// The number of significant digits in the decimal.
1717
pub num_digits: usize,
1818
/// The offset of the decimal point in the significant digits.
@@ -55,21 +55,21 @@ impl Decimal {
5555
///
5656
/// In Python:
5757
/// `-emin + p2 + math.floor((emin+ 1)*math.log(2, b)-math.log(1-2**(-p2), b))`
58-
pub const MAX_DIGITS: usize = 768;
58+
pub(super) const MAX_DIGITS: usize = 768;
5959
/// The max digits that can be exactly represented in a 64-bit integer.
60-
pub const MAX_DIGITS_WITHOUT_OVERFLOW: usize = 19;
61-
pub const DECIMAL_POINT_RANGE: i32 = 2047;
60+
pub(super) const MAX_DIGITS_WITHOUT_OVERFLOW: usize = 19;
61+
pub(super) const DECIMAL_POINT_RANGE: i32 = 2047;
6262

6363
/// Append a digit to the buffer.
64-
pub fn try_add_digit(&mut self, digit: u8) {
64+
pub(super) fn try_add_digit(&mut self, digit: u8) {
6565
if self.num_digits < Self::MAX_DIGITS {
6666
self.digits[self.num_digits] = digit;
6767
}
6868
self.num_digits += 1;
6969
}
7070

7171
/// Trim trailing zeros from the buffer.
72-
pub fn trim(&mut self) {
72+
pub(super) fn trim(&mut self) {
7373
// All of the following calls to `Decimal::trim` can't panic because:
7474
//
7575
// 1. `parse_decimal` sets `num_digits` to a max of `Decimal::MAX_DIGITS`.
@@ -83,7 +83,7 @@ impl Decimal {
8383
}
8484
}
8585

86-
pub fn round(&self) -> u64 {
86+
pub(super) fn round(&self) -> u64 {
8787
if self.num_digits == 0 || self.decimal_point < 0 {
8888
return 0;
8989
} else if self.decimal_point > 18 {
@@ -111,7 +111,7 @@ impl Decimal {
111111
}
112112

113113
/// Computes decimal * 2^shift.
114-
pub fn left_shift(&mut self, shift: usize) {
114+
pub(super) fn left_shift(&mut self, shift: usize) {
115115
if self.num_digits == 0 {
116116
return;
117117
}
@@ -152,7 +152,7 @@ impl Decimal {
152152
}
153153

154154
/// Computes decimal * 2^-shift.
155-
pub fn right_shift(&mut self, shift: usize) {
155+
pub(super) fn right_shift(&mut self, shift: usize) {
156156
let mut read_index = 0;
157157
let mut write_index = 0;
158158
let mut n = 0_u64;
@@ -202,7 +202,7 @@ impl Decimal {
202202
}
203203

204204
/// Parse a big integer representation of the float as a decimal.
205-
pub fn parse_decimal(mut s: &[u8]) -> Decimal {
205+
pub(super) fn parse_decimal(mut s: &[u8]) -> Decimal {
206206
let mut d = Decimal::default();
207207
let start = s;
208208

core/src/num/dec2flt/fpu.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Platform-specific, assembly instructions to avoid
22
//! intermediate rounding on architectures with FPUs.
33
4-
pub use fpu_precision::set_precision;
4+
pub(super) use fpu_precision::set_precision;
55

66
// On x86, the x87 FPU is used for float operations if the SSE/SSE2 extensions are not available.
77
// The x87 FPU operates with 80 bits of precision by default, which means that operations will
@@ -42,7 +42,7 @@ mod fpu_precision {
4242
/// - 0b10, double precision i.e., 64-bits
4343
/// - 0b11, double extended precision i.e., 80-bits (default state)
4444
/// The 0b01 value is reserved and should not be used.
45-
pub struct FPUControlWord(u16);
45+
pub(crate) struct FPUControlWord(u16);
4646

4747
fn set_cw(cw: u16) {
4848
// SAFETY: the `fldcw` instruction has been audited to be able to work correctly with
@@ -57,7 +57,7 @@ mod fpu_precision {
5757
}
5858

5959
/// Sets the precision field of the FPU to `T` and returns a `FPUControlWord`.
60-
pub fn set_precision<T>() -> FPUControlWord {
60+
pub(crate) fn set_precision<T>() -> FPUControlWord {
6161
let mut cw = 0_u16;
6262

6363
// Compute the value for the Precision Control field that is appropriate for `T`.
@@ -97,5 +97,5 @@ mod fpu_precision {
9797
// precision of the computation is determined on a per-operation basis.
9898
#[cfg(any(not(target_arch = "x86"), target_feature = "sse2"))]
9999
mod fpu_precision {
100-
pub fn set_precision<T>() {}
100+
pub(crate) fn set_precision<T>() {}
101101
}

core/src/num/dec2flt/table.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
//!
77
//! DO NOT MODIFY: Generated by `src/etc/dec2flt_table.py`
88
9-
pub const SMALLEST_POWER_OF_FIVE: i32 = -342;
10-
pub const LARGEST_POWER_OF_FIVE: i32 = 308;
11-
pub const N_POWERS_OF_FIVE: usize = (LARGEST_POWER_OF_FIVE - SMALLEST_POWER_OF_FIVE + 1) as usize;
9+
pub(super) const SMALLEST_POWER_OF_FIVE: i32 = -342;
10+
pub(super) const LARGEST_POWER_OF_FIVE: i32 = 308;
11+
pub(super) const N_POWERS_OF_FIVE: usize =
12+
(LARGEST_POWER_OF_FIVE - SMALLEST_POWER_OF_FIVE + 1) as usize;
1213

1314
// Use static to avoid long compile times: Rust compiler errors
1415
// can have the entire table compiled multiple times, and then
1516
// emit code multiple times, even if it's stripped out in
1617
// the final binary.
1718
#[rustfmt::skip]
18-
pub static POWER_OF_FIVE_128: [(u64, u64); N_POWERS_OF_FIVE] = [
19+
pub(super) static POWER_OF_FIVE_128: [(u64, u64); N_POWERS_OF_FIVE] = [
1920
(0xeef453d6923bd65a, 0x113faa2906a13b3f), // 5^-342
2021
(0x9558b4661b6565f8, 0x4ac7ca59a424c507), // 5^-341
2122
(0xbaaee17fa23ebf76, 0x5d79bcf00d2df649), // 5^-340

0 commit comments

Comments
 (0)