Skip to content

Commit 5a5f3a9

Browse files
committed
Auto merge of #81853 - GuillaumeGomez:rollup-xzh1z4v, r=GuillaumeGomez
Rollup of 5 pull requests Successful merges: - #81526 (btree: use Option's unwrap_unchecked()) - #81742 (Add a note about the correctness and the effect on unsafe code to the `ExactSizeIterator` docs) - #81830 (Add long error explanation for E0542) - #81835 (Improve long explanation for E0546) - #81843 (Add regression test for #29821) Failed merges: - #81836 (Add long explanation for E0547) r? `@ghost` `@rustbot` modify labels: rollup
2 parents ae00b62 + 9a82417 commit 5a5f3a9

File tree

11 files changed

+96
-35
lines changed

11 files changed

+96
-35
lines changed

compiler/rustc_error_codes/src/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ E0537: include_str!("./error_codes/E0537.md"),
285285
E0538: include_str!("./error_codes/E0538.md"),
286286
E0539: include_str!("./error_codes/E0539.md"),
287287
E0541: include_str!("./error_codes/E0541.md"),
288+
E0542: include_str!("./error_codes/E0542.md"),
288289
E0546: include_str!("./error_codes/E0546.md"),
289290
E0550: include_str!("./error_codes/E0550.md"),
290291
E0551: include_str!("./error_codes/E0551.md"),
@@ -602,7 +603,6 @@ E0781: include_str!("./error_codes/E0781.md"),
602603
E0523,
603604
// E0526, // shuffle indices are not constant
604605
// E0540, // multiple rustc_deprecated attributes
605-
E0542, // missing 'since'
606606
E0543, // missing 'reason'
607607
E0544, // multiple stability levels
608608
E0545, // incorrect 'issue'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
The `since` value is missing in a stability attribute.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0542
6+
#![feature(staged_api)]
7+
#![stable(since = "1.0.0", feature = "test")]
8+
9+
#[stable(feature = "_stable_fn")] // invalid
10+
fn _stable_fn() {}
11+
12+
#[rustc_const_stable(feature = "_stable_const_fn")] // invalid
13+
fn _stable_const_fn() {}
14+
15+
#[stable(feature = "_deprecated_fn", since = "0.1.0")]
16+
#[rustc_deprecated(
17+
reason = "explanation for deprecation"
18+
)] // invalid
19+
fn _deprecated_fn() {}
20+
```
21+
22+
To fix the issue you need to provide the `since` field.
23+
24+
```
25+
#![feature(staged_api)]
26+
#![stable(since = "1.0.0", feature = "test")]
27+
28+
#[stable(feature = "_stable_fn", since = "1.0.0")] // ok!
29+
fn _stable_fn() {}
30+
31+
#[rustc_const_stable(feature = "_stable_const_fn", since = "1.0.0")] // ok!
32+
fn _stable_const_fn() {}
33+
34+
#[stable(feature = "_deprecated_fn", since = "0.1.0")]
35+
#[rustc_deprecated(
36+
since = "1.0.0",
37+
reason = "explanation for deprecation"
38+
)] // ok!
39+
fn _deprecated_fn() {}
40+
```
41+
42+
See the [How Rust is Made and “Nightly Rust”][how-rust-made-nightly] appendix
43+
of the Book and the [Stability attributes][stability-attributes] section of the
44+
Rustc Dev Guide for more details.
45+
46+
[how-rust-made-nightly]: https://doc.rust-lang.org/book/appendix-07-nightly-rust.html
47+
[stability-attributes]: https://rustc-dev-guide.rust-lang.org/stability.html

compiler/rustc_error_codes/src/error_codes/E0546.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
A feature name is missing.
1+
The `feature` value is missing in a stability attribute.
22

33
Erroneous code example:
44

@@ -13,7 +13,7 @@ fn unstable_fn() {}
1313
fn stable_fn() {}
1414
```
1515

16-
To fix the issue you need to provide a feature name.
16+
To fix the issue you need to provide the `feature` field.
1717

1818
```
1919
#![feature(staged_api)]
@@ -25,3 +25,10 @@ fn unstable_fn() {}
2525
#[stable(feature = "stable_fn", since = "1.0.0")] // ok!
2626
fn stable_fn() {}
2727
```
28+
29+
See the [How Rust is Made and “Nightly Rust”][how-rust-made-nightly] appendix
30+
of the Book and the [Stability attributes][stability-attributes] section of the
31+
Rustc Dev Guide for more details.
32+
33+
[how-rust-made-nightly]: https://doc.rust-lang.org/book/appendix-07-nightly-rust.html
34+
[stability-attributes]: https://rustc-dev-guide.rust-lang.org/stability.html

library/alloc/src/collections/btree/map.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use core::ptr;
1111
use super::borrow::DormantMutRef;
1212
use super::node::{self, marker, ForceResult::*, Handle, NodeRef, Root};
1313
use super::search::SearchResult::*;
14-
use super::unwrap_unchecked;
1514

1615
mod entry;
1716
pub use entry::{Entry, OccupiedEntry, VacantEntry};
@@ -1433,7 +1432,7 @@ impl<K, V> Drop for IntoIter<K, V> {
14331432

14341433
unsafe {
14351434
let mut node =
1436-
unwrap_unchecked(ptr::read(&self.0.front)).into_node().forget_type();
1435+
ptr::read(&self.0.front).unwrap_unchecked().into_node().forget_type();
14371436
while let Some(parent) = node.deallocate_and_ascend() {
14381437
node = parent.into_node().forget_type();
14391438
}
@@ -1758,7 +1757,7 @@ impl<'a, K, V> Range<'a, K, V> {
17581757
}
17591758

17601759
unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
1761-
unsafe { unwrap_unchecked(self.front.as_mut()).next_unchecked() }
1760+
unsafe { self.front.as_mut().unwrap_unchecked().next_unchecked() }
17621761
}
17631762
}
17641763

@@ -1847,7 +1846,7 @@ impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
18471846

18481847
impl<'a, K, V> Range<'a, K, V> {
18491848
unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
1850-
unsafe { unwrap_unchecked(self.back.as_mut()).next_back_unchecked() }
1849+
unsafe { self.back.as_mut().unwrap_unchecked().next_back_unchecked() }
18511850
}
18521851
}
18531852

@@ -1893,7 +1892,7 @@ impl<'a, K, V> RangeMut<'a, K, V> {
18931892
}
18941893

18951894
unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
1896-
unsafe { unwrap_unchecked(self.front.as_mut()).next_unchecked() }
1895+
unsafe { self.front.as_mut().unwrap_unchecked().next_unchecked() }
18971896
}
18981897

18991898
/// Returns an iterator of references over the remaining items.
@@ -1923,7 +1922,7 @@ impl<K, V> FusedIterator for RangeMut<'_, K, V> {}
19231922

19241923
impl<'a, K, V> RangeMut<'a, K, V> {
19251924
unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
1926-
unsafe { unwrap_unchecked(self.back.as_mut()).next_back_unchecked() }
1925+
unsafe { self.back.as_mut().unwrap_unchecked().next_back_unchecked() }
19271926
}
19281927
}
19291928

library/alloc/src/collections/btree/mod.rs

-16
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,6 @@ trait Recover<Q: ?Sized> {
1919
fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
2020
}
2121

22-
/// Same purpose as `Option::unwrap` but doesn't always guarantee a panic
23-
/// if the option contains no value.
24-
/// SAFETY: the caller must ensure that the option contains a value.
25-
#[inline(always)]
26-
pub unsafe fn unwrap_unchecked<T>(val: Option<T>) -> T {
27-
val.unwrap_or_else(|| {
28-
if cfg!(debug_assertions) {
29-
panic!("'unchecked' unwrap on None in BTreeMap");
30-
} else {
31-
unsafe {
32-
core::intrinsics::unreachable();
33-
}
34-
}
35-
})
36-
}
37-
3822
#[cfg(test)]
3923
/// XorShiftRng
4024
struct DeterministicRng {

library/alloc/src/collections/btree/navigate.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::ptr;
66

77
use super::node::{marker, ForceResult::*, Handle, NodeRef};
88
use super::search::SearchResult;
9-
use super::unwrap_unchecked;
109

1110
/// Finds the leaf edges delimiting a specified range in or underneath a node.
1211
///
@@ -310,7 +309,7 @@ macro_rules! def_next_kv_uncheched_dealloc {
310309
Err(last_edge) => {
311310
unsafe {
312311
let parent_edge = last_edge.into_node().deallocate_and_ascend();
313-
unwrap_unchecked(parent_edge).forget_node_type()
312+
parent_edge.unwrap_unchecked().forget_node_type()
314313
}
315314
}
316315
}
@@ -331,7 +330,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Ed
331330
pub unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
332331
super::mem::replace(self, |leaf_edge| {
333332
let kv = leaf_edge.next_kv();
334-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
333+
let kv = unsafe { kv.ok().unwrap_unchecked() };
335334
(kv.next_leaf_edge(), kv.into_kv())
336335
})
337336
}
@@ -344,7 +343,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Ed
344343
pub unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
345344
super::mem::replace(self, |leaf_edge| {
346345
let kv = leaf_edge.next_back_kv();
347-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
346+
let kv = unsafe { kv.ok().unwrap_unchecked() };
348347
(kv.next_back_leaf_edge(), kv.into_kv())
349348
})
350349
}
@@ -359,7 +358,7 @@ impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::E
359358
pub unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
360359
let kv = super::mem::replace(self, |leaf_edge| {
361360
let kv = leaf_edge.next_kv();
362-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
361+
let kv = unsafe { kv.ok().unwrap_unchecked() };
363362
(unsafe { ptr::read(&kv) }.next_leaf_edge(), kv)
364363
});
365364
// Doing this last is faster, according to benchmarks.
@@ -374,7 +373,7 @@ impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::E
374373
pub unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
375374
let kv = super::mem::replace(self, |leaf_edge| {
376375
let kv = leaf_edge.next_back_kv();
377-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
376+
let kv = unsafe { kv.ok().unwrap_unchecked() };
378377
(unsafe { ptr::read(&kv) }.next_back_leaf_edge(), kv)
379378
});
380379
// Doing this last is faster, according to benchmarks.

library/alloc/src/collections/btree/remove.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::map::MIN_LEN;
22
use super::node::{marker, ForceResult::*, Handle, LeftOrRight::*, NodeRef};
3-
use super::unwrap_unchecked;
43

54
impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::KV> {
65
/// Removes a key-value pair from the tree, and returns that pair, as well as
@@ -77,12 +76,12 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
7776
// the element we were asked to remove. Prefer the left adjacent KV,
7877
// for the reasons listed in `choose_parent_kv`.
7978
let left_leaf_kv = self.left_edge().descend().last_leaf_edge().left_kv();
80-
let left_leaf_kv = unsafe { unwrap_unchecked(left_leaf_kv.ok()) };
79+
let left_leaf_kv = unsafe { left_leaf_kv.ok().unwrap_unchecked() };
8180
let (left_kv, left_hole) = left_leaf_kv.remove_leaf_kv(handle_emptied_internal_root);
8281

8382
// The internal node may have been stolen from or merged. Go back right
8483
// to find where the original KV ended up.
85-
let mut internal = unsafe { unwrap_unchecked(left_hole.next_kv().ok()) };
84+
let mut internal = unsafe { left_hole.next_kv().ok().unwrap_unchecked() };
8685
let old_kv = internal.replace_kv(left_kv.0, left_kv.1);
8786
let pos = internal.next_leaf_edge();
8887
(old_kv, pos)

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
#![feature(nll)]
112112
#![feature(nonnull_slice_from_raw_parts)]
113113
#![feature(auto_traits)]
114+
#![feature(option_result_unwrap_unchecked)]
114115
#![feature(or_patterns)]
115116
#![feature(pattern)]
116117
#![feature(ptr_internals)]

library/core/src/iter/traits/exact_size.rs

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
/// implement it. However, you may be able to provide a more performant
1414
/// implementation than the default, so overriding it in this case makes sense.
1515
///
16+
/// Note that this trait is a safe trait and as such does *not* and *cannot*
17+
/// guarantee that the returned length is correct. This means that `unsafe`
18+
/// code **must not** rely on the correctness of [`Iterator::size_hint`]. The
19+
/// unstable and unsafe [`TrustedLen`](super::marker::TrustedLen) trait gives
20+
/// this additional guarantee.
21+
///
1622
/// [`len`]: ExactSizeIterator::len
1723
///
1824
/// # Examples

src/test/ui/issues/issue-29821.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// build-pass
2+
3+
pub trait Foo {
4+
type FooAssoc;
5+
}
6+
7+
pub struct Bar<F: Foo> {
8+
id: F::FooAssoc
9+
}
10+
11+
pub struct Baz;
12+
13+
impl Foo for Baz {
14+
type FooAssoc = usize;
15+
}
16+
17+
static mut MY_FOO: Bar<Baz> = Bar { id: 0 };
18+
19+
fn main() {}

src/test/ui/stability-attribute/stability-attribute-sanity.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,5 @@ LL | #[rustc_deprecated(since = "a", reason = "text")]
116116

117117
error: aborting due to 19 previous errors
118118

119-
Some errors have detailed explanations: E0539, E0541, E0546, E0550.
119+
Some errors have detailed explanations: E0539, E0541, E0542, E0546, E0550.
120120
For more information about an error, try `rustc --explain E0539`.

0 commit comments

Comments
 (0)