Commit Graph

4748 Commits

Author SHA1 Message Date
Matthias Krüger
102bbbd940
Rollup merge of #97506 - JohnTitor:stabilize-nonnull-slice-from-raw-parts, r=m-ou-se,the8472
Stabilize `nonnull_slice_from_raw_parts`

FCP is done: https://github.com/rust-lang/rust/issues/71941#issuecomment-1100910416
Note that this doesn't const-stabilize `NonNull::slice_from_raw_parts` as `slice_from_raw_parts_mut` isn't const-stabilized yet. Given #67456 and #57349, it's not likely available soon, meanwhile, stabilizing only the feature makes some sense, I think.

Closes #71941
2023-03-27 08:46:50 +02:00
bors
db0cbc48d4 Auto merge of #109357 - saethlin:inline-as-deref, r=thomcc
Add #[inline] to as_deref

While working on https://github.com/rust-lang/rust/pull/109247 I found an `as_deref` call in the compiler that should have been inlined. This fixes the missing inlining (but doesn't address the perf issues I was chasing).

r? `@thomcc`
2023-03-26 18:32:17 +00:00
Matthias Krüger
776a8f4eca
Rollup merge of #109620 - eievui5:patch-1, r=compiler-errors
Correct typo (`back_box` -> `black_box`)
2023-03-26 08:39:28 +02:00
bors
48ae1b335f Auto merge of #105096 - LegionMammal978:copied-allocators, r=Amanieu
Clarify that copied allocators must behave the same

Currently, the safety documentation for `Allocator` says that a cloned or moved allocator must behave the same as the original. However, it does not specify that a copied allocator must behave the same, and it's possible to construct an allocator that permits being moved or cloned, but sometimes produces a new allocator when copied.

<details>
<summary>Contrived example which results in a Miri error</summary>

```rust
#![feature(allocator_api, once_cell, strict_provenance)]
use std::{
    alloc::{AllocError, Allocator, Global, Layout},
    collections::HashMap,
    hint,
    marker::PhantomPinned,
    num::NonZeroUsize,
    pin::Pin,
    ptr::{addr_of, NonNull},
    sync::{LazyLock, Mutex},
};

mod source_allocator {
    use super::*;

    // `SourceAllocator` has 3 states:
    // - invalid value: is_cloned == false, source != self.addr()
    // - source value:  is_cloned == false, source == self.addr()
    // - cloned value:  is_cloned == true
    pub struct SourceAllocator {
        is_cloned: bool,
        source: usize,
        _pin: PhantomPinned,
    }

    impl SourceAllocator {
        // Returns a pinned source value (pointing to itself).
        pub fn new_source() -> Pin<Box<Self>> {
            let mut b = Box::new(Self {
                is_cloned: false,
                source: 0,
                _pin: PhantomPinned,
            });
            b.source = b.addr();
            Box::into_pin(b)
        }

        fn addr(&self) -> usize {
            addr_of!(*self).addr()
        }

        // Invalid values point to source 0.
        // Source values point to themselves.
        // Cloned values point to their corresponding source.
        fn source(&self) -> usize {
            if self.is_cloned || self.addr() == self.source {
                self.source
            } else {
                0
            }
        }
    }

    // Copying an invalid value produces an invalid value.
    // Copying a source value produces an invalid value.
    // Copying a cloned value produces a cloned value with the same source.
    impl Copy for SourceAllocator {}

    // Cloning an invalid value produces an invalid value.
    // Cloning a source value produces a cloned value with that source.
    // Cloning a cloned value produces a cloned value with the same source.
    impl Clone for SourceAllocator {
        fn clone(&self) -> Self {
            if self.is_cloned || self.addr() != self.source {
                *self
            } else {
                Self {
                    is_cloned: true,
                    source: self.source,
                    _pin: PhantomPinned,
                }
            }
        }
    }

    static SOURCE_MAP: LazyLock<Mutex<HashMap<NonZeroUsize, usize>>> =
        LazyLock::new(Default::default);

    // SAFETY: Wraps `Global`'s methods with additional tracking.
    // All invalid values share blocks with each other.
    // Each source value shares blocks with all cloned values pointing to it.
    // Cloning an allocator always produces a compatible allocator:
    // - Cloning an invalid value produces another invalid value.
    // - Cloning a source value produces a cloned value pointing to it.
    // - Cloning a cloned value produces another cloned value with the same source.
    // Moving an allocator always produces a compatible allocator:
    // - Invalid values remain invalid when moved.
    // - Source values cannot be moved, since they are always pinned to the heap.
    // - Cloned values keep the same source when moved.
    unsafe impl Allocator for SourceAllocator {
        fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
            let mut map = SOURCE_MAP.lock().unwrap();
            let block = Global.allocate(layout)?;
            let block_addr = block.cast::<u8>().addr();
            map.insert(block_addr, self.source());
            Ok(block)
        }

        unsafe fn deallocate(&self, block: NonNull<u8>, layout: Layout) {
            let mut map = SOURCE_MAP.lock().unwrap();
            let block_addr = block.addr();
            // SAFETY: `block` came from an allocator that shares blocks with this allocator.
            if map.remove(&block_addr) != Some(self.source()) {
                hint::unreachable_unchecked()
            }
            Global.deallocate(block, layout)
        }
    }
}
use source_allocator::SourceAllocator;

// SAFETY: `alloc1` and `alloc2` must share blocks.
unsafe fn test_same(alloc1: &SourceAllocator, alloc2: &SourceAllocator) {
    let ptr = alloc1.allocate(Layout:🆕:<i32>()).unwrap();
    alloc2.deallocate(ptr.cast(), Layout:🆕:<i32>());
}

fn main() {
    let orig = &*SourceAllocator::new_source();
    let orig_cloned1 = &orig.clone();
    let orig_cloned2 = &orig.clone();
    let copied = &{ *orig };
    let copied_cloned1 = &copied.clone();
    let copied_cloned2 = &copied.clone();
    unsafe {
        test_same(orig, orig_cloned1);
        test_same(orig_cloned1, orig_cloned2);
        test_same(copied, copied_cloned1);
        test_same(copied_cloned1, copied_cloned2);
        test_same(orig, copied); // error
    }
}
```
</details>

This could result in issues in the future for algorithms that specialize on `Copy` types. Right now, nothing in the standard library that depends on `Allocator + Clone` is susceptible to this issue, but I still think it would make sense to specify that copying an allocator is always as valid as cloning it.
2023-03-26 01:21:12 +00:00
Evie M
323551abb1
Correct typo (back_box -> black_box) 2023-03-25 19:57:46 -04:00
bors
9fa6b3c157 Auto merge of #99929 - the8472:default-iters, r=scottmcm
Implement Default for some alloc/core iterators

Add `Default` impls to the following collection iterators:

* slice::{Iter, IterMut}
* binary_heap::IntoIter
* btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues}
* btree::set::{Iter, IntoIter, Range}
* linked_list::IntoIter
* vec::IntoIter

and these adapters:

* adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev}

For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change.

These changes will be insta-stable.

ACP: https://github.com/rust-lang/libs-team/issues/77
2023-03-25 06:29:46 +00:00
bors
24a69af213 Auto merge of #109546 - saethlin:inline-into, r=scottmcm
Add #[inline] to the Into for From impl

I was skimming through the standard library MIR and I noticed a handful of very suspicious `Into::into` calls in `alloc`. ~Since this is a trivial wrapper function, `#[inline(always)]` seems appropriate.;~ `#[inline]` works too and is a lot less spooky.

r? `@thomcc`
2023-03-25 03:09:09 +00:00
Ben Kimock
badfb17d2f Add #[inline] to the Into for From impl 2023-03-24 15:06:31 -04:00
bors
f421586eed Auto merge of #109216 - martingms:unicode-case-lut-shrink, r=Mark-Simulacrum
Shrink unicode case-mapping LUTs by 24k

I was looking into the binary bloat of a small program using `str::to_lowercase` and `str::to_uppercase`, and noticed that the lookup tables used for case mapping had a lot of zero-bytes in them. The reason for this is that since some characters map to up to three other characters when lower or uppercased, the LUTs store a `[char; 3]` for each character. However, the vast majority of cases only map to a single new character, in other words most of the entries are e.g. `(lowerc, [upperc, '\0', '\0'])`.
This PR introduces a new encoding scheme for these tables.

The changes reduces the size of my test binary by about 24K.

I've also done some `#[bench]`marks on unicode-heavy test data, and found that the performance of both `str::to_lowercase` and `str::to_uppercase` improves by up to 20%. These measurements are obviously very dependent on the character distribution of the data.

Someone else will have to decide whether this more complex scheme is worth it or not, I was just goofing around a bit and here's what came out of it 🤷‍♂️ No hard feelings if this isn't wanted!
2023-03-24 10:33:42 +00:00
Matthias Krüger
d9c05b853d
Rollup merge of #108924 - tmiasko:panic-immediate-abort, r=thomcc
panic_immediate_abort requires abort as a panic strategy

Guide `panic_immediate_abort` users away from `-Cpanic=unwind` and towards `-Cpanic=abort` to avoid an accidental use of the feature with the unwind strategy, e.g., on a targets where unwind is the default.

The `-Cpanic=unwind` combination doesn't offer the same benefits, since the code would still be generated under the assumption that functions implemented in Rust can unwind.
2023-03-24 01:22:04 +01:00
bors
e216300876 Auto merge of #108442 - scottmcm:mir-transmute, r=oli-obk
Add `CastKind::Transmute` to MIR

~~Nothing actually produces it in this commit, so I don't know how to test it, but it also means it shouldn't be possible for it to break anything.~~

Includes lowering `transmute` calls to it, so it's used.

Zulip Conversation: <https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Good.20first.20isssue/near/321849610>
2023-03-23 18:43:04 +00:00
bors
84dd6dfd9d Auto merge of #109503 - matthiaskrgr:rollup-cnp7kdd, r=matthiaskrgr
Rollup of 9 pull requests

Successful merges:

 - #108954 (rustdoc: handle generics better when matching notable traits)
 - #109203 (refactor/feat: refactor identifier parsing a bit)
 - #109213 (Eagerly intern and check CrateNum/StableCrateId collisions)
 - #109358 (rustc: Remove unused `Session` argument from some attribute functions)
 - #109359 (Update stdarch)
 - #109378 (Remove Ty::is_region_ptr)
 - #109423 (Use region-erased self type during IAT selection)
 - #109447 (new solver cleanup + implement coherence)
 - #109501 (make link clickable)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2023-03-23 07:01:03 +00:00
Scott McMurray
64cce5fc7d Add CastKind::Transmute to MIR
Updates `interpret`, `codegen_ssa`, and `codegen_cranelift` to consume the new cast instead of the intrinsic.

Includes `CastTransmute` for custom MIR building, to be able to test the extra UB.
2023-03-22 15:15:41 -07:00
Matthias Krüger
29d04ff501
Rollup merge of #109359 - Nilstrieb:bump-stdarch, r=Amanieu
Update stdarch

Bring the the `#![allow(internal_features)]` for #108955

r? `@Amanieu`
2023-03-22 22:44:41 +01:00
bors
8859fde21f Auto merge of #109497 - matthiaskrgr:rollup-6txuxm0, r=matthiaskrgr
Rollup of 10 pull requests

Successful merges:

 - #109373 (Set LLVM `LLVM_UNREACHABLE_OPTIMIZE` to `OFF`)
 - #109392 (Custom MIR: Allow optional RET type annotation)
 - #109394 (adapt tests/codegen/vec-shrink-panik for LLVM 17)
 - #109412 (rustdoc: Add GUI test for "Auto-hide item contents for large items" setting)
 - #109452 (Ignore the vendor directory for tidy tests.)
 - #109457 (Remove comment about reusing rib allocations)
 - #109461 (rustdoc: remove redundant `.content` prefix from span/a colors)
 - #109477 (`HirId` to `LocalDefId` cleanup)
 - #109489 (More general captures)
 - #109494 (Do not feed param_env for RPITITs impl side)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2023-03-22 21:35:02 +00:00
Matthias Krüger
9545ab8e12
Rollup merge of #109392 - cbeuw:composite-ret, r=JakobDegen
Custom MIR: Allow optional RET type annotation

This currently doesn't compile because the type of `RET` is inferred, which fails if RET is a composite type and fields are initialised separately.
```rust
#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;
#[custom_mir(dialect = "runtime", phase = "optimized")]
fn fn0() -> (i32, bool) {
    mir! ({
        RET.0 = 0;
        RET.1 = true;
        Return()
    })
}
```
```
error[E0282]: type annotations needed
 --> src/lib.rs:8:9
  |
8 |         RET.0 = 0;
  |         ^^^ cannot infer type

For more information about this error, try `rustc --explain E0282`.
```

This PR allows the user to manually specify the return type with `type RET = ...;` if required:

```rust
#[custom_mir(dialect = "runtime", phase = "optimized")]
fn fn0() -> (i32, bool) {
    mir! (
        type RET = (i32, bool);
        {
            RET.0 = 0;
            RET.1 = true;
            Return()
        }
    )
}
```

The syntax is not optimal, I'm happy to see other suggestions. Ideally I wanted it to be a normal type annotation like `let RET: ...;`, but this runs into the multiple parsing options error during macro expansion, as it can be parsed as a normal `let` declaration as well.

r? ```@oli-obk``` or ```@tmiasko``` or ```@JakobDegen```
2023-03-22 20:08:01 +01:00
Dylan DPC
14d06467f0
Rollup merge of #109179 - llogiq:intrinsically-option-as-slice, r=eholk
move Option::as_slice to intrinsic

````@scottmcm```` suggested on #109095 I use a direct approach of unpacking the operation in MIR lowering, so here's the implementation.

cc ````@nikic```` as this should hopefully unblock #107224 (though perhaps other changes to the prior implementation, which I left for bootstrapping, are needed).
2023-03-23 00:00:31 +05:30
Dylan DPC
d694f47baa
Rollup merge of #100311 - xfix:lines-fix-handling-of-bare-cr, r=ChrisDenton
Fix handling of trailing bare CR in str::lines

Continuing from #91191.

Fixes #94435.
2023-03-23 00:00:30 +05:30
nils
caae551ecb
Rollup merge of #106434 - clubby789:document-sum-result, r=the8472
Document `Iterator::sum/product` for Option/Result

Closes #105266

We already document the similar behavior for `collect()` so I believe it makes sense to add this too. The Option/Result implementations *are* documented on their respective pages and the page for `Sum`, but buried amongst many other trait impls which doesn't make it very discoverable.

`````@rustbot````` label +A-docs
2023-03-21 13:00:21 +01:00
Martin Gammelsæter
54f55efb9a Use hex literal for INDEX_MASK 2023-03-21 09:59:47 +01:00
bors
84c47b8279 Auto merge of #108717 - TDecki:dec2flt-inline, r=thomcc
Add inlining annotations in `dec2flt`.

Currently, the combination of `dec2flt` being generic and the `FromStr` implementaions
containing inline anttributes causes massive amounts of assembly to be generated whenever
these implementation are used. In addition, the assembly has calls to function which ought to
be inlined, but they are not (even when using lto).

This Pr fixes this.
2023-03-21 04:55:02 +00:00
clubby789
f321144a56 Add example for Option::product and Result::product 2023-03-20 16:11:59 +00:00
Andy Wang
9dc275bb54
Add documentation for type RET = ... 2023-03-20 15:23:27 +01:00
Andy Wang
9da1da94ef
Allow optional RET type annotation 2023-03-20 12:21:19 +01:00
Matthias Krüger
88caa29ae3
Rollup merge of #109273 - WaffleLapkin:slice_is_sorted_by_array_windows, r=scottmcm
Make `slice::is_sorted_by` implementation nicer

Just tweak implementation a little :)

r? `@thomcc`
2023-03-20 09:46:53 +01:00
Matthias Krüger
5ae1ce80ce
Rollup merge of #109353 - Nilstrieb:rustc-mir-building, r=compiler-errors
Fix wrong crate name in custom MIR docs
2023-03-20 07:10:34 +01:00
Matthias Krüger
fb4f015ea3
Rollup merge of #109337 - frengor:collect_into_doc, r=scottmcm
Improve `Iterator::collect_into` documentation

This improves the examples in the documentation of `Iterator::collect_into`, replacing the usages of `println!` with `assert_eq!` as suggested on [IRLO](https://internals.rust-lang.org/t/18534/9).
2023-03-20 07:10:33 +01:00
Nilstrieb
43008cedaf Add #![feature(generic_arg_infer)] to core for stdarch 2023-03-19 21:08:56 +00:00
Ben Kimock
d3352def96 Add #[inline] to as_deref 2023-03-19 14:47:31 -04:00
Nilstrieb
8d706556ea Fix wrong crate name in custom MIR docs 2023-03-19 18:27:40 +01:00
Dylan DPC
c8e112a025
Rollup merge of #108973 - est31:pin_docs, r=Mark-Simulacrum
Beautify pin! docs

This makes pin docs a little bit less jargon-y and easier to read, by

* splitting up the sentences
* making them less interrupted by punctuation
* turning the footnotes into paragraphs, as they contain useful information that shouldn't be hidden in footnotes. Footnotes also interrupt the read flow.
2023-03-19 15:33:57 +05:30
Dylan DPC
acaae90bf5
Rollup merge of #108829 - xfix:use-edition-2021-pat-in-matches, r=Mark-Simulacrum
Use Edition 2021 :pat in matches macro

This makes the macro syntax used in documentation more readable.
2023-03-19 15:33:56 +05:30
Dylan DPC
993b775849
Rollup merge of #104100 - ink-feather-org:const_iter_range, r=the8472,fee1-dead
Allow using `Range` as an `Iterator` in const contexts.

~~based on #102225 by `@fee1-dead~~`
2023-03-19 15:33:55 +05:30
fren_gor
0c51d0dc53
Improve collect_into documentation 2023-03-19 04:24:35 +01:00
Matthias Krüger
0aa0043141
Rollup merge of #109287 - scottmcm:hash-slice-size-of-val, r=oli-obk
Use `size_of_val` instead of manual calculation

Very minor thing that I happened to notice in passing, but it's both shorter and [means it gets `mul nsw`](https://rust.godbolt.org/z/Y9KxYETv5), so why not.
2023-03-18 12:04:24 +01:00
onestacked
8a9d6bf4fd Mark DoubleEndedIterator as #[const_trait] using rustc_do_not_const_check, implement const Iterator and DoubleEndedIterator for Range. 2023-03-18 09:17:37 +01:00
Andre Bogus
27e9ee9bae move Option::as_slice to intrinsic 2023-03-18 07:15:15 +01:00
Scott McMurray
35088797ae Use size_of_val instead of manual calculation
Very minor thing that I happened to notice in passing, but it's both shorter and means it gets `mul nuw`, so why not.
2023-03-17 19:55:49 -07:00
onestacked
7bc67ef6e0 Make the Step implementations const. 2023-03-17 23:04:54 +01:00
bors
13afbdaa06 Auto merge of #108862 - Mark-Simulacrum:bootstrap-bump, r=pietroalbini
Bump bootstrap compiler to 1.69 beta

r? `@pietroalbini`
2023-03-17 19:00:38 +00:00
Maybe Waffle
c2ccdfa198 Switch impls of is_sorted_by between slices and slice iters
This makes a bit more sense — iter impl converts to slice first, while
slice impl used to create iter, doing unnecessary conversions.
2023-03-17 18:10:21 +00:00
Maybe Waffle
0d53565b60 Make slice::is_sorted_by impl nicer 2023-03-17 18:04:56 +00:00
Martin Gammelsæter
355e1dda1d Improve case mapping encoding scheme
The indices are encoded as `u32`s in the range of invalid `char`s, so
that we know that if any mapping fails to parse as a `char` we should
use the value for lookup in the multi-table.

This avoids the second binary search in cases where a multi-`char`
mapping is needed.

Idea from @nikic
2023-03-16 21:42:15 +01:00
est31
f663f09467 Beautify pin! docs
This makes pin docs a little bit less jargon-y and easier to read, by

* splitting up the sentences
* making them less interrupted by punctuation
* turning the footnotes into paragraphs, as they contain useful information
  that shouldn't be hidden in footnotes. Footnotes also interrupt the read flow.
* other improvements and simplifications
2023-03-16 18:03:23 +01:00
bors
1203e0866e Auto merge of #106824 - m-ou-se:format-args-flatten, r=oli-obk
Flatten/inline format_args!() and (string and int) literal arguments into format_args!()

Implements https://github.com/rust-lang/rust/issues/78356

Gated behind `-Zflatten-format-args=yes`.

Part of #99012

This change inlines string literals, integer literals and nested format_args!() into format_args!() during ast lowering, making all of the following pairs result in equivalent hir:

```rust
println!("Hello, {}!", "World");
println!("Hello, World!");
```

```rust
println!("[info] {}", format_args!("error"));
println!("[info] error");
```

```rust
println!("[{}] {}", status, format_args!("error: {}", msg));
println!("[{}] error: {}", status, msg);
```

```rust
println!("{} + {} = {}", 1, 2, 1 + 2);
println!("1 + 2 = {}", 1 + 2);
```

And so on.

This is useful for macros. E.g. a `log::info!()` macro could just pass the tokens from the user directly into a `format_args!()` that gets efficiently flattened/inlined into a `format_args!("info: {}")`.

It also means that `dbg!(x)` will have its file, line, and expression name inlined:

```rust
eprintln!("[{}:{}] {} = {:#?}", file!(), line!(), stringify!(x), x); // before
eprintln!("[example.rs:1] x = {:#?}", x); // after
```

Which can be nice in some cases, but also means a lot more unique static strings than before if dbg!() is used a lot.
2023-03-16 13:46:52 +00:00
Martin Gammelsæter
f9bd884385 Split unicode case LUTs in single and multi variants
The majority of char case replacements are single char replacements,
so storing them as [char; 3] wastes a lot of space.

This commit splits the replacement tables for both `to_lower` and
`to_upper` into two separate tables, one with single-character mappings
and one with multi-character mappings.

This reduces the binary size for programs using all of these tables
with roughly 24K bytes.
2023-03-16 12:34:04 +01:00
Mara Bos
f2f6bcc499 Don't allow new const panic through format flattening.
panic!("a {}", "b") is still not allowed in const,
even if the hir flattens to panic!("a b").
2023-03-16 11:21:50 +01:00
Mara Bos
96d252160e Update format_args!() test to account for inlining. 2023-03-16 11:21:50 +01:00
gimbles
e5a5b90afc unequal → not equal 2023-03-15 23:55:48 +05:30
Martin Gammelsæter
8a4eb9e3a8 Skip serializing ascii chars in case LUTs
Since ascii chars are already handled by a special case in the
`to_lower` and `to_upper` functions, there's no need to waste space on
them in the LUTs.
2023-03-15 17:27:23 +01:00