3425 Commits

Author SHA1 Message Date
Jacob Pratt
f1a0dfd145 Rollup merge of #138217 - theemathas:cow_is_owned_borrowed_associated, r=dtolnay
Turn `Cow::is_borrowed,is_owned` into associated functions.

This is done because `Cow` implements `Deref`. Therefore, to avoid conflicts with an inner type having a method of the same name, we use an associated method, like `Box::into_raw`.

Tracking issue: #65143
2025-10-30 02:43:41 -04:00
Josh Stone
f25ca45fd1 Update CURRENT_RUSTC_VERSION post-bump
(cherry picked from commit 813072186c1c305ea62c7270f1514dfab5166af2)
2025-10-28 13:22:00 -07:00
bors
a8664a1534 Auto merge of #145665 - GrigorenkoPV:144707, r=tgross35
Don't require `T: RefUnwindSafe` for `vec::IntoIter<T>: UnwindSafe`

Closes rust-lang/rust#144707

r? t-libs-api
2025-10-26 06:35:34 +00:00
ltdk
ebd5bea84f Revert inference failure from Deref/Borrow constification 2025-10-23 08:51:27 -04:00
Jacob Pratt
5d55418a7d Rollup merge of #148011 - clarfonthey:const-convert-revert, r=oli-obk
Revert constification of `AsRef for Cow` due to inference failure

Reported issue: rust-lang/rust#147964
Original PR: rust-lang/rust#145279
`const AsRef` tracking issue: rust-lang/rust#143773

Should have additional crater run to verify this fixes the issue.
2025-10-23 01:22:06 -04:00
Jacob Pratt
4491efbc82 Rollup merge of #134316 - zachs18:string_replace_in_place_rebase, r=joshtriplett
Add `String::replace_first` and `String::replace_last`

Rebase of #97977 (cc `@WilliamVenner)`

> Convenience methods that use `match_indices` and `replace_range` to efficiently replace a substring in a string without reallocating, if capacity (and the implementation of `Vec::splice`) allows.

The intra-doc link to `str::replacen` is a direct url-based link to `str::replacen` in `std`'s docs to work around #98941. This means that when building only `alloc`'s docs (and not `std`'s), it will be a broken link. There is precedent for this e.g. in [`core::hint::spin_loop`](https://doc.rust-lang.org/nightly/src/core/hint.rs.html#214) which links to `std:🧵:yield_now` using a [url-based link](https://github.com/rust-lang/rust/blob/master/library/core/src/hint.rs#L265) and thus is a dead link when only building `core`'s docs.

ACP: https://github.com/rust-lang/libs-team/issues/506
2025-10-23 01:22:05 -04:00
ltdk
cb7fb35ad1 Revert inference failure from AsRef constification 2025-10-22 20:31:43 -04:00
bors
4d94478977 Auto merge of #147826 - Muscraft:update-typos, r=Noratrieb
Update typos

I saw that `typos` was a few versions out of date and figured it would be a good idea to update it. Upgrading to `1.38.1` adds the [July](https://github.com/crate-ci/typos/issues/1331), [August](https://github.com/crate-ci/typos/issues/1345), and [September](https://github.com/crate-ci/typos/issues/1370) dictionary updates. As part of this change, I also sorted the configuration file.
2025-10-22 13:11:47 +00:00
Matthias Krüger
664e3b0741 Rollup merge of #141445 - yotamofek:pr/library/from-iter-char-string, r=the8472,joshtriplett
Add `FromIterator` impls for `ascii::Char`s to `String`s

Wanted to `collect` ascii chars into a `String` while working on #141369 , and was surprised these impls don't exist. Seems to me to be simply oversight.

BTW, I only added `impl FromIterator<ascii::Char> for Cow<'_, str>`, without a corresponding `FromIterator<&Char>` impl, because there's no existing impl for `FromIterator<&char>`, but that might be oversight too.

cc #110998
2025-10-22 07:12:08 +02:00
William Venner
8ed7fca74c Add String::replace_first and String::replace_last
Rebased and modified by zachs18.

Co-authored-by: zachs18 <8355914+zachs18@users.noreply.github.com>
2025-10-21 16:11:55 -05:00
Pavel Grigorenko
144707a4b1 Don't require T: RefUnwindSafe for vec::IntoIter<T>: UnwindSafe 2025-10-21 21:18:09 +03:00
Scott Schafer
12f6b9697f chore: Update typos to 1.38.1 2025-10-20 12:20:15 -06:00
Matthias Krüger
003feb45d2 Rollup merge of #147814 - hkBst:btree-4, r=joboet
btree: some cleanup with less unsafe
2025-10-20 18:21:34 +02:00
bors
33cab8c4c7 Auto merge of #147847 - WaffleLapkin:hide_box_new, r=joboet
hide `alloc::alloc::box_new` in docs

It's an internal function which isn't supposed to be used outside standard library / `vec!`.
2025-10-19 06:41:36 +00:00
Waffle Lapkin
d17dbc7e36 hide alloc::alloc::box_new in docs
It's an internal function which isn't supposed to be used outside
standard library / `vec!`.
2025-10-18 17:08:49 +02:00
Matthias Krüger
b46db5c6b7 Rollup merge of #140153 - thaliaarchi:encode-wide-debug, r=ChrisDenton
Implement `Debug` for `EncodeWide`

Since `std::os::windows::ffi::EncodeWide` was reexported from `std::sys_common::wtf8::EncodeWide`, which has `#![allow(missing_debug_implementations)]` in the parent module, it did not implement `Debug`. When it was moved to `core`, a placeholder impl was added; fill it in.

This becomes insta-stable.

r? libs-api
2025-10-18 08:08:35 +02:00
Marijn Schouten
7c95768dbd btree: some cleanup with less unsafe 2025-10-17 16:43:01 +00:00
h3nryc0ding
1de9b49f24 remove duplicate inline macro 2025-10-15 18:48:25 +02:00
Matthias Krüger
f8b65f7bc7 Rollup merge of #147526 - bjorn3:alloc_shim_weak_shape, r=petrochenkov,RalfJung
Move computation of allocator shim contents to cg_ssa

In the future this should make it easier to use weak symbols for the allocator shim on platforms that properly support weak symbols. And it would allow reusing the allocator shim code for handling default implementations of the upcoming externally implementable items feature on platforms that don't properly support weak symbols.

In addition to make this possible, the alloc error handler is now handled in a way such that it is possible to avoid using the allocator shim when liballoc is compiled without `no_global_oom_handling` if you use `#[alloc_error_handler]`. Previously this was only possible if you avoided liballoc entirely or compiled it with `no_global_oom_handling`. You still need to avoid libstd and to define the symbol that indicates that avoiding the allocator shim is unstable.
2025-10-14 19:47:29 +02:00
Matthias Krüger
b96bd11c54 Rollup merge of #147246 - Kivooeo:btree-map-split-off-doc, r=Mark-Simulacrum
Explain not existed key in BTreeMap::split_off

Fixes https://github.com/rust-lang/rust/issues/147174

r? libs
2025-10-12 10:13:15 +02:00
bors
be0ade2b60 Auto merge of #147124 - a1phyr:improve_finish_grow, r=Mark-Simulacrum
Move more code to `RawVec::finish_grow`

This move a branch and more code into the cold method `finish_grow`, which means that less code is inlined at each `try_reserve` site. Additionally, this reduces the amount of parameters, so they can all be passed by registers.
2025-10-11 19:40:52 +00:00
bjorn3
116f4ae171 Support #[alloc_error_handler] without the allocator shim
Currently it is possible to avoid linking the allocator shim when
__rust_no_alloc_shim_is_unstable_v2 is defined when linking rlibs
directly as some build systems need. However this requires liballoc to
be compiled with --cfg no_global_oom_handling, which places huge
restrictions on what functions you can call and makes it impossible to
use libstd. Or alternatively you have to define
__rust_alloc_error_handler and (when using libstd)
__rust_alloc_error_handler_should_panic
using #[rustc_std_internal_symbol]. With this commit you can either use
libstd and define __rust_alloc_error_handler_should_panic or not use
libstd and use #[alloc_error_handler] instead. Both options are still
unstable though.

Eventually the alloc_error_handler may either be removed entirely
(though the PR for that has been stale for years now) or we may start
using weak symbols for it instead. For the latter case this commit is a
prerequisite anyway.
2025-10-10 13:04:53 +00:00
Matthias Krüger
47bd38c2f1 Rollup merge of #145685 - Qelxiros:cell_get_cloned, r=programmerjake,tgross35
add CloneFromCell and Cell::get_cloned

Tracking issue: rust-lang/rust#145329
2025-10-04 17:11:08 +02:00
Jeremy Smart
08b4641f50 add CloneFromCell and Cell::get_cloned 2025-10-01 18:47:09 -04:00
Kivooeo
8fcb7e18c1 extended doc comment 2025-10-01 21:22:53 +00:00
edwloef
8dfea22c97 implement Box::take 2025-10-01 17:21:12 +02:00
Matthias Krüger
f7c7e34ae4 Rollup merge of #146886 - taiki-e:rc-inner-align, r=Mark-Simulacrum
Add repr(align(2)) to RcInner and ArcInner

`Rc` currently assumes that `RcInner` has at least 2-byte alignment, but on AVR, `usize` has 1-byte alignment (this is because the AVR has 1-byte register sizes, so having 2-byte alignment is generally useless), breaking this assumption.

9f32ccf35f/library/alloc/src/rc.rs (L3005-L3008)

This PR adds `repr(align(2))` to force `RcInner` to always have at least 2-byte alignment.

Note that `ArcInner` doesn't need `repr(align(2))` because atomic types have the alignment same as its size. This PR adds a comment about this.
2025-09-30 20:46:45 +02:00
Taiki Endo
9e79fac035 Add repr(align(2)) to RcInner and ArcInner 2025-09-30 22:39:10 +09:00
Yotam Ofek
68a7c25078 Use Iterator::eq and (dogfood) eq_by in compiler and library 2025-09-29 08:08:05 +03:00
Benoît du Garreau
e52fe65d9a Move more code to RawVec::finish_grow
This move a branch and more code into the cold method `finish_grow`,
which means that less code is inlined at each `try_reserve` site.
Additionally, this reduces the amount of parameters, so they can all be
passed by registers.
2025-09-28 15:19:19 +02:00
bors
c7f6aa2869 Auto merge of #147042 - Noratrieb:untrack-caller-vec, r=tgross35
Remove most `#[track_caller]` from allocating Vec methods

They cause significant binary size overhead while contributing little value.

closes rust-lang/rust#146963, see that issue for more details.
2025-09-28 03:23:45 +00:00
Guillaume Gomez
254a2139f6 Remove cfg(bootstrap) for doc_cfg feature following #141925 2025-09-27 11:29:49 +02:00
Guillaume Gomez
a7ed9bf6c7 fmt 2025-09-27 11:29:49 +02:00
Guillaume Gomez
fccba2c341 Remove doc_cfg_hide feature 2025-09-27 11:29:49 +02:00
Guillaume Gomez
63aefe0737 Strenghten checks for doc(auto_cfg(show/hide)) attributes 2025-09-27 11:29:48 +02:00
Guillaume Gomez
7c00bccd3b Implement RFC 3631 2025-09-27 11:29:48 +02:00
Mark Rousskov
4e9716fbc5 Update CURRENT_RUSTC_VERSION post-bump 2025-09-26 18:41:32 -04:00
bors
7ac0330c6d Auto merge of #147037 - matthiaskrgr:rollup-xtgqzuu, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#116882 (rustdoc: hide `#[repr]` if it isn't part of the public ABI)
 - rust-lang/rust#135771 ([rustdoc] Add support for associated items in "jump to def" feature)
 - rust-lang/rust#141032 (avoid violating `slice::from_raw_parts` safety contract in `Vec::extract_if`)
 - rust-lang/rust#142401 (Add proper name mangling for pattern types)
 - rust-lang/rust#146293 (feat: non-panicking `Vec::try_remove`)
 - rust-lang/rust#146859 (BTreeMap: Don't leak allocators when initializing nodes)
 - rust-lang/rust#146924 (Add doc for `NonZero*` const creation)
 - rust-lang/rust#146933 (Make `render_example_with_highlighting` return an `impl fmt::Display`)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-09-25 20:35:49 +00:00
Noratrieb
9316d4508c Remove most #[track_caller] from allocating Vec methods
They cause significant binary size overhead while contributing little
value.

Also removes them from the wrapping String methods that do not panic.
2025-09-25 20:52:03 +02:00
Matthias Krüger
83cf8f9860 Rollup merge of #146859 - cammeresi:btree-alloc-20250920, r=joboet
BTreeMap: Don't leak allocators when initializing nodes

Memory was allocated via `Box::leak` and thence intended to be tracked and deallocated manually, but the allocator was also leaked, not tracked, and never dropped.  Now it is dropped immediately.

According to my reading of the `Allocator` trait, if a copy of the allocator remains live, then its allocations must remain live.  Since the B-tree has a copy of the allocator that will only be dropped after the nodes, it's safe to not store the allocator in each node (which would be a much more intrusive change).

Fixes: rust-lang/rust#106203
2025-09-25 18:15:09 +02:00
Matthias Krüger
fea9196e52 Rollup merge of #146293 - BenjaminBrienen:try_remove, r=joboet
feat: non-panicking `Vec::try_remove`

`if index < my_vector.len() { Some(my_vector.remove(index)) } else { None }` is annoying to write and non-panicking functions are broadly useful.

APC: https://github.com/rust-lang/libs-team/issues/649

Tracking issue: https://github.com/rust-lang/rust/issues/146954
2025-09-25 18:15:09 +02:00
Matthias Krüger
e3f7626732 Rollup merge of #141032 - petrosagg:extract-if-ub, r=joboet
avoid violating `slice::from_raw_parts` safety contract in `Vec::extract_if`

The implementation of the `Vec::extract_if` iterator violates the safety contract adverized by `slice::from_raw_parts` by always constructing a mutable slice for the entire length of the vector even though that span of memory can contain holes from items already drained. The safety contract of `slice::from_raw_parts` requires that all elements must be properly
initialized.

As an example we can look at the following code:

```rust
let mut v = vec![Box::new(0u64), Box::new(1u64)];
for item in v.extract_if(.., |x| **x == 0) {
    drop(item);
}
```

In the second iteration a `&mut [Box<u64>]` slice of length 2 will be constructed. The first slot of the slice contains the bitpattern of an already deallocated box, which is invalid.

This fixes the issue by only creating references to valid items and using pointer manipulation for the rest. I have also taken the liberty to remove the big `unsafe` blocks in place of targetted ones with a SAFETY comment. The approach closely mirrors the implementation of `Vec::retain_mut`.

**Note to reviewers:** The diff is easier to follow with whitespace hidden.
2025-09-25 18:15:07 +02:00
bors
6f34f4ee07 Auto merge of #147019 - Zalathar:rollup-boxzbmo, r=Zalathar
Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#145067 (RawVecInner: add missing `unsafe` to unsafe fns)
 - rust-lang/rust#145277 (Do not materialise X in [X; 0] when X is unsizing a const)
 - rust-lang/rust#145973 (Add `std` support for `armv7a-vex-v5`)
 - rust-lang/rust#146667 (Add an attribute to check the number of lanes in a SIMD vector after monomorphization)
 - rust-lang/rust#146735 (unstably constify float mul_add methods)
 - rust-lang/rust#146737 (f16_f128: enable some more tests in Miri)
 - rust-lang/rust#146766 (Add attributes for #[global_allocator] functions)
 - rust-lang/rust#146905 (llvm: update remarks support on LLVM 22)
 - rust-lang/rust#146982 (Remove erroneous normalization step in `tests/run-make/linker-warning`)
 - rust-lang/rust#147005 (Small string formatting cleanup)
 - rust-lang/rust#147007 (Explicitly note `&[SocketAddr]` impl of `ToSocketAddrs`)
 - rust-lang/rust#147008 (bootstrap.py: Respect build.jobs while building bootstrap tool)
 - rust-lang/rust#147013 (rustdoc: Fix documentation for `--doctest-build-arg`)
 - rust-lang/rust#147015 (Use `LLVMDisposeTargetMachine`)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-09-25 14:03:21 +00:00
Stuart Cook
2acd80cfa9 Rollup merge of #145067 - btj:patch-3, r=tgross35
RawVecInner: add missing `unsafe` to unsafe fns

Some (module-private) functions in `library/alloc/src/raw_vec/mod.rs` are unsafe (i.e. may cause UB when called from safe code) but are not marked `unsafe`. Specifically:
- `RawVecInner::grow_exact` causes UB if called with `len` and `additional` arguments such that `len + additional` is less than the current capacity. Indeed, in that case it calls [Allocator::grow](https://doc.rust-lang.org/std/alloc/trait.Allocator.html#method.grow) with a `new_layout` that is smaller than `old_layout`, which violates a safety precondition.
- The RawVecInner methods for resizing the buffer cause UB if called with an `elem_layout` different from the one used to initially allocate the buffer, because in that case `Allocator::grow` or `Allocator::shrink` are called with an `old_layout` that does not *fit* the allocated block, which violates a safety precondition.
- `RawVecInner::current_memory` might cause UB if called with an `elem_layout` different from the one used to initially allocate the buffer, because the `unchecked_mul` might overflow.
- Furthermore, these methods cause UB if called with an `elem_layout` where the size is not a multiple of the alignment. This is because `Layout::repeat` is used (in `layout_array`) to compute the allocation's layout when allocating, which includes padding to ensure alignment of array elements, but simple multiplication is used (in `current_memory`) to compute the old allocation's layout when resizing or deallocating, which would cause the layout used to resize or deallocate to not *fit* the allocated block, which violates a safety precondition.

I discovered these issues while performing formal verification of `library/alloc/src/raw_vec/mod.rs` per [Challenge 19](https://model-checking.github.io/verify-rust-std/challenges/0019-rawvec.html) of the [AWS Rust Standard Library Verification Contest](https://aws.amazon.com/blogs/opensource/verify-the-safety-of-the-rust-standard-library/).
2025-09-25 20:31:51 +10:00
Sidney Cammeresi
137d4eaf12 BTreeMap: Don't leak allocators when initializing nodes
Memory was allocated via `Box::leak` and thence intended to be tracked
and deallocated manually, but the allocator was also leaked, not
tracked, and never dropped.  Now it is dropped immediately.

According to my reading of the `Allocator` trait, if a copy of the
allocator remains live, then its allocations must remain live.  Since
the B-tree has a copy of the allocator that will only be dropped after
the nodes, it's safe to not store the allocator in each node (which
would be a much more intrusive change).
2025-09-24 18:08:32 -07:00
BenjaminBrienen
56734495e2 feature: Implement vec_try_remove
Vec::try_remove is a non-panicking version of Vec::remove
2025-09-24 15:29:09 +02:00
joboet
a875f7779e alloc: simplify Default for Box<CStr> and Rc<CStr> 2025-09-24 14:13:34 +02:00
bors
4056082360 Auto merge of #146317 - saethlin:panic=immediate-abort, r=nnethercote
Add panic=immediate-abort

MCP: https://github.com/rust-lang/compiler-team/issues/909

This adds a new panic strategy, `-Cpanic=immediate-abort`. This panic strategy essentially just codifies use of `-Zbuild-std-features=panic_immediate_abort`. This PR is intended to just set up infrastructure, and while it will change how the compiler is invoked for users of the feature, there should be no other impacts.

In many parts of the compiler, `PanicStrategy::ImmediateAbort` behaves just like `PanicStrategy::Abort`, because actually most parts of the compiler just mean to ask "can this unwind?" so I've added a helper function so we can say `sess.panic_strategy().unwinds()`.

The panic and unwind strategies have some level of compatibility, which mostly means that we can pre-compile the sysroot with unwinding panics then the sysroot can be linked with aborting panics later. The immediate-abort strategy is all-or-nothing, enforced by `compiler/rustc_metadata/src/dependency_format.rs` and this is tested for in `tests/ui/panic-runtime/`. We could _technically_ be more compatible with the other panic strategies, but immediately-aborting panics primarily exist for users who want to eliminate all the code size responsible for the panic runtime. I'm open to other use cases if people want to present them, but not right now. This PR is already large.

`-Cpanic=immediate-abort` sets both `cfg(panic = "immediate-abort")` _and_ `cfg(panic = "abort")`. bjorn3 pointed out that people may be checking for the abort cfg to ask if panics will unwind, and also the sysroot feature this is replacing used to require `-Cpanic=abort` so this seems like a good back-compat step. At least for the moment. Unclear if this is a good idea indefinitely. I can imagine this being confusing.

The changes to the standard library attributes are purely mechanical. Apart from that, I removed an `unsafe` we haven't needed for a while since the `abort` intrinsic became safe, and I've added a helpful diagnostic for people trying to use the old feature.

To test that `-Cpanic=immediate-abort` conflicts with other panic strategies, I've beefed up the core-stubs infrastructure a bit. There is now a separate attribute to set flags on it.

I've added a test that this produces the desired codegen, called `tests/run-make-cargo/panic-immediate-abort-codegen/` and also a separate run-make-cargo test that checks that we can build a binary.
2025-09-23 06:37:03 +00:00
Guillaume Gomez
9814d08545 Rollup merge of #146887 - taiki-e:rc-doc-feature, r=joboet
Remove unused #![feature(get_mut_unchecked)] in Rc and Arc examples

https://github.com/rust-lang/rust/pull/93109 removed the use of APIs enabled by this feature in these examples, but the `#![feature]` attributes ware not removed.
2025-09-22 17:17:46 +02:00
Taiki Endo
823337a4ad Remove unused #![feature(get_mut_unchecked)] in Rc and Arc examples 2025-09-22 22:15:25 +09:00