about summary refs log tree commit diff
path: root/library/core/src/hint.rs
AgeCommit message (Collapse)AuthorLines
2025-08-31Rollup merge of #145174 - 197g:issue-145148-select-unpredictable-drop, r=joboetMatthias Krüger-2/+35
Ensure consistent drop for panicking drop in hint::select_unpredictable There are a few alternatives to the implementation. The principal problem is that the selected value must be owned (in the sense of having a drop flag of sorts) when the unselected value is dropped, such that panic unwind goes through the drop of both. This ownership must then be passed on in return when the drop went smoothly. The basic way of achieving this is by extracting the selected value first, at the cost of relying on the optimizer a little more for detecting the copy as constructing the return value despite having a place in the body. Unfortunately, that causes LLVM to discard the !unpredictable annotation (for some reason that is beyond my comprehension of LLVM). <details> <summary>Extract from the build log showing an unannotated select being used</summary> ``` 2025-08-09T16:51:06.8790764Z 39: define noundef i64 `@test_int2(i1` noundef zeroext %p, i64 noundef %a, i64 noundef %b) unnamed_addr #0 personality ptr `@rust_eh_personality` { 2025-08-09T16:51:06.8791368Z check:47'0 X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found 2025-08-09T16:51:06.8791700Z 40: start: 2025-08-09T16:51:06.8791858Z check:47'0 ~~~~~~~ 2025-08-09T16:51:06.8792043Z 41: %ret.i = select i1 %p, i64 %a, i64 %b 2025-08-09T16:51:06.8792293Z check:47'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2025-08-09T16:51:06.8792686Z check:47'1 ? possible intended match 2025-08-09T16:51:06.8792946Z 42: ret i64 %ret.i 2025-08-09T16:51:06.8793127Z check:47'0 ~~~~~~~~~~~~~~~~ ``` </details> So instead, this PR includes a guard to drop the selected `MaybeUnit<T>` which is active only for the section where the unselected value is dropped. That leaves the code for selecting the result intact leading to the expected ir. That complicates the 'unselection' process a little bit since we require _both_ values as a result of that intrinsic call. Since the arguments alias, this portion as well as the drop guard uses raw pointers. Closes: rust-lang/rust#145148 Prior: rust-lang/rust#139977
2025-08-30Switch select_unpredictable guard to raw pointerAurelia Molzer-8/+11
2025-08-30Simplify select_unpredictable guard selectionAurelia Molzer-6/+6
Instead of a tuple, select the dropped value and its guard with two separate calls to the intrinsic which makes both calls have a pointer-valued argument that should be simpler in codegen. Use the same condition on all (not an inverted condition) to clarify the intent of parallel selection. This should also be a simpler value-dependency chain if the guard is deduced unused (i.e. drop_in_place a noop for the type).
2025-08-27Add spin_loop hint for LoongArchWANG Rui-33/+23
2025-08-09Adjust for codegen of unpredictable return valueAurelia Molzer-5/+31
It seems important for LLVM that we select on the values by-value instead of reading and have no intermediate store. So make sure the guards selects both potential drops but defers the return value to the second selection. Since the two options alias we use raw mutable pointers instead of mutable references as before.
2025-08-09Ensure consistent drop for hint::select_unpredictableAurelia Molzer-1/+5
There are a few alternatives to the implementation. The principal problem is that the selected value must be owned (in the sense of having any drop flag of sorts) when the unselected value is dropped, such that panic unwind goes through the drop of both. This ownership must then be passed on in return when the drop went smoothly. The basic way of achieving this is by extracting the selected value first, at the cost of relying on the optimizer a little more for detecting the copy as constructing the return value despite having a place in the body.
2025-07-25Improve and regularize comment placement in doc codeJosh Triplett-2/+0
Because doc code does not get automatically formatted, some doc code has creative placements of comments that automatic formatting can't handle. Reformat those comments to make the resulting code support standard Rust formatting without breaking; this is generally an improvement to readability as well. Some comments are not indented to the prevailing indent, and are instead aligned under some bit of code. Indent them to the prevailing indent, and put spaces *inside* the comments to align them with code. Some comments span several lines of code (which aren't the line the comment is about) and expect alignment. Reformat them into one comment not broken up by unrelated intervening code. Some comments are placed on the same line as an opening brace, placing them effectively inside the subsequent block, such that formatting would typically format them like a line of that block. Move those comments to attach them to what they apply to. Some comments are placed on the same line as a one-line braced block, effectively attaching them to the closing brace, even though they're about the code inside the block. Reformat to make sure the comment will stay on the same line as the code it's commenting.
2025-05-27Auto merge of #129658 - saethlin:spare-a-crumb, r=jhprattbors-1/+1
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-1/+1
2025-05-15Updated std doctests for wasmEric Huss-1/+1
This updates some doctests that fail to run on wasm. We will soon be supporting cross-compiled doctests, and the test-various job fails to run these tests. These tests fail because wasm32-wasip1 does not support threads.
2025-05-12update version placeholdersPietro Albini-1/+1
2025-05-08Rollup merge of #140341 - saethlin:black-box-qoi, r=Mark-SimulacrumMatthias Krüger-0/+4
Clarify black_box warning a bit Trying to bring the docs on black_box more in line with the advice that we have discussed in Zulip. https://github.com/rust-lang/rust/pull/140341#issuecomment-2832592382
2025-05-08Indicate that the warning on black_box is a general property of RustBen Kimock-0/+4
And note that the same limitation applies to all LLVM-based compilers Co-authored-by: Ralf Jung <post@ralfj.de>
2025-05-01Stabilize `select_unpredictable`Amanieu d'Antras-3/+1
FCP completed in tracking issue #133962.
2025-04-17Fix drop handling in `hint::select_unpredictable`Amanieu d'Antras-9/+21
This intrinsic doesn't drop the value that is not selected so this is manually done in the public function that wraps the intrinsic.
2025-04-13Move `select_unpredictable` to the `hint` moduleAmanieu d'Antras-0/+49
2025-02-18update version placeholdersJosh Stone-1/+1
(cherry picked from commit e4840ce59bdddb19394df008c5c26d9c493725f8)
2025-02-14core: Apply unsafe_op_in_unsafe_fnEric Huss-1/+1
2025-02-11Change the issue number for `likely_unlikely` and `cold_path`Trevor Gross-3/+3
These currently point to rust-lang/rust#26179, which is nearly a decade old and has a lot of outdated discussion. Move these features to a new tracking issue specifically for the recently added API. New tracking issue: https://github.com/rust-lang/rust/issues/136873
2025-01-27Stabilize `const_black_box`Trevor Gross-1/+3
This has been unstably const since [1], but a tracking issue was never created. Per discussion on Zulip [2], there should not be any blockers to making this const-stable. The function does not provide any functionality at compile time but does allow code reuse between const- and non-const functions, so stabilize it here. [1]: https://github.com/rust-lang/rust/pull/92226 [2]: https://rust-lang.zulipchat.com/#narrow/channel/146212-t-compiler.2Fconst-eval/topic/const_black_box
2025-01-20Rollup merge of #133695 - x17jiri:hint_likely, r=AmanieuMatthias Krüger-0/+135
Reexport likely/unlikely in std::hint Since `likely`/`unlikely` should be working now, we could reexport them in `std::hint`. I'm not sure if this is already approved or if it requires approval Tracking issue: #26179
2025-01-201. Removed 'rustc_nounwind' 2. Rewording of commentsJiri Bobek-11/+6
2025-01-15Export likely(), unlikely() and cold_path() in std::hintJiri Bobek-0/+140
2024-12-12feat: clarify how to use `black_box()`BD103-3/+89
Co-authored-by: Ben Kimock <kimockb@gmail.com>
2024-11-27update cfgsBoxy-1/+0
2024-10-25Re-do recursive const stability checksRalf Jung-1/+1
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-01add extra linebreaks so rustdoc can identify the first sentencebinarycat-0/+1
there should probably be a lint against this in rustdoc, it causes too many lines to be shown in the short documentation overviews expecially noticable for the slice primative type: https://doc.rust-lang.org/std/index.html
2024-07-30Auto merge of #128083 - Mark-Simulacrum:bump-bootstrap, r=albertlarsan68bors-2/+2
Bump bootstrap compiler to new beta https://forge.rust-lang.org/release/process.html#master-bootstrap-update-t-2-day-tuesday
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-28Update CURRENT_RUSTC_VERSIONMark Rousskov-2/+2
2024-07-04chore: remove repeat wordscuishuang-1/+1
Signed-off-by: cuishuang <imcusg@gmail.com>
2024-07-03Rollup merge of #123588 - tgross35:stabilize-assert_unchecked, r=dtolnayJacob Pratt-23/+74
Stabilize `hint::assert_unchecked` Make the following API stable, including const: ```rust // core::hint, std::hint pub const unsafe fn assert_unchecked(p: bool); ``` This PR also reworks some of the documentation and adds an example. Tracking issue: https://github.com/rust-lang/rust/issues/119131 FCP: https://github.com/rust-lang/rust/issues/119131#issuecomment-1906394087. The docs update should resolve the remaining concern.
2024-06-19Stabilize `hint_assert_unchecked`Trevor Gross-5/+3
Make both `hint_assert_unchecked` and `const_hint_assert_unchecked` stable as `hint_assert_unchecked`.
2024-06-19Update documentation for `hint::assert_unchecked`Trevor Gross-20/+73
Rearrange the sections and add an example to `core::hint::assert_unchecked`.
2024-06-19reword the hint::blackbox non-guaranteesThe 8472-1/+1
People were tripped up by the "precludes", interpreting it that this function must not ever be used in cryptographic contexts rather than the std lib merely making zero promises about it being fit-for-purpose. What remains unchanged is that if someone does try to use it *despite the warnings* then it is on them to pin their compiler versions and verify the assembly of every single binary build they do.
2024-04-15Add support for Arm64EC to the Standard LibraryDaniel Paoliello-1/+1
2024-03-23move assert_unsafe_preconditions to its own fileRalf Jung-2/+3
These macros and functions are not intrinsics, after all.
2024-03-08Distinguish between library and lang UB in assert_unsafe_preconditionBen Kimock-4/+7
2024-02-08Rewrite assert_unsafe_precondition around the new intrinsicBen Kimock-1/+1
2024-01-04Fixed ambiguity in hint.rsMing, Bai-1/+1
Needle and haystack are actually not the same, they remain constant.
2023-12-19Add `hint::assert_unchecked`Scott McMurray-0/+48
2023-10-02Rollup merge of #116350 - Nilstrieb:in-situ, r=Dylan-DPCTyler Mandry-1/+1
Improve wording of `hint::black_box` docs The wording is a bit confusing. squash of #109634
2023-10-02Improve wording of `hint::black_box` docs4gboframram-1/+1
The wording is a bit confusing. Co-authored-by: Chris Denton <christophersdenton@gmail.com>
2023-09-04Simplify `core::hint::spin_loop`Maybe Waffle-24/+17
2023-04-26Rollup merge of #108416 - pat-nel87:Issue-107957-black_box_docs, r=jyn514jyn-6/+3
black_box doc corrections for clarification - Issue #107957 Made a complete pass through the docs to help resolve https://github.com/rust-lang/rust/issues/107957 No code changes, just documentation `@rustbot` label +T-libs-api -T-libs
2023-04-22Fix grammarCaleb Robson-2/+2
2023-03-25Correct typo (`back_box` -> `black_box`)Evie M-1/+1
2023-03-07black_box hint - Adjust for improved readabilitypat-nel87-6/+3
2023-02-23black_box doc corrections - Issue #107957pat-nel87-3/+3
2023-02-15clarify correctness of `black_box`Callum Leslie-1/+2