3204 Commits

Author SHA1 Message Date
bors
427288b3ce Auto merge of #140188 - nnethercote:streamline-format-macro, r=cuviper
Streamline the `format` macro.

Removing the unnecessary local variable speeds up compilation a little.

r? `@cuviper`
2025-04-30 04:04:21 +00:00
DaniPopes
f07cc409d3
Rename sub_ptr to offset_from_unsigned in docs 2025-04-28 13:58:27 +02:00
bors
a932eb36f8 Auto merge of #123239 - Urgau:dangerous_implicit_autorefs, r=jdonszelmann,traviscross
Implement a lint for implicit autoref of raw pointer dereference - take 2

*[t-lang nomination comment](https://github.com/rust-lang/rust/pull/123239#issuecomment-2727551097)*

This PR aims at implementing a lint for implicit autoref of raw pointer dereference, it is based on #103735 with suggestion and improvements from https://github.com/rust-lang/rust/pull/103735#issuecomment-1370420305.

The goal is to catch cases like this, where the user probably doesn't realise it just created a reference.

```rust
pub struct Test {
    data: [u8],
}

pub fn test_len(t: *const Test) -> usize {
    unsafe { (*t).data.len() }  // this calls <[T]>::len(&self)
}
```

Since #103735 already went 2 times through T-lang, where they T-lang ended-up asking for a more restricted version (which is what this PR does), I would prefer this PR to be reviewed first before re-nominating it for T-lang.

----

Compared to the PR it is as based on, this PR adds 3 restrictions on the outer most expression, which must either be:
   1. A deref followed by any non-deref place projection (that intermediate deref will typically be auto-inserted)
   2. A method call annotated with `#[rustc_no_implicit_refs]`.
   3. A deref followed by a `addr_of!` or `addr_of_mut!`. See bottom of post for details.

There are several points that are not 100% clear to me when implementing the modifications:
 - ~~"4. Any number of automatically inserted deref/derefmut calls." I as never able to trigger this. Am I missing something?~~ Fixed
 - Are "index" and "field" enough?

----

cc `@JakobDegen` `@WaffleLapkin`
r? `@RalfJung`

try-job: dist-various-1
try-job: dist-various-2
2025-04-28 08:25:23 +00:00
bors
0134651fb8 Auto merge of #136316 - GrigorenkoPV:generic_atomic, r=Mark-Simulacrum
Create `Atomic<T>` type alias (rebase)

Rebase of #130543.

Additional changes:
- Switch from `allow` to `expect` for `private_bounds` on `AtomicPrimitive`
- Unhide `AtomicPrimitive::AtomicInner` from docs, because rustdoc shows the definition `pub type Atomic<T> = <T as AtomicPrimitive>::AtomicInner;` and generated links for it.
  - `NonZero` did not have this issue, because they kept the new alias private before the direction was changed.
- Use `Atomic<_>` in more places, including inside `Once`'s `Futex`. This is possible thanks to https://github.com/rust-lang/rust-clippy/pull/14125

The rest will either get moved back to #130543 or #130543 will be closed in favor of this instead.

---

* ACP: https://github.com/rust-lang/libs-team/issues/443#event-14293381061
* Tracking issue: #130539
2025-04-28 05:12:59 +00:00
Chris Denton
55f93265f0
Rollup merge of #138939 - SabrinaJewson:arc-is-unique, r=tgross35
Add `Arc::is_unique`

Adds

```rs
impl<T> Arc<T> {
    pub fn is_unique(this: &Self) -> bool;
}
```

Tracking issue: #138938
ACP: https://github.com/rust-lang/libs-team/issues/560
2025-04-28 01:58:48 +00:00
Nicholas Nethercote
bc8df506f6 Streamline the format macro.
Removing the unnecessary local variable speeds up compilation a little.
2025-04-28 06:56:13 +10:00
SabrinaJewson
2ab403441f
Add Arc::is_unique 2025-04-27 14:55:46 +01:00
Matthias Krüger
bd3af53489
Rollup merge of #137714 - DiuDiu777:doc-fix, r=tgross35
Update safety documentation for `CString::from_ptr` and `str::from_boxed_utf8_unchecked`

## PR Description​
This PR addresses missing safety documentation for two APIs:

​**1. alloc::ffi::CStr::from_raw**​

- ​`Alias`: The pointer ​must not be aliased​ (accessed via other pointers) during the reconstructed CString's lifetime.
- `Owning`: Calling this function twice on the same pointer and creating two objects with overlapping lifetimes, introduces two alive owners of the same memory. This may result in a double-free.
- `Dangling`: The prior documentation required the pointer to originate from CString::into_raw, but this constraint is incomplete. A validly sourced pointer can also cause undefined behavior (UB) if it becomes dangling. A simple Poc for this situation:
```
use std::ffi::CString;
use std::os::raw::c_char;

fn create_dangling() -> *mut c_char {
    let local_ptr: *mut c_char = {
        let valid_data = CString::new("valid").unwrap();
        valid_data.into_raw()
    };

    unsafe {
        let _x = CString::from_raw(local_ptr);
    }
    local_ptr
}

fn main() {
    let dangling = create_dangling();
    unsafe {let _y = CString::from_raw(dangling);} // Cause UB!
}
```

​**2. alloc::str::from_boxed_utf8_unchecked**​

- `ValidStr`: Bytes must contain a ​valid UTF-8 sequence.
2025-04-27 11:54:57 +02:00
Matthias Krüger
9630242e5e
Rollup merge of #137439 - clarfonthey:c-str-module, r=tgross35
Stabilise `std::ffi::c_str`

This finished FCP in #112134 but never actually got a stabilisation PR. Since the FCP in #120048 recently passed to add the `os_str` module, it would be nice to also merge this too, to ensure that both get added in the next version.

Note: The added stability attributes which *somehow* were able to be omitted before (rustc bug?) were added based on the fact that they were added in 302551388b1942bb4216bb5a15d9d55cee3643a8, which ended up in 1.85.0.

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

r? libs-api
2025-04-27 11:54:56 +02:00
LemonJ
bfdd947bbd fix missing doc in CString::from_raw and str::from_boxed_utf8_unchecked 2025-04-27 12:49:37 +08:00
Christopher Durham
4d93f60568 use generic Atomic type where possible
in core/alloc/std only for now, and ignoring test files

Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-04-27 02:18:08 +03:00
Matthias Krüger
986750ded4
Rollup merge of #140232 - nnethercote:rm-unnecessary-clones, r=SparrowLii
Remove unnecessary clones

r? `@SparrowLii`
2025-04-24 08:13:01 +02:00
Nicholas Nethercote
055a27da2a Remove some unnecessary clones.
I found these by grepping for `&[a-z_\.]*\.clone()`, i.e. expressions
like `&a.b.clone()`, which are sometimes unnecessary clones, and also
looking at clones nearby to cases like that.
2025-04-24 11:12:34 +10:00
Trevor Gross
e800bf7df4 Update compiler_builtins to 0.1.156
Includes the following changes:

* Provide `abort` on AVR [1]

[1]: https://github.com/rust-lang/compiler-builtins/pull/830
2025-04-22 05:46:51 +00:00
Chris Denton
1cb9a0d450
Rollup merge of #140118 - tamird:cstr-cleanup, r=joboet
{B,C}Str: minor cleanup

(hopefully) uncontroversial bits extracted from #139994.
2025-04-21 18:53:21 +00:00
Urgau
40ba47d3b0 Implement lint against dangerous implicit autorefs 2025-04-20 11:36:28 +02:00
Urgau
1632f624fb Add #[rustc_no_implicit_autorefs] and apply it to std methods 2025-04-20 11:36:22 +02:00
Chris Denton
3c5aef3625
Rollup merge of #139922 - jnqnfe:cstr_doc_fix, r=jhpratt
fix incorrect type in cstr `to_string_lossy()` docs

Restoring what it said prior to commit 67065fe in which it was changed incorrectly with no supporting explanation.

Closes #139835.
2025-04-19 14:01:39 +00:00
Tamir Duberstein
f727b3622b
Invert <CString as Deref>::deref and CString::as_c_str
This is consistent with the style of `ByteString`.
2025-04-18 10:31:12 -04:00
Trevor Gross
7d6f41b08e Update compiler-builtins to 0.1.155
Includes the following changes:

* Replace `#[naked]` with `#[unsafe(naked)]` [1] [2]
* Replace `bl!` with `asm_sym` [3]

[1]: https://github.com/rust-lang/compiler-builtins/pull/817
[2]: https://github.com/rust-lang/compiler-builtins/pull/821
[3]: https://github.com/rust-lang/compiler-builtins/pull/820
2025-04-17 19:08:27 +00:00
Lyndon Brown
2f0ba67919 fix incorrect type in cstr to_string_lossy() docs
Restoring what it said prior to commit 67065fe in which it was changed
incorrectly with no supporting explanation.

Closes #139835.
2025-04-16 14:42:11 +01:00
GenYuLi
1baa62e884 Fix typo in documentation
Correct the misspelling of "indentifier" to "identifier" in `library/alloc/src/fmt.rs`.
2025-04-12 22:26:38 +08:00
Stuart Cook
c8acc23d1d
Rollup merge of #139600 - tgross35:update-builtins, r=tgross35
Update `compiler-builtins` to 0.1.153

Includes the following changes:

* Avoid OOB access in `memcpy` and `memmove` [1]
* Enable intrinsics on AVR [2]
* `libm` updates to avoid using `core::arch` vector intrinsics [3]
* Re-enable `f16` on aarch64 without Neon [4]

[1]: https://github.com/rust-lang/compiler-builtins/pull/799
[2]: https://github.com/rust-lang/compiler-builtins/pull/791
[3]: https://github.com/rust-lang/compiler-builtins/pull/814
[4]: https://github.com/rust-lang/compiler-builtins/pull/809
2025-04-11 13:31:49 +10:00
Trevor Gross
b435def33c Update compiler-builtins to 0.1.153
Includes the following changes:

* Avoid OOB access in `memcpy` and `memmove` [1]
* Enable intrinsics on AVR [2]
* `libm` updates to avoid using `core::arch` vector intrinsics [3]
* Re-enable `f16` on aarch64 without Neon [4]

[1]: https://github.com/rust-lang/compiler-builtins/pull/799
[2]: https://github.com/rust-lang/compiler-builtins/pull/791
[3]: https://github.com/rust-lang/compiler-builtins/pull/814
[4]: https://github.com/rust-lang/compiler-builtins/pull/809
2025-04-10 17:40:15 +00:00
bors
6813f955a6 Auto merge of #139279 - BoxyUwU:bump-boostrap, r=jieyouxu
Bump boostrap compiler to new beta

try-job: `*msvc*`
2025-04-10 00:43:25 +00:00
Boxy
acf678bd4c intra-doc link 2025-04-09 12:29:59 +01:00
Boxy
c93005ee65 update cfgs 2025-04-09 12:29:59 +01:00
Boxy
a6c2ec04b4 replace version placeholder 2025-04-09 12:29:59 +01:00
lincot
09d5bcf1ad
Speed up String::push and String::insert
Improve performance of `String` methods by avoiding unnecessary memcpy
for the character bytes, with added codegen check to ensure compliance.
2025-04-09 13:06:10 +03:00
Jonathan Gruner
4aab8e88e4 document panic behavior of Vec::resize and Vec::resize_with 2025-04-08 00:00:38 +02:00
bors
25a615bf82 Auto merge of #138951 - jwnrt:alloc-raw-vec-strict-prov, r=Noratrieb
Replace last `usize` -> `ptr` transmute in `alloc` with strict provenance API

This replaces the `usize -> ptr` transmute in `RawVecInner::new_in` with a strict provenance API (`NonNull::without_provenance`).

The API is changed to take an `Alignment` which encodes the non-null constraint needed for `Unique` and allows us to do the construction safely.

Two internal-only APIs were added to let us avoid UB-checking in this hot code: `Layout::alignment` to get the `Alignment` type directly rather than as a `usize`, and `Unique::from_non_null` to create `Unique` in const context without a transmute.
2025-04-06 23:07:48 +00:00
bjorn3
fde54c2c03 Fix testing with randomized layouts enabled 2025-04-03 15:30:01 +00:00
Jacob Pratt
a99b5339f4
Rollup merge of #139141 - mejrs:on_unimpl, r=Noratrieb
Switch some rustc_on_unimplemented uses to diagnostic::on_unimplemented

The use on the SliceIndex impl appears unreachable, there is no mention of "vector indices" in any test output and I could not get it to show up in error messages.
2025-03-30 17:59:29 -04:00
mejrs
b1bc7255bb Delete unreacheable #[rustc_on_unimplemented] 2025-03-30 15:25:27 +02:00
bors
1799887bb2 Auto merge of #133572 - frank-king:feature/unique_arc, r=Amanieu
Implement `alloc::sync::UniqueArc`

This implements the `alloc::sync::UniqueArc` part of #112566.
2025-03-29 20:05:06 +00:00
James Wainwright
aadfd810f6 Swap usize -> ptr transmute for strict_pov API
Removes some unsafety and reduces the number of `usize` -> `ptr`
transmutes which might be helpful for CHERI-like targets in the future.
2025-03-26 21:41:11 +00:00
James Wainwright
78e9621390 Pass Alignment for RawVecInner::new_in
Encodes the safety constraint that `Unique`'s pointer must be non-zero
into the API.
2025-03-26 21:41:11 +00:00
Matthias Krüger
0c25157784
Rollup merge of #138800 - RalfJung:const_box, r=oli-obk
remove remnants of const_box feature

This feature requires major design work, and the few methods it gates currently aren't actually useful. Let's reset to a clean slate so when a design materializes, we can start from scratch.

Closes https://github.com/rust-lang/rust/issues/92521 by removing the feature it tracks.

r? ````@oli-obk````
2025-03-24 20:40:07 +01:00
Trevor Gross
95181ae170 Update compiler-builtins to 0.1.152
Includes the following changes related to unordered atomics:

* Remove element_unordered_atomic intrinsics [1]
* Remove use of `atomic_load_unordered` and undefined behaviour [2]

There are a handful of other small changes, but nothing else
user-visible.

[1]: https://github.com/rust-lang/compiler-builtins/pull/789
[2]: https://github.com/rust-lang/compiler-builtins/pull/790
2025-03-24 00:29:21 +00:00
Frank King
eb094ed7b4 Remove PartialEq::ne for UniqueArc 2025-03-22 15:14:49 +08:00
Frank King
b542a2bd4b Add more APIs for UniqueArc 2025-03-22 15:14:49 +08:00
Frank King
9e5cca5049 Make UniqueArc invariant for soundness 2025-03-22 15:14:49 +08:00
Frank King
5016467a23 Implement UniqueArc 2025-03-22 15:14:49 +08:00
Ralf Jung
7b9d5b8758 remove remnants of const_box feature 2025-03-21 21:02:06 +01:00
bjorn3
42de015549 Mark imports of #[rustc_std_internal_symbol] items with this attribute
This ensures that they will be correctly mangled in a future commit.
2025-03-17 14:06:56 +00:00
Jacob Pratt
625278bb0e
Rollup merge of #138341 - xizheyin:issue-138322, r=joboet
std: Mention clone-on-write mutation in Arc<T>

Fixes #138322

r? libs
2025-03-17 05:47:50 -04:00
Jacob Pratt
c7700406f4
Rollup merge of #136293 - hkBst:patch-32, r=Amanieu
document capacity for ZST as example

The main text already covers this, although it provides weaker guarantees, but I think an example in the right spot does not hurt. Fixes #80747
2025-03-16 21:47:42 -04:00
许杰友 Jieyou Xu (Joe)
7112951caa
Rollup merge of #138303 - DiuDiu777:rc-fix, r=Mark-Simulacrum
Fix Ptr inconsistency in {Rc,Arc}

### PR Description
This pr aims to address the problem discussed on [zulip](https://rust-lang.zulipchat.com/#narrow/channel/219381-t-libs/topic/Inconsistency.20in.20.7BRc.2CArc.7D's.20ptr.20requirements/with/504259637).

### Problem Clarification
As this post presents, the `{Rc, Arc}::{in/de-crement_strong_count_/in}` do not limit the layout of the memory that `ptr` points to, while internally `Rc::from_raw_in` is called directly.

UB doesn't just appear when the strong count is decremented to zero. Miri also detects the UB of `out-of-bounds pointer use` when increment strong count is called on a pointer with an incorrect layout(shown as below).

```rust
use std::rc::Rc;
#[repr(align(8))]
struct Aligned8(u64);

#[repr(align(16))]
struct Aligned16(u64);

fn main() {
    let rc: Rc<Aligned8> = Rc::new(Aligned8(42));
    let raw_ptr = Rc::into_raw(rc);

    unsafe {
        Rc::increment_strong_count(raw_ptr as *const Aligned16);
    }
}
```

Miri output:
```
error: Undefined Behavior: out-of-bounds pointer use: expected a pointer to 32 bytes of memory, but got alloc954 which is only 24 bytes from the end of the allocation
```
2025-03-16 09:40:06 +08:00
Matthias Krüger
448aa30b5a
Rollup merge of #138162 - ehuss:library-2024, r=cuviper
Update the standard library to Rust 2024

This updates the standard library to Rust 2024. This includes the following notable changes:

- Macros are updated to use new expression fragment specifiers. This PR includes a test to illustrate the changes, primarily allowing `const {...}` expressions now.
- Some tests show a change in MIR drop order. We do not believe this will be an observable change ([see zulip discussion](https://rust-lang.zulipchat.com/#narrow/channel/268952-edition/topic/standard.20library.20migration/near/500972873)).

Fixes https://github.com/rust-lang/rust/issues/133081
2025-03-13 10:58:21 +01:00
Matthias Krüger
0c4415cdd6
Rollup merge of #138387 - RalfJung:intrinsic-arg-names, r=oli-obk
intrinsics: remove unnecessary leading underscore from argument names

This is unnecessary since https://github.com/rust-lang/rust/pull/135840.
2025-03-12 17:59:10 +01:00