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

mem::size_of() is not a const function #34078

Closed
DemiMarie opened this issue Jun 4, 2016 · 7 comments
Closed

mem::size_of() is not a const function #34078

DemiMarie opened this issue Jun 4, 2016 · 7 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@DemiMarie
Copy link
Contributor

mem::size_of() is not a const function, even though it is always a compile-time constant. This is seemingly arbitrary and is annoying in practice.

@eefriedman
Copy link
Contributor

eefriedman commented Jun 4, 2016

I'm pretty sure fixing this would be a one-line patch: std::intrinsics::size_of is already treated as a compile-time constant.

@eefriedman
Copy link
Contributor

Err, wait, nevermind, wasn't testing correctly. Anyway, seems reasonable (although it would probably be tricky to support in certain contexts, like array bounds).

@eddyb
Copy link
Member

eddyb commented Jun 4, 2016

It's not arbitrary, just very hard to support in the current compilation, which is at odds with CTFE.
There are plans to make type-checking of constant expressions in any positions possible before everything else has been type-checked, which would allow using something like miri to evaluate a much, much, broader set of constants.

@mtanski
Copy link

mtanski commented Aug 22, 2016

This pretty annoying, you can't use it in side of trait const expressions now.

trait DataType {
   type DiskType;
   const DISK_SIZE: usize;
   // ...
}

struct UInt32;

impl DataType {
   const DiskType = u32;
   const DISK_SIZE: usize = std::mem::size_of<Self::DiskType>();
}

Clearly this doesn't work, but it seams like it should.

@burdges
Copy link

burdges commented Oct 12, 2016

I think the active thread on this is rust-lang/rfcs#1144 but the associated constant one rust-lang/rfcs#1168 looked cool too.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@SimonSapin
Copy link
Contributor

@eddyb tells me on IRC that implementation difficulties are now longer relevant now that type checking happens on demand.

@frewsxcv
Copy link
Member

There's a PR open for this now: #42859

bors added a commit that referenced this issue Jul 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants