profile
viewpoint

Ask questionsUnsafety checks for "unconst" operations do not trigger in unstable functions

Some operations, such as raw pointer comparisons, require unsafe when inside a const context. To my knowledge, none of these operations have been const-stabilized. Accordingly, the following example fails to compile due to a missing unsafe block.

#![feature(const_compare_raw_pointers)]
#![feature(const_fn)]

const fn unstable(a: *const i32, b: *const i32) -> bool {
    a == b
}

However, the check does not run for const_unstable functions. The following compiles successfully:

#![stable(feature = "foo", since = "1.33.0")]
#![feature(staged_api)]
#![feature(const_compare_raw_pointers)]
#![feature(const_fn)]

#[stable(feature = "foo", since = "1.33.0")]
#[rustc_const_unstable(feature = "const_foo", issue = "none")]
const fn unstable(a: *const i32, b: *const i32) -> bool {
    a == b
}

This is an easy problem to fix, but the root cause here is poor naming. Specifically, fn_queries::is_const_fn should be renamed is_callable_as_const_fn and is_const_fn_raw should be is_declared_const_fn. We have had other issues due to this in the past. cc @rust-lang/wg-const-eval to weigh in on the renaming.

rust-lang/rust

Answer questions ecstatic-morse

Oh, the names are different than in the email I got because you edited.

Damn. I was hoping no one would catch me :smile:.

is_const_callable_fn would indeed take feature flags into account to mirror the current behavior of is_const_fn. I originally had is_stably_const_fn, but that's not really correct, since it would also return true if the function was unstable but the requisite feature flags were set.

useful!

Related questions

Spurious NaNs produced by trig functions with valid inputs on Windows GNU toolchains hot 2
using 'cargo install xsv' on windows 10 triggers rustc internal error hot 1
chain() make collect very slow hot 1
if/while Some(n) = &mut foo sugar will leak a temporary mutable borrow to current scope in particular situation hot 1
build an empty project failed (undefined reference to `__onexitbegin') hot 1
Invalid collision with TryFrom implementation? hot 1
Crater runs for Rust 1.38.0 hot 1
Spurious NaNs produced by trig functions with valid inputs on Windows GNU toolchains hot 1
under latest MinGW, cannot link with C code using stdout hot 1
Archive all nightlies hot 1
Building LLVM with Clang fails hot 1
Internal compiler error: can't buffer lints after HIR lowering hot 1
E0373 help suggests `move async` but the correct syntax is `async move` hot 1
Tracking issue for `Option::contains` and `Result::contains` hot 1
async fn + rustfmt don't "just work" inside of RLS hot 1
Github User Rank List