Skip to content

Conversation

@tgross35
Copy link
Contributor

@tgross35 tgross35 commented Apr 7, 2024

Make the following API stable, including const:

// core::hint, std::hint

pub const unsafe fn assert_unchecked(p: bool);

This PR also reworks some of the documentation and adds an example.

Tracking issue: #119131
FCP: #119131 (comment). The docs update should resolve the remaining concern.

@rustbot
Copy link
Collaborator

rustbot commented Apr 7, 2024

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 7, 2024
@tgross35
Copy link
Contributor Author

tgross35 commented Apr 7, 2024

I don't think there is much purpose in keeping a separate const_* feature gate if const stabilization happens at the same time, so merged the gates.

r? libs-api
@rustbot label -T-libs +T-libs-api
cc @scottmcm
cc @rust-lang/wg-const-eval (and @RalfJung directly since it seems like that ping doesn't always work) for const concerns

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 7, 2024
@rustbot rustbot assigned m-ou-se and unassigned jhpratt Apr 7, 2024
@rustbot rustbot removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 7, 2024
@rust-log-analyzer

This comment has been minimized.

@tgross35 tgross35 force-pushed the stabilize-assert_unchecked branch 2 times, most recently from 7599168 to 29683ac Compare April 7, 2024 09:20
@RalfJung
Copy link
Member

RalfJung commented Apr 7, 2024

cc @rust-lang/wg-const-eval (and @RalfJung directly since it seems like that ping doesn't always work)

It doesn't? I think it always works, except that the person writing the post has to be an org member to ping org teams. (You can tell because the team doesn't get highlighted and linked if a non-org-member writes @rust-lang/wg-const-eval.)

/// /// `p` must be nonnull and valid
/// pub unsafe fn next_value(p: *const i32) -> i32 {
/// // SAFETY: caller invariants guarantee that `p` is not null
/// unsafe { hint::assert_unchecked(!p.is_null()) }
Copy link
Member

Choose a reason for hiding this comment

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

This feels like a pretty weird example. Do we not have something better? We even have to defend the example later in the documentation as being not real world.

One example that feels more illustrative is guaranteeing that the returned integer is in-bounds for some slice. For example, binary search tells LLVM that the indexes it returns are in-bounds of the length. Replicating binary search here probably is too much, but replicating a simpler algorithm (e.g., &[(u32, usize)] where you search for a particular needle and every usize is guaranteed to be an index in the array -- so before returning it you want to tell LLVM that) feels like a better example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't think there was much purpose in showing a real example of how it may be used in practice since we say elsewhere that you should only use this after benchmarking, and that a smarter optimizer may be able to figure it out at some point. So rather than showing an real usecase that might go away, I figured a dummy example of how it can actually affect codegen may be more useful.

But I am happy to change here as needed, maybe I can rework this example to make its intent more obvious.

Copy link
Member

Choose a reason for hiding this comment

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

I like this example. It's not necessarily real world, but I do think it is effective at demonstrating the principle here in a pretty small and easily digestible code snippet.

@tgross35
Copy link
Contributor Author

tgross35 commented Apr 7, 2024

cc @rust-lang/wg-const-eval (and @RalfJung directly since it seems like that ping doesn't always work)

It doesn't? I think it always works, except that the person writing the post has to be an org member to ping org teams. (You can tell because the team doesn't get highlighted and linked if a non-org-member writes @rust-lang/wg-const-eval.)

Ah, I remember a couple of times when @ed the team on a PR and you said you didn't get it, must have just been from before I was a member then. Makes sense.

@tgross35 tgross35 force-pushed the stabilize-assert_unchecked branch from 29683ac to 4f77927 Compare April 7, 2024 17:12
@bors
Copy link
Collaborator

bors commented Apr 24, 2024

☔ The latest upstream changes (presumably #104087) made this pull request unmergeable. Please resolve the merge conflicts.

@c410-f3r
Copy link
Contributor

c410-f3r commented May 1, 2024

ping @BurntSushi

Was your concern resolved by the documentation presented in this PR?

/// /// `p` must be nonnull and valid
/// pub unsafe fn next_value(p: *const i32) -> i32 {
/// // SAFETY: caller invariants guarantee that `p` is not null
/// unsafe { hint::assert_unchecked(!p.is_null()) }
Copy link
Member

Choose a reason for hiding this comment

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

I like this example. It's not necessarily real world, but I do think it is effective at demonstrating the principle here in a pretty small and easily digestible code snippet.

@BurntSushi
Copy link
Member

@c410-f3r Yes, I'd be happy to resolve my concern once this is merged.

@c410-f3r
Copy link
Contributor

c410-f3r commented May 2, 2024

Thanks @BurntSushi

@jhpratt
Copy link
Member

jhpratt commented Jun 9, 2024

@BurntSushi Given that FCP is blocked on docs and this PR is open to both stabilize and resolve the concern, what would you prefer? Should the PR be split up such that docs can be merged, the concern resolved, and FCP started, or should the concern be lifted on the basis that it will be resolved at the same time as stabilization? I'd prefer the latter for simplicity.

@tgross35
Copy link
Contributor Author

tgross35 commented Jun 9, 2024

Ah this slipped my mind but is waiting on me, I’ll incorporate the feedback tomorrow. I think burntsushi said somehow that this PR is suitable to resolve the docs concern.

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 9, 2024
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 9, 2024
@BurntSushi
Copy link
Member

BurntSushi commented Jun 9, 2024

Yeah I see the conflict here. I think when I was reviewing this, I thought this was just a doc update. Yes, in the future, I think that should be de-coupled from stabilization itself, otherwise we have a chicken & egg problem. Generally speaking, I'd like to see doc updates merged before resolving my concern. But I've resolved the concern in #119131.

@tgross35 tgross35 force-pushed the stabilize-assert_unchecked branch from 4f77927 to 75e0985 Compare June 19, 2024 18:40
@rust-log-analyzer

This comment has been minimized.

@tgross35 tgross35 force-pushed the stabilize-assert_unchecked branch from 75e0985 to 3ec924c Compare June 19, 2024 18:59
tgross35 added 2 commits June 19, 2024 19:31
Rearrange the sections and add an example to
`core::hint::assert_unchecked`.
Make both `hint_assert_unchecked` and `const_hint_assert_unchecked`
stable as `hint_assert_unchecked`.
@tgross35 tgross35 force-pushed the stabilize-assert_unchecked branch from 3ec924c to 5745c22 Compare June 19, 2024 23:53
@tgross35
Copy link
Contributor Author

I updated the docs based on the feedback. Since you have been pretty involved here,
r? @BurntSushi

@rustbot rustbot assigned BurntSushi and unassigned m-ou-se Jun 19, 2024
@tgross35
Copy link
Contributor Author

tgross35 commented Jul 2, 2024

Oh whoops, forgot the important labels

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 2, 2024
@dtolnay dtolnay assigned dtolnay and unassigned BurntSushi Jul 3, 2024
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

@dtolnay
Copy link
Member

dtolnay commented Jul 3, 2024

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 3, 2024

📌 Commit 5745c22 has been approved by dtolnay

It is now in the queue for this repository.

@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 Jul 3, 2024
@bors bors merged commit db59225 into rust-lang:master Jul 3, 2024
@rustbot rustbot added this to the 1.81.0 milestone Jul 3, 2024
@tgross35 tgross35 deleted the stabilize-assert_unchecked branch July 3, 2024 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.