Skip to content

Commit 5969ad4

Browse files
committed
Update is_val_statically_known docs
* Add `Type Requirements` section, listing the allowed inputs, as requested by #121115 * Add `Pointers` subsection, explaining is_val_statically_known handles pointers. * Add `Stability concerns` section, referring to other documentation relating to consistency in `const` functions.
1 parent ce609db commit 5969ad4

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

Diff for: library/core/src/intrinsics.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -2717,10 +2717,45 @@ where
27172717
/// particular value, ever. However, the compiler will generally make it
27182718
/// return `true` only if the value of the argument is actually known.
27192719
///
2720-
/// When calling this in a `const fn`, both paths must be semantically
2721-
/// equivalent, that is, the result of the `true` branch and the `false`
2722-
/// branch must return the same value and have the same side-effects *no
2723-
/// matter what*.
2720+
/// # Stability concerns
2721+
///
2722+
/// While it is safe to call, this intrinsic may behave differently in
2723+
/// a `const` context than otherwise. See the [`const_eval_select`]
2724+
/// documentation for an explanation of the issues this can cause. Unlike
2725+
/// `const_eval_select`, this intrinsic isn't guaranteed to behave
2726+
/// deterministically even in a `const` context.
2727+
///
2728+
/// # Type Requirements
2729+
///
2730+
/// `T` must be either a `bool`, a `char`, a primitive numeric type (e.g. `f32`,
2731+
/// but not `NonZeroISize`), or any thin pointer (e.g. `*mut String`).
2732+
/// Any other argument types *may* cause a compiler error.
2733+
///
2734+
/// ## Pointers
2735+
///
2736+
/// When the input is a pointer, only the pointer itself is
2737+
/// ever considered. The pointee has no effect. Currently, these functions
2738+
/// behave identically:
2739+
///
2740+
/// ```
2741+
/// #![feature(is_val_statically_known)]
2742+
/// #![feature(core_intrinsics)]
2743+
/// # #![allow(internal_features)]
2744+
/// #![feature(strict_provenance)]
2745+
/// use std::intrinsics::is_val_statically_known;
2746+
///
2747+
/// fn foo(x: &i32) -> bool {
2748+
/// is_val_statically_known(x)
2749+
/// }
2750+
///
2751+
/// fn bar(x: &i32) -> bool {
2752+
/// is_val_statically_known(
2753+
/// (x as *const i32).addr()
2754+
/// )
2755+
/// }
2756+
/// # _ = foo(&5_i32);
2757+
/// # _ = bar(&5_i32);
2758+
/// ```
27242759
#[rustc_const_unstable(feature = "is_val_statically_known", issue = "none")]
27252760
#[rustc_nounwind]
27262761
#[unstable(feature = "core_intrinsics", issue = "none")]

0 commit comments

Comments
 (0)