Commit graph

5880 commits

Author SHA1 Message Date
The8472
82b4544ddc add benchmarks and tests for Hash and Eq impls on Path
The tests check for consistency between Ord, Eq and Hash
2021-11-09 20:54:00 +01:00
est31
eeaa2f16aa Extend the const_swap feature
This makes the inherent method ptr::swap unstably
const, as well as slice::swap{,_unchecked}.
2021-11-09 20:09:56 +01:00
Jubilee Young
caf206b820 Stabilize File::options()
Renames File::with_options to File::options, per consensus in
rust-lang/rust#65439, and stabilizes it.
2021-11-09 10:22:28 -08:00
Matthias Krüger
9c1aa12ff1
Rollup merge of #90723 - asquared31415:box_docs, r=jyn514
Better document `Box` and `alloc::alloc::box_free` connection

The internal `alloc::alloc::box_free` function requires that its signature matches the `owned_box` struct's declaration, but previously that connection was only documented on the `box_free` function.

This PR makes the documentation two-way to help anyone making theoretical changes to `Box` to see the connection, since changes are more likely to originate from `Box`.
2021-11-09 19:00:46 +01:00
bors
d6082292a6 Auto merge of #86041 - bstrie:unmagic-array-copy, r=jackh726
Replace Copy/Clone compiler magic on arrays with library impls

With const generics the compiler no longer needs to fake these impls.
2021-11-09 17:13:44 +00:00
Yuki Okushi
d638c1d13c
Rollup merge of #87530 - bstrie:commentsync, r=bstrie
Add comments regarding superfluous `!Sync` impls
2021-11-09 22:02:21 +09:00
asquared31415
db4e60b29f document Box and box_free connection 2021-11-09 07:13:53 -05:00
Julian Frimmel
60a9d5a5a9 Re-enable copy[_nonoverlapping]() runtime checks
This commit re-enables the debug checks for valid usages of the two
functions `copy()` and `copy_nonoverlapping()`. Those checks were com-
mented out in #79684 in order to make the functions const. All that's
been left was a FIXME, that could not be resolved until there is was way
to only do the checks at runtime.
Since #89247 there is such a way: `const_eval_select()`. This commit
uses that new intrinsic in order to either do nothing (at compile time)
or to do the old checks (at runtime).

The change itself is rather small: in order to make the checks usable
with `const_eval_select`, they are moved into a local function (one for
`copy` and one for `copy_nonoverlapping` to keep symmetry).

The change does not break referential transparency, as there is nothing
you can do at compile time, which you cannot do on runtime without get-
ting undefined behavior. The CTFE-engine won't allow missuses. The other
way round is also fine.
2021-11-09 10:02:09 +01:00
bstrie
61b1394ac7 Attempt to address perf regressions with #[inline] 2021-11-08 15:51:56 -05:00
bstrie
3024efff59 Update Copy/Clone documentation WRT arrays 2021-11-08 13:11:59 -05:00
bstrie
ce1143e94d impl Copy/Clone for arrays in std, not in compiler 2021-11-08 13:11:58 -05:00
bstrie
86c0ef8adc Add comments regarding superfluous !Sync impls 2021-11-08 13:07:20 -05:00
Guillaume Gomez
f07f800364
Rollup merge of #90494 - Meziu:armv6k-3ds-target, r=sanxiyn
ARMv6K Horizon OS panic change

After a small change to `backtrace-rs` ([#448](https://github.com/rust-lang/backtrace-rs/pull/448)), `PanicStrategy::Unwind` is now fully supported.
2021-11-08 15:15:22 +01:00
Tomoaki Kawada
f17077002b kmc-solid: Avoid the use of asm_const 2021-11-08 19:13:31 +09:00
bors
fecfc0e6cc Auto merge of #89310 - joshtriplett:available-concurrency-affinity, r=m-ou-se
Make `std:🧵:available_concurrency` support process-limited number of CPUs

Use `libc::sched_getaffinity` and count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited via `taskset` or similar.

This also covers cgroup cpusets.
2021-11-07 11:53:25 +00:00
bors
90a273b785 Auto merge of #90348 - Amanieu:asm_feature_gates, r=joshtriplett
Add features gates for experimental asm features

This PR splits off parts of `asm!` into separate features because they are not ready for stabilization.

Specifically this adds:
- `asm_const` for `const` operands.
- `asm_sym` for `sym` operands.
- `asm_experimental_arch` for architectures other than x86, x86_64, arm, aarch64 and riscv.

r? `@nagisa`
2021-11-07 04:59:42 +00:00
Amanieu d'Antras
eb32c00216 Add features gates for experimental asm features 2021-11-07 01:23:53 +00:00
Jacob Pratt
0cdbeaa2a3
Stabilize const_raw_ptr_deref for *const T
This stabilizes dereferencing immutable raw pointers in const contexts.
It does not stabilize `*mut T` dereferencing. This is placed behind the
`const_raw_mut_ptr_deref` feature gate.
2021-11-06 17:05:15 -04:00
Matthias Krüger
0a5640b55f use matches!() macro in more places 2021-11-06 16:13:14 +01:00
bors
7276a6a117 Auto merge of #90297 - dtolnay:dotzero, r=petrochenkov
Append .0 to unsuffixed float if it would otherwise become int token

Previously the unsuffixed f32/f64 constructors of `proc_macro::Literal` would create literal tokens that are definitely not a float:

```rust
Literal::f32_unsuffixed(10.0)  // 10
Literal::f32_suffixed(10.0)    // 10f32
Literal::f64_unsuffixed(10.0)  // 10
Literal::f64_suffixed(10.0)    // 10f64
```

Notice that the `10` are actually integer tokens if you were to reparse them, not float tokens.

This diff updates `Literal::f32_unsuffixed` and `Literal::f64_unsuffixed` to produce tokens that unambiguously parse as a float. This matches longstanding behavior of the proc-macro2 crate's implementation of these APIs dating back at least 3.5 years, so it's likely an unobjectionable behavior.

```rust
Literal::f32_unsuffixed(10.0)  // 10.0
Literal::f32_suffixed(10.0)    // 10f32
Literal::f64_unsuffixed(10.0)  // 10.0
Literal::f64_suffixed(10.0)    // 10f64
```

Fixes https://github.com/dtolnay/syn/issues/1085.
2021-11-06 07:15:05 +00:00
Josh Stone
6edaaa6db8 Also note tool expectations of fork vs clone3
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2021-11-05 14:49:24 -07:00
Josh Stone
fa2eee7bf2 Update another comment on fork vs. clone3 2021-11-05 14:48:52 -07:00
Josh Stone
85b55ce00d Only use clone3 when needed for pidfd
In #89522 we learned that `clone3` is interacting poorly with Gentoo's
`sandbox` tool. We only need that for the unstable pidfd extensions, so
otherwise avoid that and use a normal `fork`.
2021-11-05 14:48:41 -07:00
Yuki Okushi
0b3a002805
Reorder widening_impls to make the doc clearer 2021-11-05 11:55:51 -07:00
bors
d22dd65835 Auto merge of #90604 - mbartlett21:iterator-reexports, r=kennytm
Re-export some iterators from `core` in `std`

These iterators seem to have been forgotten to be re-exported from `std` (through `alloc`)

These are stable:

`core::slice::{SplitInclusive, SplitInclusiveMut}`

This one is still unstable:

`core::slice::EscapeAscii` (cc #77174)
2021-11-05 12:22:13 +00:00
bors
489ec310d2 Auto merge of #90577 - matthiaskrgr:clippy_perf_nov, r=petrochenkov
clippy::perf fixes
2021-11-05 09:17:39 +00:00
mbartlett21
ed63c71d61
Fix str::SplitInclusive stabilisation date 2021-11-05 17:46:58 +10:00
mbartlett21
d606dbe256
Add feature to alloc so we can re-export. 2021-11-05 17:35:07 +10:00
mbartlett21
9ae92ad19a
Re-export core::slice::EscapeAscii 2021-11-05 17:14:57 +10:00
mbartlett21
03d1f24db8
Re-export core::slice::SplitInclusive[Mut] 2021-11-05 15:44:43 +10:00
Yuki Okushi
5aa6eaf4c0
Rollup merge of #90556 - scottmcm:carrying_comments, r=joshtriplett
Add more text and examples to `carrying_{add|mul}`

`feature(bigint_helper_methods)` tracking issue https://github.com/rust-lang/rust/issues/85532

cc `````@clarfonthey`````
2021-11-05 10:32:46 +09:00
Matthias Krüger
28ef4169cc clippy::perf fixes 2021-11-04 21:07:56 +01:00
Scott McMurray
dc2c2603c6 Add more text and examples to `carrying_{add|mul}" 2021-11-04 00:51:45 -07:00
bors
4061c04079 Auto merge of #87467 - inquisitivecrystal:ref-unwind, r=dtolnay
Implement `RefUnwindSafe` for `Rc<T>`

This PR implements `RefUnwindSafe` for `Rc<T>`, where `T: RefUnwindSafe`.

This impl was omitted by an apparent oversight. `Rc<T>` already implements `UnwindSafe`. `Arc<T>` implements both `UnwindSafe` and `RefUnwindSafe`. There is no reason why an `&Rc<T>` is any less unwind safe than a `Rc<T>` or an `&Arc<T>`, so this should be safe to add.

Resolves #45924.
2021-11-04 06:54:21 +00:00
bors
0b4ac62dda Auto merge of #90392 - solid-rs:fix-solid-support, r=Mark-Simulacrum
kmc-solid: Fix SOLID target

This PR is a follow-up for #86191 and necessary to make the [`*-kmc-solid_*`](https://doc.rust-lang.org/nightly/rustc/platform-support/kmc-solid.html) Tier 3 targets actually usable.

 - Bumps `libc` to 0.2.106, which includes <https://github.com/rust-lang/libc/pull/2227>.
 - Applies the change made by #89324 to this target's target-specific code.
2021-11-04 03:48:43 +00:00
The8472
7afe6f52e4 Make RawVec private to alloc
RawVec was previously exposed for compiler-internal use (libarena specifically) in 1acbb0a935

Since it is unstable, doc-hidden and has no associated tracking issue it was never meant for public use. And since
it is no longer used outside alloc itself it can be made private again.

Also remove some functions that are dead due to lack of internal users.
2021-11-03 20:52:16 +01:00
bors
87ec5680c9 Auto merge of #90421 - thomcc:friendship-ended-with-ssize_t-now-ptrdiff_t-is-my-best-friend, r=joshtriplett
Replace `std::os::raw::c_ssize_t` with `std::os::raw::c_ptrdiff_t`

The discussions in #88345 brought up that `ssize_t` is not actually the signed index type defined in stddef.h, but instead it's `ptrdiff_t`. It seems pretty clear that the use of `ssize_t` here was a mistake on my part, and that if we're going to bother having a isize-alike for FFI in `std::os::raw`, it should be `ptrdiff_t` and not `ssize_t`.

Anyway, both this and `c_size_t` are dubious in the face of the discussion in https://internals.rust-lang.org/t/pre-rfc-usize-is-not-size-t/15369, and any RFC/project-group/etc that handles those issues there should contend with these types in some manner, but that doesn't mean we shouldn't fix something wrong like this, even if it is unstable.

All that said, `size_t` is *vastly* more common in function signatures than either `ssize_t` or `ptrdiff_t`, so I'm going to update the tracking issue's list of unresolved questions to note that perhaps we only want `c_size_t` — I mostly added the signed version for symmetry, rather than to meet a need. (Given this, I'm also fine with modifying this patch to instead remove `c_ssize_t` without a replacement)

CC `@magicant` (who brought the issue up)
CC `@chorman0773` (who has a significantly firmer grasp on the minutae of the C standard than I do)

r? `@joshtriplett` (original reviewer, active in the discussions around this)
2021-11-03 05:36:30 +00:00
inquisitivecrystal
bd194da4c2 Implement RefUnwindSafe for Rc<T> 2021-11-02 16:30:55 -07:00
bors
c3190c1eb4 Auto merge of #90442 - ChrisDenton:win-tls-dtor, r=alexcrichton
Windows thread-local keyless drop

`#[thread_local]` allows us to maintain a per-thread list of destructors. This also avoids the need to synchronize global data (which is particularly tricky within the TLS callback function).

r? `@alexcrichton`
2021-11-02 12:15:08 +00:00
Meziu
d379147219 Updated backtrace submodule 2021-11-02 12:31:34 +01:00
bors
6384dca100 Auto merge of #90439 - m-ou-se:thread-is-running, r=Mark-Simulacrum
Add JoinHandle::is_running.

This adds:
```rust
impl<T> JoinHandle<T> {
    /// Checks if the the associated thread is still running its main function.
    ///
    /// This might return `false` for a brief moment after the thread's main
    /// function has returned, but before the thread itself has stopped running.
    pub fn is_running(&self) -> bool;
}
```
The usual way to check if a background thread is still running is to set some atomic flag at the end of its main function. We already do that, in the form of dropping an Arc which will reduce the reference counter. So we might as well expose that information.

This is useful in applications with a main loop (e.g. a game, gui, control system, ..) where you spawn some background task, and check every frame/iteration whether the background task is finished to .join() it in that frame/iteration while keeping the program responsive.
2021-11-02 08:11:57 +00:00
Chris Denton
1048651fa3
Run destructors from existing tls callback 2021-11-01 15:19:49 +00:00
Mara Bos
978ebd9c8c Add tracking issue for thread_is_running. 2021-11-01 15:04:24 +01:00
Tomoaki Kawada
26a6cc4515 itron: Rename itron:🧵:{available_conccurrency -> available_parallelism}
Catching up with commit b4615b5bf9
2021-11-01 10:45:51 +09:00
Tomoaki Kawada
15af06795c Bump libc dependency of std to 0.2.106 2021-11-01 10:45:49 +09:00
Thom Chiovoloni
8d19819781
Re-add std::os::raw::c_ssize_t, with more accurate documentation 2021-10-31 13:01:57 -07:00
Chris Denton
9212f4070e
Windows thread-local keyless drop
`#[thread_local]` allows us to maintain a per-thread list of destructors. This also avoids the need to synchronize global data (which is particularly tricky within the TLS callback function).
2021-10-31 16:09:35 +00:00
bors
c7e4740ec1 Auto merge of #86336 - camsteffen:char-array-pattern, r=joshtriplett
impl Pattern for char array

Closes #39511
Closes #86329
2021-10-31 15:45:39 +00:00
Mara Bos
67362b301b Add test for JoinHandle::is_running. 2021-10-31 15:23:36 +01:00
Mara Bos
d718b1a795 Add JoinHandle::is_running. 2021-10-31 15:09:36 +01:00
Matthias Krüger
455a79acab
Rollup merge of #90431 - jkugelman:must-use-std-o-through-z, r=joshtriplett
Add #[must_use] to remaining std functions (O-Z)

I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is half of the remaining items from the `std` crate, from O-Z.

`panicking::take_hook` has a side effect: it unregisters the current panic hook, returning it. I almost ignored it, but the documentation example shows `let _ = panic::take_hook();`, so following suit I went ahead and added a `#[must_use]`.

```rust
std::panicking   fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>;
```

I added these functions that clippy did not flag:

```rust
std::path::Path   fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool;
std::path::Path   fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool;
std::path::Path   fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf;
std::path::Path   fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
```

Parent issue: #89692

r? `@joshtriplett`
2021-10-31 13:20:07 +01:00
Matthias Krüger
26f505c433
Rollup merge of #90430 - jkugelman:must-use-std-a-through-n, r=joshtriplett
Add #[must_use] to remaining std functions (A-N)

I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is half of the remaining items from the `std` crate, from A-N.

I added these functions myself. Clippy predictably ignored the `mut` ones, but I don't know why the rest weren't flagged. Check them closely, please? Maybe I overlooked good reasons.

```rust
std::backtrace::Backtrace                                   const fn disabled() -> Backtrace;
std::backtrace::Backtrace<'a>                               fn frames(&'a self) -> &'a [BacktraceFrame];
std::collections::hash_map::RawOccupiedEntryMut<'a, K, V>   fn key_mut(&mut self) -> &mut K;
std::collections::hash_map::RawOccupiedEntryMut<'a, K, V>   fn get_mut(&mut self) -> &mut V;
std::collections::hash_map::RawOccupiedEntryMut<'a, K, V>   fn get_key_value(&mut self) -> (&K, &V);
std::collections::hash_map::RawOccupiedEntryMut<'a, K, V>   fn get_key_value_mut(&mut self) -> (&mut K, &mut V);
std::env                                                    fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString>;
std::env                                                    fn split_paths<T: AsRef<OsStr> + ?Sized>(unparsed: &T) -> SplitPaths<'_>;
std::io::Error                                              fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)>;
```

Parent issue: #89692

r? `@joshtriplett`
2021-10-31 13:20:06 +01:00
Matthias Krüger
88e5ae2dd3
Rollup merge of #89786 - jkugelman:must-use-len-and-is_empty, r=joshtriplett
Add #[must_use] to len and is_empty

Parent issue: #89692

r? `@joshtriplett`
2021-10-31 13:20:05 +01:00
Matthias Krüger
6c5aa765fb
Rollup merge of #89068 - bjorn3:restructure_rt2, r=joshtriplett
Restructure std::rt (part 2)

A couple more cleanups on top of https://github.com/rust-lang/rust/pull/89011

Blocked on #89011
2021-10-31 13:20:04 +01:00
Matthias Krüger
ff6d8ecd64
Rollup merge of #90427 - jkugelman:must-use-alloc-leak, r=joshtriplett
Add #[must_use] to alloc functions that would leak memory

As [requested](https://github.com/rust-lang/rust/pull/89899#issuecomment-955600779) by `@joshtriplett.`

> Please do go ahead and add the ones whose only legitimate use for ignoring the return value is leaking memory. (In a separate PR please.) I think it's sufficiently error-prone to call something like alloc and ignore the result that it's legitimate to require `let _ =` for that.

I added `realloc` myself. Clippy ignored it because of its `mut` argument.

```rust
alloc/src/alloc.rs:123:1   alloc   unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8;
```

Parent issue: #89692

r? `@joshtriplett`
2021-10-31 09:20:27 +01:00
Matthias Krüger
d4bdcdb1ec
Rollup merge of #89951 - ojeda:stable-unwrap_unchecked, r=dtolnay
Stabilize `option_result_unwrap_unchecked`

Closes https://github.com/rust-lang/rust/issues/81383.

Stabilization report: https://github.com/rust-lang/rust/issues/81383#issuecomment-944498212.

```@rustbot``` label +A-option-result +T-libs-api
2021-10-31 09:20:27 +01:00
Matthias Krüger
95750ae439
Rollup merge of #89897 - jkugelman:must-use-core, r=joshtriplett
Add #[must_use] to remaining core functions

I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is everything remaining from the `core` crate.

Ignored by clippy for reasons unknown:

```rust
core::alloc::Layout   unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self;
core::any             const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str;
```

Ignored by clippy because of `mut`:

```rust
str   fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str);
```

<del>
Ignored by clippy presumably because a caller might want `f` called for side effects. That seems like a bad usage of `map` to me.

```rust
core::cell::Ref<'b, T>   fn map<U: ?Sized, F>(orig: Ref<'b, T>, f: F) -> Ref<'b, T>;
core::cell::Ref<'b, T>   fn map_split<U: ?Sized, V: ?Sized, F>(orig: Ref<'b, T>, f: F) -> (Ref<'b, U>, Ref<'b, V>);
```
</del>

Parent issue: #89692

r? ```@joshtriplett```
2021-10-31 09:20:26 +01:00
Matthias Krüger
e79e9f5e2a
Rollup merge of #89839 - jkugelman:must-use-mem-ptr-functions, r=joshtriplett
Add #[must_use] to mem/ptr functions

There's a lot of low-level / unsafe stuff here. Are there legit use cases for ignoring any of these return values?

* No regressions in `./x.py test --stage 1 library/std src/tools/clippy`.
* One regression in `./x.py test --stage 1 src/test/ui`. Fixed.
* I am unable to run `./x.py doc` on my machine so I'll need to wait for the CI to verify doctests pass. I eyeballed all the adjacent tests and they all look okay.

Parent issue: #89692

r? ```@joshtriplett```
2021-10-31 09:20:25 +01:00
Matthias Krüger
a26b1d2259
Rollup merge of #89835 - jkugelman:must-use-expensive-computations, r=joshtriplett
Add #[must_use] to expensive computations

The unifying theme for this commit is weak, admittedly. I put together a list of "expensive" functions when I originally proposed this whole effort, but nobody's cared about that criterion. Still, it's a decent way to bite off a not-too-big chunk of work.

Given the grab bag nature of this commit, the messages I used vary quite a bit. I'm open to wording changes.

For some reason clippy flagged four `BTreeSet` methods but didn't say boo about equivalent ones on `HashSet`. I stared at them for a while but I can't figure out the difference so I added the `HashSet` ones in.

```rust
// Flagged by clippy.
alloc::collections::btree_set::BTreeSet<T>   fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T>;
alloc::collections::btree_set::BTreeSet<T>   fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>) -> SymmetricDifference<'a, T>
alloc::collections::btree_set::BTreeSet<T>   fn intersection<'a>(&'a self, other: &'a BTreeSet<T>) -> Intersection<'a, T>;
alloc::collections::btree_set::BTreeSet<T>   fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T>;

// Ignored by clippy, but not by me.
std::collections::HashSet<T, S>              fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S>;
std::collections::HashSet<T, S>              fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, S>) -> SymmetricDifference<'a, T, S>
std::collections::HashSet<T, S>              fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S>;
std::collections::HashSet<T, S>              fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S>;
```

Parent issue: #89692

r? ```@joshtriplett```
2021-10-31 09:20:24 +01:00
Matthias Krüger
3cf3910c15
Rollup merge of #89833 - jkugelman:must-use-rc-downgrade, r=joshtriplett
Add #[must_use] to Rc::downgrade

Missed this in previous PR https://github.com/rust-lang/rust/pull/89796#issuecomment-941456006

Parent issue: #89692

r? ```@joshtriplett```
2021-10-31 09:20:23 +01:00
Matthias Krüger
15a0cddff3
Rollup merge of #89677 - maxwase:is-symlink-stabilization, r=joshtriplett
Stabilize `is_symlink()` for `Metadata` and `Path`

I'm not fully sure about `since` version, correct me if I'm wrong

Needs update after stabilization: [cargo-test-support](8063672238/crates/cargo-test-support/src/paths.rs (L202))

Linked issue: #85748
2021-10-31 09:20:22 +01:00
Matthias Krüger
e7be8a2c07
Rollup merge of #89446 - chrismit3s:issue-88715-fix, r=joshtriplett
Add paragraph to ControlFlow docs to menion it works with the ? operator (#88715)

fixes #88715

r? ```@steveklabnik```
2021-10-31 09:20:21 +01:00
John Kugelman
e129d49f88 Add #[must_use] to remaining std functions (A-N) 2021-10-30 23:44:02 -04:00
John Kugelman
a81d4b18ea Add #[must_use] to remaining std functions (O-Z) 2021-10-30 23:37:32 -04:00
John Kugelman
42e0282d52 Add #[must_use] to alloc functions that would leak memory 2021-10-30 22:19:07 -04:00
bors
0a09858b05 Auto merge of #90306 - kornelski:slicecloneasset, r=joshtriplett
track_caller for slice length assertions

`clone_from_slice` was missing `#[track_caller]`, and its assert did not report a useful location.

These are small generic methods, so hopefully track_caller gets inlined into nothingness, but it may be worth running a benchmark on this.
2021-10-31 01:56:40 +00:00
Josh Triplett
7c9611d124 Make std:🧵:available_concurrency support process-limited number of CPUs
Use libc::sched_getaffinity and count the number of CPUs in the returned
mask. This handles cases where the process doesn't have access to all
CPUs, such as when limited via taskset or similar.
2021-10-31 01:38:14 +02:00
Josh Triplett
23be29aace Update libc to 0.2.106 to get definitions for CPU_* 2021-10-31 01:38:05 +02:00
John Kugelman
6745e8da06 Add #[must_use] to len and is_empty 2021-10-30 19:25:12 -04:00
John Kugelman
887503ad14 Add #[must_use] to mem/ptr functions 2021-10-30 18:54:48 -04:00
Matthias Krüger
0da75bcc9c
Rollup merge of #90401 - mkroening:hermit-condvar, r=joshtriplett
hermit: Implement Condvar::wait_timeout

This implements `Condvar::wait_timeout` for the `hermit` target.

See
* https://github.com/hermitcore/rust/pull/2
* https://github.com/hermitcore/rust/pull/5

CC: `@stlankes`
2021-10-31 00:33:25 +02:00
Matthias Krüger
1adb664392
Rollup merge of #89899 - jkugelman:must-use-alloc, r=joshtriplett
Add #[must_use] to remaining alloc functions

I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is everything remaining from the `alloc` crate.

I ignored these because they might be used to purposefully leak memory... or other allocator shenanigans? I dunno. I'll add them if y'all tell me to.

```rust
alloc::alloc          unsafe fn alloc(layout: Layout) -> *mut u8;
alloc::alloc          unsafe fn alloc_zeroed(layout: Layout) -> *mut u8;
alloc::sync::Arc<T>   fn into_raw(this: Self) -> *const T;
```

I don't know why clippy ignored these. I added them myself:

```rust
alloc::collections::btree_map::BTreeMap<K, V>   fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>;
alloc::collections::btree_set::BTreeSet<T>      fn range<K: ?Sized, R>(&self, range: R) -> Range<'_, T>;
```

I added these non-mutating `mut` functions:

```rust
alloc::collections::btree_map::BTreeMap<K, V>     fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>;
alloc::collections::btree_map::BTreeMap<K, V>     fn iter_mut(&mut self) -> IterMut<'_, K, V>;
alloc::collections::btree_map::BTreeMap<K, V>     fn values_mut(&mut self) -> ValuesMut<'_, K, V>;
alloc::collections::linked_list::LinkedList<T>    fn iter_mut(&mut self) -> IterMut<'_, T>;
alloc::collections::linked_list::LinkedList<T>    fn cursor_front_mut(&mut self) -> CursorMut<'_, T>;
alloc::collections::linked_list::LinkedList<T>    fn cursor_back_mut(&mut self) -> CursorMut<'_, T>;
alloc::collections::linked_list::LinkedList<T>    fn front_mut(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::LinkedList<T>    fn back_mut(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn current(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn peek_next(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn peek_prev(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn front_mut(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn back_mut(&mut self) -> Option<&mut T>;
```

I moved a few existing `#[must_use]`s from functions onto the iterator types they return: `IntoIterSorted`, `IntoKeys`, `IntoValues`.

Parent issue: #89692

r? `@joshtriplett`
2021-10-31 00:33:24 +02:00
Matthias Krüger
d872d7fd00
Rollup merge of #89789 - jkugelman:must-use-thread-builder, r=joshtriplett
Add #[must_use] to thread::Builder

I copied the wording of the [`fmt::Debug` builders](https://doc.rust-lang.org/src/core/fmt/builders.rs.html#444).

Affects:

```rust
std/src/thread/mod.rs:289:5   std:🧵:Builder   fn new() -> Builder;
std/src/thread/mod.rs:318:5   std:🧵:Builder   fn name(mut self, name: String) -> Builder;
std/src/thread/mod.rs:341:5   std:🧵:Builder   fn stack_size(mut self, size: usize) -> Builder;
```

Parent issue: #89692

r? `@joshtriplett`
2021-10-31 00:33:23 +02:00
John Kugelman
68b0d86294 Add #[must_use] to remaining core functions 2021-10-30 18:21:29 -04:00
Thom Chiovoloni
d429d0df33
Replace std::os::raw::c_ssize_t with std::os::raw::c_ptrdiff_t 2021-10-30 10:54:34 -07:00
bors
2609fab8e4 Auto merge of #90205 - mati865:link-modifiers-in-rustc, r=petrochenkov
Repace use of `static_nobundle` with `native_link_modifiers` within Rust codebase

This fixes warnings when building Rust and running tests:
```
warning: library kind `static-nobundle` has been superseded by specifying `-bundle` on library kind `static`. Try `static:-bundle`
warning: `rustc_llvm` (lib) generated 2 warnings (1 duplicate)
```
2021-10-30 16:22:49 +00:00
Matthias Krüger
b531364a1a
Rollup merge of #90377 - WaffleLapkin:const_slice_from_raw_parts, r=oli-obk
Make `core::slice::from_raw_parts[_mut]` const

Responses to #90012 seem to allow ``@rust-lang/wg-const-eval`` to decide on use of `const_eval_select`, so we can make `core::slice::from_raw_parts[_mut]` const :)

---
This PR marks the following APIs as const:
```rust
// core::slice
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T];
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T];
```
---

Resolves #90011
r? ``@oli-obk``
2021-10-30 14:37:01 +02:00
Matthias Krüger
86087f906d
Rollup merge of #90371 - Veykril:patch-2, r=jyn514
Fix incorrect doc link

Looks like a copy paste mistake
2021-10-30 14:36:59 +02:00
Matthias Krüger
20bb93210d
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
Make most std::ops traits const on numeric types

This PR makes existing implementations of `std::ops` traits (`Add`, `Sub`, etc) [`impl const`](https://github.com/rust-lang/rust/issues/67792) where possible.
This affects:
- All numeric primitives (`u*`, `i*`, `f*`)
- `NonZero*`
- `Wrapping`

This is under the `rustc_const_unstable` feature `const_ops`.
I will write tests once I know what can and can't be kept for the final version of this PR.

Since this is my first PR to rustc (and hopefully one of many), please give me feedback on how to better handle the PR process wherever possible. Thanks

[Zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Const.20std.3A.3Aops.20traits.20PR)
2021-10-30 14:36:58 +02:00
bors
2b643e9871 Auto merge of #89174 - ChrisDenton:automatic-verbatim-paths, r=dtolnay
Automatically convert paths to verbatim for filesystem operations that support it

This allows using longer paths without the user needing to `canonicalize` or manually prefix paths. If the path is already verbatim then this has no effect.

Fixes: #32689
2021-10-30 07:21:21 +00:00
bors
6d42707cde Auto merge of #90346 - ferrocene:pa-short-circuit, r=oli-obk
Replace some operators in libcore with their short-circuiting equivalents

In libcore there are a few occurrences of bitwise operators used in boolean expressions instead of their short-circuiting equivalents. This makes it harder to perform some kinds of source code analysis over libcore, for example [MC/DC] code coverage (a requirement in safety-critical environments).

This PR aims to remove as many bitwise operators in boolean expressions from libcore as possible, without any performance regression and without other changes. This means not all bitwise operators are removed, only the ones that don't have any difference with their short-circuiting counterparts. This already simplifies achieving MC/DC coverage, and the other functions can be changed in future PRs.

The PR is best reviewed commit-by-commit, and each commit has the resulting assembly in the message.

## Checked integer methods

These methods recently switched to bitwise operators in PRs https://github.com/rust-lang/rust/pull/89459 and https://github.com/rust-lang/rust/pull/89351. I confirmed bitwise operators are needed in most of the functions, except these two:

* `{integer}::checked_div` ([Godbolt link (nightly)](https://rust.godbolt.org/z/17efh5jPc))
* `{integer}::checked_rem` ([Godbolt link (nightly)](https://rust.godbolt.org/z/85qGWc94K))

`@tspiteri` already mentioned this was the case in https://github.com/rust-lang/rust/pull/89459#issuecomment-932728384, but opted to also switch those two to bitwise operators for consistency. As that makes MC/DC analysis harder this PR proposes switching those two back to short-circuiting operators.

## `{unsigned_ints}::carrying_add`

[Godbolt link (1.56.0)](https://rust.godbolt.org/z/vG9vx8x48)

In this instance replacing the `|` with `||` produces the exact same assembly when optimizations are enabled, so switching to the short-circuiting operator shouldn't have any impact.

## `{unsigned_ints}::borrowing_sub`

[Godbolt link (1.56.0)](https://rust.godbolt.org/z/asEfKaGE4)

In this instance replacing the `|` with `||` produces the exact same assembly when optimizations are enabled, so switching to the short-circuiting operator shouldn't have any impact.

## String UTF-8 validation

[Godbolt link (1.56.0)](https://rust.godbolt.org/z/a4rEbTvvx)

In this instance replacing the `|` with `||` produces practically the same assembly, with the two operands for the "or" swapped:

```asm
; Old
mov  rax, qword ptr [rdi + rdx + 8]
or   rax, qword ptr [rdi + rdx]
test rax, r9
je   .LBB0_7

; New
mov  rax, qword ptr [rdi + rdx]
or   rax, qword ptr [rdi + rdx + 8]
test rax, r8
je   .LBB0_7
```

[MC/DC]: https://en.wikipedia.org/wiki/Modified_condition/decision_coverage
2021-10-29 21:50:46 +00:00
Maybe Waffle
afaa54a99d Apply changes proposed in the review 2021-10-29 23:45:09 +03:00
Maybe Waffle
878ac10fe1 Use proper issue number for feature(const_slice_from_raw_parts) 2021-10-29 22:45:10 +03:00
Martin Kröning
42cab439f5 hermit: Implement Condvar::wait_timeout 2021-10-29 17:20:03 +02:00
bors
88a5a984fe Auto merge of #90380 - Mark-Simulacrum:revert-89558-query-stable-lint, r=lcnr
Revert "Add rustc lint, warning when iterating over hashmaps"

Fixes perf regressions introduced in https://github.com/rust-lang/rust/pull/90235 by temporarily reverting the relevant PR.
2021-10-29 04:55:51 +00:00
Matthias Krüger
ae244d8b78
Rollup merge of #90336 - mbartlett21:patch-4, r=Mark-Simulacrum
Remove extra lines in examples for `Duration::try_from_secs_*`

None of the other examples have extra lines below the `#![feature(...)]` statements, so I thought it appropriate that these examples shouldn't either.
2021-10-29 00:30:30 +02:00
Mark Rousskov
3215eeb99f
Revert "Add rustc lint, warning when iterating over hashmaps" 2021-10-28 11:01:42 -04:00
Maybe Waffle
991a296ce7 Make core::slice::from_raw_parts[_mut] const 2021-10-28 17:15:25 +03:00
Lukas Wirth
29a4e4a009
Fix incorrect doc link 2021-10-28 11:51:00 +02:00
bors
4e0d3973fa Auto merge of #90347 - matthiaskrgr:rollup-rp2ms7j, r=matthiaskrgr
Rollup of 5 pull requests

Successful merges:

 - #90239 (Consistent big O notation in map.rs)
 - #90267 (fix: inner attribute followed by outer attribute causing ICE)
 - #90288 (Add hint for people missing `TryFrom`, `TryInto`, `FromIterator` import pre-2021)
 - #90304 (Add regression test for #75961)
 - #90344 (Add tracking issue number to const_cstr_unchecked)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2021-10-27 18:42:13 +00:00
Matthias Krüger
623c3e144e
Rollup merge of #90344 - xfix:tracking-issue-const_cstr_unchecked, r=Mark-Simulacrum
Add tracking issue number to const_cstr_unchecked

Also created a tracking issue, see #90343.

I think it makes sense to stabilize this somewhat soon considering abuse of `transmute` to have this feature in constants, see https://crates.io/crates/cstr for an example. Code can be rewritten to use `mem::transmute` to work on stable.
2021-10-27 18:25:47 +02:00
Matthias Krüger
088dc91e0a
Rollup merge of #90239 - r00ster91:patch-1, r=fee1-dead
Consistent big O notation in map.rs

Follow up to #89216
2021-10-27 18:25:43 +02:00
r00ster91
b1b4c6cb00 Remove big O notation 2021-10-27 17:43:14 +02:00
bors
dd757b9e06 Auto merge of #90273 - nbdd0121:const, r=fee1-dead
Clean up special function const checks

Mark them as const and `#[rustc_do_not_const_check]` instead of hard-coding them in const-eval checks.

r? `@oli-obk`
`@rustbot` label A-const-eval T-compiler
2021-10-27 15:32:42 +00:00
Pietro Albini
68a4460b61
replace & with && in {integer}::checked_rem
Using short-circuit operators makes it easier to perform some kinds of
source code analysis, like MC/DC code coverage (a requirement in
safety-critical environments). The optimized x86 assembly is the same
between the old and new versions:

```
xor eax, eax
test esi, esi
je .LBB0_1
cmp edi, -2147483648
jne .LBB0_4
cmp esi, -1
jne .LBB0_4
ret
.LBB0_1:
ret
.LBB0_4:
mov eax, edi
cdq
idiv esi
mov eax, 1
ret
```
2021-10-27 17:01:05 +02:00
Pietro Albini
81130fe188
replace & with && in {integer}::checked_div
Using short-circuit operators makes it easier to perform some kinds of
source code analysis, like MC/DC code coverage (a requirement in
safety-critical environments). The optimized x86 assembly is the same
between the old and new versions:

```
xor eax, eax
test esi, esi
je .LBB0_1
cmp edi, -2147483648
jne .LBB0_4
cmp esi, -1
jne .LBB0_4
ret
.LBB0_1:
ret
.LBB0_4:
mov eax, edi
cdq
idiv esi
mov edx, eax
mov eax, 1
ret
```
2021-10-27 17:00:57 +02:00
Pietro Albini
a5a8bb0125
replace | with || in string validation
Using short-circuiting operators makes it easier to perform some kinds
of source code analysis, like MC/DC code coverage (a requirement in
safety-critical environments). The optimized x86_64 assembly is
equivalent between the old and new versions.

Old assembly of that condition:

```
mov  rax, qword ptr [rdi + rdx + 8]
or   rax, qword ptr [rdi + rdx]
test rax, r9
je   .LBB0_7
```

New assembly of that condition:

```
mov  rax, qword ptr [rdi + rdx]
or   rax, qword ptr [rdi + rdx + 8]
test rax, r8
je   .LBB0_7
```
2021-10-27 17:00:49 +02:00
Pietro Albini
9fb66969e3
replace | with || in {unsigned_int}::borrowing_sub
Using short-circuiting operators makes it easier to perform some kinds
of source code analysis, like MC/DC code coverage (a requirement in
safety-critical environments). The optimized x86_64 assembly is the same
between the old and new versions:

```
mov eax, edi
add dl, -1
sbb eax, esi
setb dl
ret
```
2021-10-27 17:00:46 +02:00
Pietro Albini
5913ef6660
replace | with || in {unsigned_int}::carrying_add
Using short-circuiting operators makes it easier to perform some kinds
of source code analysis, like MC/DC code coverage (a requirement in
safety-critical environments). The optimized x86_64 assembly is the same
between the old and new versions:

```
mov eax, edi
add dl, -1
adc eax, esi
setb dl
ret
```
2021-10-27 17:00:36 +02:00
Konrad Borowski
50ca08c5f5 Add tracking issue number to const_cstr_unchecked 2021-10-27 15:18:25 +02:00