Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafety checks for "unconst" operations do not trigger in unstable functions #72394

Closed
ecstatic-morse opened this issue May 20, 2020 · 3 comments · Fixed by #72401
Closed

Unsafety checks for "unconst" operations do not trigger in unstable functions #72394

ecstatic-morse opened this issue May 20, 2020 · 3 comments · Fixed by #72401
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-stability Area: `#[stable]`, `#[unstable]` etc. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented May 20, 2020

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.

@ecstatic-morse ecstatic-morse added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-stability Area: `#[stable]`, `#[unstable]` etc. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed B-unstable Blocker: Implemented in the nightly compiler and unstable. labels May 20, 2020
@ecstatic-morse ecstatic-morse changed the title Unsafety checks for "unconst" operations do not fire for unstable functions Unsafety checks for "unconst" operations do not fire trigger in unstable functions May 20, 2020
@ecstatic-morse ecstatic-morse added A-const-fn and removed A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels May 20, 2020
@RalfJung
Copy link
Member

Yeah I recall being confused by those names in the past, too. Definite 👍 for changing them.

As for concrete proposals, I don't actually remember what these queries are doing, but your suggestions sound reasonable. :)

@RalfJung
Copy link
Member

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

So does is_callable_as_const_fn take the context (feature flags etc) into account? The name sounds like it would. What about is_const_callable_fn, to make it a bit shorter?

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented May 20, 2020

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

Damn. I was hoping no one would catch me 😄.

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.

ecstatic-morse added a commit to ecstatic-morse/rust that referenced this issue May 20, 2020
@ecstatic-morse ecstatic-morse changed the title Unsafety checks for "unconst" operations do not fire trigger in unstable functions Unsafety checks for "unconst" operations do not trigger in unstable functions May 20, 2020
@bors bors closed this as completed in 401b3ae May 27, 2020
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-stability Area: `#[stable]`, `#[unstable]` etc. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants