Commit graph

14326 commits

Author SHA1 Message Date
Mads Marquart 3fe58393bc macOS: Use libc definitions for copyfile
`COPYFILE_ALL` is not yet exposed in `libc`, but the rest of what we need is, so use those definitions instead of manually defining them.
2024-04-05 04:25:39 +02:00
Matthias Krüger ad300b6738
Rollup merge of #123431 - slanterns:literal_byte_character_c_string_stabilize, r=dtolnay
Stabilize `proc_macro_byte_character` and `proc_macro_c_str_literals`

This PR stabilizes `proc_macro_byte_character` and `proc_macro_c_str_literals`:

```rust
// proc_macro::Literal

impl Literal {
    pub fn byte_character(byte: u8) -> Literal;
    pub fn c_string(string: &CStr) -> Literal
}
```

<br>

Tracking issue: https://github.com/rust-lang/rust/issues/115268, https://github.com/rust-lang/rust/issues/119750.
Implementation PR: https://github.com/rust-lang/rust/pull/112711, https://github.com/rust-lang/rust/pull/119651.

FCPs already completed in their respective tracking issues.

Closes https://github.com/rust-lang/rust/issues/115268. Closes https://github.com/rust-lang/rust/issues/119750.

r? libs-api
2024-04-04 14:51:18 +02:00
Matthias Krüger ee5009e745
Rollup merge of #123389 - ChrisDenton:dont-panic-on-startup, r=joboet
Avoid panicking unnecessarily on startup

On Windows, in `lang_start` we add an exception handler to catch stack overflows and we also reserve some stack space for the handler. Both of these are useful but they're not strictly necessary. The standard library has to work without them (e.g. if Rust is used from a foreign entry point) and the negative effect of not doing them is limited (i.e. you don't get the friendly stack overflow message).

As we really don't want to panic pre-main unless absolutely necessary, it now won't panic on failure. I've added some debug assertions so as to avoid programmer error.
2024-04-04 14:51:17 +02:00
Chris Denton 7b8f93ef4c
Add comments about using debug_assert 2024-04-04 10:48:11 +00:00
Jacob Pratt 875d254750
Rollup merge of #122356 - devnexen:dfbsd_build_fix, r=jhpratt
std::rand: fix dragonflybsd after #121942.
2024-04-03 20:17:04 -04:00
Slanterns fbc56dfac1
Stabilize Literal::c_string 2024-04-04 05:04:27 +08:00
Slanterns 61ac7812c6
Stabilize Literal::byte_character 2024-04-04 05:00:49 +08:00
Matthias Krüger 80d592cc24
Rollup merge of #122964 - joboet:pointer_expose, r=Amanieu
Rename `expose_addr` to `expose_provenance`

`expose_addr` is a bad name, an address is just a number and cannot be exposed. The operation is actually about the provenance of the pointer.

This PR thus changes the name of the method to `expose_provenance` without changing its return type. There is sufficient precedence for returning a useful value from an operation that does something else without the name indicating such, e.g. [`Option::insert`](https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.insert) and [`MaybeUninit::write`](https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write).

Returning the address is merely convenient, not a fundamental part of the operation. This is implied by the fact that integers do not have provenance since
```rust
let addr = ptr.addr();
ptr.expose_provenance();
let new = ptr::with_exposed_provenance(addr);
```
must behave exactly like
```rust
let addr = ptr.expose_provenance();
let new = ptr::with_exposed_provenance(addr);
```
as the result of `ptr.expose_provenance()` and `ptr.addr()` is the same integer. Therefore, this PR removes the `#[must_use]` annotation on the function and updates the documentation to reflect the important part.

~~An alternative name would be `expose_provenance`. I'm not at all opposed to that, but it makes a stronger implication than we might want that the provenance of the pointer returned by `ptr::with_exposed_provenance`[^1] is the same as that what was exposed, which is not yet specified as such IIUC. IMHO `expose` does not make that connection.~~

A previous version of this PR suggested `expose` as name, libs-api [decided on](https://github.com/rust-lang/rust/pull/122964#issuecomment-2033194319) `expose_provenance` to keep the symmetry with `with_exposed_provenance`.

CC `@RalfJung`
r? libs-api

[^1]: I'm using the new name for `from_exposed_addr` suggested by #122935 here.
2024-04-03 22:11:00 +02:00
joboet 989660c3e6
rename expose_addr to expose_provenance 2024-04-03 16:00:38 +02:00
bors ceab6128fa Auto merge of #123390 - tgross35:f16-f128-libs-basic-impls-bootstrap, r=jhpratt
Put basic impls for f16 and f128 behind cfg(not(bootstrap))

We will lose `f16` and `f128` in the beta compiler after the revert for <https://github.com/rust-lang/rust/issues/123282> lands. Change what was added in <https://github.com/rust-lang/rust/pull/123085> to be behind `#[cfg(not(bootstrap))]` to account for this.
2024-04-03 12:32:34 +00:00
Jubilee 0c0d88864a
Rollup merge of #123388 - tshepang:consistency, r=jhpratt
use a consistent style for links
2024-04-02 23:44:29 -07:00
Jubilee abb0393595
Rollup merge of #122411 - alexcrichton:wasm32-wasip2-cabi-realloc, r=m-ou-se
Provide cabi_realloc on wasm32-wasip2 by default

This commit provides a component model intrinsic in the standard library
by default on the `wasm32-wasip2` target. This intrinsic is not
required by the component model itself but is quite common to use, for
example it's needed if a wasm module receives a string or a list.

The intention of this commit is to provide an overridable definition in
the standard library through a weak definition of this function. That
means that downstream crates can provide their own customized and more
specific versions if they'd like, but the standard library's version
should suffice for general-purpose use.
2024-04-02 23:44:28 -07:00
Jacob Pratt 9c1c0bfcb2
Rollup merge of #123203 - jkarneges:context-ext, r=Amanieu
Add `Context::ext`

This change enables `Context` to carry arbitrary extension data via a single `&mut dyn Any` field.

```rust
#![feature(context_ext)]

impl Context {
    fn ext(&mut self) -> &mut dyn Any;
}

impl ContextBuilder {
    fn ext(self, data: &'a mut dyn Any) -> Self;

    fn from(cx: &'a mut Context<'_>) -> Self;
    fn waker(self, waker: &'a Waker) -> Self;
}
```

Basic usage:

```rust
struct MyExtensionData {
    executor_name: String,
}

let mut ext = MyExtensionData {
    executor_name: "foo".to_string(),
};

let mut cx = ContextBuilder::from_waker(&waker).ext(&mut ext).build();

if let Some(ext) = cx.ext().downcast_mut::<MyExtensionData>() {
    println!("{}", ext.executor_name);
}
```

Currently, `Context` only carries a `Waker`, but there is interest in having it carry other kinds of data. Examples include [LocalWaker](https://github.com/rust-lang/rust/issues/118959), [a reactor interface](https://github.com/rust-lang/libs-team/issues/347), and [multiple arbitrary values by type](https://docs.rs/context-rs/latest/context_rs/). There is also a general practice in the ecosystem of sharing data between executors and futures via thread-locals or globals that would arguably be better shared via `Context`, if it were possible.

The `ext` field would provide a low friction (to stabilization) solution to enable experimentation. It would enable experimenting with what kinds of data we want to carry as well as with what data structures we may want to use to carry such data.

Dedicated fields for specific kinds of data could still be added directly on `Context` when we have sufficient experience or understanding about the problem they are solving, such as with `LocalWaker`. The `ext` field would be for data for which we don't have such experience or understanding, and that could be graduated to dedicated fields once proven.

Both the provider and consumer of the extension data must be aware of the concrete type behind the `Any`. This means it is not possible for the field to carry an abstract interface. However, the field can carry a concrete type which in turn carries an interface. There are different ways one can imagine an interface-carrying concrete type to work, hence the benefit of being able to experiment with such data structures.

## Passing interfaces

Interfaces can be placed in a concrete type, such as a struct, and then that type can be casted to `Any`. However, one gotcha is `Any` cannot contain non-static references. This means one cannot simply do:

```rust
struct Extensions<'a> {
    interface1: &'a mut dyn Trait1,
    interface2: &'a mut dyn Trait2,
}

let mut ext = Extensions {
    interface1: &mut impl1,
    interface2: &mut impl2,
};

let ext: &mut dyn Any = &mut ext;
```

To work around this without boxing, unsafe code can be used to create a safe projection using accessors. For example:

```rust
pub struct Extensions {
    interface1: *mut dyn Trait1,
    interface2: *mut dyn Trait2,
}

impl Extensions {
    pub fn new<'a>(
        interface1: &'a mut (dyn Trait1 + 'static),
        interface2: &'a mut (dyn Trait2 + 'static),
        scratch: &'a mut MaybeUninit<Self>,
    ) -> &'a mut Self {
        scratch.write(Self {
            interface1,
            interface2,
        })
    }

    pub fn interface1(&mut self) -> &mut dyn Trait1 {
        unsafe { self.interface1.as_mut().unwrap() }
    }

    pub fn interface2(&mut self) -> &mut dyn Trait2 {
        unsafe { self.interface2.as_mut().unwrap() }
    }
}

let mut scratch = MaybeUninit::uninit();
let ext: &mut Extensions = Extensions::new(&mut impl1, &mut impl2, &mut scratch);

// ext can now be casted to `&mut dyn Any` and back, and used safely
let ext: &mut dyn Any = ext;
```

## Context inheritance

Sometimes when futures poll other futures they want to provide their own `Waker` which requires creating their own `Context`. Unfortunately, polling sub-futures with a fresh `Context` means any properties on the original `Context` won't get propagated along to the sub-futures. To help with this, some additional methods are added to `ContextBuilder`.

Here's how to derive a new `Context` from another, overriding only the `Waker`:

```rust
let mut cx = ContextBuilder::from(parent_cx).waker(&new_waker).build();
```
2024-04-02 20:37:40 -04:00
Jacob Pratt e9ef8e1efa
Rollup merge of #122935 - RalfJung:with-exposed-provenance, r=Amanieu
rename ptr::from_exposed_addr -> ptr::with_exposed_provenance

As discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/To.20expose.20or.20not.20to.20expose/near/427757066).

The old name, `from_exposed_addr`, makes little sense as it's not the address that is exposed, it's the provenance. (`ptr.expose_addr()` stays unchanged as we haven't found a better option yet. The intended interpretation is "expose the provenance and return the address".)

The new name nicely matches `ptr::without_provenance`.
2024-04-02 20:37:39 -04:00
Justin Karneges 036085dfec set tracking issue 2024-04-02 15:45:53 -07:00
bors 88c2f4f5f5 Auto merge of #123385 - matthiaskrgr:rollup-v69vjbn, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - #123198 (Add fn const BuildHasherDefault::new)
 - #123226 (De-LLVM the unchecked shifts [MCP#693])
 - #123302 (Make sure to insert `Sized` bound first into clauses list)
 - #123348 (rustdoc: add a couple of regression tests)
 - #123362 (Check that nested statics in thread locals are duplicated per thread.)
 - #123368 (CFI: Support non-general coroutines)
 - #123375 (rustdoc: synthetic auto trait impls: accept unresolved region vars for now)
 - #123378 (Update sysinfo to 0.30.8)

Failed merges:

 - #123349 (Fix capture analysis for by-move closure bodies)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-04-02 21:23:53 +00:00
Trevor Gross 049a917535 Put basic impls for f16 and f128 behind cfg(not(bootstrap))
We will lose `f16` and `f128` in the beta compiler after the revert for
<https://github.com/rust-lang/rust/issues/123282> lands. Change what was
added in <https://github.com/rust-lang/rust/pull/123085> to be behind
`#[cfg(not(bootstrap))]` to account for this.
2024-04-02 16:19:55 -04:00
Chris Denton e457b77e2a
Avoid panicking unnecessarily on startup 2024-04-02 19:41:58 +00:00
Tshepang Mbambo a6b2d12c92 use a consistent style for links 2024-04-02 21:41:16 +02:00
Matthias Krüger 1b0e46f8a0
Rollup merge of #123226 - scottmcm:u32-shifts, r=WaffleLapkin
De-LLVM the unchecked shifts [MCP#693]

This is just one part of the MCP (https://github.com/rust-lang/compiler-team/issues/693), but it's the one that IMHO removes the most noise from the standard library code.

Seems net simpler this way, since MIR already supported heterogeneous shifts anyway, and thus it's not more work for backends than before.

r? WaffleLapkin
2024-04-02 21:22:01 +02:00
Matthias Krüger d63ddef803
Rollup merge of #123198 - krtab:build_hasher_default_const_new, r=Amanieu
Add fn const BuildHasherDefault::new

See [tracking issue](https://github.com/rust-lang/rust/issues/123197) for justification.
2024-04-02 21:22:00 +02:00
bors a77322c16f Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco
Add `Ord::cmp` for primitives as a `BinOp` in MIR

Update: most of this OP was written months ago.  See https://github.com/rust-lang/rust/pull/118310#issuecomment-2016940014 below for where we got to recently that made it ready for review.

---

There are dozens of reasonable ways to implement `Ord::cmp` for integers using comparison, bit-ops, and branches.  Those differences are irrelevant at the rust level, however, so we can make things better by adding `BinOp::Cmp` at the MIR level:

1. Exactly how to implement it is left up to the backends, so LLVM can use whatever pattern its optimizer best recognizes and cranelift can use whichever pattern codegens the fastest.
2. By not inlining those details for every use of `cmp`, we drastically reduce the amount of MIR generated for `derive`d `PartialOrd`, while also making it more amenable to MIR-level optimizations.

Having extremely careful `if` ordering to μoptimize resource usage on broadwell (#63767) is great, but it really feels to me like libcore is the wrong place to put that logic.  Similarly, using subtraction [tricks](https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign) (#105840) is arguably even nicer, but depends on the optimizer understanding it (https://github.com/llvm/llvm-project/issues/73417) to be practical.  Or maybe [bitor is better than add](https://discourse.llvm.org/t/representing-in-ir/67369/2?u=scottmcm)?  But maybe only on a future version that [has `or disjoint` support](https://discourse.llvm.org/t/rfc-add-or-disjoint-flag/75036?u=scottmcm)?  And just because one of those forms happens to be good for LLVM, there's no guarantee that it'd be the same form that GCC or Cranelift would rather see -- especially given their very different optimizers.  Not to mention that if LLVM gets a spaceship intrinsic -- [which it should](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Suboptimal.20inlining.20in.20std.20function.20.60binary_search.60/near/404250586) -- we'll need at least a rustc intrinsic to be able to call it.

As for simplifying it in Rust, we now regularly inline `{integer}::partial_cmp`, but it's quite a large amount of IR.  The best way to see that is with 8811efa88b (diff-d134c32d028fbe2bf835fef2df9aca9d13332dd82284ff21ee7ebf717bfa4765R113) -- I added a new pre-codegen MIR test for a simple 3-tuple struct, and this PR change it from 36 locals and 26 basic blocks down to 24 locals and 8 basic blocks.  Even better, as soon as the construct-`Some`-then-match-it-in-same-BB noise is cleaned up, this'll expose the `Cmp == 0` branches clearly in MIR, so that an InstCombine (#105808) can simplify that to just a `BinOp::Eq` and thus fix some of our generated code perf issues.  (Tracking that through today's `if a < b { Less } else if a == b { Equal } else { Greater }` would be *much* harder.)

---

r? `@ghost`
But first I should check that perf is ok with this
~~...and my true nemesis, tidy.~~
2024-04-02 19:21:44 +00:00
Steve Lau bb439900dd style: fmt 2024-04-02 14:29:38 +08:00
Steve Lau 6ad96825fc fix: build on haiku by adding missing import 2024-04-02 14:18:31 +08:00
bors 6bbd8c519a Auto merge of #122945 - andy-k:sorted-vec-example, r=jhpratt
improve example on inserting to a sorted vector to avoid shifting equal elements
2024-04-02 03:14:05 +00:00
Jubilee 48b2a517fc
Rollup merge of #123323 - devnexen:thread_set_name_solaris_fix, r=workingjubilee
std:🧵 set_name change for solaris/illumos.

truncate down to 32 (31 + 1) for solaris/illumos.
2024-04-01 17:22:10 -07:00
David Carlier ca36fe310e
std:🧵 set_name change for solaris/illumos.
truncate down to 32 (31 + 1) for solaris/illumos.
2024-04-01 22:16:13 +01:00
bors a7e3b1c8c5 Auto merge of #123315 - devnexen:thread_get_name_solaris, r=ChrisDenton
std:🧵 adding get_name implementation for solaris/illumos.

THREAD_NAME_MAX is 32 (31 max + 1 for the null terminator).
2024-04-01 16:38:55 +00:00
bors c518e5aeec Auto merge of #123265 - joboet:guardians_of_the_unix, r=ChrisDenton
Refactor stack overflow handling

Currently, every platform must implement a `Guard` that protects a thread from stack overflow. However, UNIX is the only platform that actually does so. Windows has a different mechanism for detecting stack overflow, while the other platforms don't detect it at all. Also, the UNIX stack overflow handling is split between `sys::pal::unix::stack_overflow`, which implements the signal handler, and `sys::pal::unix::thread`, which detects/installs guard pages.

This PR cleans this by getting rid of `Guard` and unifying UNIX stack overflow handling inside `stack_overflow` (commit 1). Therefore we can get rid of `sys_common::thread_info`, which stores `Guard` and the current `Thread` handle and move the `thread::current` TLS variable into `thread` (commit 2).

The second commit is not strictly speaking necessary. To keep the implementation clean, I've included it here, but if it causes too much noise, I can split it out without any trouble.
2024-04-01 14:35:38 +00:00
joboet d7b55e4c90
update comment 2024-04-01 15:28:27 +02:00
David Carlier 747d19326b
std:🧵 adding get_name implementation for solaris/illumos.
THREAD_NAME_MAX is 32 (31 max + 1 for the null terminator).
2024-04-01 10:01:21 +01:00
beetrees 0bbaa2505b
Fix error message for env! when env var is not valid Unicode 2024-04-01 05:44:45 +01:00
bors 66de611196 Auto merge of #123270 - JaniM:janim/string-alloc-doc, r=workingjubilee
doc: mention heap allocation earlier in String docs

Just a tiny addition.

Helps with #123263.
2024-03-31 22:39:04 +00:00
Jani Mustonen 418535b798 doc: mention heap allocation earlier in String docs
Just a tiny addition.

Helps with #123263.
2024-04-01 00:04:57 +03:00
bors 8058136502 Auto merge of #123299 - workingjubilee:rollup-2z8amaj, r=workingjubilee
Rollup of 5 pull requests

Successful merges:

 - #123180 (Rewrite `core-no-fp-fmt-parse` test in Rust)
 - #123267 (std:🧵 adding get_name haiku implementation.)
 - #123268 (warn against implementing Freeze)
 - #123271 (doc: describe panic conditions for SliceIndex implementations)
 - #123295 (add myself to compiler review rotation)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-03-31 20:35:15 +00:00
Jubilee 42ca32673a
Rollup merge of #123271 - JaniM:janim/sliceindex-doc, r=Nilstrieb
doc: describe panic conditions for SliceIndex implementations

Implementation note: The most probable place for users to find the documentation is at https://doc.rust-lang.org/std/slice/trait.SliceIndex.html

On that page, documentation added to specific methods will not be visible. As such, I opted to add the comments to the impl blocks directly.

Helps with #121568.
2024-03-31 13:18:17 -07:00
Jubilee 9ff4c70476
Rollup merge of #123268 - RalfJung:dont-freeze, r=Nilstrieb
warn against implementing Freeze

As [requested](https://github.com/rust-lang/rust/pull/123184#issuecomment-2028531388) by `@workingjubilee`
2024-03-31 13:18:17 -07:00
Ralf Jung 602401c4d4 warn against implementing Freeze 2024-03-31 22:15:48 +02:00
David Carlier e5c5ed00a5 std:🧵 adding get_name haiku implementation.
follow-up #123233
2024-03-31 17:47:44 +01:00
Jani Mustonen 4ca3151568 doc: describe panic conditions for SliceIndex implementations
Implementation note: The most probable place for users to find
the documentation is at https://doc.rust-lang.org/std/slice/trait.SliceIndex.html

On that page, documentation added to specific methods will not
be visible. As such, I opted to add the comments to the impl blocks
directly.

Helps with #121568.
2024-03-31 16:13:25 +03:00
Ralf Jung 42972f52de catch_panic: warn about panicking payload drop 2024-03-31 11:54:16 +02:00
joboet 7668418101
std: move thread::current TLS variable out of thread_info 2024-03-31 11:28:24 +02:00
joboet 5b9d7ab558
std: move UNIX stack overflow guard page handling into stack_overflow.rs 2024-03-31 11:24:33 +02:00
bors 5f358a848c Auto merge of #123233 - devnexen:thread_get_name_bsd, r=joboet
std:🧵 adding freebsd/netbsd to the linux's get_name implementa…

…tion.
2024-03-31 04:18:47 +00:00
bors 1aedc9640c Auto merge of #123181 - stepancheg:pointee-metadata-debug, r=the8472,Amanieu
Require Debug for Pointee::Metadata

Useful for debugging.
2024-03-31 00:09:41 +00:00
bors 5da1a1b59a Auto merge of #123085 - tgross35:f16-f128-step4.0-libs-basic-impls, r=Amanieu
Add basic trait impls for `f16` and `f128`

Split off part of <https://github.com/rust-lang/rust/pull/122470> so the compiler doesn't ICE because it expects primitives to have some minimal traits.

Fixes <https://github.com/rust-lang/rust/issues/123074>
2024-03-30 21:58:49 +00:00
bors 8df7e723ea Auto merge of #99322 - GKFX:const-int-parse, r=Mark-Simulacrum
Make {integer}::from_str_radix constant

This commit makes FromStr on integers constant so that `const x: u32 = "23".parse();` works. More practical use-case is with environment variables at build time as discussed in https://github.com/rust-lang/rfcs/issues/1907.

Tracking issue #59133.

ACP: https://github.com/rust-lang/libs-team/issues/74
2024-03-30 19:56:58 +00:00
David Carlier c749483e26 std:🧵 adding freebsd/netbsd to the linux's get_name implementation. 2024-03-30 16:01:47 +00:00
Matthias Krüger 558880ab88
Rollup merge of #123201 - Wilfred:patch-2, r=Nilstrieb
Improve wording in std::any explanation

Prefer 'log' over 'log out' to avoid confusion, and use backticks consistently.
2024-03-30 14:30:50 +01:00
George Bateman 3855b8bb60
Make {integer}::from_str_radix constant 2024-03-30 12:43:58 +00:00