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

const_fn usage (with Cell fields) #71

Open
Gilnaa opened this issue Dec 11, 2022 · 4 comments
Open

const_fn usage (with Cell fields) #71

Gilnaa opened this issue Dec 11, 2022 · 4 comments

Comments

@Gilnaa
Copy link
Owner

Gilnaa commented Dec 11, 2022

Previously on memoffset:

Constant evaluation mostly stable with the exception of the inability to take address of a Cell in const which still requires nightly and #![feature(const_refs_to_cell)].

@overlookmotel
Copy link

As far as I can see, this also blocks getting offset of any field of a struct with generics e.g.:

struct Foo<T> {
  t: T,
  n: u8,
}

// Both these produce compiler error
impl<T> Foo<T> {
  const OFFSET_T: usize = memoffset::offset_of!(Self, t);
  const OFFSET_N: usize = memoffset::offset_of!(Self, n);
}

Compiler error:

error[E0658]: cannot borrow here, since the borrowed element
may contain interior mutability

Bounding T to T: Copy + Clone (so it's definitely not a cell type) doesn't help.

The error seems to come from the let base_ptr = line:

const OFFSET_T: usize = {
  let uninit = memoffset::__priv::mem::MaybeUninit::<Self>::uninit();
  let base_ptr: *const Self = uninit.as_ptr();
  // ...
};

Is there any workaround for this on stable Rust? This comment says "Fortunately it can be gotten around by passing in a reference to the const fn", but I'm unclear what function they're referring to.

@RalfJung
Copy link
Collaborator

RalfJung commented Mar 4, 2024

There will be a work-around soon: Rust 1.77 is slated to ship with a native offset_of! macro, making this crate largely obsolete. The scheduled release date is March 21st.

@overlookmotel
Copy link

Thanks for coming back so fast Ralf.

Amazing! Champagne for everyone! 🎉

While we're talking, can I ask you something else on the same sort of topic?
https://users.rust-lang.org/t/obtain-type-layout-information-at-compile-time/88380

The part which is relevant is where I asked:

Ralf Jung tantilizingly mentioned [on his blog about rustc_layout]
(https://www.ralfj.de/blog/2020/04/04/layout-debugging.html) "some time ago I wrote an awful hack for this based on rustc debug tracing". Ralf, if you're out there, can you share this awful hack please?!

@RalfJung
Copy link
Collaborator

RalfJung commented Mar 4, 2024

The blog post says that the hack is no longer needed, it describes the much nicer way to do that. I don't have the hack any more, it was probably some random info! or trace! somewhere in rustc.

This is getting off-topic, please reach out to me on Zulip for further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants