Skip to content

Tracking issue for calling standard library const fns #46038

Closed
@SimonSapin

Description

@SimonSapin
Contributor

The functions below in the standard library are const fn with individual feature gates for calling them in constant expressions. This issue is tracking the stabilization of such calls.

CC #24111, the super-set feature which also includes defining new const fn items.

  • std::sync::Once::new
  • core::cell::Cell::new
  • core::cell::RefCell::new
  • core::cell::UnsafeCell::new
  • core::sync::AtomicBool::new
  • core::sync::AtomicPtr::new
  • core::sync::Atomic$IntegerType::new
  • core::mem::size_of
  • core::mem::align_of
  • core::ptr::null
  • core::ptr::null_mut
  • <$IntegerType>::max_value
  • <$IntegerType>::min_value

Items in core:: are also re-exported in std::.

Activity

SimonSapin

SimonSapin commented on Nov 16, 2017

@SimonSapin
ContributorAuthor

Feature gates are:

  • const_once_new
  • const_cell_new
  • const_refcell_new
  • const_unsafe_cell_new
  • const_size_of
  • const_align_of
  • const_ptr_null
  • const_ptr_null_mut
  • const_min_value
  • const_max_value
  • const_atomic_bool_new
  • const_atomic_ptr_new
  • const_atomic_i8_new
  • const_atomic_u8_new
  • const_atomic_i16_new
  • const_atomic_u16_new
  • const_atomic_i32_new
  • const_atomic_u32_new
  • const_atomic_i64_new
  • const_atomic_u64_new
  • const_atomic_isize_new
  • const_atomic_usize_new
added
B-unstableBlocker: Implemented in the nightly compiler and unstable.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Nov 16, 2017
aturon

aturon commented on Nov 16, 2017

@aturon
Member

Kicking off stabilization discussion for the const-ness of these functions in std.

@rfcbot fcp merge

added
proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
on Nov 16, 2017
rfcbot

rfcbot commented on Nov 16, 2017

@rfcbot
Collaborator

Team member @aturon has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

rfcbot

rfcbot commented on Nov 16, 2017

@rfcbot
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

added
final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
and removed
proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
on Nov 16, 2017
chabapok

chabapok commented on Nov 16, 2017

@chabapok

How to about mark std::time::Duration::{from_millis, new, from_secs, from_micros} as const_fn?

sfackler

sfackler commented on Nov 16, 2017

@sfackler
Member

new can panic so it can't be const.

durka

durka commented on Nov 17, 2017

@durka
Contributor

@sfackler sure it can, the same way an indexing expression can be const (panic turns into const evaluation error).

sfackler

sfackler commented on Nov 18, 2017

@sfackler
Member

@durka How?

#![feature(const_fn)]

const fn foo(a: i32) {
    if a > 10 {
        panic!();
    }
}

fn main() {}
error[E0019]: constant function contains unimplemented expression type
 --> src/main.rs:4:5
  |
4 | /     if a > 10 {
5 | |         panic!();
6 | |     }
  | |_____^

error: aborting due to previous error
#![feature(const_fn)]

const fn foo(a: i32) {
    panic!();
}

fn main() {}
error[E0015]: calls in constant functions are limited to constant functions, struct and enum constructors
 --> src/main.rs:4:5
  |
4 |     panic!();
  |     ^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate

error: aborting due to previous error
eddyb

eddyb commented on Nov 18, 2017

@eddyb
Member

I think this is "what is allowed right now" vs "what we could allow" (i.e. miri evaluates panics just fine).

durka

durka commented on Nov 18, 2017

@durka
Contributor
SimonSapin

SimonSapin commented on Nov 18, 2017

@SimonSapin
ContributorAuthor

There are many more fns that could be made const once miri lands, and we should definitely consider them then. This issue is specifically about those that are already const, and stabilizing calls to them Very Soon®.

rfcbot

rfcbot commented on Nov 26, 2017

@rfcbot
Collaborator

The final comment period is now complete.

added a commit that references this issue on Nov 26, 2017
6c5f53e
SimonSapin

SimonSapin commented on Nov 26, 2017

@SimonSapin
ContributorAuthor

I’ve submitted #46287

added a commit that references this issue on Nov 29, 2017

Rollup merge of rust-lang#46287 - SimonSapin:stable-constness, r=aturon

0e78c29
solutiontracking

solutiontracking commented on Mar 30, 2024

@solutiontracking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @durka@eddyb@kennytm@SimonSapin@aturon

        Issue actions

          Tracking issue for calling standard library `const fn`s · Issue #46038 · rust-lang/rust