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

Constify pointer::is_aligned{,to} #102753

Closed
wants to merge 7 commits into from

Conversation

lukas-code
Copy link
Member

In this PR i've made <*const T>::is_aligned and <*const T>::is_aligned_to unstably const.
The main motivation here is to allow something like debug_assert!(is_aligned_and_not_null(ptr)) in const fns without any const_eval_select hacks.

If we don't know the address of a pointer in const eval, this function only returns true if we know for sure that the pointer is aligned. This means that in some cases a pointer can never be aligned, which matches the behavior of align_offset1.

Tracking issue for is_aligned: #96284

Footnotes

  1. Note that currently align_offset will always return usize::MAX in CTFE, but it could return a useful value for some alignments in the future.

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot rustbot added 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 Oct 6, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 6, 2022
@rustbot
Copy link
Collaborator

rustbot commented Oct 6, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@RalfJung
Copy link
Member

RalfJung commented Oct 7, 2022

Cc @rust-lang/wg-const-eval

I am not entirely sure about this... pointers are wonky in const and this function does not behave the way people will expect it to behave.

Also I think we should keep this and align_offset consistent, and we don't need a new intrinsic for this if we just do align_offset == 0.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 7, 2022

Also I think we should keep this and align_offset consistent, and we don't need a new intrinsic for this if we just do align_offset == 0.

yea, it should not add a new intrinsic, but be implemented in terms of the existing one.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 7, 2022

I am not entirely sure about this... pointers are wonky in const and this function does not behave the way people will expect it to behave.

the same is true for align_offset and [T]::align_to... which kind makes me want to just declare them "never gonna be const" and instead push the obligation to the caller who can use const_eval_select to skip the check

@saethlin
Copy link
Member

saethlin commented Oct 7, 2022

I think doing that would make those interfaces more popular, people should be using the standard library instead of re-implementing these. Right now I know of a few people who specifically avoid them because of the vague caveat in the docs. If we remove that, there should be a proper announcement of the change in direction we're taking in designing pointer interfaces.

And I for one would like pressure for const_eval_select to be more usable.

@RalfJung
Copy link
Member

RalfJung commented Oct 7, 2022

Pressure doesn't help. People doing the work help. :)

@oli-obk
Copy link
Contributor

oli-obk commented Oct 7, 2022

And I for one would like pressure for const_eval_select to be more usable.

I'm on it 😆

@saethlin
Copy link
Member

saethlin commented Oct 7, 2022

I think I meant a different kind of pressure. If I can tell a compelling story, it's easy to motivate community members to pitch in.

@lukas-code
Copy link
Member Author

I've put up an alternate PR which doesn't need a new intrinsic and uses align_offset instead: #102795

@rustbot
Copy link
Collaborator

rustbot commented Oct 20, 2022

The Miri subtree was changed

cc @rust-lang/miri

@rustbot rustbot added T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic labels Oct 20, 2022
@rustbot rustbot added T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. T-style Relevant to the style team, which will review and decide on the PR/issue. labels Oct 20, 2022
@rustbot
Copy link
Collaborator

rustbot commented Oct 20, 2022

Some changes occurred in const_evaluatable.rs

cc @lcnr

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes occurred in src/librustdoc/clean/types.rs

cc @camelid

Some changes occurred in HTML/CSS themes.

cc @GuillaumeGomez

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @estebank, @TaKO8Ki

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in HTML/CSS/JS.

cc @GuillaumeGomez, @Folyd, @jsha

Some changes occurred in src/tools/cargo

cc @ehuss

rustc_macros::diagnostics was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @estebank, @TaKO8Ki

Some changes occurred in need_type_info.rs

cc @lcnr

rustc_errors::translation was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @estebank, @TaKO8Ki

A change occurred in the Ayu theme.

cc @Cldfire

@lukas-code
Copy link
Member Author

Whoops, sorry for the massive ping. Pushed something here that I shouldn't have. Anyway, closing this in favor of #102795.

@lukas-code lukas-code closed this Oct 20, 2022
@lukas-code lukas-code deleted the constify-is-aligned branch October 20, 2022 19:42
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 19, 2022
…lign-offset, r=oli-obk

Constify `is_aligned` via `align_offset`

Alternative to rust-lang#102753

Make `align_offset` work in const eval (and not always return `usize::MAX`) and then use that to constify `is_aligned{_to}`.

Tracking Issue: rust-lang#104203
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Nov 20, 2022
…et, r=oli-obk

Constify `is_aligned` via `align_offset`

Alternative to rust-lang/rust#102753

Make `align_offset` work in const eval (and not always return `usize::MAX`) and then use that to constify `is_aligned{_to}`.

Tracking Issue: rust-lang/rust#104203
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request Feb 10, 2023
…et, r=oli-obk

Constify `is_aligned` via `align_offset`

Alternative to rust-lang/rust#102753

Make `align_offset` work in const eval (and not always return `usize::MAX`) and then use that to constify `is_aligned{_to}`.

Tracking Issue: rust-lang/rust#104203
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-infra Relevant to the infrastructure 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. T-style Relevant to the style team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants