Skip to content

Allow more const fns in std #1835

@F001

Description

@F001
Contributor

Functions in std which could be evaluated in compile time should be qualified by const.

Such as:

fn size_of<T>() -> usize;
fn size_of_val<T: ?sized>(_: &T) -> usize;
fn type_name<T: ?sized>() -> &'static str;
fn type_id<T: ?Sized + 'static> () -> u64;
fn needs_drop<T>() -> bool;
......

I suppose they should be const fn. And there are more. Is it correct?

Activity

steveklabnik

steveklabnik commented on Dec 30, 2016

@steveklabnik
Member

In general, this is something that is desired, but until const fn is closer to becoming stable, has not been seriously investigated, as far as I know.

added
T-libs-apiRelevant to the library API team, which will review and decide on the RFC.
on Jan 4, 2017
oli-obk

oli-obk commented on Jan 9, 2017

@oli-obk
Contributor

I think all of these require incremental compilation, as the compiler needs to generate those function monomorphizations through compiler magic, but cannot do so before it compiled the types these functions are called on.

eddyb

eddyb commented on Jan 9, 2017

@eddyb
Member

@oli-obk It's orthogonal to the "redo as little as possible" part of incremental recompilation. I suppose "on-demand" overlaps enough to count, but hasn't been an explicit part of the incremental recompilation effort so far.

aidanhs

aidanhs commented on Jul 6, 2017

@aidanhs
Member

This is partially fixed by rust-lang/rust#42859 ({size,align}_of).

trissylegs

trissylegs commented on Mar 28, 2018

@trissylegs

Could size_of_val be const? I guess things like size_of_val("boop") is const, but something like size_of_val::<Write>(some_boxed_writer) could be entirely runtime dependent. Like a boxed trait returned from a dynamically linked library.

oli-obk

oli-obk commented on Mar 28, 2018

@oli-obk
Contributor

I don't see why it shouldn't be const fn, but I don't know if the current const qualification system can handle it. I'd say we go to the low hanging fruit first

trissylegs

trissylegs commented on Mar 28, 2018

@trissylegs

Just realized that it's not possible to make a runtime size dependent variable at compile time and therefor my point is moot.

Centril

Centril commented on Apr 26, 2018

@Centril
Contributor

Some of these are const now, so I'm closing this issue. I'd just create a thread on internals or file a PR if you want something to be const.

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

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@eddyb@oli-obk@nrc@Centril

        Issue actions

          Allow more `const fn`s in std · Issue #1835 · rust-lang/rfcs