diff options
| author | bors <bors@rust-lang.org> | 2019-08-04 07:53:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-08-04 07:53:25 +0000 |
| commit | 5170a3f45af6a7a2a2e46a115daca8f31379b3a8 (patch) | |
| tree | 9fcd5857d51599703a80391889e98de7d5ea874f | |
| parent | 2c13edcd9d64f640261c488e04459733313f3843 (diff) | |
| parent | 9b5623f8bcc5f69fb3aac5a473231bee70234a51 (diff) | |
| download | rust-5170a3f45af6a7a2a2e46a115daca8f31379b3a8.tar.gz rust-5170a3f45af6a7a2a2e46a115daca8f31379b3a8.zip | |
Auto merge of #63233 - RalfJung:get_unchecked, r=Centril
clarify that unchecked indexing is UB even if the reference is never used
| -rw-r--r-- | src/libcore/slice/mod.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index b06511cad97..c8257d30488 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -292,10 +292,13 @@ impl<T> [T] { /// Returns a reference to an element or subslice, without doing bounds /// checking. /// - /// This is generally not recommended, use with caution! For a safe - /// alternative see [`get`]. + /// This is generally not recommended, use with caution! + /// Calling this method with an out-of-bounds index is *[undefined behavior]* + /// even if the resulting reference is not used. + /// For a safe alternative see [`get`]. /// /// [`get`]: #method.get + /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html /// /// # Examples /// @@ -317,10 +320,13 @@ impl<T> [T] { /// Returns a mutable reference to an element or subslice, without doing /// bounds checking. /// - /// This is generally not recommended, use with caution! For a safe - /// alternative see [`get_mut`]. + /// This is generally not recommended, use with caution! + /// Calling this method with an out-of-bounds index is *[undefined behavior]* + /// even if the resulting reference is not used. + /// For a safe alternative see [`get_mut`]. /// /// [`get_mut`]: #method.get_mut + /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html /// /// # Examples /// @@ -2629,11 +2635,17 @@ pub trait SliceIndex<T: ?Sized>: private_slice_index::Sealed { /// Returns a shared reference to the output at this location, without /// performing any bounds checking. + /// Calling this method with an out-of-bounds index is *[undefined behavior]* + /// even if the resulting reference is not used. + /// [undefined behavior]: ../../reference/behavior-considered-undefined.html #[unstable(feature = "slice_index_methods", issue = "0")] unsafe fn get_unchecked(self, slice: &T) -> &Self::Output; /// Returns a mutable reference to the output at this location, without /// performing any bounds checking. + /// Calling this method with an out-of-bounds index is *[undefined behavior]* + /// even if the resulting reference is not used. + /// [undefined behavior]: ../../reference/behavior-considered-undefined.html #[unstable(feature = "slice_index_methods", issue = "0")] unsafe fn get_unchecked_mut(self, slice: &mut T) -> &mut Self::Output; |
