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

add the align(ptr) charter #43

Closed
wants to merge 2 commits into from
Closed

add the align(ptr) charter #43

wants to merge 2 commits into from

Conversation

Lokathor
Copy link

@Lokathor Lokathor commented Aug 1, 2020

This is a charter for #35

Closes #35

RENDERED

@nikomatsakis
Copy link
Contributor

Makes sense! Relatively simple.

@rfcbot fcp merge

@scottmcm scottmcm self-assigned this Aug 3, 2020
@rfcbot
Copy link
Collaborator

rfcbot commented Aug 3, 2020

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

Concerns:

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.

Comment on lines +16 to +21
#[cfg_attr(target_pointer_width = "16", repr(C, align(2)))]
#[cfg_attr(target_pointer_width = "32", repr(C, align(4)))]
#[cfg_attr(target_pointer_width = "64", repr(C, align(8)))]
pub struct AtomicPtr<T> {
p: UnsafeCell<*mut T>,
}
Copy link
Member

Choose a reason for hiding this comment

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

Why is AtomicPtr defined like this?

UnsafeCell is #[repr(transparent)] and AtomicPtr is #[repr(C)]; my understanding is that the minimum alignment of AtomicPtr therefore must be equal to the minimum alignment of *mut _ .

Unless there's a platform in which the minimum alignment of AtomicPtr must be greater than the minimum alignment of *mut _, the cfg_attrs and align(*) modifiers seem redundant: AtomicPtr could be annotated with just #[repr(C)].

Am I missing something?

Copy link
Author

Choose a reason for hiding this comment

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

I don't think you're missing anything.

@rfcbot
Copy link
Collaborator

rfcbot commented Aug 27, 2020

🔔 This is now entering its final comment period, as per the review above. 🔔

psst @nikomatsakis, I wasn't able to add the final-comment-period label, please do so.

@kennytm kennytm added the final-comment-period The FCP has started, most (if not all) team members are in agreement label Aug 28, 2020
@rfcbot
Copy link
Collaborator

rfcbot commented Sep 6, 2020

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

psst @nikomatsakis, I wasn't able to add the finished-final-comment-period label, please do so.

@comex
Copy link

comex commented Sep 7, 2020

Hmm… a bit too late now, but I hadn’t noticed that this charter proposal had evolved to define “the general concept of const expressions within an align attribute” as out of scope. It’s not the end of the world, but I’m disappointed: it adds unnecessary complexity to the language (a magic ptr attribute value when align_of::<*const u8>() would do) just to save time compared to the more general solution, which is not all that complex to specify (though perhaps harder to implement).

It would also add an edge case if align is ever extended to support arbitrary const expressions in the future: there would be ambiguity in case there happened to be a const ptr: usize in scope.

@Lokathor
Copy link
Author

Lokathor commented Sep 7, 2020

I mean, you're right.

And another thing is that the open conversation above where it was pointed out that all the align stuff is probably pointless since it'll have the correct alignment anyway.

So honestly, this particular project group might be a dud.

Then again, if the conclusion of the group is "nothing that's in scope for us turned out to be a good idea", then that's ultimately an okay conclusion.

@joshtriplett
Copy link
Member

@rfcbot concern coordinate-with-const-generics

Thanks for suggesting that, @comex!

We discussed this in the meeting today, and we should coordinate this with the const eval folks, to figure out if this could use const evaluation so that #[repr(align(align_of::<*const u8>()))] could work.

We might not need const generic support quite yet, though that might be useful at some point.

@scottmcm
Copy link
Member

scottmcm commented Sep 7, 2020

FWIW, align(size) in particular feels like something that would be very hard to write using a const generics expression, so there may be value in some "canned" values here even if the general thing is also supported.

@Lokathor
Copy link
Author

Lokathor commented Sep 7, 2020

Zulip topic created to check on the feasibility of general const expression usage: https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/const.20eval.20within.20attributes

@jswrenn
Copy link
Member

jswrenn commented Sep 8, 2020

FWIW, align(size) in particular feels like something that would be very hard to write using a const generics expression, so there may be value in some "canned" values here even if the general thing is also supported.

align(size) actually isn't too hard to whip up with const generics: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=66801018f8aa2447b41d4c0099bd1d88

(I don't think there's a satisfactory, analogous trick for packed(N), unfortunately.)


So far, the `align` attribute only allows for an integer literal as the input. This project group aims to add a new alignment input, `ptr`, which is always the value of the local pointer size.

It has also been suggested that `size` be allowed as an input value, to request that a type have alignment equal to its own size.
Copy link
Member

Choose a reason for hiding this comment

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

The align modifier only sets a minimum alignment. Oddly, Rust doesn't have an ergonomic way to specify the alignment of a type is exactly equal to a value.

You can set a maximum alignment with the packed modifier, but it's an error to have both align and packed annotations on the same type, even if they're not contradictory:

error[E0587]: type has conflicting packed and align representation hints
 --> src/main.rs:1:30
  |
1 | #[repr(align(1), packed(2))] pub struct Foo;
  |                              ^^^^^^^^^^^^^^^

@scottmcm
Copy link
Member

scottmcm commented Sep 8, 2020

align(size) actually isn't too hard to whip up with const generics

I think it was unclear what I meant by align(size) here. Specifically, I've been using that to mean alignof(T)==sizeof(T), by adding just enough padding to round the total size of the struct up to a power of two. And doing that in a const fn would, I think, require reimplementing the repr(rust) size calculation over all the fields. Which is doable eventually (once size_of and math work in const generics), but not trivial.

@jswrenn
Copy link
Member

jswrenn commented Sep 8, 2020

Surprisingly, size_of already works!?

Using my demo, you could implement what you describe by desugaring this:

#[repr(C, align(size))]
struct Foo {
  bar: u8,
  baz: u16,
}

...to this:

#[repr(C)]
struct Foo {
  _align: Align<{
    #[repr(C)]
    struct Foo {
      bar: u8,
      baz: u8,
    }
    if let Some(align) =  mem::size_of::<Foo>().checked_next_power_of_two() {
      align
    } else {
      usize::MAX
    }
  }>,
  bar: u8,
  baz: u16,
}

@joshtriplett
Copy link
Member

We discussed this in the lang team meeting, and we'd like to charter this group, but we believe based on the linked discussions that this should be doable with simple const expressions. @Lokathor proposed to update the charter to account for this. We'll wait for that updated charter, then merge.

@nikomatsakis
Copy link
Contributor

@joshtriplett can you resolve your outstanding concern?

@nikomatsakis
Copy link
Contributor

or are you waiting for the final edits

@Lokathor
Copy link
Author

Lokathor commented Oct 1, 2020

I was going to update the charter based on feedback about const eval in the attribute usage and we were going to discuss again. Hopefully I can do that this weekend.

@Lokathor
Copy link
Author

Lokathor commented Oct 5, 2020

I no longer have time to work on this, particularly if the solution is going to potentially be more complicated.

Discussed at the meeting Oct 5, 2020.

  • We decided to close this as "postponed" since no one else currently has the bandwidth either.

Closes #35

@Lokathor Lokathor closed this Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
final-comment-period The FCP has started, most (if not all) team members are in agreement T-lang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MCP: repr(align(ptr))
8 participants