Skip to content

Commit 97cde9f

Browse files
committed
Auto merge of rust-lang#94447 - matthiaskrgr:rollup-d8rj2xv, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#89793 (Add `slice::{from_ptr_range, from_mut_ptr_range} `) - rust-lang#92642 (Update search location from a relative path to absolute) - rust-lang#93389 (regression for issue 90847) - rust-lang#93413 (Fix broken link from rustdoc docs to ayu theme) - rust-lang#94365 (Fix MinGW target detection in raw-dylib) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents edda7e9 + ea39f46 commit 97cde9f

File tree

8 files changed

+151
-3
lines changed

8 files changed

+151
-3
lines changed

compiler/rustc_codegen_llvm/src/back/archive.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
151151
output_path.with_extension("lib")
152152
};
153153

154-
let mingw_gnu_toolchain = self.config.sess.target.llvm_target.ends_with("pc-windows-gnu");
154+
let target = &self.config.sess.target;
155+
let mingw_gnu_toolchain =
156+
target.vendor == "pc" && target.os == "windows" && target.env == "gnu";
155157

156158
let import_name_and_ordinal_vector: Vec<(String, Option<u16>)> = dll_imports
157159
.iter()

library/core/src/slice/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub use raw::{from_raw_parts, from_raw_parts_mut};
7171
#[stable(feature = "from_ref", since = "1.28.0")]
7272
pub use raw::{from_mut, from_ref};
7373

74+
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
75+
pub use raw::{from_mut_ptr_range, from_ptr_range};
76+
7477
// This function is public only because there is no other way to unit test heapsort.
7578
#[unstable(feature = "sort_internals", reason = "internal to sort module", issue = "none")]
7679
pub use sort::heapsort;

library/core/src/slice/raw.rs

+111
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
33
use crate::array;
4+
use crate::ops::Range;
45
use crate::ptr;
56

67
/// Forms a slice from a pointer and a length.
@@ -177,3 +178,113 @@ pub const fn from_ref<T>(s: &T) -> &[T] {
177178
pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
178179
array::from_mut(s)
179180
}
181+
182+
/// Forms a slice from a pointer range.
183+
///
184+
/// This function is useful for interacting with foreign interfaces which
185+
/// use two pointers to refer to a range of elements in memory, as is
186+
/// common in C++.
187+
///
188+
/// # Safety
189+
///
190+
/// Behavior is undefined if any of the following conditions are violated:
191+
///
192+
/// * The `start` pointer of the range must be a [valid] and properly aligned pointer
193+
/// to the first element of a slice.
194+
///
195+
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
196+
/// the last element, such that the offset from the end to the start pointer is
197+
/// the length of the slice.
198+
///
199+
/// * The range must contain `N` consecutive properly initialized values of type `T`:
200+
///
201+
/// * The entire memory range of this slice must be contained within a single allocated object!
202+
/// Slices can never span across multiple allocated objects.
203+
///
204+
/// * The memory referenced by the returned slice must not be mutated for the duration
205+
/// of lifetime `'a`, except inside an `UnsafeCell`.
206+
///
207+
/// * The total length of the range must be no larger than `isize::MAX`.
208+
/// See the safety documentation of [`pointer::offset`].
209+
///
210+
/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
211+
///
212+
/// # Caveat
213+
///
214+
/// The lifetime for the returned slice is inferred from its usage. To
215+
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
216+
/// source lifetime is safe in the context, such as by providing a helper
217+
/// function taking the lifetime of a host value for the slice, or by explicit
218+
/// annotation.
219+
///
220+
/// # Examples
221+
///
222+
/// ```
223+
/// #![feature(slice_from_ptr_range)]
224+
///
225+
/// use core::slice;
226+
///
227+
/// let x = [1, 2, 3];
228+
/// let range = x.as_ptr_range();
229+
///
230+
/// unsafe {
231+
/// assert_eq!(slice::from_ptr_range(range), &x);
232+
/// }
233+
/// ```
234+
///
235+
/// [valid]: ptr#safety
236+
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
237+
pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
238+
// SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
239+
unsafe { from_raw_parts(range.start, range.end.offset_from(range.start) as usize) }
240+
}
241+
242+
/// Performs the same functionality as [`from_ptr_range`], except that a
243+
/// mutable slice is returned.
244+
///
245+
/// # Safety
246+
///
247+
/// Behavior is undefined if any of the following conditions are violated:
248+
///
249+
/// * The `start` pointer of the range must be a [valid] and properly aligned pointer
250+
/// to the first element of a slice.
251+
///
252+
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
253+
/// the last element, such that the offset from the end to the start pointer is
254+
/// the length of the slice.
255+
///
256+
/// * The range must contain `N` consecutive properly initialized values of type `T`:
257+
///
258+
/// * The entire memory range of this slice must be contained within a single allocated object!
259+
/// Slices can never span across multiple allocated objects.
260+
///
261+
/// * The memory referenced by the returned slice must not be accessed through any other pointer
262+
/// (not derived from the return value) for the duration of lifetime `'a`.
263+
/// Both read and write accesses are forbidden.
264+
///
265+
/// * The total length of the range must be no larger than `isize::MAX`.
266+
/// See the safety documentation of [`pointer::offset`].
267+
///
268+
/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.
269+
///
270+
/// # Examples
271+
///
272+
/// ```
273+
/// #![feature(slice_from_ptr_range)]
274+
///
275+
/// use core::slice;
276+
///
277+
/// let mut x = [1, 2, 3];
278+
/// let range = x.as_mut_ptr_range();
279+
///
280+
/// unsafe {
281+
/// assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
282+
/// }
283+
/// ```
284+
///
285+
/// [valid]: ptr#safety
286+
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
287+
pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
288+
// SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
289+
unsafe { from_raw_parts_mut(range.start, range.end.offset_from(range.start) as usize) }
290+
}

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#![feature(pin_macro)]
4747
#![feature(sort_internals)]
4848
#![feature(slice_take)]
49+
#![feature(slice_from_ptr_range)]
4950
#![feature(maybe_uninit_uninit_array)]
5051
#![feature(maybe_uninit_array_assume_init)]
5152
#![feature(maybe_uninit_write_slice)]

library/core/tests/slice.rs

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::cell::Cell;
22
use core::cmp::Ordering;
33
use core::mem::MaybeUninit;
44
use core::result::Result::{Err, Ok};
5+
use core::slice;
56

67
#[test]
78
fn test_position() {
@@ -2480,3 +2481,24 @@ take_tests! {
24802481
(take_mut_oob_max_range_to_inclusive, (..=usize::MAX), None, empty_max_mut!()),
24812482
(take_mut_in_bounds_max_range_from, (usize::MAX..), Some(&mut [] as _), empty_max_mut!()),
24822483
}
2484+
2485+
#[test]
2486+
fn test_slice_from_ptr_range() {
2487+
let arr = ["foo".to_owned(), "bar".to_owned()];
2488+
let range = arr.as_ptr_range();
2489+
unsafe {
2490+
assert_eq!(slice::from_ptr_range(range), &arr);
2491+
}
2492+
2493+
let mut arr = [1, 2, 3];
2494+
let range = arr.as_mut_ptr_range();
2495+
unsafe {
2496+
assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
2497+
}
2498+
2499+
let arr: [Vec<String>; 0] = [];
2500+
let range = arr.as_ptr_range();
2501+
unsafe {
2502+
assert_eq!(slice::from_ptr_range(range), &arr);
2503+
}
2504+
}

src/doc/not_found.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function on_submit(event) {
8484
if (form['from'].value === 'duckduckgo') {
8585
document.location.href = form.action + '?q=' + encodeURIComponent(q + ' site:doc.rust-lang.org');
8686
} else if (form['from'].value === 'library') {
87-
document.location.href = 'std/index.html?search=' + encodeURIComponent(q);
87+
document.location.href = '/std/index.html?search=' + encodeURIComponent(q);
8888
}
8989
}
9090

src/doc/rustdoc/src/what-to-include.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ rustdoc --theme awesome.css src/lib.rs
118118

119119
Here is an example of a new theme, [Ayu].
120120

121-
[Ayu]: https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/static/themes/ayu.css
121+
[Ayu]: https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/static/css/themes/ayu.css
122122
[API Guidelines]: https://rust-lang.github.io/api-guidelines/documentation.html#rustdoc-does-not-show-unhelpful-implementation-details-c-hidden
123123
[Documentation tests]: documentation-tests.md
124124
[on this blog]: https://blog.guillaume-gomez.fr/articles/2016-09-16+Generating+doc+with+rustdoc+and+a+custom+theme
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
3+
#![allow(incomplete_features)]
4+
#![feature(generic_const_exprs)]
5+
#![feature(adt_const_params)]
6+
7+
struct Foo<const A: [(); 0 + 0]> where [(); 0 + 0]: Sized;
8+
9+
fn main() {}

0 commit comments

Comments
 (0)