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

Make [T]::len and str::len const fn #50863

Merged
merged 2 commits into from May 22, 2018
Merged

Make [T]::len and str::len const fn #50863

merged 2 commits into from May 22, 2018

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented May 18, 2018

r? @gankro

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 18, 2018
@oli-obk
Copy link
Contributor Author

oli-obk commented May 18, 2018

cc @Centril

@leonardo-m
Copy link

A simple common use case:

fn main() {
    const A: [u32; 3] = [1, 2, 3];
    const B: [u32; A.len()] = [10, 20, 30];
}

unsafe {
mem::transmute::<&[T], Repr<T>>(self).len
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@Centril Centril added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. labels May 18, 2018
@Gankra
Copy link
Contributor

Gankra commented May 19, 2018

Seems solid. I don’t have review rights tho

@oli-obk
Copy link
Contributor Author

oli-obk commented May 19, 2018

r? @Centril

@rust-highfive rust-highfive assigned Centril and unassigned Gankra May 19, 2018
@oli-obk
Copy link
Contributor Author

oli-obk commented May 19, 2018

Hot potato passing until success

r? @SimonSapin

@rust-highfive rust-highfive assigned SimonSapin and unassigned Centril May 19, 2018
buf[i] = c;
}
buf
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this assert some equality?

@rust-highfive

This comment has been minimized.

@SimonSapin
Copy link
Contributor

@bors r=SimonSapin,Gankro

@bors
Copy link
Contributor

bors commented May 21, 2018

📌 Commit 2788f66 has been approved by SimonSapin,Gankro

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 21, 2018
kennytm added a commit to kennytm/rust that referenced this pull request May 22, 2018
bors added a commit that referenced this pull request May 22, 2018
Rollup of 15 pull requests

Successful merges:

 - #50846 (Add E0665)
 - #50849 (CheckLoopVisitor: also visit closure arguments)
 - #50863 (Make `[T]::len` and `str::len` const fn)
 - #50875 (rustdoc: use "short form" doc(cfg) printing even when combined with other conditionals)
 - #50913 (Fix typo in cell.rs)
 - #50914 (Issue #50636: Improve error diagnostic with missing commas after struct fields.)
 - #50931 (Inline `try_get`.)
 - #50932 (Optimize seen Predicate filtering.)
 - #50945 (Stabilize feature from_ref)
 - #50946 (rustc: Fix procedural macros generating lifetime tokens)
 - #50947 (rustdoc: set tab width in rust source blocks)
 - #50952 (Add the 2018 edition of the book to doc.rust-lang.org)
 - #50958 (Micro-optimization on PR#50697)
 - #50961 (Fix FileCheck finding with MSVC)
 - #50963 (Right-size the `VecDeque` in `coerce_unsized`.)

Failed merges:
@bors bors merged commit 2788f66 into rust-lang:master May 22, 2018
@leonardo-m
Copy link

It doesn't work in this equally common case:

#![feature(const_slice_len)]
#![allow(unused_variables)]

fn main() {
    let a: [u8; 3] = [10, 20, 30];
    let b: [u8; a.len()] = Default::default();
    // error[E0435]: attempt to use a non-constant value in a constant
}

@oli-obk oli-obk deleted the const_len branch May 23, 2018 09:34
@oli-obk
Copy link
Contributor Author

oli-obk commented May 23, 2018

That will never work with const eval as it is setup currently. Supporting such cases (I'd call them "lazy const eval") would technically be possible but definitely requires an RFC.

#[rustc_const_unstable(feature="const_str_as_bytes")]
pub const fn as_bytes(&self) -> &[u8] {
unsafe {
union Slices<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can/should the union be moved outside the unsafe block? it's definition isn't unsafe iirc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that could certainly be done, there's no difference here and this seems very self-contained to me. YMMV, so feel free to open a PR about that if you want :)

kennytm added a commit to kennytm/rust that referenced this pull request May 24, 2018
move type out of unsafe block

from rust-lang#50863 (comment)

move the union definition outside of the unsafe block as it's definition is not unsafe
@rodrimati1992
Copy link
Contributor

rodrimati1992 commented Aug 19, 2019

Is this likely to stabilize soon?I need either this or the layout of &[T] and str to be guaranteed,to be able to to construct ffi-safe slices in constants without using hacks.

The hack I am currently using is(approximately):

#[repr(C)]
pub struct StaticSlice<T: 'static> {
    s: &'static [T],
    conversion: extern fn(*const [usize;2])->RSlice<'static,T>,
}

#[repr(C)]
pub struct RSlice<'a,T>{
    ptr:*const T,
    len:usize,
    _marker:PhantomData<&'a T>,
}

Along with a static assertions that &[T] is:

  • 2*usize in size
  • usize in alignment

The function pointer takes in a *const &[T] casted to *const [usize;2] which the static assertion guarantees is valid.

@oli-obk
Copy link
Contributor Author

oli-obk commented Aug 20, 2019

Stabilization is blocked on #51909

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants