Skip to content

Tracking Issue for const offset_from (const_ptr_offset_from) #92980

@RalfJung

Description

@RalfJung
Member

The feature gate for the issue is #![feature(const_ptr_offset_from)].

This tracks constness of the following function(s):

impl<T> *const T {
  const unsafe fn offset_from(self, origin: *const T) -> isize
}

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.

Steps

Unresolved Questions

Implementation history

Activity

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Jan 16, 2022
RalfJung

RalfJung commented on Jan 16, 2022

@RalfJung
MemberAuthor

Cc @rust-lang/wg-const-eval

added a commit that references this issue on Jan 17, 2022
9612038
kupiakos

kupiakos commented on Mar 5, 2022

@kupiakos
Contributor

I'd like to see if we can work out the kinks and stabilize this sooner rather than later. Since addr_of! is stable, this being stabilized would enable us to do something quite powerful: stable const-time offset_of. See here for an example. With that, we can get stable const-time calculation of padding byte locations, something that could be very helpful for the zerocopy library and related firmware code.

RalfJung

RalfJung commented on Mar 5, 2022

@RalfJung
MemberAuthor

See here for an example.

Note that that macro has some subtle flaws, see https://github.com/Gilnaa/memoffset for a bullet-proof (and also nightly-const-compatible) version. In particular it protects against .field triggering deref coercions, which make your version of the macro unsound.

RalfJung

RalfJung commented on Mar 5, 2022

@RalfJung
MemberAuthor

But regarding this tracking issue, I think it would be good to fix rust-lang/miri#1950 before stabilizing, to ensure we actually check the UB for this function properly. (Miri and CTFE share that implementation.) Other than that, I am not aware of any blockers. I think we should do the change suggested in #92512, but that can happen post-stabilization.

kupiakos

kupiakos commented on Mar 5, 2022

@kupiakos
Contributor

Good call on the Deref soundness, rest assured I'm not copying playground code into production 😃

I'll see if I can spend some time looking at the change or issue.

In the meantime, I think I may have found an alternative way to calculate offset_of! in stable CTFE that doesn't depend on pointer arithmetic, it's just more expensive at compile time (obviously this contains the same Deref flaw currently)

RalfJung

RalfJung commented on Mar 11, 2022

@RalfJung
MemberAuthor

Stabilization report

I would like to propose this feature for stabilization: it allows const-code to call

impl<T> *const T {
  const unsafe fn offset_from(self, origin: *const T) -> isize
}

Click here for method docs

offset_from has been stabilized for runtime code more than 1.5 years ago (see #41079). Its CTFE implementation is shared with Miri and has been working fine for a long time; #94827 works out the last kink (but even that is optional since we do not guarantee to detect all UB). This is the last feature that is missing to be able to implement offset_of! in a const-compatible way, which has been a long time coming. :)

This cycle we are also on track to const-stabilize a whole lot of other pointer offset functions (#71499), so this fits quite well.

The tests for this feature are at

In the future I would like to relax the UB restrictions of this function (#92512), but that should be backwards compatible so it does not have to block stabilization.

@rust-lang/lang can I ask for the FCP process to be initiated?

RalfJung

RalfJung commented on Mar 11, 2022

@RalfJung
MemberAuthor

Or is this for @rust-lang/libs-api ? It is const-exposing an intrinsic (that could not be implemented in pure Rust), so... possibly both.

added
T-langRelevant to the language team
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Mar 11, 2022
scottmcm

scottmcm commented on Mar 11, 2022

@scottmcm
Member

Both teams sounds reasonable, yeah.

Teams, do we want to allow offset-between-pointers calculations in const?

Stabilization report from Ralf: #92980 (comment)

@rfcbot fcp merge

One correction to the report: it's T: Sized only, due to a where clause:

#[stable(feature = "ptr_offset_from", since = "1.47.0")]
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
#[inline]
pub const unsafe fn offset_from(self, origin: *const T) -> isize
where
T: Sized,
{
let pointee_size = mem::size_of::<T>();
assert!(0 < pointee_size && pointee_size <= isize::MAX as usize);
// SAFETY: the caller must uphold the safety contract for `ptr_offset_from`.
unsafe { intrinsics::ptr_offset_from(self, origin) }
}

rfcbot

rfcbot commented on Mar 11, 2022

@rfcbot
Collaborator

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

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), 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.

21 remaining items

added a commit that references this issue on Aug 27, 2022
de6ab5a
added a commit that references this issue on Aug 27, 2022
539e408
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCE-help-wantedCall for participation: Help is requested to fix this issue.S-tracking-ready-to-stabilizeStatus: This is ready to stabilize; it may need a stabilization report and a PRT-langRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @nikomatsakis@RalfJung@yaahc@kupiakos@apiraino

    Issue actions

      Tracking Issue for const offset_from (const_ptr_offset_from) · Issue #92980 · rust-lang/rust