about summary refs log tree commit diff
path: root/library/core/src/slice/index.rs
AgeCommit message (Collapse)AuthorLines
2025-09-23Auto merge of #146317 - saethlin:panic=immediate-abort, r=nnethercotebors-2/+2
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-22assert_unsafe_precondition: fix some incorrect check_language_ubRalf Jung-1/+1
2025-09-21Change the cfg to a dashBen Kimock-2/+2
2025-09-21Add panic=immediate-abortBen Kimock-2/+2
2025-09-10Rollup merge of #144765 - Qelxiros:range-inclusive-last, r=jhprattMatthias Krüger-0/+41
inclusive `Range`s: change `end` to `last` Tracking issue: rust-lang/rust#125687 ACP: rust-lang/libs-team#511
2025-09-08change end to lastJeremy Smart-0/+41
2025-09-06clean up some old const trait impl syntaxNathaniel McCallum-1/+1
2025-08-21Consolidate panicking functions in `slice/index.rs`Karl Meakin-97/+103
Consolidate all the panicking functions in `slice/index.rs` to use a single `slice_index_fail` function, similar to how it is done in `str/traits.rs`.
2025-08-06tidyBoxy-2/+2
2025-07-15constify `Index` trait and its slice implsOli Scherer-4/+6
2025-07-15constify `SliceIndex` traitOli Scherer-11/+24
2025-06-02Remove bootstrap cfgs from library/Josh Stone-53/+4
2025-05-30`slice.get(i)` should use a slice projection in MIR, like `slice[i]` doesScott McMurray-7/+49
2025-05-27Auto merge of #129658 - saethlin:spare-a-crumb, r=jhprattbors-0/+6
Add some track_caller info to precondition panics Currently, when you encounter a precondition check, you'll always get the caller location of the implementation of the precondition checks. But with this PR, you'll be told the location of the invalid call. Which is useful. I thought of this while looking at https://github.com/rust-lang/rust/pull/129642#issuecomment-2311703898. The changes to `tests/ui/const*` happen because the const-eval interpreter skips `#[track_caller]` frames in its backtraces. The perf implications of this are: * Increased debug binary sizes. The caller_location implementation requires that the additional data we want to display here be stored in const allocations, which are deduplicated but not across crates. There is no impact on optimized build sizes. The panic path and the caller location data get optimized out. * The compile time hit to opt-incr-patched bitmaps happens because the patch changes the line number of some function calls with precondition checks, causing us to go from 0 dirty CGUs to 1 dirty CGU. * The other compile time hits are marginal but real, and due to doing a handful of new queries. Adding more useful data isn't completely free.
2025-05-21Add some track_caller info to precondition panicsBen Kimock-0/+6
2025-05-17Switch library rustc_unimplemented to use `Self` and `This`mejrs-1/+1
2024-11-05add const_eval_select macro to reduce redundancyRalf Jung-1/+1
also move internal const_panic helpers to a better location
2024-11-03add const_panic macro to make it easier to fall back to non-formatting panic ↵Ralf Jung-49/+19
in const
2024-10-25Re-do recursive const stability checksRalf Jung-1/+3
Fundamentally, we have *three* disjoint categories of functions: 1. const-stable functions 2. private/unstable functions that are meant to be callable from const-stable functions 3. functions that can make use of unstable const features This PR implements the following system: - `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions. - `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category. - `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls. Also, several holes in recursive const stability checking are being closed. There's still one potential hole that is hard to avoid, which is when MIR building automatically inserts calls to a particular function in stable functions -- which happens in the panic machinery. Those need to *not* be `rustc_const_unstable` (or manually get a `rustc_const_stable_indirect`) to be sure they follow recursive const stability. But that's a fairly rare and special case so IMO it's fine. The net effect of this is that a `#[unstable]` or unmarked function can be constified simply by marking it as `const fn`, and it will then be const-callable from stable `const fn` and subject to recursive const stability requirements. If it is publicly reachable (which implies it cannot be unmarked), it will be const-unstable under the same feature gate. Only if the function ever becomes `#[stable]` does it need a `#[rustc_const_unstable]` or `#[rustc_const_stable]` marker to decide if this should also imply const-stability. Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to use unstable const lang features (including intrinsics), or (b) `#[stable]` functions that are not yet intended to be const-stable. Adding `#[rustc_const_stable]` is only needed for functions that are actually meant to be directly callable from stable const code. `#[rustc_const_stable_indirect]` is used to mark intrinsics as const-callable and for `#[rustc_const_unstable]` functions that are actually called from other, exposed-on-stable `const fn`. No other attributes are required.
2024-09-08remove const_slice_index annotations, it never had a feature gate anywayRalf Jung-10/+0
2024-09-08add FIXME(const-hack)Ralf Jung-3/+6
2024-07-29Reformat `use` declarations.Nicholas Nethercote-2/+1
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-26Fix doc nitsJohn Arundel-6/+9
Many tiny changes to stdlib doc comments to make them consistent (for example "Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph breaks, backticks for monospace style, and other minor nits. https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
2024-07-05add `new_range_api` for RFC 3550Peter Jaszkowiak-2/+122
This includes a `From<legacy::RangeInclusive> for RangeInclusive` impl for convenience, instead of the `TryFrom` impl from the RFC. Having `From` is highly convenient and the assertion is unlikely to be a problem in practice. This includes re-exports of all existing `Range` types under `core::range`, plus the range-related traits (`RangeBounds`, `Step`, `OneSidedRange`) and the `Bound` enum. Currently the iterators are just wrappers around the old range types, and most other trait impls delegate to the old rage types as well. Also includes an `.iter()` shorthand for `.clone().into_iter()`
2024-06-15Redo SliceIndex implementationsScott McMurray-32/+86
2024-05-23Add assert_unsafe_precondition to unchecked_{add,sub,neg,mul,shl,shr} methodsltdk-3/+2
2024-03-31doc: describe panic conditions for SliceIndex implementationsJani Mustonen-0/+11
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-23move assert_unsafe_preconditions to its own fileRalf Jung-1/+1
These macros and functions are not intrinsics, after all.
2024-03-20step cfgsMark Rousskov-19/+3
2024-03-11Rollup merge of #121148 - clarfonthey:try-range, r=dtolnayJacob Pratt-4/+55
Add slice::try_range This adds a fallible version of the unstable `slice::range` (tracking: #76393) which is highly requested in the tracking issue. Hoping this can slide by without an ACP (since the feature is already being tracked), but let me know otherwise.
2024-03-10docs: Correct ptr/ref verbiage in SliceIndex docs.matt rice-4/+4
Fixes #122234
2024-03-08Distinguish between library and lang UB in assert_unsafe_preconditionBen Kimock-19/+33
2024-03-02const_eval_select: make it safe but be careful with what we expose on stable ↵Ralf Jung-1/+6
for now
2024-02-19Add more inline(always) to fix opt-level=z test on wasm32Ben Kimock-2/+2
2024-02-19Convert debug_assert_nounwind to intrinsics::debug_assertionsBen Kimock-8/+12
2024-02-15Add slice::try_rangeltdk-4/+55
2024-01-13libs: use `assert_unchecked` instead of intrinsicjoboet-1/+1
2023-12-04use `assume(idx < self.len())` in `[T]::get_unchecked`bendn-1/+4
2023-11-25Convert many `assert_unsafe_precondition` to `debug_assert_nounwind`Gary Guo-46/+29
2023-10-16Make `rustc_onunimplemented` export path agnosticNilstrieb-4/+1
This makes it so that all the matchers that match against paths use the definition path instead of the export path. This removes all duplication around `std`/`alloc`/`core`. This is not necessarily optimal because we now depend on internal implementation details like `core::ops::control_flow::ControlFlow`, which is not very nice and probably not acceptable for a stable `on_unimplemented`. An alternative would be to just string-replace normalize away `alloc`/`core` to `std` as a special case, keeping the export paths but making it so that we're still fully standard library flavor agnostic.
2023-07-31impl SliceIndex<str> for (Bound<usize>, Bound<usize>)Matt Fellenz-3/+3
2023-06-27Make `rustc_on_unimplemented` std-agnosticRageking8-1/+4
2023-04-16rm const traits in libcoreDeadbeef-15/+12
2023-03-05Use `nuw` when calculating slice lengths from `Range`sScott McMurray-3/+6
An `assume` would definitely not be worth it, but since the flag is almost free we might as well tell LLVM this, especially on `_unchecked` calls where there's no obvious way for it to deduce it. (Today neither safe nor unsafe indexing gets it: <https://rust.godbolt.org/z/G1jYT548s>)
2023-01-21debug assertions for `slice::split_at_unchecked`, `str::get_unchecked`Peter Jaszkowiak-3/+2
2022-11-29Adjust inlining attributes around panic_immediate_abortBen Kimock-10/+11
2022-10-26Print the precondition we violated, and visible through output captureBen Kimock-10/+26
Co-authored-by: Ralf Jung <post@ralfj.de>
2022-09-22Auto merge of #100982 - fee1-dead-contrib:const-impl-requires-const-trait, ↵bors-0/+1
r=oli-obk Require `#[const_trait]` on `Trait` for `impl const Trait` r? `@oli-obk`
2022-09-19Optimize `array::IntoIter`Scott McMurray-0/+75
`.into_iter()` on arrays was slower than it needed to be (especially compared to slice iterator) since it uses `Range<usize>`, which needs to handle degenerate ranges like `10..4`. This PR adds an internal `IndexRange` type that's like `Range<usize>` but with a safety invariant that means it doesn't need to worry about those cases -- it only handles `start <= end` -- and thus can give LLVM more information to optimize better. I added one simple demonstration of the improvement as a codegen test.
2022-09-16Require `#[const_trait]` for `const` `impl`sDeadbeef-0/+1