Commit Graph

10315 Commits

Author SHA1 Message Date
Dylan DPC
d6ea99d2ed
Rollup merge of #103702 - WaffleLapkin:lift-sized-bounds-from-pointer-methods-where-applicable, r=m-ou-se
Lift `T: Sized` bounds from some `strict_provenance` pointer methods

This PR removes requirement for `T` (pointee type) to be `Sized` to call `pointer::{addr, expose_addr, with_addr, map_addr}`. These functions don't use `T`'s size, so there is no reason for them to require this. Updated public API:

cc ``@Gankra,`` #95228
r? libs-api
2023-01-18 15:55:36 +05:30
Matthias Krüger
788671c1c6
Rollup merge of #106997 - Sp00ph:introselect, r=scottmcm
Add heapsort fallback in `select_nth_unstable`

Addresses #102451 and #106933.

`slice::select_nth_unstable` uses a quick select implementation based on the same pattern defeating quicksort algorithm that `slice::sort_unstable` uses. `slice::sort_unstable` uses a recursion limit and falls back to heapsort if there were too many bad pivot choices, to ensure O(n log n) worst case running time (known as introsort). However, `slice::select_nth_unstable` does not have such a fallback strategy, which leads to it having a worst case running time of O(n²) instead. #102451 links to a playground which generates pathological inputs that show this quadratic behavior. On my machine, a randomly generated slice of length `1 << 19` takes ~200µs to calculate its median, whereas a pathological input of the same length takes over 2.5s. This PR adds an iteration limit to `select_nth_unstable`, falling back to heapsort, which ensures an O(n log n) worst case running time (introselect). With this change, there was no noticable slowdown for the random input, but the same pathological input now takes only ~1.2ms. In the future it might be worth implementing something like Median of Medians or Fast Deterministic Selection instead, which guarantee O(n) running time for all possible inputs. I've left this as a `FIXME` for now and only implemented the heapsort fallback to minimize the needed code changes.

I still think we should clarify in the `select_nth_unstable` docs that the worst case running time isn't currently O(n) (the original reason that #102451 was opened), but I think it's a lot better to be able to guarantee O(n log n) instead of O(n²) for the worst case.
2023-01-18 06:59:22 +01:00
Matthias Krüger
47ccca0c86
Rollup merge of #106992 - joboet:alloc_remove_box_syntax, r=thomcc
Remove unused `#![feature(box_syntax)]` in `alloc`
2023-01-18 06:59:21 +01:00
Matthias Krüger
0ed2549802
Rollup merge of #106889 - scottmcm:windows-mut, r=cuviper
Mention the lack of `windows_mut` in `windows`

This is a common request, going back to at least 2015 (#23783), so mention in the docs that it can't be done and offer a workaround using <https://doc.rust-lang.org/std/cell/struct.Cell.html#method.as_slice_of_cells>.

(See also URLO threads like <https://internals.rust-lang.org/t/a-windows-mut-method-on-slice/16941/10?u=scottmcm>.)
2023-01-17 20:21:27 +01:00
Markus Everling
273c6c3913 Add heapsort fallback in select_nth_unstable 2023-01-17 19:38:37 +01:00
joboet
be9c363066
refactor[alloc]: remove unused box syntax feature 2023-01-17 18:55:44 +01:00
Dylan DPC
1b2d595c14
Rollup merge of #106922 - ChayimFriedman2:patch-5, r=workingjubilee
Avoid unsafe code in `to_ascii_[lower/upper]case()`
2023-01-17 20:33:04 +05:30
Dylan DPC
f91f369949
Rollup merge of #106148 - chenyukang:yukang/fix-105061-unused, r=lcnr
Fix unused_parens issue for higher ranked function pointers

fixes #105061

r? `@lcnr`
2023-01-17 20:33:03 +05:30
onestacked
7355ab3fe3 Constify TypeId ordering impls 2023-01-16 21:26:03 +01:00
Chayim Refael Friedman
8dbc878a35
Avoid unsafe code in to_ascii_[lower/upper]case() 2023-01-16 01:15:06 +02:00
bors
9e75dddf60 Auto merge of #106393 - the8472:use-ptr-sub, r=scottmcm
Simplify manual ptr arithmetic in slice::Iter with ptr_sub

The old code was introduced in #61885, which predates the ptr_sub method and underlying intrinsic. The codegen test still passes.

r? `@scottmcm`
2023-01-15 18:39:40 +00:00
The 8472
9db0134018 replace manual ptr arithmetic with ptr_sub 2023-01-15 17:38:05 +01:00
bors
bbb36fe545 Auto merge of #105851 - dtolnay:peekmutleak, r=Mark-Simulacrum
Leak amplification for peek_mut() to ensure BinaryHeap's invariant is always met

In the libs-api team's discussion around #104210, some of the team had hesitations around exposing malformed BinaryHeaps of an element type whose Ord and Drop impls are trusted, and which does not contain interior mutability.

For example in the context of this kind of code:

```rust
use std::collections::BinaryHeap;
use std::ops::Range;
use std::slice;

fn main() {
    let slice = &mut ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    let cut_points = BinaryHeap::from(vec![4, 2, 7]);
    println!("{:?}", chop(slice, cut_points));
}

// This is a souped up slice::split_at_mut to split in arbitrary many places.
//
// usize's Ord impl is trusted, so 1 single bounds check guarantees all those
// output slices are non-overlapping and in-bounds
fn chop<T>(slice: &mut [T], mut cut_points: BinaryHeap<usize>) -> Vec<&mut [T]> {
    let mut vec = Vec::with_capacity(cut_points.len() + 1);
    let max = match cut_points.pop() {
        Some(max) => max,
        None => {
            vec.push(slice);
            return vec;
        }
    };

    assert!(max <= slice.len());

    let len = slice.len();
    let ptr: *mut T = slice.as_mut_ptr();
    let get_unchecked_mut = unsafe {
        |range: Range<usize>| &mut *slice::from_raw_parts_mut(ptr.add(range.start), range.len())
    };

    vec.push(get_unchecked_mut(max..len));
    let mut end = max;
    while let Some(start) = cut_points.pop() {
        vec.push(get_unchecked_mut(start..end));
        end = start;
    }
    vec.push(get_unchecked_mut(0..end));
    vec
}
```

```console
[['7', '8', '9'], ['4', '5', '6'], ['2', '3'], ['0', '1']]
```

In the current BinaryHeap API, `peek_mut()` is the only thing that makes the above function unsound.

```rust
let slice = &mut ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
let mut cut_points = BinaryHeap::from(vec![4, 2, 7]);
{
    let mut max = cut_points.peek_mut().unwrap();
    *max = 0;
    std::mem::forget(max);
}
println!("{:?}", chop(slice, cut_points));
```

```console
[['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], [], ['2', '3'], ['0', '1']]
```

Or worse:

```rust
let slice = &mut ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
let mut cut_points = BinaryHeap::from(vec![100, 100]);
{
    let mut max = cut_points.peek_mut().unwrap();
    *max = 0;
    std::mem::forget(max);
}
println!("{:?}", chop(slice, cut_points));
```

```console
[['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], [], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\u{1}', '\0', '?', '翾', '?', '翾', '\0', '\0', '?', '翾', '?', '翾', '?', '啿', '?', '啿', '?', '啿', '?', '啿', '?', '啿', '?', '翾', '\0', '\0', '񤬐', '啿', '\u{5}', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\u{8}', '\0', '`@',` '\0', '\u{1}', '\0', '?', '翾', '?', '翾', '?', '翾', '
thread 'main' panicked at 'index out of bounds: the len is 33 but the index is 33', library/core/src/unicode/unicode_data.rs:319:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

---

This PR makes `peek_mut()` use leak amplification (https://doc.rust-lang.org/1.66.0/nomicon/leaking.html#drain) to preserve the heap's invariant even in the situation that `PeekMut` gets leaked.

I'll also follow up in the tracking issue of unstable `drain_sorted()` (#59278) and `retain()` (#71503).
2023-01-15 08:59:55 +00:00
Matthias Krüger
cdf462275a
Rollup merge of #106880 - tspiteri:borrowing-sub-typo, r=cuviper
doc: fix typo
2023-01-15 01:01:39 +01:00
Matthias Krüger
445e3841da
Rollup merge of #106867 - sunfishcode:sunfishcode/std-os-fd-stable-version, r=m-ou-se
Fix the stability attributes for `std::os::fd`.

As `@bjorn3` pointed out [here], I used the wrong stability attribute in #98368 when making `std::os::fd` public. I set it to Rust 1.63, which was when io-safety was stabilized, but it should be Rust 1.66, which was when `std::os::fd` was stabilized.

[here]: https://github.com/rust-lang/rust/pull/98368#discussion_r1063721420
2023-01-15 01:01:38 +01:00
Scott McMurray
38917ee9e9 Mention the lack of windows_mut in windows 2023-01-14 15:31:32 -08:00
David Tolnay
23501703fb
Document guarantees about BinaryHeap invariant 2023-01-14 13:28:30 -08:00
David Tolnay
aedb756020
Leak amplification for peek_mut() to ensure BinaryHeap's invariant is always met 2023-01-14 13:28:22 -08:00
David Tolnay
4fe167f83a
Add test of leaking a binary_heap PeekMut 2023-01-14 13:28:14 -08:00
Trevor Spiteri
208b781bda doc: fix typo 2023-01-14 22:09:14 +01:00
Dan Gohman
287c65838c Fix the stability attributes for std::os::fd.
As @bjorn3 pointed out [here], I used the wrong stability attribute in #98368
when making `std::os::fd` public. I set it to Rust 1.63, which was when
io-safety was stabilized, but it should be Rust 1.66, which was when
`std::os::fd` was stabilized.

[here]: https://github.com/rust-lang/rust/pull/98368#discussion_r1063721420
2023-01-14 09:47:34 -08:00
Matthias Krüger
e0eb63a73c
Rollup merge of #106860 - anden3:doc-double-spaces, r=Dylan-DPC
Remove various double spaces in the libraries.

I was just pretty bothered by this when reading the source for a function, and was suggested to check if this happened elsewhere.
2023-01-14 18:45:29 +01:00
Matthias Krüger
43134714f5
Rollup merge of #106661 - mjguzik:linux_statx, r=Mark-Simulacrum
Stop probing for statx unless necessary

As is the current toy program:
fn main() -> std::io::Result<()> {
    use std::fs;

    let metadata = fs::metadata("foo.txt")?;

    assert!(!metadata.is_dir());
    Ok(())
}

... observed under strace will issue:
[snip]
statx(0, NULL, AT_STATX_SYNC_AS_STAT, STATX_ALL, NULL) = -1 EFAULT (Bad address) statx(AT_FDCWD, "foo.txt", AT_STATX_SYNC_AS_STAT, STATX_ALL, {stx_mask=STATX_ALL|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=0, ...}) = 0

While statx is not necessarily always present, checking for it can be delayed to the first error condition. Said condition may very well never happen, in which case the check got avoided altogether.

Note this is still suboptimal as there still will be programs issuing it, but bulk of the problem is removed.

Tested by forbidding the syscall for the binary and observing it correctly falls back to newfstatat.

While here tidy up the commentary, in particular by denoting some problems with the current approach.
2023-01-14 18:45:26 +01:00
Matthias Krüger
085d2f1f09
Rollup merge of #105526 - Xiretza:iter-from-generator-derive, r=scottmcm
libcore: make result of iter::from_generator Clone

`@rustbot` label +A-generators
2023-01-14 18:45:25 +01:00
André Vennberg
2fea03f5e6 Fix some missed double spaces. 2023-01-14 18:26:38 +01:00
André Vennberg
0b35f448f8 Remove various double spaces in source comments. 2023-01-14 17:22:04 +01:00
yukang
c67903ef21 fix issues in unused lint 2023-01-14 17:11:04 +08:00
Yuki Okushi
a05d06f7c5
Rollup merge of #106799 - scottmcm:remove-unused-generics, r=cuviper
Stop having unused lifetimes on some `impl`s

See <https://doc.rust-lang.org/nightly/std/cmp/trait.PartialOrd.html#impl-PartialOrd%3COsStr%3E-for-PathBuf>, where these lifetimes show up even though they're not needed:
![image](https://user-images.githubusercontent.com/18526288/212257802-da275167-38f9-4e2c-aafc-d44f0fc6a7c6.png)

With this PR, the unneeded lifetimes are no longer there:
![image](https://user-images.githubusercontent.com/18526288/212257938-0097c4bf-1247-4c91-8445-5bf0dde1b501.png)
2023-01-14 12:04:36 +09:00
Yuki Okushi
9b502a4765
Rollup merge of #106762 - WaffleLapkin:atomicptr+as_mut_ptr, r=m-ou-se
Add `AtomicPtr::as_mut_ptr`

See https://github.com/rust-lang/rust/issues/66893#issuecomment-720125447

r? thomcc
2023-01-14 12:04:35 +09:00
Yuki Okushi
feac18c0d6
Rollup merge of #106692 - eggyal:mv-binary_heap.rs-binary_heap/mod.rs, r=Mark-Simulacrum
mv binary_heap.rs binary_heap/mod.rs

I confess this request is somewhat selfish, as it's made in order to ease synchronisation with my [copse](https://crates.io/crates/copse) crate (see eggyal/copse#6 for explanation). I wholly understand that such grounds may be insufficient to justify merging this request—but no harm in asking, right?
2023-01-14 12:04:33 +09:00
Yuki Okushi
6d8ac8389e
Rollup merge of #105172 - alexs-sh:issue-98861-fix-next, r=scottmcm
Added error documentation for write_fmt

This continuation of work at rust-lang#98861
2023-01-14 12:04:32 +09:00
Yuki Okushi
6702f20ca7
Rollup merge of #104965 - zacklukem:p-option-as_ref-docs, r=scottmcm
reword Option::as_ref and Option::map examples

The description for the examples of `Option::as_ref` and `Option::map` imply that the example is only doing type conversion, when it is actually finding the length of a string.

Changes the wording to imply that some operation is being run on the value contained in the `Option`

closes #104476
2023-01-14 12:04:31 +09:00
Matthias Krüger
720137b5da
Rollup merge of #106701 - ibraheemdev:sync-sender-spin, r=Amanieu
Fix `mpsc::SyncSender` spinning behavior

Resolves https://github.com/rust-lang/rust/issues/106668.
2023-01-13 19:16:44 +01:00
Scott McMurray
4949fb0419 Stop having unused lifetimes on some impls 2023-01-12 22:55:58 -08:00
bors
279f1c9d8c Auto merge of #106004 - fee1-dead-contrib:const-closures, r=oli-obk
Const closures

cc https://github.com/rust-lang/rust/issues/106003
2023-01-13 05:04:48 +00:00
Yuki Okushi
7e5d477ac5
Rollup merge of #106740 - petar-dambovaliev:float-iterator-hint, r=Nilstrieb
Adding a hint on iterator type errors

Issue reference https://github.com/rust-lang/rust/issues/106728

- [x] add a case in the attribute
- [x] add a test

closes #106728
2023-01-13 05:47:23 +09:00
Yuki Okushi
3f21b812be
Rollup merge of #106446 - bzEq:fix-unwind-lsda, r=Amanieu
[LSDA] Take ttype_index into account when taking unwind action

If `cs_action != 0`, we should check the `ttype_index` field in action record. If `ttype_index == 0`, a clean up action is taken; otherwise catch action is taken.

This can fix unwind failure on AIX which uses LLVM's libunwind by default. IIUC, rust's LSDA is borrowed from c++ and I'm assuming itanium-cxx-abi https://itanium-cxx-abi.github.io/cxx-abi/exceptions.pdf should be followed, so the fix follows what libcxxabi does. See ec48682ce9/libcxxabi/src/cxa_personality.cpp (L152) for use of `ttype_index`.
2023-01-13 05:47:21 +09:00
Petar Dambovaliev
bdf990022a add note for float iterator 2023-01-12 15:29:53 +01:00
bors
606c390725 Auto merge of #106760 - compiler-errors:rollup-0bogyco, r=compiler-errors
Rollup of 8 pull requests

Successful merges:

 - #103236 (doc: rewrite doc for signed int::{carrying_add,borrowing_sub})
 - #103800 (Stabilize `::{core,std}::pin::pin!`)
 - #106097 (Migrate mir_build diagnostics 2 of 3)
 - #106170 (Move autoderef to `rustc_hir_analysis`)
 - #106323 (Stabilize f16c_target_feature)
 - #106360 (Tweak E0277 `&`-removal suggestions)
 - #106524 (Label `struct/enum constructor` instead of `fn item`, mention that it should be called on type mismatch)
 - #106739 (Remove `<dyn AstConv<'tcx>>::fun(c, ...)` calls in favour of `c.astconv().fun(...)`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2023-01-12 10:46:19 +00:00
Maybe Waffle
22b4c68895 Make // SAFETY comment part of the doctest, and not surrounding code 2023-01-12 07:28:43 +00:00
Maybe Waffle
f1a63bc2dd Remove unused mut from a doctest 2023-01-12 07:27:51 +00:00
Maybe Waffle
a513c84a5b Add AtomicPtr::as_mut_ptr 2023-01-12 07:27:36 +00:00
Michael Goulet
244b90edee
Rollup merge of #106323 - starkat99:stabilize-f16c_target_feature, r=petrochenkov
Stabilize f16c_target_feature

Resolves https://github.com/rust-lang/stdarch/issues/1234

Library PR for stabilizing corresponding intrinsics: https://github.com/rust-lang/stdarch/pull/1366

See also #44839 tracking issue for target_feature
2023-01-11 22:25:48 -08:00
Michael Goulet
2e17a5d406
Rollup merge of #103800 - danielhenrymantilla:stabilize-pin-macro, r=dtolnay
Stabilize `::{core,std}::pin::pin!`

As discussed [over here](https://github.com/rust-lang/rust/issues/93178#issuecomment-1295843548), it looks like a decent time to stabilize the `pin!` macro.

### Public API

```rust
// in module `core::pin`

/// API: `fn pin<T>($value: T) -> Pin<&'local mut T>`
pub macro pin($value:expr $(,)?) {
    …
}
```

  - Tracking issue: #93178

(now all this needs is an FCP by the proper team?)
2023-01-11 22:25:47 -08:00
Michael Goulet
e23b0fd318
Rollup merge of #103236 - tspiteri:redoc-int-adc-sbb, r=m-ou-se
doc: rewrite doc for signed int::{carrying_add,borrowing_sub}

Reword the documentation for bigint helper methods, signed `int::{carrying_add,borrowing_sub}` (#85532).

This change is a follow-up to #101889, which was for the unsigned methods.
2023-01-11 22:25:46 -08:00
Matthias Krüger
db8301b7f8
Rollup merge of #106638 - RalfJung:realstd, r=thomcc
std tests: use __OsLocalKeyInner from realstd

This is basically the same as https://github.com/rust-lang/rust/pull/100201, but for __OsLocalKeyInner:

Some std tests are failing in Miri on Windows because [this static](a377893da2/library/std/src/sys/windows/thread_local_key.rs (L234-L239)) is getting duplicated, and Miri does not handle that properly -- Miri does not support this magic `.CRT$XLB` linker section, but instead just looks up this particular hard-coded static in the standard library. This PR lets the test suite use the std static instead of having its own copy.

Fixes https://github.com/rust-lang/miri/issues/2754
r? `@thomcc`
2023-01-12 06:52:37 +01:00
Ibraheem Ahmed
8917e99362 rework and document backoff behavior of sync::mpsc 2023-01-11 21:29:14 -05:00
Deadbeef
ca1eb4309e test use in libcore 2023-01-12 02:28:38 +00:00
Daniel Henry-Mantilla
48b7e2a5b9
Stabilize ::{core,std}::pin::pin! 2023-01-11 14:09:14 -08:00
Mateusz Guzik
b49aa8d53e Stop probing for statx unless necessary
As is the current toy program:
fn main() -> std::io::Result<()> {
    use std::fs;

    let metadata = fs::metadata("foo.txt")?;

    assert!(!metadata.is_dir());
    Ok(())
}

... observed under strace will issue:
[snip]
statx(0, NULL, AT_STATX_SYNC_AS_STAT, STATX_ALL, NULL) = -1 EFAULT (Bad address)
statx(AT_FDCWD, "foo.txt", AT_STATX_SYNC_AS_STAT, STATX_ALL, {stx_mask=STATX_ALL|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=0, ...}) = 0

While statx is not necessarily always present, checking for it can be
delayed to the first error condition. Said condition may very well never
happen, in which case the check got avoided altogether.

Note this is still suboptimal as there still will be programs issuing
it, but bulk of the problem is removed.

Tested by forbidding the syscall for the binary and observing it
correctly falls back to newfstatat.

While here tidy up the commentary, in particular by denoting some
problems with the current approach.
2023-01-11 17:10:08 +00:00