Commit Graph

3468 Commits

Author SHA1 Message Date
Bastian Kauschke d27007fd6d add tests for array_chunks 2020-07-30 10:50:34 +02:00
Bastian Kauschke d405347f09 adds `slice::array_chunks` 2020-07-30 10:50:34 +02:00
Bastian Kauschke 870b7cbb11 improve chunks + windows err on size 0 2020-07-30 10:50:34 +02:00
Dylan MacKenzie 9caf0b5222 Make `{align,size}_of_val` `const` 2020-07-29 16:50:15 -07:00
Manish Goregaokar 2050128302
Rollup merge of #74910 - RalfJung:fence, r=Mark-Simulacrum
fence docs: fix example Mutex

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

Cc @pca006132
2020-07-29 16:38:31 -07:00
bors 6fd4c3f20f Auto merge of #72488 - KodrAus:stabilize/const_type_id, r=nikomatsakis
Stabilize const_type_id feature

The tracking issue for `const_type_id` points to the ill-fated #41875. So I'm re-energizing `TypeId` shenanigans by opening this one up to see if there's anything blocking us from stabilizing the constification of type ids.

Will wait for CI before pinging teams/groups.

-----

This PR stabilizes the `const_type_id` feature, which allows `TypeId::of` (and the underlying unstable intrinsic) to be called in constant contexts.

There are some [sanity tests](https://github.com/rust-lang/rust/blob/master/src/test/ui/consts/const-typeid-of-rpass.rs) that demonstrate its usage, but I’ve included some more below.

As a simple example, you could create a constant item that contains some type ids:

```rust
use std::any::TypeId;

const TYPE_IDS: [TypeId; 2] = [
    TypeId::of::<u32>(),
    TypeId::of::<i32>(),
];

assert_eq!(TypeId::of::<u32>(), TYPE_IDS[0]);
```

Type ids can also now appear in associated constants. You could create a trait that associates each type with its constant type id:

```rust
trait Any where Self: 'static {
    const TYPE_ID: TypeId = TypeId::of::<Self>();
}

impl<T: 'static> Any for T { }

assert_eq!(TypeId::of::<usize>(), usize::TYPE_ID);
```

`TypeId::of` is generic, which we saw above in the way the generic `Self` argument was used. This has some implications for const evaluation. It means we can make trait impls evaluate differently depending on information that wasn't directly passed through the trait system. This violates the _parametricity_ property, which requires all instances of a generic function to behave the same way with respect to its generic parameters. That's not unique to `TypeId::of`, other generic const functions based on compiler intrinsics like `mem::align_of` can also violate parametricity. In practice Rust doesn't really have type parametricity anyway since it monomorphizes generics into concrete functions, so violating it using type ids isn’t new.

As an example of how impls can behave differently, you could combine constant type ids with the `const_if_match` feature to dispatch calls based on the type id of the generic `Self`, rather than based on information about `Self` that was threaded through trait bounds. It's like a rough-and-ready form of specialization:

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

trait Specialized where Self: 'static {
    // An associated constant that determines the function to call
    // at compile-time based on `TypeId::of::<Self>`.
    const CALL: fn(&Self) = {
        const USIZE: TypeId = TypeId::of::<usize>();

        match TypeId::of::<Self>() {
            // Use a closure for `usize` that transmutes the generic `Self` to
            // a concrete `usize` and dispatches to `Self::usize`.
            USIZE => |x| Self::usize(unsafe { &*(x as *const Self as *const usize) }),
            // For other types, dispatch to the generic `Self::default`.
            _ => Self::default,
        }
    };

    fn call(&self) {
        // Call the function we determined at compile-time
        (Self::CALL)(self)
    }

    fn default(x: &Self);
    fn usize(x: &usize);
}

// Implement our `Specialized` trait for any `Debug` type.
impl<T: fmt::Debug + 'static> Specialized for T {
    fn default(x: &Self) {
        println!("default: {:?}", x);
    }

    fn usize(x: &usize) {
        println!("usize: {:?}", x);
    }
}

// Will print "usize: 42"
Specialized::call(&42usize);

// Will print "default: ()"
Specialized::call(&());
```

Type ids have some edges that this stabilization exposes to more contexts. It's possible for type ids to collide (but this is a bug). Since they can change between compiler versions, it's never valid to cast a type id to its underlying value.
2020-07-29 15:58:32 +00:00
carbotaniuman 784dd22aac add `unsigned_abs` to signed integers 2020-07-29 10:38:58 -05:00
Ralf Jung 897149a883 fence docs: fix example Mutex 2020-07-29 15:45:42 +02:00
bors 517385b31b Auto merge of #74894 - JohnTitor:rollup-4ine62a, r=JohnTitor
Rollup of 8 pull requests

Successful merges:

 - #74266 (Clean up E0720 explanation)
 - #74671 (add const generics array coercion test)
 - #74707 (Add str::[r]split_once)
 - #74814 (Fix RefUnwindSafe & UnwinsSafe impls for lazy::SyncLazy)
 - #74859 (Update outdated readme)
 - #74864 (ayu theme: Change doccomment color to `#a1ac88`)
 - #74872 (Enable to ping RISC-V group via triagebot)
 - #74891 (handle ConstEquate in rustdoc)

Failed merges:

r? @ghost
2020-07-29 01:38:00 +00:00
Erik Desjardins c596e01b8e add track_caller to RefCell::{borrow, borrow_mut}
So panic messages point at the offending borrow.
2020-07-28 15:45:25 -04:00
Alexis Bourget 36bb5e8a42 Clarify the doc for MaybeUninit::zeroed on incorrect use 2020-07-28 18:54:15 +02:00
Christian Duerr f3d7645fb7
Add trailing comma support to matches macro 2020-07-28 18:42:13 +02:00
Tim Diekmann 076ef66ba2 Remove in-place allocation and revert to separate methods for zeroed allocations
Fix docs
2020-07-28 12:41:18 +02:00
Aleksey Kladov 6e9dc7d9ff Add str::[r]split_once
This is useful for quick&dirty parsing of key: value config pairs
2020-07-28 09:58:20 +02:00
Tomasz Miąsko 0a51a9fb00 Add #[inline] to RawWaker::new 2020-07-28 06:14:52 +02:00
Ashley Mannix e3856616ee bump const type id stabilization to 1.46.0 2020-07-28 13:30:29 +10:00
Ashley Mannix cac16c9793 stabilize const_type_id feature 2020-07-28 13:30:29 +10:00
mark 2c31b45ae8 mv std libs to library/ 2020-07-27 19:51:13 -05:00